Class JsonFormatter (3.23.0)

public sealed class JsonFormatter

Reflection-based converter from messages to JSON.

Inheritance

object > JsonFormatter

Namespace

Google.Protobuf

Assembly

Google.Protobuf.dll

Remarks

Instances of this class are thread-safe, with no mutable state.

This is a simple start to get JSON formatting working. As it's reflection-based, it's not as quick as baking calls into generated messages - but is a simpler implementation. (This code is generally not heavily optimized.)

Constructors

JsonFormatter(Settings)

public JsonFormatter(JsonFormatter.Settings settings)

Creates a new formatted with the given settings.

Parameter
NameDescription
settingsJsonFormatterSettings

The settings.

Properties

Default

public static JsonFormatter Default { get; }

Returns a formatter using the default settings.

Property Value
TypeDescription
JsonFormatter

Methods

Format(IMessage)

public string Format(IMessage message)

Formats the specified message as JSON.

Parameter
NameDescription
messageIMessage

The message to format.

Returns
TypeDescription
string

The formatted message.

Remarks

This method delegates to Format(IMessage, int) with indentationLevel = 0.

Format(IMessage, int)

public string Format(IMessage message, int indentationLevel)

Formats the specified message as JSON.

Parameters
NameDescription
messageIMessage

The message to format.

indentationLevelint

Indentation level to start at.

Returns
TypeDescription
string

The formatted message.

Remarks

To keep consistent indentation when embedding a message inside another JSON string, set indentationLevel. E.g:

var response = $@"{{
      ""data"": { Format(message, indentationLevel: 1) }
    }}"

Format(IMessage, TextWriter)

public void Format(IMessage message, TextWriter writer)

Formats the specified message as JSON.

Parameters
NameDescription
messageIMessage

The message to format.

writerTextWriter

The TextWriter to write the formatted message to.

Remarks

This method delegates to Format(IMessage, TextWriter, int) with indentationLevel = 0.

Format(IMessage, TextWriter, int)

public void Format(IMessage message, TextWriter writer, int indentationLevel)

Formats the specified message as JSON. When Indentation is not null, start indenting at the specified indentationLevel.

Parameters
NameDescription
messageIMessage

The message to format.

writerTextWriter

The TextWriter to write the formatted message to.

indentationLevelint

Indentation level to start at.

Remarks

To keep consistent indentation when embedding a message inside another JSON string, set indentationLevel.

ToDiagnosticString(IMessage)

public static string ToDiagnosticString(IMessage message)

Converts a message to JSON for diagnostic purposes with no extra context.

Parameter
NameDescription
messageIMessage

The message to format for diagnostic purposes.

Returns
TypeDescription
string

The diagnostic-only JSON representation of the message

Remarks

This differs from calling Format(IMessage) on the default JSON formatter in its handling of Any. As no type registry is available in ToString() calls, the normal way of resolving the type of an Any message cannot be applied. Instead, a JSON property named @value is included with the base64 data from the Value property of the message.

The value returned by this method is only designed to be used for diagnostic purposes. It may not be parsable by JsonParser, and may not be parsable by other Protocol Buffer implementations.

WriteValue(TextWriter, object)

public void WriteValue(TextWriter writer, object value)

Writes a single value to the given writer as JSON. Only types understood by Protocol Buffers can be written in this way. This method is only exposed for advanced use cases; most users should be using Format(IMessage) or Format(IMessage, TextWriter).

Parameters
NameDescription
writerTextWriter

The writer to write the value to. Must not be null.

valueobject

The value to write. May be null.

Remarks

Delegates to WriteValue(TextWriter, object, int) with indentationLevel = 0.

WriteValue(TextWriter, object, int)

public void WriteValue(TextWriter writer, object value, int indentationLevel)

Writes a single value to the given writer as JSON. Only types understood by Protocol Buffers can be written in this way. This method is only exposed for advanced use cases; most users should be using Format(IMessage) or Format(IMessage, TextWriter).

Parameters
NameDescription
writerTextWriter

The writer to write the value to. Must not be null.

valueobject

The value to write. May be null.

indentationLevelint

The current indentationLevel. Not used when Indentation is null.