views:

117

answers:

2

I'm trying to keep from depending on open source or third party libraries such as Json.NET to parse incoming JSON from an HttpWebResponse. Why? Because the more reliance on open source frameworks to aid in your implementations, the more your app has to rely on those dependencies...I don't like my apps to be depenent on a lot of libraries for many reasons if at all possible. I'm ok with using stuff like Enterprise Library because it's supported by MS but I'm taking more open source libraries.

Anyway, I'm trying to figure out the best way to parse incoming JSON server-side in .NET 3.5.

I know this is going to get a lot of responses and I've even used the .NET 3.5 JavaScriptSerializer to serialize data to JSON but now I'm trying to figure out the best and most simple way to do the reverse without again, having to use a 3rd party / open source framework to aid in this.

+2  A: 

You can use JavaScriptSerializer for deserialization too: JavaScriptSerializer.Deserialize Method

As for avoiding third party libraries you are missing a lot if you are only using those provided by Microsoft. There are many excellent OSS libraries. Here is a nice overview of this topic: Thoughts on Microsoft History and OSS

Giorgi
yes but you always run the risk of it not being supported, or whatever... I don't just use MS third parties but I don't use a lot of third party libraries for sure.
CoffeeAddict
what about WebClient.DownloadString(response) I looked up that method but I don't really know if it can perse the JSON into a string like it can XML from a response.
CoffeeAddict
Did you read the article I linked to? You risk with Microsoft too.
Giorgi
ok yes but you're comparing apples to oranges in that most open source frameworks such as for example json.NET have 1 or just a few people supporting it. And you're relying on the community in that you think that it's going to support your needs and support every .net version that comes out..I would rather stick wtith MS as much as possible even if they don't have the resources x years down the road. But in the meantime what I use from them I know works...and I know what it works with or without. Using a one-person library is risky as hell (e.g. Spark)
CoffeeAddict
Yes there's risk with MS but surely not like there is with one guy running the damn thing.
CoffeeAddict
The `JavaScriptSerializer` class was marked obsolete in .NET 3.5. Microsoft recommends the `DataContractJsonSerializer` now.
Wallace Breza
Wallace, thank you.
CoffeeAddict
+2  A: 

The Microsoft recommended JSON serializer is DataContractJsonSerializer This class exists within the System.Runtime.Serialization assembly

The sample demonstrates deserializing from JSON data into an object.

MemoryStream stream1 = new MemoryStream();     
Person p2 = (Person)ser.ReadObject(stream1);

To serialize an instance of the Person type to JSON, create the DataContractJsonSerializer first and use the WriteObject method to write JSON data to a stream.

Person p = new Person();
//Set up Person object...
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
ser.WriteObject(stream1, p);

Update: Added Helper class

Here is a sample helper class that you can use for simple To/From Json serialization:

public static class JsonHelper
{
    public static string ToJson<T>(T instance)
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var tempStream = new MemoryStream())
        {
            serializer.WriteObject(tempStream, instance);
            return Encoding.Default.GetString(tempStream.ToArray());
        }
    }

    public static T FromJson<T>(string json)
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var tempStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            return (T)serializer.ReadObject(tempStream);
        }
    }
}
Wallace Breza
do you happen to know when this class was created? Meaning I wonder if this is a newer framework class than lets say the .NET 3.5 JavaScript serialize. Because I'd prefer to go with whatever is the latest since it's probably not going to be outdated as soon...if at all.
CoffeeAddict
@CoffeeAddict, I added a sample helper class that provides both serialization and deserialization.
Wallace Breza
where did you see that MS recommends this..I'm on MSDN but do not see that.
CoffeeAddict
`DataContractJsonSerializer` is newer and came out with .NET 3.5. The `JavaScriptSerializer` was originally introduced in ASP.NET 1.0 Extensions for ASP.NET 2.0.
Wallace Breza
Yes, see that. I'm just trying to figure out which way to go here basically now. JavaScriptSerializer vs. the DataContract
CoffeeAddict
Thanks for the example, it helps a lot in terms of realizing more about the DataJsonSerializer..
CoffeeAddict
thanks a lot. It's just you never know which way to go these days...
CoffeeAddict
I think you can also use the WebClient object as well to deserialize to a string using the WebClient.DownloadString() method
CoffeeAddict