This article is a mirror article of machine translation, please click here to jump to the original article.

View: 1794|Reply: 7

[Tips] Some tips on System.Text.Json manipulating JSON

[Copy link]
Posted on 2024-11-25 08:39:50 | | | |
Prior to .NET Core 3.1, we had support for object serialization and deserialization by requiring references to third-party packages such as Newtonsoft.Json, and now .NET already has built-in support for JSON operations. The System.Text.Json namespace provides functionality for serializing and deserializing JavaScript object notations (JSON). System.Text.Json library is included.NET Core 3.1 and later runtimeMiddle.

JsonSerializerOptions thread safe

If you passSame optionIf you reuse JsonSerializerOptions, do not create a new instance of JsonSerializerOptions every time you use it. Reuse the same instance for each call. This guide applies to the code you write for your custom transformer, as well as when you call JsonSerializer.Serialize or JsonSerializer.Deserialize. It is safe to use the same instance in multiple threads. The metadata cache on the option instance is thread-safe, and the instance is immutable after the first serialization or deserialization.

Reference:The hyperlink login is visible.

Serialization and deserialization

The code is as follows:



Use the JSON configuration in ASP.NET Core

If you want to use the JSON global configuration in ASP.NET Core, you can inject the following in the constructor:

Use the web version to configure scenarios

The JsonSerializerDefaults enumeration specifies the scenario-based default serialization options that can be used to construct a JsonSerializerOptions instance.

The code is as follows:




Reference:The hyperlink login is visible.





Previous:Windows Server {xxx} is not registering with DCOM within the required timeout.
Next:Front-end web pages execute functions in wasm in 3 ways
 Landlord| Posted on 2024-12-18 19:07:36 |
Use JsonNode to read a value of a JSON string

Sometimes, we may only need to read a value in the JSON string, create a new object, and then deserialize it into this object is more troublesome, you can directly use JsonNode to read a value of the JSON string, the code is as follows:



 Landlord| Posted on 2024-12-30 13:29:28 |
Serialized as a UTF-8 byte array (Utf8Bytes)

Serializing as a UTF-8 byte array is more important than using a string-based approach5-10% faster。 This is because bytes (UTF-8) do not need to be converted to strings (UTF-16).

To serialize to a UTF-8 byte array, call the JsonSerializer.SerializeToUtf8Bytes method:


The hyperlink login is visible.
 Landlord| Posted on 2025-1-2 09:04:00 |
Use System.Text.Json to remove data with strings as null or "", deserialize the json string into an object object, if there are hundreds of fields, many of which are recorded as null, to save transfer, these fields can be removed. System.Text.Json deserializes the json string into System.Text.Json.JsonElement.



The code is as follows:



JsonObject does not respect JsonIgnoreCondition.WhenWritingNull:The hyperlink login is visible.
 Landlord| Posted on 2025-2-18 13:53:03 |
Use JsonObject to create an object directly and serialize it into a json string, as shown in the following figure:



Code:

 Landlord| Posted on 2025-2-28 09:17:31 |
By [JsonPropertyNameproperty to customize the serialization name of the field



 Landlord| Posted on 2025-11-19 14:40:03 |
Use JsonNode to get a Json string collection object

Code:

 Landlord| Posted on4 days ago |
Array objects are serialized into JSON strings



Code:
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com