Utf8Json icon indicating copy to clipboard operation
Utf8Json copied to clipboard

Question: deserializing string as number

Open yotamoron opened this issue 7 years ago • 3 comments

Hi.

We are considering switching to Utf8Json from Newtonsoft. From one of our external providers, we receive a json string, where we have something like: ... "someField": "123", ...

and in the corresponding object, we have: ... int somefield; ...

Newtonsoft knows how to treat the string properly and parse it as a number, while Utf8Json throws an exception with: expected:'Number Token', actual:'"0"', at offset:297

How can we fix that in Utf8Json?

Thanks.

yotamoron avatar May 15 '18 11:05 yotamoron

Same problem. Help! Thanks.

hll60 avatar Aug 30 '19 07:08 hll60

I think that you'll have to create your own IJsonFormatterResolver and use it when serializing/deserializing to/from json. So: IJsonFormatterResolver intAsStringResolver = <Create the new resolver that will handle the issue>; String json = Utf8Json.JsonSerializer.ToJsonString(myObject, intAsStringResolver);

In the resolver, you'll have to implement IJsonFormatter that will be returned by the resolver when needed, and in this IJsonFormatter you can handle this issue.

Hope this helps.

yotamoron avatar Aug 30 '19 08:08 yotamoron

@yotamoron how did you create your own intAsStringResolver? I am having the same problem. Please help. Thanks

penguinawesome avatar May 13 '20 15:05 penguinawesome