• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTg0

JSON Serializer, Deserializer extension

Two simple method for Serialize and Deserialize object to JSON

4
553 1 0 0 0 3

public static class JSONExtension
    {
        public static string ToJSON(this object obj )
        {
            if (obj != null)
            {
                var json = string.Empty;
                var ser = new DataContractJsonSerializer(obj.GetType());

                using (var ms = new MemoryStream())
                {
                    ser.WriteObject(ms, obj);
                    json = Encoding.Default.GetString(ms.ToArray());
                }
                return json;
            }
            return null;
        }

        public static T FromJSON<T>( this string json )
        {
            if (json == null)
                return default(T);

            using (var ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(json)))
            {
                var ser = new DataContractJsonSerializer(typeof(T));
                return (T)ser.ReadObject(ms);
            }
        }

Eg:

    var c = new CompetitionModel();
                    c.Name = "Johan";
                    c.Result = "22341112";

                    c.NewsLetter = false;

                    var jsonString = c.ToJSON();


var compModel = jsonString.FromJSON<CompetitionModel>();

Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

json

Mark 'json' tag as 'like'

Mark 'json' tag as 'ignore'


 @johannormen "Code Contributor"
1.24k
July 13, 2010 9:07 AM
edited July 22, 2010 11:47 PM

Fork

 JSON Serializer, Deserializer extension -  @johannormen Tuesday 13, 2010 9:07 AM


0 Feedback


You must log in before you can give any feedback


3 Discussion(s)

Newest Oldest
0

Nice! But why not use UTF-8 encoding instead of ASCII?

...
using (var ms = new MemoryStream(**Encoding.UTF8**.GetBytes(json)))
...
link | flag  | Reply

 @toha73 "Code Contributor"
716
Tuesday 13, 2010 11:09 AM

1

Ohh... before I did read this I already uploaded the same code, but in different language, F#. Well, now you can compare language differences... (less noise with f#)

http://www.forkcan.com/viewcode/198/Convert-an-object-to-json-and-json-to-object

link | flag  | Reply

 @thoriumi
781
Wednesday 14, 2010 3:57 PM

0

Got problem with åäö between flash client that I used in this case for receiving a JSON that I converted to my model. Byt you can use the encoding that are most suited for you, just changed that line :) I think the code located on this page is no RULE only guidance ;)

cool!

link | flag  | Reply

 @johannormen "Code Contributor"
1.24k
Thursday 15, 2010 3:14 PM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010