-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Utf8JsonReader/Writer should expose useful (currently internal) methods. #54016
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsBackground and MotivationThe goal is to implement a Json read/write default behavior that correctly handles "big integers" in the context of ECMAScript without data loss and using strings for all numbers. JSON format defines no limitation on numeric values. 64 bits integer and BigInteger can be write and read without loss in JSON.
These APIs are de facto required if one want to implement a specific treatement for numbers (and may also help people that deal with strange/exotic/ill-formed Json in input as well as for output). Currently, only the (internal) converters can use them. Proposed APIThe proposal is rather simple since it's about publishing existing internals. Only numeric types that are not "ECMAScript compliant" can be concerned, or all the types. Minimalist approach (publicly expose the currently internal following methods):On the Utf8JsonWriter, there is nothing to do: void WriteNumberValueAsString(ulong value);
void WriteNumberValueAsString(long value);
void WriteNumberValueAsString(decimal value); On the Utf8JsonReader. The Get/TryGet pattern should be exposed: bool TryGetInt64WithQuotes( out long value );
long GetInt64WithQuotes();
bool TryGetUInt64WithQuotes( out ulong value );
ulong GetUInt64WithQuotes();
bool TryGetDecimalWithQuotes( out decimal value );
decimal GetDecimalWithQuotes(); Note: The current GetXXXWithQuotes implementations are called by converters that guard their calls. Full types covering.For the sake of completness, the same API for other numbers (that are "ECMAScript compliant") can be exposed: byte, sbyte, short, ushort, etc. Alternative DesignsThe RisksNone (since this doesn't weaken the Validation layer).
|
My personal choice would be the "FromString" naming: writer.WriteNumberValueAsString( d );
...
d = reader.GetDecimalFromString(); |
If I'm understanding the request correctly, it should still be possible to achieve this using a custom converter that converts the number to a string and passing to either of the existing Honestly, I don't believe this meets the bar for inclusion to System.Text.Json. The appropriate layer for expressing these types of concerns is converter layer and I believe the existing APIs should be sufficient to achieve this. |
So you and/or Microsoft and/or the official Json serializer layer would ever be the only one to implement basically optimized (understand: not stupidly unefficient) serialization code? I may have missed to explain one important thing: I'm working at the writer level, I have NO "serializer" at hand (this beast is "above" the level of the generated code that implement serialization). May you, please, reconsider this (hard to understand) answer? |
I'm not sure I follow, would it not be possible to implement a similarly efficient method on your side if necessary?
I'm sure this should not be a blocker, since the above could be expressed as extension methods on the writer and reader classes. |
With the new Raw write, you're right. I will happily have to duplicate code... ;) |
We can revisit if there is strong evidence that lots of people need this, but in practice there are always minute variations in user requirements. In the interest of keeping the library size small, we prioritize having a small set of APIs that unblock users (even if it does require a bit of boilerplate on their side) rather than exposing functionality for every conceivable use case. |
Background and Motivation
The goal is to implement a Json read/write default behavior that correctly handles "big integers" in the context of ECMAScript without data loss and using strings for all numbers.
Important: This is at the Utf8JsonReader/Writer level, not at the serializer level (the actual de/serialization code is generated and uses the reader/writer).
JSON format defines no limitation on numeric values. 64 bits integer and BigInteger can be write and read without loss in JSON.
Among the basic types, the long (Int64), ulong (UInt64) and Decimal (and of course, the new BigInt) representations are not ECMAScript compliant. They must use a string representation, just like JsonNumberHandling.WriteAsString specifies it.
These APIs are de facto required if one want to implement a specific treatement for numbers (and may also help people that deal with strange/exotic/ill-formed Json in input as well as for output). Currently, only the (internal) converters can use them.
Proposed API
The proposal is rather simple since it's about publishing existing internals. Only numeric types that are not "ECMAScript compliant" can be concerned, or all the types.
Minimalist approach (publicly expose the currently internal following methods):
On the Utf8JsonWriter, there is nothing to do:
On the Utf8JsonReader. The Get/TryGet pattern should be exposed:
Note: The current GetXXXWithQuotes implementations are called by converters that guard their calls.
Full types covering
For the sake of completness, the same API for other numbers (that are "ECMAScript compliant") can be exposed:
byte
,sbyte
,short
,ushort
, etc. (even if it's not strictly required to support ECMAScript).Alternative Designs
The
Get
name pattern may be changed from[Try]GetXXXWithQuotes
to[Try]GetQuotedXXX
or (to enforce the parallel with the writer) to[Try]GetXXXFromString
.Usage
Instead of writing this unefficient code:
We'll be able to write:
Risks
None (since this doesn't weaken the Validation layer).
The text was updated successfully, but these errors were encountered: