ohler55 / ojg

Optimized JSON for Go
MIT License
839 stars 50 forks source link

Double value parse error on iOS platform #107

Closed hw260635 closed 1 year ago

hw260635 commented 1 year ago

input jsonRequestString: {"sessionId":0,"data":{"type":"request_device_set","info_id":"","data":{"sessionId":0,"data":{"WDRC_6_GT":[0.00014944501139803467,0.00015953007112451845]}},"device_id":""}}

requestObj, error := oj.ParseString(jsonRequestString)

output requestObj: map[data:map[data:map[data:map[WDRC_6_GT:[0.0019242805885176385 0.002054137614082889]] sessionId:0] device_id: info_id: type:request_device_set] sessionId:0]]

values in WDRC_6_GT error

ohler55 commented 1 year ago

I'll look at it tonight.

ohler55 commented 1 year ago

Please try the bignum-fix branch.

ohler55 commented 1 year ago

Release v1.17.2 with the fix.

xujianjun2000 commented 1 year ago

Release v1.17.2 with the fix.

Thx Peter, This fix is really helpful to my work! And just have some confusion on "bignum-fix branch" when tried : the json is : { arr :[7.347709823877271e-05, 0.00010903867569251947]} pseudo code: 1) obj = oj.ParseString(json) 2) arr = JsonPath("$..arr").First(obj).([]any) 3) then range and check arr's element type using (e.g.) switch element.(type) the first element gives float64 (7.347709823877271e-05) the second gives json.Number (0.00010903867569251947)

can I detemine this json.Number is a float64, not a int64?

ohler55 commented 1 year ago

Numbers are initially parsed as int64 or float64 until the number of digits exceeds the limits of each type and then the type reverts to json.Number. A json.Number is neither an int64 nor a float64. It can be convert to either but with a loss of precision if to a float64. If to an int64 and error is returned if the json.Number is out of range. Basically json.Number is a string that represents a number. It is up to the caller to decide what to do with it. That includes converting to int64, float64, or big numbers.

xujianjun2000 commented 1 year ago

Numbers are initially parsed as int64 or float64 until the number of digits exceeds the limits of each type and then the type reverts to json.Number. A json.Number is neither an int64 nor a float64. It can be convert to either but with a loss of precision if to a float64. If to an int64 and error is returned if the json.Number is out of range. Basically json.Number is a string that represents a number. It is up to the caller to decide what to do with it. That includes converting to int64, float64, or big numbers.

That's great guide on my case! Thank you so much!