Preserving exact type in object[] serialization.

Nov 5, 2010 at 12:04 PM

I'am trying to serialize array of objects (object[]). It works great, if non-value types are used (like my own classes). Unfortunatelly if I try to serialize array full of value-types, exact types are not preserved.

Example:

object[] forSerialization = new object[] { 1m, 1.0f, 3 };

If I serialize this array and then deserialize it, I won't get exact types (decimal, float, int). Instead I would get double, double and int64. 

So is it possible to preserve value type type?

Regards,

Bozydar

 


Coordinator
Nov 5, 2010 at 1:30 PM
Edited Nov 5, 2010 at 1:32 PM
Nope. There is no way in your example to determine the exact .NET type from a piece of json text.
Nov 5, 2010 at 4:04 PM

But let's say I don't need any level of compatibility with json standard...  I use json.net for internal communication...

Is it possible to write custom JsonConverter for object[] (and object) and serialize all value-types as objects (or with original type hint)? Will it work?

 

Coordinator
Nov 6, 2010 at 12:14 AM

Sure, you can do what you want in a JsonConverter.