Class JsonFormatter

public sealed class JsonFormatter : object

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(JsonFormatter.Settings)

public JsonFormatter(JsonFormatter.Settings settings)

Creates a new formatted with the given settings.

Parameter
TypeNameDescription
JsonFormatter.Settingssettings

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
TypeNameDescription
IMessagemessage

The message to format.

Returns
TypeDescription
String

The formatted message.

Format(IMessage, TextWriter)

public void Format(IMessage message, TextWriter writer)

Formats the specified message as JSON.

Parameters
TypeNameDescription
IMessagemessage

The message to format.

TextWriterwriter

The TextWriter to write the formatted message to.

ToDiagnosticString(IMessage)

public static string ToDiagnosticString(IMessage message)

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

Parameter
TypeNameDescription
IMessagemessage

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 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
TypeNameDescription
TextWriterwriter

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

Objectvalue

The value to write. May be null.