-
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
System.Text.JsonSerializer - Provide a mechanism to begin deseralization at a specific name/value pair. #49598
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsBackground and MotivationIt is common to enclose json serialized data in a named wrapper object i.e. Please consider the FRED API which is widely used in the finance world. Note the following:
How might we efficiently implement clients to call the FRED API and deserialize either XML or Json data? One approach is to create a base class as shown below with an abstract This class also provides methods to download data by calling various APIs as defined by the data provider. In theory these methods should not need to be overridden by an inheriting class as the deseralization work is done only in the
Here is an example of how an XML client might be implemented. Note we can pass a string in indicating where in the document the deserializer should start its work. This string is used to define the
Here is an example of a JSONClient. Unlike the XML implementation we have no mechanism to tell If we use use a wrapper class we must a) define a wrapper class for each type of object defined by the API, and b) override all Get... methods on the base i.e. Another option is to write custom
Proposed APIThe functionality might be implemented by adding a property like
Usage ExamplesGiven the following Json:
This code will produce a List containing one object of the structure shown above:
Alternative DesignsPlease suggest. RisksNone that I'm aware of.
|
We are planning on releasing a new writeable DOM with |
@eiriktsarpalis Would you mind showing a brief example of how it might be used in the problem I've illustrated? |
The API is still being finalized but the rough idea is encapsulated in the design doc under API Walkthrough. Instead of passing the contents of |
Addressed by #29690 in .NET 6 |
Background and Motivation
It is common to enclose json serialized data in a named wrapper object i.e.
{ "data": {...} }
. The wrapper object is typically ignored. It would be useful and convenient to be able to directJsonSerializer
to deserialize only the portion of a document starting at a specific name/value pair. This would allow the developer to avoid the use of temporary wrapper classes or custom JsonConverters.Please consider the FRED API which is widely used in the finance world. Note the following:
<categories>...</categories>
for xml data and"categories": {...}
for json data.How might we efficiently implement clients to call the FRED API and deserialize either XML or Json data?
One approach is to create a base class as shown below with an abstract
Parse
method that can be implemented by respective Json / XML overrides.This class also provides methods to download data by calling various APIs as defined by the data provider. In theory these methods should not need to be overridden by an inheriting class as the deseralization work is done only in the
Parse
method :Here is an example of how an XML client might be implemented. Note we can pass a string in indicating where in the document the deserializer should start its work. This string is used to define the
XmlRootAttribute
object as shown. This implementation is basically effortless and allows us to use the stream returned by the Download method:Here is an example of a JSONClient. Unlike the XML implementation we have no mechanism to tell
JsonSerializer
where to begin it's work.If we use use a wrapper class we must a) define a wrapper class for each type of object defined by the API, and b) override all Get... methods on the base i.e.
Parse<List<Category>>
becomesParse<List<CategoryWrapper>>
. In the example code below I use aJsonDocument
to avoid these steps. However, this results in a costly string allocation and precludes us from using the stream provided by theDownload
method.Another option is to write custom
JsonConverter
classes but this really defeats the purpose using a library for a well-known serialization protocol.Proposed API
The functionality might be implemented by adding a property like
RootElement
toJsonSerializerOptions
:Usage Examples
Given the following Json:
This code will produce a List containing one object of the structure shown above:
Alternative Designs
Please suggest.
Risks
None that I'm aware of.
The text was updated successfully, but these errors were encountered: