-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New article - Migrate from Newtonsoft.Json #16225
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
Changes from 1 commit
db7bdad
c9e7a21
88cd245
0f60978
3c634dc
d994f56
6d4216c
2594e49
823b581
7dd85d6
3a2ab3d
c546273
07cc159
20f1095
41019bc
0665118
7243deb
163ed4e
b5bd3e7
7771245
a1eae47
b023d84
55d99cc
a78c5ac
9c331ff
618aca8
84a65bc
e4a27a1
6b279eb
2feef06
99619d1
ec3d07c
1919f9d
0f9a494
16b73b3
4618b0c
1bcb4f3
66dbd62
805f425
6fc6377
57fb760
35434dd
93d7564
2610c4a
957f976
9fc486a
bc30ca2
9a057bb
93194b2
2273cf4
1c34eb5
0890e2e
5a0b857
b721d7b
f6b0a55
26930e8
b68ba5e
80ed897
c2191e6
43a657a
7b53d91
fd05648
303d8e5
63e02ab
65be137
72a8a34
50b3574
fd61261
942e7c2
f6d6623
2fa28ee
27d4fd4
55ee7b7
b3eb4f4
2470d9e
30277d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -520,11 +520,13 @@ The following sections explain recommended programming patterns for using `Utf8J | |
|
|
||
| ### Write with UTF-8 text | ||
|
|
||
| To achieve the best possible performance while using the `Utf8JsonWriter`, write JSON payloads already encoded as UTF-8 text rather than as UTF-16 strings. For example, if you're writing string literals, consider caching them as static byte arrays, and write those instead. | ||
| To achieve the best possible performance while using the `Utf8JsonWriter`, write JSON payloads already encoded as UTF-8 text rather than as UTF-16 strings. Use <xref:System.Text.Json.JsonEncodedText> to cache and pre-encode known string property names and values as statics, and pass those to the writer, rather than using UTF-16 string literals. This is faster than caching and using UTF-8 byte arrays. | ||
|
|
||
| This approach also works if you need to do custom escaping. `System.Text.Json` doesn't let you disable escaping while writing a string. However, you could pass in your own custom <xref:System.Text.Encodings.Web.JavaScriptEncoder> as an option to the writer, or create your own `JsonEncodedText` which uses your `JavascriptEncoder` do to the escaping and then write the `JsonEncodedText` instead of the string. For more information, see [Customize character encoding](system-text-json-how-to.md#customize-character-encoding). | ||
|
|
||
| ### Write raw values | ||
|
|
||
| The `Newtonsoft.Json` `WriteRawValue` method writes raw JSON where a value is expected. <xref:System.Text.Json> has no direct equivalent, but here's a workaround: | ||
| The `Newtonsoft.Json` `WriteRawValue` method writes raw JSON where a value is expected. <xref:System.Text.Json> has no direct equivalent, but here's a workaround that ensures only valid JSON is written: | ||
|
|
||
| ```csharp | ||
| using JsonDocument doc = JsonDocument.Parse(string); | ||
|
|
@@ -533,16 +535,16 @@ doc.WriteTo(writer); | |
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't provide equivalent of granular I don't think a lot of people use the |
||
| ### Customize character escaping | ||
|
|
||
| The [StringEscapeHandling](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_StringEscapeHandling.htm) setting of `JsonTextWriter` offers options to escape all non-ASCII characters **or** HTML characters. By default, Utf8JsonWriter escapes all non-ASCII **and** HTML characters. This escaping is done for defense-in-depth security reasons. To specify a different escaping policy, create a <xref:System.Text.Encodings.Web.JavaScriptEncoder> and set <xref:System.Text.Json.JsonWriterOptions.Encoder?displayProperty=nameWithType>. For more information, see [Customize character encoding](system-text-json-how-to.md#customize-character-encoding). | ||
| The [StringEscapeHandling](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_StringEscapeHandling.htm) setting of `JsonTextWriter` offers options to escape all non-ASCII characters **or** HTML characters. By default, `Utf8JsonWriter` escapes all non-ASCII **and** HTML characters. This escaping is done for defense-in-depth security reasons. To specify a different escaping policy, create a <xref:System.Text.Encodings.Web.JavaScriptEncoder> and set <xref:System.Text.Json.JsonWriterOptions.Encoder?displayProperty=nameWithType>. For more information, see [Customize character encoding](system-text-json-how-to.md#customize-character-encoding). | ||
|
|
||
| ### Customize JSON format | ||
|
|
||
| `JsonTextWriter` includes the following settings, for which `Utf8JsonWriter` has no equivalent: | ||
|
|
||
| * [Indentation](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_Indentation.htm) - How many characters to indent. `Utf8JsonWriter` always does 2-character indentation. | ||
| [IndentChar](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_IndentChar.htm) - What character to use for indentation. `Utf8JsonWriter` always uses whitespace. | ||
| [QuoteChar](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_QuoteChar.htm) - What character to use to surround string values. `Utf8JsonWriter` always uses double quotes. | ||
| [QuoteName](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_QuoteName.htm) - Whether or not to surround property names with quotes. `Utf8JsonWriter` always uses quotes. | ||
| * [Indentation](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_Indentation.htm) - Specifies how many characters to indent. `Utf8JsonWriter` always does 2-character indentation. | ||
| * [IndentChar](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_IndentChar.htm) - Specifies the character to use for indentation. `Utf8JsonWriter` always uses whitespace. | ||
| * [QuoteChar](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_QuoteChar.htm) - Specifies the character to use to surround string values. `Utf8JsonWriter` always uses double quotes. | ||
| * [QuoteName](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_QuoteName.htm) - Specifies whether or not to surround property names with quotes. `Utf8JsonWriter` always surrounds them with quotes. | ||
|
|
||
| There are no workarounds that would let you customize the JSON produced by `Utf8JsonWriter` in these ways. | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.