469,631 Members | 1,296 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Complicate json object?

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
Aug 13 '08 #1
1 1599
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: ms****@microsoft.com.

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

http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: =?Utf-8?B?QmlsbHkgWmhhbmc=?= <ht****@noemail.noemail>
Subject: Complicate json object?
Date: Tue, 12 Aug 2008 18:48:09 -0700
>
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
Aug 13 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Luke Matuszewski | last post: by
2 posts views Thread by Kevin Newman | last post: by
3 posts views Thread by Adam | last post: by
1 post views Thread by Red Daly | last post: by
23 posts views Thread by dhtmlkitchen | last post: by
9 posts views Thread by Jon Paal [MSMD] | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.