CrystalJson

Namespace: SnowBank.Data.Json · class

Helper class to serialize, parse or deserialize JSON documents

Properties

DisableReflection

static bool DisableReflection { get; set; }

Diagnostic switch: when true, any value that would be (de)serialized through the runtime reflection path (a type with no source-generated converter, handled by CrystalJsonTypeResolver) throws JsonReflectionDisabledException instead. Off by default.

Set it while checking that a code path stays on the source-generated or JSON DOM track for Native AoT: a hidden reflection fallback then throws, naming the type, instead of working until the trimmer removes a member. Process-wide (not thread-scoped); set it once at startup, before the resolver caches any type definition, so every reflected type trips on first use.

IsReflectionSupported

static bool IsReflectionSupported { get; }

Whether the runtime reflection (de)serialization path is available.

Defaults to . An application that publishes trimmed or Native AoT can set the MSBuild feature SnowBank.Data.Json.CrystalJson.IsReflectionSupported=false (or the matching runtime config switch): the trimmer then substitutes this property to and removes the whole reflection subtree, so a source-generated consumer builds with no trim warnings. Every reflection entry point is guarded by this property, so with reflection off a reflected type throws .

Methods

Convert

static void Convert<TState>(TState state, Action<CrystalJsonWriter, TState> handler, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Performs a custom seriazation operation, using a pooled CrystalJsonWriter.

  • state — State that is forwarded to handler
  • handler — Handler that receives a pooled writer, and forwarded state, and will use the writer to produce some JSON and consume the result.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

The handler is expected to call any of the GetString, GetUtf8Slice or similar methods, before returning.

The handler MUST NOT expose the pooled writer to the outside! Doing this would break the application

static TResult Convert<TState, TResult>(TState state, Func<CrystalJsonWriter, TState, TResult> handler, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Performs a custom serialization operation, using a pooled CrystalJsonWriter.

  • state — State that is forwarded to handler
  • handler — Handler that receives a pooled writer, and forwarded state, and will use the writer to produce some JSON and consume the result.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: The result of the custom serialization (usually a String, Slice or any other value)

The handler is expected to call any of the GetString, GetUtf8Slice or similar methods, to extract the content of the writer.

The handler MUST NOT expose the pooled writer to the outside! Doing this would break the application

ConvertAsync

static Task ConvertAsync<TState>(TState state, Func<CrystalJsonWriter, TState, Task> handler, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Performs a custom seriazation operation, using a pooled CrystalJsonWriter.

  • state — State that is forwarded to handler
  • handler — Handler that receives a pooled writer, and forwarded state, and will use the writer to produce some JSON and consume the result.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

The handler is expected to call any of the GetString, GetUtf8Slice or similar methods, before returning.

The handler MUST NOT expose the pooled writer to the outside! Doing this would break the application

static Task<TResult> ConvertAsync<TState, TResult>(TState state, Func<CrystalJsonWriter, TState, Task<TResult>> handler, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Performs a custom serialization operation, using a pooled CrystalJsonWriter.

  • state — State that is forwarded to handler
  • handler — Handler that receives a pooled writer, and forwarded state, and will use the writer to produce some JSON and consume the result.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: The result of the custom serialization (usually a String, Slice or any other value)

The handler is expected to call any of the GetString, GetUtf8Slice or similar methods, to extract the content of the writer.

The handler MUST NOT expose the pooled writer to the outside! Doing this would break the application

Deserialize

static TValue Deserialize<TValue>(string jsonText)

De-serializes a JSON text literal into a value of type TValue

  • jsonText — JSON text document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(byte[] jsonBytes)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(Slice jsonBytes)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(string jsonText, TValue defaultValue)

De-serializes a JSON text literal into a value of type TValue

  • jsonText — JSON text document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(byte[] jsonBytes, TValue defaultValue)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(Slice jsonBytes, TValue defaultValue)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(ReadOnlySpan<byte> jsonBytes, TValue defaultValue)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(ReadOnlyMemory<byte> jsonBytes, TValue defaultValue)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse

Returns: Deserialized instance

static TValue Deserialize<TValue>(string jsonText, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(byte[] jsonBytes, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(Slice jsonBytes, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(ReadOnlySpan<byte> jsonBytes, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(ReadOnlyMemory<byte> jsonBytes, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(string jsonText, IJsonDeserializer<TValue> serializer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(string jsonText, TValue defaultValue, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(byte[] jsonBytes, TValue defaultValue, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(Slice jsonBytes, TValue defaultValue, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(ReadOnlySpan<byte> jsonBytes, TValue defaultValue, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue Deserialize<TValue>(ReadOnlyMemory<byte> jsonBytes, TValue defaultValue, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver = null)

De-serializes a JSON text literal into a value of type TValue

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

Dump

static string Dump(object value)

Serializes a boxed value into an indented JSON string, suitable for humans (logging, troubleshooting, ...)

static string Dump<TValue>(TValue value)

Serializes a value into an indented JSON string, suitable for humans (logging, troubleshooting, ...)

LoadFrom

static TValue LoadFrom<TValue>(TextReader source, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Deserializes the content of a source of JSON text into an instance of type TValue

  • source — Source of text to parse
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue LoadFrom<TValue>(Stream source, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Deserializes the content of a stream into an instance of type TValue

  • source — File to read
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Deserialized instance

static TValue LoadFrom<TValue>(string path, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null, LoadOptions options = 0)

Deserializes the content of a file on disk into an instance of type TValue

  • path — Path to the file to read
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)
  • options — Read options

Returns: Deserialized instance

MaybeJsonDocument

static bool MaybeJsonDocument(byte[] jsonBytes)

Checks if the buffer is likely to contain a JSON Object or Array

  • jsonBytes — Buffer contains UTF-8 encoded text to inspect

Returns: true if the buffer MAY contain either a JSON Object ({...}) or JSON Array ([...])

Note: this uses a heuristic that does not perform a complete analysis, and may return false positives!

static bool MaybeJsonDocument(Slice jsonBytes)

Checks if the buffer is likely to contain a JSON Object or Array

  • jsonBytes — Buffer contains UTF-8 encoded text to inspect

Returns: true if the buffer MAY contain either a JSON Object ({...}) or JSON Array ([...])

Note: this uses a heuristic that does not perform a complete analysis, and may return false positives!

static bool MaybeJsonDocument(ReadOnlySpan<byte> jsonBytes)

Checks if the buffer is likely to contain a JSON Object or Array

  • jsonBytes — Buffer contains UTF-8 encoded text to inspect

Returns: true if the buffer MAY contain either a JSON Object ({...}) or JSON Array ([...])

Note: this uses a heuristic that does not perform a complete analysis, and may return false positives!

Parse

static JsonValue Parse(string jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal, and returns the corresponding JSON value

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonText is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(byte[] jsonBytes, CrystalJsonSettings settings = null)

Parses a JSON buffer, and returns the corresponding JSON value

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonBytes is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(Slice jsonBytes, CrystalJsonSettings settings = null)

Parses a JSON buffer, and returns the corresponding JSON value

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonBytes is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(ReadOnlySpan<byte> jsonBytes, CrystalJsonSettings settings = null)

Parses a JSON buffer, and returns the corresponding JSON value

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonBytes is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(ReadOnlyMemory<byte> jsonBytes, CrystalJsonSettings settings = null)

Parses a JSON buffer, and returns the corresponding JSON value

  • jsonBytes — UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonBytes is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(ref ReadOnlySequence<byte> jsonBytes, CrystalJsonSettings settings = null)

Parses a JSON sequence of buffer, and returns the corresponding JSON value

  • jsonBytes — Sequence of buffers containing the UTF-8 encoded JSON document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonBytes is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(ReadOnlySpan<char> jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal, and returns the corresponding JSON value

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonText is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(ReadOnlyMemory<char> jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal, and returns the corresponding JSON value

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonText is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue Parse(ref ReadOnlySequence<char> jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal sequence, and returns the corresponding JSON value

  • jsonText — Sequence of buffers containing the JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If jsonText is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

ParseFragment

static JsonValue ParseFragment(ReadOnlySpan<char> jsonText, out int charsRead, CrystalJsonSettings settings = null)

Parses the next JSON fragment from a text literal

  • jsonText — JSON text document to parse
  • charsRead — Receives the number of characters read from jsonText, or 0 if no fragment was found.
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value, or Missing if jsonText is empty or composed only of white-space characters

When called in a loop to consume a multi-fragment buffer, the caller should stop when charsRead is 0.

This methods will skip any initial whitespaces (which will be included in charsRead), but will not skip any trailing whitespaces.

ParseFrom

static JsonValue ParseFrom(TextReader reader, CrystalJsonSettings settings = null)

Reads the content of a reader, and returns the corresponding JSON value

  • reader — Instance from which to read the JSON text document
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If reader is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue ParseFrom(Stream source, CrystalJsonSettings settings = null)

Reads the content of a file, and returns the corresponding JSON value

  • source — Instance from which to read the JSON document
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON value. If source is empty, will return Missing

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static JsonValue ParseFrom(string path, CrystalJsonSettings settings = null, LoadOptions options = 0)

Reads the content of a file, and returns the corresponding JSON value

  • path — Instance from which to read the JSON document
  • settings — Serialization settings (use default JSON settings if null)
  • options — Options used for streaming operations.

Returns: Corresponding JSON value. If the file is missing or empty and the ReturnNullIfMissing flag is set in options, Missing will be returned instead.

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

ParseFromAsync

static Task<JsonValue> ParseFromAsync(string path, CancellationToken ct)

Reads the content of a file, and returns the corresponding JSON value

  • path — Instance from which to read the JSON document
  • ct — Token used to cancel the operation

Returns: Corresponding JSON value.

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please use one of the overloads that accepts a CrystalJsonSettings parameter.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static Task<JsonValue> ParseFromAsync(string path, CrystalJsonSettings settings, CancellationToken ct)

Reads the content of a file, and returns the corresponding JSON value

  • path — Instance from which to read the JSON document
  • settings — Serialization settings (use default JSON settings if null)
  • ct — Token used to cancel the operation

Returns: Corresponding JSON value.

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static Task<JsonValue> ParseFromAsync(Stream source, CrystalJsonSettings settings, CancellationToken ct)

Reads the content of a file, and returns the corresponding JSON value

  • source — Instance from which to read the JSON document
  • settings — Serialization settings (use default JSON settings if null)
  • ct — Token used to cancel the operation

Returns: Corresponding JSON value.

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

static Task<JsonValue> ParseFromAsync(string path, CrystalJsonSettings settings, LoadOptions options, CancellationToken ct)

Reads the content of a file, and returns the corresponding JSON value

  • path — Instance from which to read the JSON document
  • settings — Serialization settings (use default JSON settings if null)
  • options — Options used for streaming operations.
  • ct — Token used to cancel the operation

Returns: Corresponding JSON value. If the file is missing or empty and the ReturnNullIfMissing flag is set in options, Missing will be returned instead.

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please configure the settings accordingly.

If the result is always expected to be an Array or an Object, please call AsArray or AsObject on the result.

SaveTo

static void SaveTo(string path, object value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null, SaveOptions options = 0)

Serializes a boxed value (of any type) into the file at the specified path

  • path — Path to the output file
  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)
  • options — Save settings

static void SaveTo<T>(string path, T value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null, SaveOptions options = 0)

Serializes a value (of any type) into the file at the specified path

  • path — Path to the output file
  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)
  • options — Save settings

Serialize

static string Serialize(object value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a boxed value (of any type)

  • value — Instance to serialize (can be null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

static string Serialize<T>(T value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value of type T into a string literal

  • value — Instance to serialize (can be null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

static string Serialize(object value, Type declaredType, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a boxed value (of any type)

  • value — Instance to serialize (can be null)
  • declaredType — Type of the field or property, as declared in the parent type.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

static string Serialize<T>(T value, IJsonSerializer<T> serializer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value of type T into a string literal, using a customer serializer

  • value — Instance to serialize (can be null)
  • serializer — Custom serializer for this type
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

static StringBuilder Serialize(object value, StringBuilder buffer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a boxed value (of any type) into the specified buffer

  • value — Instance to serialize (can be null)
  • buffer — Destination buffer (created automatically if null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: The value of buffer, for call chaining

SerializeJson

static string SerializeJson(JsonValue value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value (of any type)

  • value — Instance to serialize (can be null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

static string SerializeJson(IJsonSerializable value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value that implements IJsonSerializable

  • value — Instance to serialize (can be null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

SerializeTo

static TextWriter SerializeTo(TextWriter output, object value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a boxed value (of any type) into the specified output

  • output — Output for the JSON document
  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: The output instance, for call chaining

static TextWriter SerializeTo<T>(TextWriter output, T value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value (of any type) into the specified output

  • output — Output for the JSON document
  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: The output instance, for call chaining

static void SerializeTo<T>(Stream output, T value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value (of any type) into the specified stream

  • output — Output for the JSON document
  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

ToBytes

static byte[] ToBytes(object value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a boxed value into an in-memory buffer

Returns: Byte array that contains the resulting JSON document

static byte[] ToBytes<T>(T value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value into an in-memory buffer

Returns: Byte array that contains the resulting JSON document

static byte[] ToBytes<T>(T value, IJsonSerializer<T> serializer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value into an in-memory buffer

Returns: Byte array that contains the resulting JSON document

ToJsonSlice

static Slice ToJsonSlice(JsonValue value, CrystalJsonSettings settings = null)

Serializes a JSON value into an UTF-8 encoded Slice

  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)

Returns: Slice of memory that contains the utf-8 encoded JSON document

static SliceOwner ToJsonSlice(JsonValue value, ArrayPool<byte> pool, CrystalJsonSettings settings = null)

Serializes a JSON value into an UTF-8 encoded Slice

  • value — Instance to serialize (of any type)
  • pool — Pool used to allocate the content of the slice (use Shared if null)
  • settings — Serialization settings (use default JSON settings if null)

Returns: Slice of memory that contains the utf-8 encoded JSON document

ToSlice

static Slice ToSlice(object value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a boxed value into an UTF-8 encoded Slice

  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Slice of memory that contains the utf-8 encoded JSON document

static Slice ToSlice<T>(T value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value into an UTF-8 encoded Slice

  • value — Instance to serialize (of any type)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Slice of memory that contains the utf-8 encoded JSON document

static Slice ToSlice<T>(T value, IJsonSerializer<T> serializer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value of type T into a Slice, using a customer serializer

  • value — Instance to serialize (can be null)
  • serializer — Custom serializer
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

static SliceOwner ToSlice<T>(T value, ArrayPool<byte> pool, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value of type T into a SliceOwner using the specified pool

  • value — Instance to serialize (can be null)
  • pool — Pool used to allocate the content of the slice (use Shared if null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

The SliceOwner returned MUST be disposed; otherwise, the rented buffer will not be returned to the pool.

static SliceOwner ToSlice<T>(ReadOnlySpan<T> values, ArrayPool<byte> pool, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a span of values of type T as a JSON Array into a SliceOwner using the specified pool

  • values — Span of the instances to serialize (can be null)
  • pool — Pool used to allocate the content of the slice (use Shared if null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `[ 123, 456, 789, ... ]`, `[ true, true, false, ... ]`, `[ "ABC", "DEF", "GHI", ... ]`, `[ { "foo":..., "bar": ... }, { "foo":..., "bar": ... }, ... ]`, `[ [ ... ], [ ... ], ... ]`, ...

The SliceOwner returned MUST be disposed; otherwise, the rented buffer will not be returned to the pool.

static SliceOwner ToSlice(object value, ArrayPool<byte> pool, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)

Serializes a boxed value into an UTF-8 encoded Slice

  • value — Instance to serialize (of any type)
  • pool — Pool used to allocate the content of the slice (use Shared if null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Slice of memory that contains the utf-8 encoded JSON document

The SliceOwner returned MUST be disposed; otherwise, the rented buffer will not be returned to the pool.

static Slice ToSlice(object value, Type type, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)

Serializes a boxed value into an UTF-8 encoded Slice.

  • value — Instance to serialize (of any type).
  • type — The advertised type of the instance, if known.
  • settings — Serialization settings (use default JSON settings if null).
  • resolver — Custom type resolver (use default behavior if null).

Returns: Slice of memory that contains the UTF-8 encoded JSON document.

If the type of the value is an interface or abstract class, or if the value is a derived type, the serialized document may include an additional attribute with the original type name, which may not be recognized by other libraries or platforms.

static SliceOwner ToSlice<T>(T value, IJsonSerializer<T> serializer, ArrayPool<byte> pool, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes a value of type T into a SliceOwner, using a customer serializer, and the specified pool

  • value — Instance to serialize (can be null)
  • serializer — Custom serializer
  • pool — Pool used to allocate the content of the slice (use Shared if null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: `123`, `true`, `"ABC"`, `{ "foo":..., "bar": ... }`, `[ ... ]`, ...

The SliceOwner returned MUST be disposed; otherwise, the rented buffer will not be returned to the pool.

static SliceOwner ToSlice(object value, Type type, ArrayPool<byte> pool, CrystalJsonSettings settings, ICrystalJsonTypeResolver resolver)

Serializes a boxed value into an UTF-8 encoded Slice

  • value — Instance to serialize (of any type)
  • type — The advertised type of the instance, if known.
  • pool — Pool used to allocate the content of the slice (use Shared if null)
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Slice of memory that contains the utf-8 encoded JSON document

The SliceOwner returned MUST be disposed; otherwise, the rented buffer will not be returned to the pool.

If type is an interface or abstract class, or if value is a derived type of type, the serialized document may include an additional attribute with the original type name, which may not be recognized by other libraries or platforms.

Warmup

static void Warmup()

Runs a warmup sequence that will force the JIT visit the most commonly used types and methods at least once

You should only call this from a custom unit test or benchmark framework.

Fields

DefaultResolver

static readonly CrystalJsonTypeResolver DefaultResolver

Default type resolver