Исходный код вики Сценарии
Редактировал(а) Alexandr Fokin 2025/02/17 00:08
Последние авторы
author | version | line-number | content |
---|---|---|---|
1 | |(% style="width:152px" %)JsonDocument Dispose|(% style="width:1340px" %)((( | ||
2 | Обращаю внимание, что JsonDocument реализует интерфейс IDisposable (скорее всего для переиспользования памяти). | ||
3 | Не всегда удобно (особенно, если это часть Entity/Dto), но если нужно снизить нагрузку на GC (финализацию), то нужно всегда вызывать Dispose. | ||
4 | |||
5 | Memory management when using JsonDocument | ||
6 | [[https:~~/~~/github.com/npgsql/efcore.pg/issues/1746>>https://github.com/npgsql/efcore.pg/issues/1746]] | ||
7 | |||
8 | |||
9 | UPD1: | ||
10 | JsonDocument is IDisposable | ||
11 | [[https:~~/~~/learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/use-dom#jsondocument-is-idisposable>>https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/use-dom#jsondocument-is-idisposable]] | ||
12 | \\В документации сказано, что допустимо использовать JsonElement, выполнив следующую код: | ||
13 | {{code language="c#"}}public JsonElement GetJson(string jsonString) | ||
14 | { | ||
15 | using (JsonDocument doc = JsonDocument.Parse(jsonString)) | ||
16 | { | ||
17 | return doc.RootElement.Clone(); | ||
18 | } | ||
19 | }{{/code}} | ||
20 | ))) | ||
21 | |(% style="width:152px" %)Базовые опции|(% style="width:1340px" %)((( | ||
22 | (% id="cke_bm_182S" style="display:none" %) (%%)How to customize character encoding with System.Text.Json | ||
23 | [[https:~~/~~/docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-encoding>>https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-encoding]] | ||
24 | |||
25 | {{code language="c#"}} | ||
26 | new JsonSerializerOptions() | ||
27 | { | ||
28 | //Игнорирование null элементы | ||
29 | IgnoreNullValues = true, | ||
30 | // Форматирование читаемое/компкатный | ||
31 | WriteIndented = true, | ||
32 | |||
33 | // Кодировка символов | ||
34 | //Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic), | ||
35 | Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, | ||
36 | |||
37 | // Регистронезависимость | ||
38 | // How to enable case-insensitive property name matching with System.Text.Json | ||
39 | // https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-casing | ||
40 | PropertyNameCaseInsensitive = true, | ||
41 | |||
42 | // Enum как строка | ||
43 | Converters = { | ||
44 | new System.Text.Json.Serialization.JsonStringEnumConverter() | ||
45 | } | ||
46 | }; | ||
47 | {{/code}} | ||
48 | ))) | ||
49 | |(% style="width:152px" %)Создание или изменение json объекта|(% style="width:1340px" %)((( | ||
50 | JsonNode | ||
51 | [[https:~~/~~/docs.microsoft.com/en-us/dotnet/api/system.text.json.nodes.jsonnode?view=net-6.0>>https://docs.microsoft.com/en-us/dotnet/api/system.text.json.nodes.jsonnode?view=net-6.0]] | ||
52 | \\How to add property in existing json using System.Text.Json library? | ||
53 | [[https:~~/~~/stackoverflow.com/questions/58302522/how-to-add-property-in-existing-json-using-system-text-json-library>>https://stackoverflow.com/questions/58302522/how-to-add-property-in-existing-json-using-system-text-json-library]] | ||
54 | \\Is there a way to convert between JsonDocument and JsonNode? | ||
55 | [[https:~~/~~/stackoverflow.com/questions/73047801/is-there-a-way-to-convert-between-jsondocument-and-jsonnode>>https://stackoverflow.com/questions/73047801/is-there-a-way-to-convert-between-jsondocument-and-jsonnode]] | ||
56 | ))) | ||
57 | |(% style="width:152px" %)Распознать строковые значения как число.|(% style="width:1340px" %){{code language="c#"}}[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] | ||
58 | // или | ||
59 | new JsonSerializerOptions() | ||
60 | { | ||
61 | NumberHandling = JsonNumberHandling.AllowReadingFromString | ||
62 | }{{/code}}((( | ||
63 | |||
64 | |||
65 | System.Text.Json: Deserialize JSON with automatic casting | ||
66 | [[https:~~/~~/stackoverflow.com/questions/59097784/system-text-json-deserialize-json-with-automatic-casting>>https://stackoverflow.com/questions/59097784/system-text-json-deserialize-json-with-automatic-casting]] | ||
67 | ))) | ||
68 | |(% style="width:152px" %)JsonConverter. | ||
69 | Реализация собственной (кастомной) логики сериализации типов|(% style="width:1340px" %)((( | ||
70 | How to write custom converters for JSON serialization (marshalling) in .NET | ||
71 | [[https:~~/~~/learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-6-0>>https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-6-0]] | ||
72 | |||
73 | System.Text.Json deserialization fails with JsonException "read to much or not enough" | ||
74 | [[https:~~/~~/stackoverflow.com/questions/62147178/system-text-json-deserialization-fails-with-jsonexception-read-to-much-or-not-e>>https://stackoverflow.com/questions/62147178/system-text-json-deserialization-fails-with-jsonexception-read-to-much-or-not-e]] | ||
75 | ))) | ||
76 | |(% style="width:152px" %)Обход свойств|(% style="width:1340px" %){{code language="c#"}}EnumerateObject(){{/code}} |