-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Expose TryRead() as public on JsonConverter #60904
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsBackground and motivationThe current JsonConverter has an API ProposalThe proposal is to change the visibilty on public class JsonConverter<T>
{
public virtual bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, out T? value);
} API Usagevoid Run()
{
Stream someHugeStream = Request.Body; // This stream is so large that it causes `Utf8JsonReader.IsFinalBlock == false` when calling custom converters.
var options = JsonSerializerOptions() { Converters = new MyCustomObjectConverter() };
JsonSerializer.DeserializeAsync<List<MyCustomObject>>(someHugeStream, options);
}
public class MyCustomObjectConverter : JsonConverter<MyCustomObject>
{
...
public override bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, out MyCumstomObject value)
{
// Handle case where `reader.IsFinalBlock == false`, for example by calling `reader.TrySkip()`
...
if (reader.trySkip())
{
value = null;
return false;
}
...
return true;
}
} Alternative DesignsNo response RisksNo response
|
Related to #36785. Exposing some variation of the currently internal Note however that it's not as simple as removing the |
Note that A custom converter doesn't have to worry about the "out of data" scenario since there is a "read ahead" feature for custom converters, and for the write case the buffer is flushed, if necessary, after the converter returns from Write(). This is different from the internal converters which have a finer granularity of per-property and per-element ReadAsync\WriteAsync support. |
This issue has been marked with the When ready to submit an amended proposal, please ensure that the original post in this issue has been updated, following the API proposal template and examples as provided in the guidelines. |
Per the request in #29902, we should consider offering similar methods on the |
Closing in favor of #63795. |
Background and motivation
The current JsonConverter has an
interanal virtual OnTryRead()
here.For custom converter implementations that call e.g.
reader.TrySkip()
it would be neat to be able to actually returnfalse
if the conversion fails.The suggestion is motivated by this SO question: https://stackoverflow.com/questions/63038334/how-do-i-handle-partial-json-in-a-jsonconverter-while-using-deserializeasync-on
API Proposal
The proposal is to change the visibilty on
OnTryRead()
(and maybeOnTryWrite()
) topublic
instead ofinternal
, but without theReadStack parameter
. Something like:API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: