Complicate json object?

  • Thread starter Thread starter Billy Zhang
  • Start date Start date
B

Billy Zhang

I have a asp.net app, in client I create a Complicate json string,
for example: { a: ‘a’, b : { b ; ‘b’ } }

If .net has corresponding type, it could be
deserialized, for example:

DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(A));
using (MemoryStream ms = new
MemoryStream(Encoding.Unicode.GetBytes(jsonText))) {
//serializer.WriteObject(ms);
//return Encoding.Default.GetString(ms.ToArray());
A a = (A)serializer.ReadObject(ms);
return a;

}

But the question is if there are no corresponding .net type, in .net 3.5
could we serialize it into anonymous type? Or any other way to solve this
problem?

-Billy zhang
 
Hi Billy,

As for the WCF JSON serializer, I'm afraid so far it doesn't support
serialization upon anonymous type. Actually all the .NET serialization
formatter cannot serialize with anonymous type. I've tried directly
serialize it via the following code but it report a type is not serialable
error:

====================
var obj = new { Name = "abc", Title = "abc" };

DataContractJsonSerializer jsonserializer = new
DataContractJsonSerializer(obj.GetType());

MemoryStream ms = new MemoryStream();
ms.Close();

jsonserializer.WriteObject(ms, obj);
=========================

Also, anonymous's autogenerated type name is random(based on runtime,
appdomain info) which is not reliable and persistable in districuted
environment. For your scenario, if you have some complex data blocks which
will be constructed on the fly(without an existing definted type), I think
it would be better to manually pass the data as string parameter and
manually parse it in code. How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we

can improve the support we provide to you. Please feel free to let my
manager know what you think of

the level of service provided. You can send feedback directly to my manager
at: (e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response

from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take

approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution.

The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump

analysis issues. Issues of this nature are best handled working with a
dedicated Microsoft Support

Engineer by contacting Microsoft Customer Support Services (CSS) at

http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
Hi Billy,

Is there anything else we can help on this?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we

can improve the support we provide to you. Please feel free to let my
manager know what you think of

the level of service provided. You can send feedback directly to my manager
at: (e-mail address removed).

--------------------
X-Tomcat-ID: 2894094
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Wed, 13 Aug 2008 03:38:58 GMT
Subject: RE: Complicate json object?
 
Back
Top