Preserving exact type in object[] serialization.

Nov 5, 2010 at 11:04 AM

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.


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?




Nov 5, 2010 at 12:30 PM
Edited Nov 5, 2010 at 12: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 3:04 PM

But let's say I don't need any level of compatibility with json standard...  I use 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?


Nov 5, 2010 at 11:14 PM

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