Reflection-based converter from messages to JSON.
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. |
Settings controlling JSON formatting. |
string Format( IMessage message )
Formats the specified message as JSON.
Details | |||
---|---|---|---|
Parameters |
|
||
Returns |
The formatted message.
|
void Format( IMessage message, TextWriter writer )
Formats the specified message as JSON.
Details | |||||
---|---|---|---|---|---|
Parameters |
|
||||
Returns |
The formatted message.
|
JsonFormatter( Settings settings )
Creates a new formatted with the given settings.
Details | |||
---|---|---|---|
Parameters |
|
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 |
|
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 |
|
||
Returns |
The diagnostic-only JSON representation of the message
|