I am updating some apps for .NET Core 3.x, and as part of that I'm trying to move from Json.NET
to the new System.Text.Json
classes. With Json.NET, I could deserialize an anonymous type like so:
var token = JsonConvert.DeserializeAnonymousType(jsonStr, new { token = "" }).token;
Is there an equivalent method in the new namespace?
Json does case-insensitive property name matching by default. The System. Text. Json default is case-sensitive, which gives better performance since it's doing an exact match.
Deserializes the specified JSON string into an Apex object of the specified type. deserializeUntyped(jsonString) Deserializes the specified JSON string into collections of primitive data types. serialize(objectToSerialize) Serializes Apex objects into JSON content.
Obsolete. The value types allowed by the JsonSchema. JSON Schema validation has been moved to its own package. See https://www.newtonsoft.com/jsonschema for more details.
Json was basically scrapped by Microsoft with the coming of . NET Core 3.0 in favor of its newer offering designed for better performance, System.
Depending on your scenario, an alternative approach is to use anonymous types, like this: Basically you select properties from another object and format them as desired, and then serialize it. If you need to deserialize the JSON created from the anonymous type, you can deserialize to a dynamic object.
Deserialize (String, Type, JsonSerializerOptions) Parses the text representing a single JSON value into an instance of a specified type.
As of . Net 5.0, deserialization of immutable types -- and thus anonymous types -- is supported by System.Text.Json. From How to use immutable types and non-public accessors with System.Text.Json: System.Text.Json can use a parameterized constructor, which makes it possible to deserialize an immutable class or struct.
Use the library directly, not through a framework such as ASP.NET Core. Use the JsonSerializer class with custom types to serialize from and deserialize into. For information about how to read and write JSON data without using JsonSerializer, see How to use the JSON DOM, Utf8JsonReader, and Utf8JsonWriter.
As of .Net 5.0, deserialization of immutable types -- and thus anonymous types -- is supported by System.Text.Json
. From How to use immutable types and non-public accessors with System.Text.Json:
System.Text.Json
can use a parameterized constructor, which makes it possible to deserialize an immutable class or struct. For a class, if the only constructor is a parameterized one, that constructor will be used.
As anonymous types have exactly one constructor, they can now be deserialized successfully. To do so, define a helper method like so:
public static partial class JsonSerializerExtensions
{
public static T? DeserializeAnonymousType<T>(string json, T anonymousTypeObject, JsonSerializerOptions? options = default)
=> JsonSerializer.Deserialize<T>(json, options);
public static ValueTask<TValue?> DeserializeAnonymousTypeAsync<TValue>(Stream stream, TValue anonymousTypeObject, JsonSerializerOptions? options = default, CancellationToken cancellationToken = default)
=> JsonSerializer.DeserializeAsync<TValue>(stream, options, cancellationToken); // Method to deserialize from a stream added for completeness
}
And now you can do:
var token = JsonSerializerExtensions.DeserializeAnonymousType(jsonStr, new { token = "" }).token;
Demo fiddle here.
Please try this library I wrote as an extension to System.Text.Json to offer missing features: https://github.com/dahomey-technologies/Dahomey.Json.
You will find support for anonymous types.
Setup json extensions by calling on JsonSerializerOptions the extension method SetupExtensions defined in the namespace Dahomey.Json:
JsonSerializerOptions options = new JsonSerializerOptions();
options.SetupExtensions();
Then serialize your class with the JsonSerializerExtensions static type:
var token = JsonSerializerExtensions.DeserializeAnonymousType(jsonStr, new { token = "" }, options).token;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With