runtime: Add non-try APIs of TryGetXValue to Utf8JsonReader that throw.
We already have the following “convert to .NET type” APIs on Utf8JsonReader:
public ref partial struct Utf8JsonReader
{
// These throw InvalidOperationException if there is a token type mismatch
public string GetStringValue();
public bool GetBooleanValue();
// These throw InvalidOperationException if there is a token type mismatch (i.e. not JsonTokenType.Number).
public bool TryGetInt32Value(out int value);
public bool TryGetInt64Value(out long value);
public bool TryGetSingleValue(out float value);
public bool TryGetDoubleValue(out double value);
public bool TryGetDecimalValue(out decimal value);
}
Add the following:
public ref partial struct Utf8JsonReader
{
// These throw InvalidOperationException if there is a token type mismatch.
// These throw OverflowException if the number value doesn't fit into the .NET primitive.
public int GetInt32Value();
public long GetInt64Value();
public float GetSingleValue();
public double GetDoubleValue();
public decimal GetDecimalValue();
}
Questions:
- What should the semantics of these APIs be (particularly the TryGet APIs)? Should we throw on token type mismatch or return false?
- Should we drop the suffix
Value
from the names? - Should we rename these to remove the type name from their names (similar to SequenceReader, BinaryPrimitives, Utf8Parser, etc.)? i.e. just overload:
GetValue(out X value)
andTryGetValue(out X value)
- We can only do that if they are void returning with an out parameter. If they return the type, then we need the type names within the method name to disambiguate.
- Do we need other numeric overloads like
GetInt16
,GetUInt64
, etc.?
cc @JeremyKuhne, @GrabYourPitchforks, @KrzysztofCwalina, @stephentoub, @terrajobst , @bartonjs
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 25 (25 by maintainers)
Commits related to this issue
- CoreFx #33320 Implement Get APIs using TryGet — committed to WinCPP/corefx by WinCPP 5 years ago
- CoreFx #33320 Implement Get APIs using TryGet — committed to WinCPP/corefx by WinCPP 5 years ago
- CoreFx #33320 Implement Get APIs using TryGet — committed to WinCPP/corefx by WinCPP 5 years ago
- Implement Get APIs using TryGet counterparts (#34604) * CoreFx #33320 Implement Get APIs using TryGet * CoreFx #33320 Implement Get APIs using TryGet * Fixed NITs — committed to dotnet/corefx by WinCPP 5 years ago
Do we really need methods for Decimal parsing on
Utf8JsonReader
?Decimal is specialized type just like BigInteger (and many other types one can thing of). We should enable parsing of all specialized types via composition: Get the Span with the value and pass it to the parse method of the specialized type.
Fixed in https://github.com/dotnet/corefx/pull/34604
@WinCPP SR.cs is autogenerated from Strings.resx. If you build the library after editing Strings.resx it should regen the SR file and Intellisense gets happy. (If you care capable of ignoring Intellisense telling you it doesn’t exist you can just add it + use it, then rebuild to confirm happy).
But would it be proper to throw overflow exception regardless?
In this case, yes. The checks you mentioned would never be hit since Utf8JsonReader.GetInt32() is only ever called on valid JSON (the JsonReader would throw on invalid input), i.e. the JSON number exists and is correctly formatted (i.e. not empty, not just a ‘-’/‘+’, etc.).
Do you have a customer for JSON+BigInteger? If not, I’d hold off, just to keep the layering more flexible.
public int GetInt32()
looks like it has more precedent thanpublic int GetInt32Value()
.So, maybe