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
Name Description
settings JsonFormatterSettings

The settings.

Properties

Default

public static JsonFormatter Default { get; }

Returns a formatter using the default settings.

Property Value
Type Description
JsonFormatter

Methods

Format(IMessage)

public string Format(IMessage message)

Formats the specified message as JSON.

Parameter
Name Description
message IMessage

The message to format.

Returns
Type Description
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
Name Description
message IMessage

The message to format.

indentationLevel int

Indentation level to start at.

Returns
Type Description
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
Name Description
message IMessage

The message to format.

writer TextWriter

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
Name Description
message IMessage

The message to format.

writer TextWriter

The TextWriter to write the formatted message to.

indentationLevel int

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
Name Description
message IMessage

The message to format for diagnostic purposes.

Returns
Type Description
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
Name Description
writer TextWriter

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

value object

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
Name Description
writer TextWriter

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

value object

The value to write. May be null.

indentationLevel int

The current indentationLevel. Not used when Indentation is null.