mgholam / fastJSON

Smallest, fastest polymorphic JSON serializer
https://www.codeproject.com/Articles/159450/fastJSON-Smallest-Fastest-Polymorphic-JSON-Seriali
MIT License
478 stars 148 forks source link

disable automatic parsing values #101

Closed LukAss741 closed 4 years ago

LukAss741 commented 5 years ago

Is there any way to disable automatic parsing values? I need everything to be kept as string. I tried AutoConvertStringToNumbers = false but it has not effect for me. In my use case I dynamically parse jsons from different sources with different structures. Hence I parse it to Dictionary<string, object>.

this code:

foreach (var item in fastJSON.JSON.ToObject<Dictionary<string, object>>("{\"a\":\"123\",\"b\":123,\"c\":\"1.23\",\"d\":1.23,\"e\":\"0.23\",\"f\":\"0.23\",\"g\":\"123e-2\"}", new 
fastJSON.JSONParameters { AutoConvertStringToNumbers = false}))
{
    Console.WriteLine(item.Value + "\ttype: " + item.Value.GetType());
    //decimal value = decimal.Parse((string)item.Value); // not possible
}

outputs:

123     type: System.String
123     type: System.Int64
1.23    type: System.String
1,23    type: System.Decimal
0.23    type: System.String
0.23    type: System.String
123e-2  type: System.String

so for System.Int64 decimal value = decimal.Parse((string)item.Value) is not possible.

I would like output to be:

123     type: System.String
123     type: System.String
1.23    type: System.String
1.23    type: System.String
0.23    type: System.String
0.23    type: System.String
123e-2  type: System.String

so I can be sure all values are string and decide myself how to process it and whether or not to parse it.

This is not only case for numbers. I would like values of all kinds to be kept as strings by default.

mgholam commented 5 years ago

fastJSON reads strings as string and numbers as long or decimal if it has a point or not. AutoConvertStringToNumbers= false only works when trying to put the values into an object, not when reading the json into an internal dictionary before processing.