Class JsonFormatter
Reflection-based converter from messages to JSON.
Inheritance
Inherited Members
Namespace: Google.Protobuf
Assembly: Google.Protobuf.dll
Syntax
public sealed class JsonFormatter
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)
Creates a new formatted with the given settings.
Declaration
public JsonFormatter(JsonFormatter.Settings settings)
Parameters
Type | Name | Description |
---|---|---|
JsonFormatter.Settings | settings | The settings. |
Properties
Default
Returns a formatter using the default settings.
Declaration
public static JsonFormatter Default { get; }
Property Value
Type | Description |
---|---|
JsonFormatter |
Methods
Format(IMessage)
Formats the specified message as JSON.
Declaration
public string Format(IMessage message)
Parameters
Type | Name | Description |
---|---|---|
IMessage | message | The message to format. |
Returns
Type | Description |
---|---|
System.String | The formatted message. |
Format(IMessage, TextWriter)
Formats the specified message as JSON.
Declaration
public void Format(IMessage message, TextWriter writer)
Parameters
Type | Name | Description |
---|---|---|
IMessage | message | The message to format. |
System.IO.TextWriter | writer | The TextWriter to write the formatted message to. |
ToDiagnosticString(IMessage)
Converts a message to JSON for diagnostic purposes with no extra context.
Declaration
public static string ToDiagnosticString(IMessage message)
Parameters
Type | Name | Description |
---|---|---|
IMessage | message | The message to format for diagnostic purposes. |
Returns
Type | Description |
---|---|
System.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 System.Object.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)
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).
Declaration
public void WriteValue(TextWriter writer, object value)
Parameters
Type | Name | Description |
---|---|---|
System.IO.TextWriter | writer | The writer to write the value to. Must not be null. |
System.Object | value | The value to write. May be null. |