JsonEncoding
Namespace: SnowBank.Text · class
Provides methods for encoding and escaping JSON strings.
Methods
Append
static StringBuilder Append(StringBuilder sb, string text)
Encodes a string literal into a JSON string, and appends the result to a StringBuilder.
sb— The targetStringBuilderto which the encoded string will be appended.text— The string literal to encode.
Returns: The same StringBuilder instance with the encoded string appended.
If text is null, the method appends the null literal.
If text is an empty string, the method appends an empty JSON string ("").
If text contains characters that need escaping, the method encodes the string appropriately.
ComputeEscapedSize
static int ComputeEscapedSize(string text, bool withQuotes = true)
Computes the required buffer capacity to encode the specified string
text— Text to inspectwithQuotes— Iftrue(default), include the two double quotes around the string ("...")
Returns: the length of text if no escaping is required a greater value if at least one character needs to be escaped.4 if text is null ("null").
static int ComputeEscapedSize(ReadOnlySpan<char> text, bool withQuotes = true)
Computes the required buffer capacity to encode the specified string
text— Text to inspectwithQuotes— Iftrue(default), include the two double quotes around the string ("...")
Returns: the length of text if no escaping is required a greater value if at least one character needs to be escaped.
Encode
static string Encode(string text)
Encodes a string literal into a valid JSON string literal
text— Text to encode.
Returns: String with the correct escaping and surrounded by double-quotes ("..."), or "null" if text is null.
EncodeJsonString("foo") => "\"foo\""
static string Encode(ReadOnlySpan<char> text)
Encodes a string literal into a valid JSON string literal
text— Text to encode.
Returns: String with the correct escaping and surrounded by double-quotes ("..."), or "null" if text is null.
EncodeJsonString("foo") => "\"foo\""
EncodeTo
static void EncodeTo(ref ValueStringWriter destination, string text)
Encodes the specified text into the provided text buffer.
destination— The writer to which the encoded text will be written.text— The text to encode.
The double quotes ("....") are automatically added, unless the string is null in which case the literal 'null' literal is used
static void EncodeTo(ref ValueStringWriter destination, ReadOnlySpan<char> text)
Encodes the specified text into the provided text buffer.
destination— The writer to which the encoded text will be written.text— The text to encode.
The double quotes ("....") are automatically added, unless the string is null in which case the literal 'null' is used.
static void EncodeTo(ref SliceWriter destination, ReadOnlySpan<char> text, bool withQuotes = true)
Encodes the specified text into the provided byte buffer.
destination— The writer to which the encoded text will be written as UTF-8 bytes.text— The text to encode.withQuotes— Determines whether the encoded text should be enclosed in double quotes.
The double quotes ("....") are automatically added, unless the string is null in which case the literal 'null' is used.
static void EncodeTo(ref ValueBuffer<byte> destination, ReadOnlySpan<char> text, bool withQuotes = true)
Encodes the specified text into the provided byte buffer.
destination— The writer to which the encoded text will be written as UTF-8 bytes.text— The text to encode.withQuotes— Specifies whether the encoded text should be enclosed in double quotes. Defaults totrue.
The double quotes ("....") are automatically added, unless the string is null in which case the literal 'null' is used.
IndexOfFirstInvalidChar
static int IndexOfFirstInvalidChar(ReadOnlySpan<char> s)
Finds the position in the string of the first character that would need to be escaped in a valid JSON string
s— Text to inspect
Returns: Index of the first invalid character, or -1 if all the characters are valid
NeedsEscaping
static bool NeedsEscaping(char c)
Checks if a character requires escaping before being written to a JSON document
c— Character to inspect
Returns: false if the character is valid, or true if it must be escaped
static bool NeedsEscaping(string s)
Checks if a string requires escaping before being written to a JSON document
s— Text to inspect
Returns: false if all characters are valid, or true if at least one character must be escaped
static bool NeedsEscaping(ReadOnlySpan<char> text)
Checks if a string requires escaping before being written to a JSON document
text— Text to inspect
Returns: false if all characters are valid, or true if at least one character must be escaped
TryEncodeTo
static bool TryEncodeTo(ReadOnlySpan<char> text, Span<char> destination, out int charsWritten, bool withQuotes = true)
Attempts to encode the specified text into the provided output span, if it is large enough.
text— The text to encode.destination— The span to which the encoded text will be written.charsWritten— Outputs the number of characters written to the span, including any double quotes.withQuotes— Iftrue(default), includes double quotes around the string ("...").
Returns: true if the buffer was large enough; otherwise, false.
The buffer may contain partially serialized data after this method returns.
static bool TryEncodeTo(ReadOnlySpan<char> text, Span<byte> destination, out int bytesWritten, bool withQuotes = true)
Attempts to encode the specified text into the provided span as UTF-8 bytes.
text— The text to encode.destination— The span of bytes where the encoded JSON will be written as UTF-8 bytes.bytesWritten— When this method returns, contains the number of bytes written to the destination span.withQuotes— If set totrue, the encoded JSON will include surrounding quotes.
Returns: true if the encoding was successful and the entire encoded JSON fits within the destination span; otherwise, false.
This method encodes the input text as JSON, handling necessary escaping of characters.
If withQuotes is true, the encoded JSON will be surrounded by double quotes.
If the destination span is too small to hold the encoded JSON, the method returns false and bytesWritten is set to 0.