0

I am building a simple twitter application. I Originally wanted to use XML but after i did a little search and found JSON is much better and faster so i decided to use it.

However after searching for hours i couldn't find a single hello world tutorial that uses .net libraries for JSON. All the tutorials point out to using third party libraries for JSON. Since i cannot use anything out of the .net would somebody give me a few tidbits on how to get started.

Specifically using System.Runtime.Serialization.Json on .net4.0

Win Coder
  • 6,628
  • 11
  • 54
  • 81

4 Answers4

1

For one deserialization method of arbitrary Json using generic objects, see my answer here:

C# deserialize dynamic JSON

For serialization, I have been happy with the ServiceStack.Text implementation of ToJson.

https://github.com/ServiceStack/ServiceStack.Text

If you tool around their site a bit, you can find precompiled binaries as well to download. Don't be scared about all the other stuff ServiceSTack does (unless it can help you--you only need the ServiceStack.Text namespace).

Community
  • 1
  • 1
SAJ14SAJ
  • 1,698
  • 1
  • 13
  • 32
  • Your original question said no libraries outside of .NET--that is not the same thing as nothing except native Microsoft stuff. Still, the deserialization part is much harder, which is described in the other question. You can use the same Native Microsoft library to serialize as well. – SAJ14SAJ Nov 23 '12 at 05:55
  • Sorry for being ambiguous(english isn't my first language). Its a school project and i am limited to only using stuff produced by Microsoft in .net 4.0(not 4.5) – Win Coder Nov 23 '12 at 06:21
0

Go to this link MSDN JSON

But this library does not contains more features compared to other library available on internet. Plus they are open source. So in case you change your mind go through as
First of all decide which library you are going to use such as:

1. JSON.NET
2. JSON-Sharp
3. Newtonsoft.JSON
and many others (List of JSON libraries)
then find tutorial according to libraries.

I prefer Newtonsoft as it has some additional feature.
Also consider viewing specification of each library as they provide slightly different implementation for some datatypes such as dateTime (This will not be problem if you are not using cross platform communication such as producer in java and consumer in C# and vice versa).

Chaturvedi Dewashish
  • 1,469
  • 2
  • 15
  • 39
0

I am not more experienced on JSON but currently i am working on JSON serialization and deserialization without using any third party JSON library. Please follow below steps:

  1. Create a common utility for serialize any object: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Serialization;

    namespace Test.Utility
    {
        public class CommonUtility
        {
            public static JavaScriptSerializer javaScriptSerializer = new    JavaScriptSerializer();
    
            public static string SerializeJson(object o)
            {
                return javaScriptSerializer.Serialize(o);
            }
        }
    }
    
  2. Then add Test.Utility reference where you want to serialize:

    using Test.Utility;
    
  3. Then you write code for JSON serialize:

    var Info = new
    {
        TeamCode = TeamCodeTxt.Text,
        TeamName = TeamNameTxt.Text
    };
    

    String jsonSerializedStr=CommonUtility.SerializeJson(Info);

  4. Write code for deserialization:

    class TestInfo
    {
        public TeamCode{set;get;}
        public TeamName{set;get;}
    }
    Info objInfo=new Info();
    objInfo=serializer.Deserialize<TestInfo>(Info);
    
Ramakant Shukla
  • 137
  • 1
  • 10
0

Using System.Runtime.Serialization.Json

[DataContract]
[Serializable]
public class SuperObj
{
  [DataMember]
  public string Foo { get; set; }

  [DataMember]
  public string Bar { get; set; }

  [DataMember]
  public int Baz { get; set; }

  [DataMember]
  public DateTime Qux { get; set; }
}

Can be serialized to a string using

var serializer = new DataContractJsonSerializer(typeof(SuperObj));

using (var stream = new MemoryStream())
{
  serializer.WriteObject(stream, obj);
  var jsonString = Encoding.UTF8.GetString(stream.ToArray());
  Console.WriteLine(jsonString);
}

That string can be deserialized back into an object

var serializer = new DataContractJsonSerializer(typeof(SuperObj));

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
  var obj2 = (SuperObj)serializer.ReadObject(stream);
  Console.WriteLine("SuperObj");
  Console.WriteLine(obj2.Foo);
  Console.WriteLine(obj2.Bar);
  Console.WriteLine(obj2.Baz);
  Console.WriteLine(obj2.Qux);
}

Like others have said though, the third party libraries tend to be better (I've used ServiceStack.Text and Json.Net)

wdavo
  • 5,070
  • 1
  • 19
  • 20