Struct JsonEncoder
- Namespace
- ShapeShift.Json
- Assembly
- ShapeShift.Json.dll
A ShapeShift encoder that writes UTF-8 JSON.
public ref struct JsonEncoder : IEncoder
- Implements
- Inherited Members
Constructors
JsonEncoder(Utf8JsonWriter, bool)
A ShapeShift encoder that writes UTF-8 JSON.
public JsonEncoder(Utf8JsonWriter writer, bool allowNamedFloatingPointValues = false)
Parameters
writerUtf8JsonWriterThe underlying JSON writer.
allowNamedFloatingPointValuesboolWhether named non-finite floating-point strings are enabled.
Properties
Rfc8259StringEncoder
Gets a string encoder that escapes exactly those characters required by RFC 8259.
public static JavaScriptEncoder Rfc8259StringEncoder { get; }
Property Value
Remarks
JsonSerializer uses this encoder automatically. Callers that construct their own Utf8JsonWriter for use with JsonEncoder should assign this value to Encoder.
Output produced with this encoder must be HTML-encoded before it is embedded in HTML.
Writer
Gets the underlying JSON writer for advanced custom converters.
public Utf8JsonWriter Writer { get; }
Property Value
Methods
WriteEndMap()
Writes the end of the innermost open map.
public void WriteEndMap()
WriteEndVector()
Writes the end of the innermost open vector.
public void WriteEndVector()
WriteNull()
Writes an explicit null.
public void WriteNull()
WritePropertyName(scoped ReadOnlySpan<char>)
Writes the name of the map entry whose value comes next.
public void WritePropertyName(scoped ReadOnlySpan<char> name)
Parameters
nameReadOnlySpan<char>The property name.
Remarks
Valid only directly inside a map. Many formats encode a key differently from a string value, so this is a distinct operation rather than a call to WriteValue(scoped ReadOnlySpan<char>).
WriteStartMap(int?)
Writes the beginning of a map.
public void WriteStartMap(int? propertyCount)
Parameters
propertyCountint?The number of entries that will follow, when the caller knows it; otherwise null. A length-prefixed format uses it; a format that writes an explicit end token may ignore it, but must then also ignore it consistently so its decoder never reports a count it did not write.
WriteStartVector(int?)
Writes the beginning of a vector.
public void WriteStartVector(int? itemCount)
Parameters
WriteValue(bool)
Writes a Boolean.
public void WriteValue(bool value)
Parameters
valueboolThe value to write.
WriteValue(DateTime)
Writes a date and time.
public void WriteValue(DateTime value)
Parameters
valueDateTimeThe value to write.
Remarks
The representation must preserve whatever the format's decoder promises to read back, including the DateTimeKind if the round trip is to be faithful.
WriteValue(decimal)
Writes an exact decimal number.
public void WriteValue(decimal value)
Parameters
valuedecimalThe value to write.
Remarks
Binary floating point cannot hold a decimal exactly. A format without a native decimal type should carry its text so that both the digits and the scale survive.
WriteValue(double)
Writes a double-precision floating-point number.
public void WriteValue(double value)
Parameters
valuedoubleThe value to write.
WriteValue(Half)
Writes a half-precision floating-point number.
public void WriteValue(Half value)
Parameters
valueHalfThe value to write.
WriteValue(Int128)
Writes a signed 128-bit integer.
public void WriteValue(Int128 value)
Parameters
valueInt128The value to write.
Remarks
Few formats have a native 128-bit type. Choose a documented, interoperable encoding (such as decimal text) rather than one that narrows the value.
WriteValue(long)
Writes a signed 64-bit integer.
public void WriteValue(long value)
Parameters
valuelongThe value to write.
Remarks
A format is free to choose the narrowest representation that holds the value, provided its decoder widens it back losslessly.
WriteValue(BigInteger)
Writes an arbitrarily large integer.
public void WriteValue(BigInteger value)
Parameters
valueBigIntegerThe value to write.
WriteValue(scoped ReadOnlySpan<byte>)
Writes a binary value.
public void WriteValue(scoped ReadOnlySpan<byte> value)
Parameters
valueReadOnlySpan<byte>The bytes to write.
Exceptions
- NotSupportedException
Thrown when the format has no binary representation.
WriteValue(scoped ReadOnlySpan<char>)
Writes text without requiring the caller to materialize a string.
public void WriteValue(scoped ReadOnlySpan<char> value)
Parameters
valueReadOnlySpan<char>The text to write.
Remarks
This is the allocation-free path converters prefer; implement it directly rather than by forwarding to WriteValue(string).
WriteValue(float)
Writes a single-precision floating-point number.
public void WriteValue(float value)
Parameters
valuefloatThe value to write.
WriteValue(string)
Writes a string.
public void WriteValue(string value)
Parameters
valuestringThe value to write.
WriteValue(TimeSpan)
Writes a duration.
public void WriteValue(TimeSpan value)
Parameters
valueTimeSpanThe value to write.
WriteValue(UInt128)
Writes an unsigned 128-bit integer.
public void WriteValue(UInt128 value)
Parameters
valueUInt128The value to write.
WriteValue(ulong)
Writes an unsigned 64-bit integer.
public void WriteValue(ulong value)
Parameters
valueulongThe value to write.