-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON parsing with "marshal.to" raises exception if float attribute value has no decimal delimiter #16675
Comments
when true:
import std/[jsonutils,json]
type Payment* = object
currency*: string
amount*: float64
desc*: string
var p: Payment
p.fromJson("{ \"currency\": \"EUR\", \"amount\": 5 }".parseJson, opt = Joptions(allowMissingKeys: true))
doAssert p.amount == 5.0
let p2 = "{ \"currency\": \"EUR\", \"amount\": 5 }".parseJson.jsonTo(Payment, opt = Joptions(allowMissingKeys: true)) jsonutils is the future IMO, it has more options, supports tuples (named, unnamed), supports object variants etc;
@darklynx would you consider your first PR to fix either of those? the 1st item is the easiest |
Confirmed. You can only use "to" on strings created by "$$". |
@darklynx you can also consider planetis-m/eminim but be aware it isn't a fully featured std/json replacement nim-lang/RFCs#247 |
Thanks a lot for explanation to everyone! @Araq just as I thought 👍 I'm going to report the potential issues with current code generated by OpenAPI generator and suggest the usage of |
JsonParsingError is raised if marshaling a number without decimal delimiter into a property of float type
Example
Current Output
Expected Output
Possible Solution
Replacing
5
with5.0
in the JSON example provides expected result:Additional Information
I'm very new to Nim, and I might be just wrong about my expectations towards the
marshal.to
method. I have a feeling that it was not originally designed to perform JSON parsing by the JSON rules, it just accidentally can do that because "marshal" and "unmarshal" are using JSON representation in Nim.However, I have discovered this issue while trying to use OpenAPI generator for Nim language. It uses
marshal.to
method when parsing incoming JSON response into a structure of expected type: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/nim-client/api.mustache#L24It works quite nice, unlike
json.to(parseJson("..."), T.typedesc)
construction, which fails on optional properties: if JSON message does not contain some fields that are defined by type:Maybe I should just report a bug in the "OpenAPI generator" project to the author of Nim client generator. But I would like first to get confirmation that
marchal.to
will never be a fully functional JSON parser.Nim version (I'm using home brew version):
The text was updated successfully, but these errors were encountered: