Is this possible to not change Int32 type during serialization/deserialization?

Aug 12, 2010 at 2:21 PM
Edited Aug 12, 2010 at 2:23 PM


I have the dictionary with the following initial data:

dictStart = {
  "ReportDate": "3/7/2010 12:00:00 AM",
  "Id": 395,
  "Name": "TEST",
  "Currency": "USD" 

where   "Id" has  Int32 type

Then I use


 string json = JsonConvert.SerializeObject(dictStart, Formatting.Indented);

dictFinish = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);


After deserialization the "Id" field has Int64 type in "dictFinish" dictionary, that causes problem for us.

Is this possible to not change "Id" type during serialization/deserialization ?

Is this configurable some way?

Thanks in advance, Alex.


Aug 12, 2010 at 2:44 PM

I've found in code that this logic is defined like this:


 else if (nonBase10)
        numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
          ? Convert.ToInt64(number, 16)
          : Convert.ToInt64(number, 8);
        numberType = JsonToken.Integer;
        numberValue = Convert.ToInt64(number, CultureInfo.InvariantCulture);
        numberType = JsonToken.Integer;

So as far as I understood there are no abilities to know what exactly number type is passed outside...

Am I right?

Aug 12, 2010 at 10:16 PM
Edited Aug 12, 2010 at 10:18 PM

What do you mean by not change it?

The number that is coming from the JSON text has no type. An Int64 is used to store JSON integers because it is the largest and will hold the greatest range of possible numbers.

If you want it deserialized to a specific type then you will need to deserialize to a class so Json.NET will know to convert the Id integer to an Int32.

Aug 13, 2010 at 9:50 AM

JamesNK, thanks for your answering my question.

When I asked you, i didnt know that "The number that is coming from the JSON text has no type".

Then I discovered that by debugging and source analyzing.

Thanks, Alex.