Click here to Skip to main content
Click here to Skip to main content

Tagged as

Strongly typed deserialization of ASP.NET 3.5 ScriptService JSON in Managed code. e.g. {"d":{"__type":

, 14 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
NOTE: This article presents another solution that does not depend on JSON.net.Doing interop with an ASP.NET ScriptService in JavaScript is simple, just deserialize the payload, e.g. result.d.But if you are consuming with managed code it gets a bit tricky.The 3.5 stack wraps the payload in...

NOTE: This article presents another solution that does not depend on JSON.net.

Doing interop with an ASP.NET ScriptService in JavaScript is simple, just deserialize the payload, e.g. result.d.

But if you are consuming with managed code it gets a bit tricky.

The 3.5 stack wraps the payload in a 'd' container making deserialization to a similarly shaped type or a type in a shared library (if you are using a shared library, why use JSON? There are reasons... Poke tongue | ;-P ) impossible using any of the ms serializers.

My solution is to use Newtonsoft's JSON.NET with a simple generic helper class to unwrap the payload..

public class AjaxWrapper<T>
{
    public T d;   
}
and then deserialize with JSON.NET....
var response = HttpRequestHelper.AjaxApp([get some json from an asp.net 3.5 scriptservice]);
 
// response = "{"d":{"__type":"MyNamespace.MyType","Assertions":0,....."

AjaxWrapper<MyNamespace.MyType> result = Newtonsoft.Json.JsonConvert.DeserializeObject<AjaxWrapper<MyNamespace.MyType>>(response);
 

Easy.....

In the example, the typenames are the same for clarity but the type specified in the json is ignored by JSON.NET.

As long as the client-side type is similarly shaped you are golden.

Note: this applies to web services, not webhttp wcf services as there are attributes to get them to server POJO JSON that can be directly consumed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sky Sanders
Software Developer (Senior) Salient Solutions
United States United States
My name is Sky Sanders and I am an end-to-end, front-to-back software solutions architect with more than 20 years experience in IT infrastructure and software development, the last 10 years being focused primarily on the Microsoft .NET platform.
 
My motto is 'I solve problems.' and I am currently available for hire.
 
I can be contacted at sky.sanders@gmail.com

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 14 Apr 2010
Article Copyright 2010 by Sky Sanders
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid