CrystalJsonWriter
Namespace: SnowBank.Data.Json · class
Implements: IDisposable
Serialize values into JSON
Constructors
CrystalJsonWriter
CrystalJsonWriter(int initialCapacity, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)
CrystalJsonWriter(TextWriter output, int autoFlush, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)
CrystalJsonWriter(Stream output, int autoFlush, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)
Properties
DateFormatting
DateFormat DateFormatting { get; }
Format used to convert dates
Depth
int Depth { get; }
Current depth when serializing (0 for top level)
DiscardClass
bool DiscardClass { get; }
Specifies if we wil discard the "_class" attribute
DiscardDefaults
bool DiscardDefaults { get; }
Specifies if we will discard value type members that have a default value (0, false, null for Nullable
DiscardNulls
bool DiscardNulls { get; }
Specified if we wil discard reference type members that are null
FloatFormatting
FloatFormat FloatFormatting { get; }
Format used to convert floating point numbers
Formatted
bool Formatted { get; }
Specifies whether the writer will insert spaces between tokens (to enhance readability by humans)
Indented
bool Indented { get; }
Specifies whether the writer will automatically indent all values (to enhance readability by humans)
JavaScript
bool JavaScript { get; }
Specifies if we are targeting JavaScript, instead of JSON
If , all strings will be escaped using single quotes ('), and property names will only be quoted if necessary
Output
TextWriter Output { get; }
Resolver
ICrystalJsonTypeResolver Resolver { get; }
Settings
CrystalJsonSettings Settings { get; }
Stream
Stream Stream { get; }
Methods
BeginArray
State BeginArray()
Start a new JSON array
Returns: Previous state, that should be passed to the corresponding EndArray
BeginInlineArray
State BeginInlineArray()
Start a new JSON inline array
Returns: Previous state, that should be passed to the corresponding EndArray
Inline arrays will attempt to keep all elements on a single line. Use this when serialing "vector" or "tuples" that are not techincally an array, but are expressed as an array (the XYZ coordinates of a point, a key/value pair, ...)
BeginObject
State BeginObject()
Start a new JSON object
Returns: Previous state, that should be passed to the corresponding EndObject
CopyTo
int CopyTo(Span<char> buffer)
int CopyTo(StringBuilder buffer)
void CopyTo(IBufferWriter<byte> buffer)
void CopyTo(Stream destination)
CopyToUtf8
int CopyToUtf8(Span<byte> buffer)
Dispose
void Dispose()
EndArray
void EndArray(State state)
End a JSON array
state— Value that was returned by the call to BeginArray for this array
The caller should store the state in a local variable. Mixing states between objects and arrays will CORRUPT the resulting JSON document!
EndInlineArray
void EndInlineArray(State state)
End a JSON inline array
state— Value that was returned by the call to BeginArray for this array
The caller should store the state in a local variable. Mixing states between objects and arrays will CORRUPT the resulting JSON document!
EndObject
void EndObject(State state)
End a JSON object
state— Value that was returned by the call to BeginObject for this object
The caller should store the state in a local variable. Mixing states between objects and arrays will CORRUPT the resulting JSON document!
EnterDepth
void EnterDepth(object value)
Enters one level of the object graph, WITHOUT pushing the instance onto the visited-objects stack
value— Instance about to be written; used only to name the offending type if the cap is reached
The depth half of MarkVisited, and nothing else: source-generated converters call this around the body they write, so a reference cycle running through generated code raises a typed exception instead of an uncatchable StackOverflowException (see MaxDepth).
Deliberately NOT the visited-objects stack: true cycle detection allocates and scans per level, and stays a reflection-path feature (WithoutObjectTracking); the generated path only pays for the counter.
The caller must pair this with LeaveDepth. Like MarkVisited/Leave, the pairing is not exception-safe by design: a writer whose serialization threw is not reusable anyway.
void EnterDepth(Type type)
Enters one level of the object graph without boxing a value-type instance, WITHOUT pushing anything onto the visited-objects stack
type— Static type of the instance about to be written; used only to name the offending type if the cap is reached
Same depth-only guard as EnterDepth, for source-generated converters over a value type: a struct passed to the Object overload would be boxed on every call just to name it in the failure message. The static Type loses no information, because value.GetType() and typeof(T) always agree for a struct.
Reference types keep using the Object overload: it never boxes, and instance.GetType() can be more derived than the converter's static type, which the failure message preserves.
The caller must pair this with LeaveDepth.
Flush
void Flush(bool last = false)
FlushAsync
Task FlushAsync(CancellationToken ct)
Task FlushAsync(bool last, CancellationToken ct)
GetString
string GetString()
Returns the JSON text written, and clear the writer
Returns: JSON text
GetUtf8Slice
Slice GetUtf8Slice()
SliceOwner GetUtf8Slice(ArrayPool<byte> pool)
Initialize
void Initialize(TextWriter output, int autoFlush, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)
void Initialize(Stream output, int autoFlush, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)
Leave
void Leave(object value)
Mark the current object as completed, and remove it from the loop tracking list
value— Same value that was passed to MarkVisited
LeaveDepth
void LeaveDepth()
Leaves the level entered by the matching call to EnterDepth
MarkVisited
void MarkVisited(object value)
Mark an instance as already visited, and perform infinite loop detection
value— Instance currently being serialized
The caller should call once this instance has been handled. Failure to do so will leak memory, and also prevent from serializing the same object multiple times (cached singletons, ...)
PopAttributes
void PopAttributes(JsonPropertyAttribute attributes)
PushAttributes
JsonPropertyAttribute PushAttributes(JsonPropertyAttribute attributes)
ResetState
void ResetState()
Reset the state of the writer, so that it can be reused to write a new JSON document
Only use when reusing the same writer in a loop or batch. The caller must be careful to reset the internal state of the inner TextWriter that is used by this instance!
TryCopyTo
bool TryCopyTo(Span<char> buffer, out int written)
TryCopyToUtf8
bool TryCopyToUtf8(Span<byte> buffer, out int written)
VisitArray
void VisitArray<T>(T[] array, IJsonSerializer<T> serializer)
void VisitArray<T>(ReadOnlySpan<T> array, IJsonSerializer<T> serializer)
void VisitArray<T>(IEnumerable<T> array, IJsonSerializer<T> serializer)
void VisitArray<T>(IEnumerable<T> array, Action<CrystalJsonWriter, T> action)
VisitDictionary
void VisitDictionary<TValue>(ICollection<KeyValuePair<string, TValue>> items, IJsonSerializer<TValue> serializer)
void VisitDictionary<TValue>(Dictionary<string, TValue> items, IJsonSerializer<TValue> serializer)
VisitJsonSerializableArray
void VisitJsonSerializableArray<TSerializable>(ReadOnlySpan<TSerializable> array)
void VisitJsonSerializableArray<TSerializable>(IEnumerable<TSerializable> array)
VisitValue
void VisitValue<T>(T value)
void VisitValue(object value, Type declaredType)
void VisitValue<T>(T value, IJsonSerializer<T> serializer)
VisitValueJsonSerializable
void VisitValueJsonSerializable<TSerializable>(TSerializable value)
VisitXmlNode
void VisitXmlNode(XmlNode node)
WillBeDiscarded
bool WillBeDiscarded(JsonValue value)
Tests if the specified value would have been discarded when calling WriteField
WriteArray
void WriteArray<T>(IEnumerable<T> items)
void WriteArray<T>(T[] array)
void WriteArray<T>(Span<T> array)
void WriteArray<T>(ReadOnlySpan<T> array)
void WriteArray(string[] array)
void WriteArray(ReadOnlySpan<string> array)
void WriteArray(int[] array)
void WriteArray(ReadOnlySpan<int> array)
void WriteArray(long[] array)
void WriteArray(ReadOnlySpan<long> array)
void WriteArray(float[] array)
void WriteArray(ReadOnlySpan<float> array)
void WriteArray(double[] array)
void WriteArray(ReadOnlySpan<double> array)
void WriteArray(Guid[] array)
void WriteArray(ReadOnlySpan<Guid> array)
void WriteArray(Uuid128[] array)
void WriteArray(ReadOnlySpan<Uuid128> array)
void WriteArray<TKey, TValue>(ICollection<KeyValuePair<TKey, TValue>> source)
void WriteArray<T>(T[] array, int offset, int count)
WriteBuffer
void WriteBuffer(byte[] bytes)
void WriteBuffer(ReadOnlySpan<byte> bytes)
void WriteBuffer(Slice bytes)
void WriteBuffer(byte[] bytes, int offset, int count)
WriteComment
void WriteComment(string comment)
Write a comment
Not all JSON parser will accept comments! Only use when you know that all parsers that will consume this understand and allow comments!
WriteDateOnlyIso8601
void WriteDateOnlyIso8601(DateOnly date)
Writes a date using the ISO 8601 format: "YYYY-MM-DD"
WriteDateTimeIso8601
void WriteDateTimeIso8601(DateTime date)
Writes a date using the ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.ffff+TZ"
void WriteDateTimeIso8601(DateTimeOffset date)
Writes a date with offset using the ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.ffff+TZ"
WriteDateTimeJavaScript
void WriteDateTimeJavaScript(DateTime date)
Writes a date, using the Javascript format: new Date(123456789)
void WriteDateTimeJavaScript(DateTimeOffset date)
Writes a date with offset, using the Javascript format: new Date(123456789)
WriteDateTimeMicrosoft
void WriteDateTimeMicrosoft(DateTime date)
Writes a date, using Microsoft's custom encoding "\/Date(....)\/"
void WriteDateTimeMicrosoft(DateTimeOffset date)
Writes a date with offset, using Microsoft's custom encoding "\/Date(....)\/"
WriteDictionary
void WriteDictionary(ICollection<KeyValuePair<string, object>> items)
void WriteDictionary<TValue>(ICollection<KeyValuePair<string, TValue>> items)
void WriteDictionary<TValue>(Dictionary<string, TValue> items)
WriteEmptyArray
void WriteEmptyArray()
Write the empty array literal ([] or [ ])
WriteEmptyObject
void WriteEmptyObject()
Write the empty object literal ({} or { })
WriteEmptyString
void WriteEmptyString()
Write the empty string literal ("")
WriteEnum
void WriteEnum<TEnum>(TEnum value)
void WriteEnum(Enum value)
WriteEnumInteger
void WriteEnumInteger<TEnum>(TEnum value)
void WriteEnumInteger(Enum value)
WriteEnumString
void WriteEnumString<TEnum>(TEnum value)
void WriteEnumString(Enum value)
WriteField
void WriteField(string name, string value)
void WriteField(JsonEncodedPropertyName name, string value)
void WriteField(string name, ReadOnlySpan<char> value)
void WriteField(JsonEncodedPropertyName name, ReadOnlySpan<char> value)
void WriteField(string name, ReadOnlyMemory<char> value)
void WriteField(JsonEncodedPropertyName name, ReadOnlyMemory<char> value)
void WriteField(string name, StringBuilder value)
void WriteField(JsonEncodedPropertyName name, StringBuilder value)
void WriteField(string name, bool value)
void WriteField(JsonEncodedPropertyName name, bool value)
void WriteField(string name, bool? value)
void WriteField(JsonEncodedPropertyName name, bool? value)
void WriteField(string name, int value)
void WriteField(JsonEncodedPropertyName name, int value)
void WriteField(string name, int? value)
void WriteField(JsonEncodedPropertyName name, int? value)
void WriteField(string name, uint value)
void WriteField(JsonEncodedPropertyName name, uint value)
void WriteField(string name, uint? value)
void WriteField(JsonEncodedPropertyName name, uint? value)
void WriteField(string name, long value)
void WriteField(JsonEncodedPropertyName name, long value)
void WriteField(string name, long? value)
void WriteField(JsonEncodedPropertyName name, long? value)
void WriteField(string name, ulong value)
void WriteField(JsonEncodedPropertyName name, ulong value)
void WriteField(string name, ulong? value)
void WriteField(JsonEncodedPropertyName name, ulong? value)
void WriteField(string name, float value)
void WriteField(JsonEncodedPropertyName name, float value)
void WriteField(string name, float? value)
void WriteField(JsonEncodedPropertyName name, float? value)
void WriteField(string name, double value)
void WriteField(JsonEncodedPropertyName name, double value)
void WriteField(string name, double? value)
void WriteField(JsonEncodedPropertyName name, double? value)
void WriteField(string name, decimal value)
void WriteField(JsonEncodedPropertyName name, decimal value)
void WriteField(string name, decimal? value)
void WriteField(JsonEncodedPropertyName name, decimal? value)
void WriteField(string name, Half value)
void WriteField(JsonEncodedPropertyName name, Half value)
void WriteField(string name, Half? value)
void WriteField(JsonEncodedPropertyName name, Half? value)
void WriteField(string name, DateTime value)
void WriteField(JsonEncodedPropertyName name, DateTime value)
void WriteField(string name, DateTime? value)
void WriteField(JsonEncodedPropertyName name, DateTime? value)
void WriteField(string name, DateTimeOffset value)
void WriteField(JsonEncodedPropertyName name, DateTimeOffset value)
void WriteField(string name, DateTimeOffset? value)
void WriteField(JsonEncodedPropertyName name, DateTimeOffset? value)
void WriteField(string name, DateOnly value)
void WriteField(JsonEncodedPropertyName name, DateOnly value)
void WriteField(string name, DateOnly? value)
void WriteField(JsonEncodedPropertyName name, DateOnly? value)
void WriteField(string name, TimeOnly value)
void WriteField(JsonEncodedPropertyName name, TimeOnly value)
void WriteField(string name, TimeOnly? value)
void WriteField(JsonEncodedPropertyName name, TimeOnly? value)
void WriteField(string name, TimeSpan value)
void WriteField(JsonEncodedPropertyName name, TimeSpan value)
void WriteField(string name, TimeSpan? value)
void WriteField(JsonEncodedPropertyName name, TimeSpan? value)
void WriteField(string name, Guid value)
void WriteField(JsonEncodedPropertyName name, Guid value)
void WriteField(string name, Guid? value)
void WriteField(JsonEncodedPropertyName name, Guid? value)
void WriteField(string name, Uuid128 value)
void WriteField(JsonEncodedPropertyName name, Uuid128 value)
void WriteField(string name, Uuid128? value)
void WriteField(JsonEncodedPropertyName name, Uuid128? value)
void WriteField(string name, Uuid96 value)
void WriteField(JsonEncodedPropertyName name, Uuid96 value)
void WriteField(string name, Uuid96? value)
void WriteField(JsonEncodedPropertyName name, Uuid96? value)
void WriteField(string name, Uuid80 value)
void WriteField(JsonEncodedPropertyName name, Uuid80 value)
void WriteField(string name, Uuid80? value)
void WriteField(JsonEncodedPropertyName name, Uuid80? value)
void WriteField(string name, Uuid64 value)
void WriteField(JsonEncodedPropertyName name, Uuid64 value)
void WriteField(string name, Uuid64? value)
void WriteField(string name, Instant value)
void WriteField(JsonEncodedPropertyName name, Instant value)
void WriteField(string name, Instant? value)
void WriteField(JsonEncodedPropertyName name, Instant? value)
void WriteField(string name, Duration value)
void WriteField(JsonEncodedPropertyName name, Duration value)
void WriteField(string name, Duration? value)
void WriteField(JsonEncodedPropertyName name, Duration? value)
void WriteField(string name, ZonedDateTime value)
void WriteField(JsonEncodedPropertyName name, ZonedDateTime value)
void WriteField(string name, ZonedDateTime? value)
void WriteField(JsonEncodedPropertyName name, ZonedDateTime? value)
void WriteField(string name, LocalDateTime value)
void WriteField(JsonEncodedPropertyName name, LocalDateTime value)
void WriteField(string name, LocalDateTime? value)
void WriteField(JsonEncodedPropertyName name, LocalDateTime? value)
void WriteField(string name, LocalDate value)
void WriteField(string name, LocalDate? value)
void WriteField(JsonEncodedPropertyName name, LocalDate? value)
void WriteField(string name, LocalTime value)
void WriteField(JsonEncodedPropertyName name, LocalTime value)
void WriteField(string name, LocalTime? value)
void WriteField(JsonEncodedPropertyName name, LocalTime? value)
void WriteField(string name, OffsetDateTime value)
void WriteField(JsonEncodedPropertyName name, OffsetDateTime value)
void WriteField(string name, OffsetDateTime? value)
void WriteField(JsonEncodedPropertyName name, OffsetDateTime? value)
void WriteField(string name, Offset value)
void WriteField(JsonEncodedPropertyName name, Offset value)
void WriteField(string name, Offset? value)
void WriteField(JsonEncodedPropertyName name, Offset? value)
void WriteField(string name, DateTimeZone value)
void WriteField(JsonEncodedPropertyName name, DateTimeZone value)
void WriteField(string name, Version value)
void WriteField(JsonEncodedPropertyName name, Version value)
void WriteField(string name, IPAddress value)
void WriteField(JsonEncodedPropertyName name, IPAddress value)
void WriteField(string name, Uri value)
void WriteField(JsonEncodedPropertyName name, Uri value)
void WriteField(string name, JsonValue value)
void WriteField(JsonEncodedPropertyName name, JsonValue value)
void WriteField(string name, JsonNull value)
void WriteField(string name, JsonObject value)
void WriteField(string name, JsonArray value)
void WriteField(string name, JsonString value)
void WriteField(string name, JsonNumber value)
void WriteField(string name, JsonBoolean value)
void WriteField(string name, JsonDateTime value)
void WriteField(JsonEncodedPropertyName name, JsonNull value)
void WriteField(JsonEncodedPropertyName name, JsonObject value)
void WriteField(JsonEncodedPropertyName name, JsonArray value)
void WriteField(JsonEncodedPropertyName name, JsonString value)
void WriteField(JsonEncodedPropertyName name, JsonNumber value)
void WriteField(JsonEncodedPropertyName name, JsonBoolean value)
void WriteField(JsonEncodedPropertyName name, JsonDateTime value)
void WriteField<T>(string name, T value)
void WriteField<T>(JsonEncodedPropertyName name, T value)
void WriteField<T>(string name, T? value)
void WriteField<T>(JsonEncodedPropertyName name, T? value)
void WriteField(string name, object value, Type declaredType)
void WriteField(JsonEncodedPropertyName name, object value, Type declaredType)
void WriteField<T>(string name, T value, IJsonSerializer<T> serializer)
void WriteField<T>(JsonEncodedPropertyName name, T value, IJsonSerializer<T> serializer)
WriteFieldArray
void WriteFieldArray(string name, string[] values)
void WriteFieldArray(JsonEncodedPropertyName name, string[] values)
void WriteFieldArray(string name, ReadOnlySpan<string> values)
void WriteFieldArray(JsonEncodedPropertyName name, ReadOnlySpan<string> values)
void WriteFieldArray(string name, List<string> values)
void WriteFieldArray(string name, IEnumerable<string> values)
void WriteFieldArray(JsonEncodedPropertyName name, List<string> values)
void WriteFieldArray(JsonEncodedPropertyName name, IEnumerable<string> values)
void WriteFieldArray(string name, int[] values)
void WriteFieldArray(JsonEncodedPropertyName name, int[] values)
void WriteFieldArray(string name, ReadOnlySpan<int> values)
void WriteFieldArray(JsonEncodedPropertyName name, ReadOnlySpan<int> values)
void WriteFieldArray(string name, long[] values)
void WriteFieldArray(JsonEncodedPropertyName name, long[] values)
void WriteFieldArray(string name, ReadOnlySpan<long> values)
void WriteFieldArray(JsonEncodedPropertyName name, ReadOnlySpan<long> values)
void WriteFieldArray(string name, float[] values)
void WriteFieldArray(JsonEncodedPropertyName name, float[] values)
void WriteFieldArray(string name, ReadOnlySpan<float> values)
void WriteFieldArray(JsonEncodedPropertyName name, ReadOnlySpan<float> values)
void WriteFieldArray(string name, double[] values)
void WriteFieldArray(JsonEncodedPropertyName name, double[] values)
void WriteFieldArray(string name, ReadOnlySpan<double> values)
void WriteFieldArray(JsonEncodedPropertyName name, ReadOnlySpan<double> values)
void WriteFieldArray(string name, Guid[] values)
void WriteFieldArray(JsonEncodedPropertyName name, Guid[] values)
void WriteFieldArray(string name, ReadOnlySpan<Guid> values)
void WriteFieldArray(JsonEncodedPropertyName name, ReadOnlySpan<Guid> values)
void WriteFieldArray(string name, Uuid128[] values)
void WriteFieldArray(JsonEncodedPropertyName name, Uuid128[] values)
void WriteFieldArray(string name, ReadOnlySpan<Uuid128> values)
void WriteFieldArray(JsonEncodedPropertyName name, ReadOnlySpan<Uuid128> values)
void WriteFieldArray<T>(string name, T[] items)
void WriteFieldArray<T>(JsonEncodedPropertyName name, T[] items)
void WriteFieldArray<T>(string name, ReadOnlySpan<T> array)
void WriteFieldArray<T>(JsonEncodedPropertyName name, ReadOnlySpan<T> array)
void WriteFieldArray<T>(string name, IEnumerable<T> items)
void WriteFieldArray<T>(JsonEncodedPropertyName name, IEnumerable<T> items)
void WriteFieldArray<T>(JsonEncodedPropertyName name, ReadOnlySpan<T> array, IJsonSerializer<T> serializer)
void WriteFieldArray<T>(JsonEncodedPropertyName name, T[] items, IJsonSerializer<T> serializer)
void WriteFieldArray<T>(JsonEncodedPropertyName name, List<T> items, IJsonSerializer<T> serializer)
void WriteFieldArray<T>(JsonEncodedPropertyName name, IEnumerable<T> items, IJsonSerializer<T> serializer)
WriteFieldDictionary
void WriteFieldDictionary<TValue>(string name, ICollection<KeyValuePair<string, TValue>> map)
Writes a field that contains a dictionary expressed as a JSON Object
name— Name of the fieldmap— Dictionary to write
void WriteFieldDictionary<TValue>(JsonEncodedPropertyName name, ICollection<KeyValuePair<string, TValue>> map)
Writes a field that contains a dictionary expressed as a JSON Object
name— Name of the fieldmap— Dictionary to write
void WriteFieldDictionary<TValue>(string name, Dictionary<string, TValue> map)
Writes a field that contains a dictionary expressed as a JSON Object
name— Name of the fieldmap— Dictionary to write
void WriteFieldDictionary<TValue>(JsonEncodedPropertyName name, Dictionary<string, TValue> map)
Writes a field that contains a dictionary expressed as a JSON Object
name— Name of the fieldmap— Dictionary to write
void WriteFieldDictionary<TValue>(string name, ICollection<KeyValuePair<string, TValue>> map, IJsonSerializer<TValue> serializer)
Writes a field that contains a dictionary expressed as a JSON Object, using a custom serializer
name— Name of the fieldmap— Dictionary to writeserializer— Custom value serializer
void WriteFieldDictionary<TValue>(JsonEncodedPropertyName name, ICollection<KeyValuePair<string, TValue>> map, IJsonSerializer<TValue> serializer)
Writes a field that contains a dictionary expressed as a JSON Object, using a custom serializer
name— Name of the fieldmap— Dictionary to writeserializer— Custom value serializer
void WriteFieldDictionary<TValue>(string name, Dictionary<string, TValue> map, IJsonSerializer<TValue> serializer)
Writes a field that contains a dictionary expressed as a JSON Object, using a custom serializer
name— Name of the fieldmap— Dictionary to writeserializer— Custom value serializer
void WriteFieldDictionary<TValue>(JsonEncodedPropertyName name, Dictionary<string, TValue> map, IJsonSerializer<TValue> serializer)
Writes a field that contains a dictionary expressed as a JSON Object, using a custom serializer
name— Name of the fieldmap— Dictionary to writeserializer— Custom value serializer
WriteFieldEnumNumber
void WriteFieldEnumNumber<TEnum>(JsonEncodedPropertyName name, TEnum value)
Writes a field with an enum value forced to its numeric form, regardless of the settings (per-member Number)
void WriteFieldEnumNumber<TEnum>(JsonEncodedPropertyName name, TEnum? value)
Writes a field with an enum value forced to its numeric form, regardless of the settings (per-member Number)
WriteFieldEnumString
void WriteFieldEnumString<TEnum>(JsonEncodedPropertyName name, TEnum value)
Writes a field with an enum value forced to its string form, regardless of the settings (per-member String)
void WriteFieldEnumString<TEnum>(JsonEncodedPropertyName name, TEnum? value)
Writes a field with an enum value forced to its string form, regardless of the settings (per-member String)
WriteFieldJsonSerializable
void WriteFieldJsonSerializable<TSerializable>(string name, TSerializable value)
void WriteFieldJsonSerializable<TSerializable>(JsonEncodedPropertyName name, TSerializable value)
WriteFieldJsonSerializableArray
void WriteFieldJsonSerializableArray<TSerializable>(string name, ReadOnlySpan<TSerializable> array)
void WriteFieldJsonSerializableArray<TSerializable>(string name, TSerializable[] items)
void WriteFieldJsonSerializableArray<TSerializable>(string name, List<TSerializable> items)
void WriteFieldJsonSerializableArray<TSerializable>(string name, IEnumerable<TSerializable> items)
void WriteFieldJsonSerializableArray<TSerializable>(JsonEncodedPropertyName name, ReadOnlySpan<TSerializable> array)
void WriteFieldJsonSerializableArray<TSerializable>(JsonEncodedPropertyName name, TSerializable[] items)
void WriteFieldJsonSerializableArray<TSerializable>(JsonEncodedPropertyName name, List<TSerializable> items)
void WriteFieldJsonSerializableArray<TSerializable>(JsonEncodedPropertyName name, IEnumerable<TSerializable> items)
WriteFieldNull
void WriteFieldNull(string name)
void WriteFieldNull(JsonEncodedPropertyName name)
WriteFieldNumberString
void WriteFieldNumberString(JsonEncodedPropertyName name, int value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, uint value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, long value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, ulong value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, float value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, double value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, decimal value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, Half value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, int? value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, uint? value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, long? value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, ulong? value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, float? value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, double? value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, decimal? value)
Writes a field with a numeric value forced to its string form (per-member String)
void WriteFieldNumberString(JsonEncodedPropertyName name, Half? value)
Writes a field with a numeric value forced to its string form (per-member String)
WriteFieldSeparator
void WriteFieldSeparator()
Write a coma separator (,) between two fields, unless this is the first element of an array
WriteHeadSeparator
void WriteHeadSeparator()
Properly indent the first element of an array
WriteInlineFieldSeparator
void WriteInlineFieldSeparator()
Write a comma between elements of an inline array, unless this is the first element
WriteInlineHeadSeparator
void WriteInlineHeadSeparator()
WriteInlinePair
void WriteInlinePair(int key, bool value)
void WriteInlinePair(int key, int value)
void WriteInlinePair(int key, string value)
void WriteInlinePair(int key, ReadOnlySpan<char> value)
void WriteInlinePair(int key, ReadOnlyMemory<char> value)
void WriteInlinePair(int key, StringBuilder value)
void WriteInlinePair(int key, JsonValue value)
void WriteInlinePair<TValue>(int key, TValue value)
void WriteInlinePair(string key, bool value)
void WriteInlinePair(string key, int value)
void WriteInlinePair(string key, string value)
void WriteInlinePair(string key, ReadOnlySpan<char> value)
void WriteInlinePair(string key, ReadOnlyMemory<char> value)
void WriteInlinePair(string key, JsonValue value)
void WriteInlinePair<TValue>(string key, TValue value)
void WriteInlinePair(string key, StringBuilder value)
void WriteInlinePair(JsonValue key, string value)
void WriteInlinePair(JsonValue key, long value)
void WriteInlinePair(JsonValue key, long? value)
WriteInlineTailSeparator
void WriteInlineTailSeparator()
WriteName
void WriteName(string name)
Writes a property name that is KNOWN to not require any escaping.
name— Name of the property that MUST NOT REQUIRE ANY ESCAPING!
Calling this with a .NET object property or field name (obtained via reflection or nameof(...)) is OK, but calling with a dictionary key or user-input is NOT safe!
void WriteName(JsonEncodedPropertyName name)
Writes a property name that is KNOWN to not require any escaping.
name— Name of the property that MUST NOT REQUIRE ANY ESCAPING!
Calling this with a .NET object property or field name (obtained via reflection or nameof(...)) is OK, but calling with a dictionary key or user-input is NOT safe!
void WriteName(long name)
Writes a numerical property name
name— Name to write
This method inserts any required , separator, and appends the :
This is used for objects with keys that are integers like: { "0": ..., "1": ...., ....}.
// first field
writer.WriteName(123L); // => `"123": `
writer.WriteValue(/*...*/);
// following fields
writer.WriteName(456L); // => `, "456": `
writer.WriteValue(/*...*/);
void WriteName(int name)
Writes a numerical property name
name— Name to write
This method inserts any required , separator, and appends the :
This is used for objects with keys that are integers like: { "0": ..., "1": ...., ....}.
// first field
writer.WriteName(123); // => `"123": `
writer.WriteValue(/*...*/);
// following fields
writer.WriteName(456); // => `, "456": `
writer.WriteValue(/*...*/);
WriteNameEscaped
void WriteNameEscaped(string name)
Writes a property name that MAY require escaping.
name— Name of the property that will be escaped if necessary
This method should be used whenever the origin of key is not controlled, and may contain any character that would require escaping ('\', '"', ...).
void WriteNameEscaped(ReadOnlySpan<char> name)
Writes a property name that MAY require escaping.
name— Name of the property that will be escaped if necessary
This method should be used whenever the origin of key is not controlled, and may contain any character that would require escaping ('\', '"', ...).
WriteNull
void WriteNull()
Write the null literal (null)
WritePair
void WritePair(int key, bool value)
void WritePair(int key, int value)
void WritePair(int key, string value)
void WritePair(int key, ReadOnlySpan<char> value)
void WritePair(int key, ReadOnlyMemory<char> value)
void WritePair(int key, StringBuilder value)
void WritePair(int key, JsonValue value)
void WritePair<TValue>(int key, TValue value)
void WritePair(string key, bool value)
void WritePair(string key, int value)
void WritePair(string key, string value)
void WritePair(string key, ReadOnlySpan<char> value)
void WritePair(string key, ReadOnlyMemory<char> value)
void WritePair(string key, StringBuilder value)
void WritePair(string key, JsonValue value)
void WritePair<TValue>(string key, TValue value)
WriteRaw
void WriteRaw(string rawJson)
[CAUTION] Writes a raw JSON literal into the output buffer, without any checks or encoding.
rawJson— JSON snippet that is already encoded
"Danger, Will Robinson !!!" Only use it if you know what you are doing, such as outputting already encoded JSON constants or in very specific use cases where performance supersedes safety!
void WriteRaw(ReadOnlySpan<char> rawJson)
[CAUTION] Writes a raw JSON literal into the output buffer, without any checks or encoding.
rawJson— JSON snippet that is already encoded
"Danger, Will Robinson !!!" Only use it if you know what you are doing, such as outputting already encoded JSON constants or in very specific use cases where performance supersedes safety!
void WriteRaw(ref DefaultInterpolatedStringHandler rawJson)
[CAUTION] Writes a raw JSON literal into the output buffer, without any checks or encoding.
rawJson— JSON snippet that is already encoded
"Danger, Will Robinson !!!" Only use it if you know what you are doing, such as outputting already encoded JSON constants or in very specific use cases where performance supersedes safety!
WriteTailSeparator
void WriteTailSeparator()
Write a comma between elements of an array
WriteUnsafeName
void WriteUnsafeName(string name)
WriteValue
void WriteValue<TJsonSerializable>(TJsonSerializable value)
Writes a IJsonSerializable instance
value— Value to write
void WriteValue(JsonValue value)
Writes a JsonValue
value— Value to write
writer.WriteValue(JsonString.Return("hello")); // => `"hello"`
void WriteValue(string value)
Writes a String
value— Value to write
writer.WriteValue("hello"); // => `"hello"`
void WriteValue(ReadOnlySpan<char> value)
Writes a span of characters
value— Value to write
writer.WriteValue("hello".AsSpan()); // => `"hello"`
void WriteValue(ReadOnlyMemory<char> value)
Writes a span of characters
value— Value to write
writer.WriteValue("hello".AsMemory()); // => `"hello"`
void WriteValue(char value)
Writes a character
value— Value to write
writer.WriteValue('A'); // => `"A"`
void WriteValue(char? value)
Writes a character
value— Value to write
writer.WriteValue((char?) 'A'); // => `"A"`
writer.WriteValue((char?) null); // => `null`
void WriteValue(StringBuilder value)
Writes the content of a StringBuilder
value— Value to write
var sb = new StringBuilder().Append("Hello").Append(", ").Append("World!");
writer.WriteValue(sb); // => `"Hello, World!"`
void WriteValue(bool value)
Writes a Boolean
value— Value to write
writer.WriteValue(false); // => `false`
writer.WriteValue(true); // => `true`
void WriteValue(bool? value)
Writes a nullable Boolean
value— Value to write
writer.WriteValue((bool?) null)); // => `null`
writer.WriteValue((bool?) false); // => `false`
writer.WriteValue((bool?) true); // => `true`
void WriteValue(byte value)
Writes a Byte, as a number literal
value— Value to write
writer.WriteValue((byte) 42); // => `42`
void WriteValue(byte? value)
Writes a nullable Byte, as a number literal
value— Value to write
writer.WriteValue((byte?) null); // => `null`
writer.WriteValue((byte?) 42); // => `42`
void WriteValue(sbyte value)
Writes a SByte, as a number literal
value— Value to write
writer.WriteValue((sbyte) 42); // => `42`
void WriteValue(sbyte? value)
Writes a nullable SByte, as a number literal
value— Value to write
writer.WriteValue((sbyte?) null); // => `null`
writer.WriteValue((sbyte?) 42); // => `42`
void WriteValue(short value)
Writes a Int16, as a number literal
value— Value to write
writer.WriteValue((short) 42); // => `42`
void WriteValue(short? value)
Writes a nullable Int16, as a number literal
value— Value to write
writer.WriteValue((short?) null); // => `null`
writer.WriteValue((short?) 42); // => `42`
void WriteValue(ushort value)
Writes a UInt16, as a number literal
value— Value to write
writer.WriteValue((ushort) 42); // => `42`
void WriteValue(ushort? value)
Writes a nullable UInt16, as a number literal
value— Value to write
writer.WriteValue((ushort?) null); // => `null`
writer.WriteValue((ushort?) 42); // => `42`
void WriteValue(int value)
Writes a Int32, as a number literal
value— Value to write
writer.WriteValue(42); // => `42`
void WriteValue(int? value)
Writes a nullable Int32, as a number literal
value— Value to write
writer.WriteValue((int?) null); // => `null`
writer.WriteValue((int?) 42); // => `42`
void WriteValue(uint value)
Writes a UInt32, as a number literal
value— Value to write
writer.WriteValue(42u); // => `42`
void WriteValue(uint? value)
Writes a nullable UInt32, as a number literal
value— Value to write
writer.WriteValue((uint?) null); // => `null`
writer.WriteValue((uint?) 42); // => `42`
void WriteValue(long value)
Writes a Int64, as a number literal
value— Value to write
writer.WriteValue(42l); // => `42`
void WriteValue(long? value)
Writes a nullable Int64, as a number literal
value— Value to write
writer.WriteValue((long?) null); // => `null`
writer.WriteValue((long?) 42l); // => `42`
void WriteValue(ulong value)
Writes a UInt64, as a number literal
value— Value to write
writer.WriteValue(42ul); // => `42`
void WriteValue(ulong? value)
Writes a nullable UInt64, as a number literal
value— Value to write
writer.WriteValue((ulong?) null); // => `null`
writer.WriteValue((ulong?) 42ul); // => `42`
void WriteValue(float value)
Writes a Single, as a number literal
value— Value to write
writer.WriteValue(1.23f); // => `1.23`
writer.WriteValue(MathF.PI); // => `3.1415927`
void WriteValue(float? value)
Writes a nullable Single, as a number literal
value— Value to write
writer.WriteValue((float?) null); // => `null`
writer.WriteValue((float?) 1.23f); // => `1.23`
void WriteValue(double value)
Writes a Double, as a number literal
value— Value to write
writer.WriteValue(1.23d); // => `1.23`
writer.WriteValue(Math.PI); // => `3.141592653589793`
void WriteValue(double? value)
Writes a nullable Double, as a number literal
value— Value to write
writer.WriteValue((double?) null); // => `null`
writer.WriteValue((double?) 1.23d); // => `1.23`
void WriteValue(decimal value)
Writes a Decimal, as a number literal
value— Value to write
writer.WriteValue(1.23m); // => `1.23`
void WriteValue(decimal? value)
Writes a nullable Decimal, as a number literal
value— Value to write
writer.WriteValue((decimal?) null); // => `null`
writer.WriteValue((decimal?) 1.23m); // => `1.23`
void WriteValue(Half value)
Writes a Half, as a number literal
value— Value to write
writer.WriteValue((Half) 1.23); // => `1.23`
void WriteValue(Half? value)
Writes a nullable Half, as a number literal
value— Value to write
writer.WriteValue((Half?) null); // => `null`
writer.WriteValue((Half?) 1.23); // => `1.23`
void WriteValue(Int128 value)
Writes a nullable Int128, as a number literal
value— Value to write
writer.WriteValue((Int128) 42); // => `42`
void WriteValue(Int128? value)
Writes a nullable Int128, as a number literal
value— Value to write
writer.WriteValue((Int128?) null); // => `null`
writer.WriteValue((Int128?) 42); // => `42`
void WriteValue(UInt128 value)
Writes a nullable UInt128, as a number literal
value— Value to write
writer.WriteValue((UInt128) 42u); // => `42`
void WriteValue(UInt128? value)
Writes a nullable UInt128, as a number literal
value— Value to write
writer.WriteValue((UInt128?) null); // => `null`
writer.WriteValue((UInt128?) 42u); // => `42`
void WriteValue(DateTime value)
Writes a DateTime, as a string literal
value— Value to write
writer.WriteValue(DateTime.UnixEpoch); // => `"1970-01-01T00:00:00.000"`
writer.WriteValue(DateTime.Now); // => `"2025-06-16T17:46:17.934"`
void WriteValue(DateTime? value)
Writes a nullable DateTime, as a string literal
value— Value to write
writer.WriteValue((DateTime?) null); // => `null`
writer.WriteValue((DateTime?) DateTime.UnixEpoch); // => `"1970-01-01T00:00:00.000"`
void WriteValue(DateTimeOffset value)
Write a DateTimeOffset, using the configured formatting
void WriteValue(DateTimeOffset? value)
Writes a nullable DateTimeOffset, as a string literal
value— Value to write
writer.WriteValue((DateTimeOffset?) null); // => `null`
writer.WriteValue((DateTimeOffset?) DateTimeOffset.UnixEpoch); // => `"1970-01-01T00:00:00.000+00:00"`
void WriteValue(DateOnly value)
Writes a DateOnly, as a string literal
value— Value to write
writer.WriteValue(DateOnly.FromDateTime(DateTime.UnixEpoch)); // => `"1970-01-01"`
void WriteValue(DateOnly? value)
Writes a nullable DateOnly, as a string literal
value— Value to write
writer.WriteValue((DateOnly?) null); // => `null`
writer.WriteValue((DateOnly?) DateOnly.FromDateTime(DateTime.UnixEpoch)); // => `"1970-01-01"`
void WriteValue(TimeOnly value)
Writes a TimeOnly, as a number of seconds
value— Value to write
writer.WriteValue(TimeOnly.MinValue); // => `0`
writer.WriteValue(new TimeOnly(23, 59, 59)); // => `86399`
void WriteValue(TimeOnly? value)
Writes a nullable TimeOnly, as a number of seconds
value— Value to write
writer.WriteValue((TimeOnly?) null); // => `null`
writer.WriteValue((TimeOnly?) new TimeOnly(23, 59, 59)); // => `86399`
void WriteValue(TimeSpan value)
Writes a TimeSpan, as a number of seconds (or an ISO 8601 duration string when Iso8601Durations is set)
value— Value to write
writer.WriteValue(TimeSpan.Zero); // => `0`
writer.WriteValue(TimeSpan.FromHours(1)); // => `3600`, or `"PT1H"` under WithIso8601Durations()
void WriteValue(TimeSpan? value)
Writes a nullable TimeSpan, as a number of seconds
value— Value to write
writer.WriteValue((TimeSpan?) null); // => `null`
writer.WriteValue((TimeSpan?) TimeSpan.FromHours(1)); // => `3600`
void WriteValue(Guid value)
Writes a Guid, as a string literal
value— Value to write
writer.WriteValue(Guid.Empty); // => `null`
writer.WriteValue(Guid.NewGuid()); // => `"68c7c18f-4490-455b-80ba-f26792604d56"`
void WriteValue(Guid? value)
Writes a nullable Guid, as a number of seconds
value— Value to write
writer.WriteValue((Guid?) null); // => `null`
writer.WriteValue((Guid?) Guid.NewGuid()); // => `"68c7c18f-4490-455b-80ba-f26792604d56"`
void WriteValue(Uuid128 value)
Writes a Uuid128, as a string literal
value— Value to write
writer.WriteValue(Uuid128.NewUuid()); // => `"68c7c18f-4490-455b-80ba-f26792604d56"`
void WriteValue(Uuid128? value)
Writes a nullable Uuid128, as a string literal
value— Value to write
writer.WriteValue((Guid?) null); // => `null`
writer.WriteValue((Guid?) Uuid128.NewUuid()); // => `"68c7c18f-4490-455b-80ba-f26792604d56"`
void WriteValue(Uuid96 value)
void WriteValue(Uuid96? value)
void WriteValue(Uuid80 value)
void WriteValue(Uuid80? value)
void WriteValue(Uuid64 value)
void WriteValue(Uuid64? value)
void WriteValue(Duration value)
void WriteValue(Duration? value)
void WriteValue(Instant date)
void WriteValue(Instant? value)
void WriteValue(LocalDateTime date)
void WriteValue(LocalDateTime? value)
void WriteValue(ZonedDateTime date)
void WriteValue(ZonedDateTime? value)
void WriteValue(OffsetDateTime date)
void WriteValue(OffsetDateTime? value)
void WriteValue(Offset offset)
void WriteValue(Offset? value)
void WriteValue(LocalDate date)
void WriteValue(LocalDate? value)
void WriteValue(LocalTime time)
void WriteValue(LocalTime? value)
void WriteValue(DateTimeZone zone)
void WriteValue(Version value)
void WriteValue(IPAddress value)
void WriteValue(Uri value)
Fields
MaxDepth
const int MaxDepth
Maximum nesting depth of a serialized object graph, on every JSON path (reflection, source-generated, and the parser)
64 is the default maximum depth of both System.Text.Json and Newtonsoft.Json, reading and writing; the parser enforces this same constant, so anything this library writes, it can read back. MaxDepth is locked to this value too.
The depth guard cannot tell a reference cycle from an acyclic graph that is simply deeper than the cap: both raise the same error. On the source-generated Pack path the guards travel inside the CrystalJsonPackContext that Pack takes by ref, so they survive the collection pack helpers and custom member converters; a true cycle there raises the recursion error at first re-entry, before this cap.