Google.Protobuf.JsonFormatter

Reflection-based converter from messages to JSON.

Summary

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 and Destructors

JsonFormatter(Settings settings)
Creates a new formatted with the given settings.

Properties

Default
Returns a formatter using the default settings.

Public functions

Format(IMessage message)
string
Formats the specified message as JSON.
Format(IMessage message, TextWriter writer)
void
Formats the specified message as JSON.
WriteValue(TextWriter writer, object value)
void
Writes a single value to the given writer as JSON.

Public static functions

ToDiagnosticString(IMessage message)
string
Converts a message to JSON for diagnostic purposes with no extra context.

Classes

Google.Protobuf.JsonFormatter.Settings

Settings controlling JSON formatting.

Properties

Default

JsonFormatter Default

Returns a formatter using the default settings.

Public functions

Format

string Format(
  IMessage message
)

Formats the specified message as JSON.

Details
Parameters
message
The message to format.
Returns
The formatted message.

Format

void Format(
  IMessage message,
  TextWriter writer
)

Formats the specified message as JSON.

Details
Parameters
message
The message to format.
writer
The TextWriter to write the formatted message to.
Returns
The formatted message.

JsonFormatter

 JsonFormatter(
  Settings settings
)

Creates a new formatted with the given settings.

Details
Parameters
settings
The settings.

WriteValue

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).

Details
Parameters
writer
The writer to write the value to. Must not be null.
value
The value to write. May be null.

Public static functions

ToDiagnosticString

string ToDiagnosticString(
  IMessage message
)

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

This differs from calling Format(IMessage) on the default JSON formatter in its handling of Any. As no type registry is available in object.ToString calls, the normal way of resolving the type of an Any message cannot be applied. Instead, a JSON property named is included with the base64 data from the Any.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.

Details
Parameters
message
The message to format for diagnostic purposes.
Returns
The diagnostic-only JSON representation of the message