Skip to content
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

Add option to parse quoted numbers #156

Open
traderqq opened this issue Nov 17, 2021 · 3 comments
Open

Add option to parse quoted numbers #156

traderqq opened this issue Nov 17, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@traderqq
Copy link

It would be great if such option will be implemented.
Often external services provide json data not really caring whether numbers are quoted or not, and this results in more complicated code to support it.
Microsoft added it too in System.Text.Json some time ago (dotnet/runtime#30255)

@Tornhoof Tornhoof added the enhancement New feature or request label Nov 17, 2021
@Tornhoof
Copy link
Owner

If you know for certain that a number is quoted, you can take a look at https://github.com/Tornhoof/SpanJson/blob/master/SpanJson.Tests/CustomSerializerTests.cs

This test has a custom quoted long formatter.

For the case where you need to handle both at runtime this is not that easy to solve efficiently

@traderqq
Copy link
Author

This only work when numbers are always quoted, and to support both quoted and not I'm ending up with two classes anyway (which I was trying to avoid - real structure is the same, only format differs).
In my case, quotes in numbers does not come entirely arbitrary, when I subscribe I know format.
My hope was that I could assign property a type, say long, and using some magic from the lib it will be cast to long whether it quoted or not.

@ferrarimartin
Copy link

ferrarimartin commented Jun 10, 2022

I've implemented this as we also get bad formatted json, and we don't know whether numbers will be quoted or not. We're using it in production, but I'd rather get the bad json fixed when we can. The change is rather simple, it's just processing quotes inside ReadUtf8NumberInternal() and ReadUtf8NumberDigits() (and probably the Utf16 versions too, but we're not using those). This library is really good, so thank you!!!
I think processing bad json is not very good, but I have this change, and also allowing identifiers without quotes (for example, "{ Number: 5}", and bool values between quotes. If you are really thinking about implementing this, I might contribute (if my code makes any sense, of course).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants