C# Class Google.Protobuf.JsonFormatter

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

Exibir arquivo Open project: mirror/chromium Class Usage Examples

Private Properties

Property Type Description
CanWriteSingleValue bool
HexEncodeUtf16CodeUnit void
IsDefaultValue bool
JsonFormatter System
ToCamelCase string
ToCamelCaseForFieldMask string
WriteAny void
WriteDiagnosticOnlyAny void
WriteDictionary void
WriteDuration void
WriteFieldMask void
WriteList void
WriteMessage void
WriteMessageFields bool
WriteNull void
WriteString void
WriteStruct void
WriteStructFieldValue void
WriteTimestamp void
WriteValue void
WriteWellKnownTypeValue void

Public Methods

Method Description
Format ( IMessage message ) : string

Formats the specified message as JSON.

Format ( IMessage message, TextWriter writer ) : void

Formats the specified message as JSON.

JsonFormatter ( Settings settings ) : System

Creates a new formatted with the given settings.

ToDiagnosticString ( IMessage message ) : string

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 @value 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.

Private Methods

Method Description
CanWriteSingleValue ( object value ) : bool

Returns whether or not a singular value can be represented in JSON. Currently only relevant for enums, where unknown values can't be represented. For repeated/map fields, this always returns true.

HexEncodeUtf16CodeUnit ( TextWriter writer, char c ) : void
IsDefaultValue ( IFieldAccessor accessor, object value ) : bool
JsonFormatter ( ) : System
ToCamelCase ( string input ) : string
ToCamelCaseForFieldMask ( string input ) : string

Camel-case converter with added strictness for field mask formatting.

WriteAny ( TextWriter writer, IMessage value ) : void
WriteDiagnosticOnlyAny ( TextWriter writer, IMessage value ) : void
WriteDictionary ( TextWriter writer, IDictionary dictionary ) : void
WriteDuration ( TextWriter writer, IMessage value ) : void
WriteFieldMask ( TextWriter writer, IMessage value ) : void
WriteList ( TextWriter writer, IList list ) : void
WriteMessage ( TextWriter writer, IMessage message ) : void
WriteMessageFields ( TextWriter writer, IMessage message, bool assumeFirstFieldWritten ) : bool
WriteNull ( TextWriter writer ) : void
WriteString ( TextWriter writer, string text ) : void

Writes a string (including leading and trailing double quotes) to a builder, escaping as required.

Other than surrogate pair handling, this code is mostly taken from src/google/protobuf/util/internal/json_escaping.cc.

WriteStruct ( TextWriter writer, IMessage message ) : void
WriteStructFieldValue ( TextWriter writer, IMessage message ) : void
WriteTimestamp ( TextWriter writer, IMessage value ) : void
WriteValue ( TextWriter writer, object value ) : void
WriteWellKnownTypeValue ( TextWriter writer, MessageDescriptor descriptor, object value ) : void

Central interception point for well-known type formatting. Any well-known types which don't need special handling can fall back to WriteMessage. We avoid assuming that the values are using the embedded well-known types, in order to allow for dynamic messages in the future.

Method Details

Format() public method

Formats the specified message as JSON.
public Format ( IMessage message ) : string
message IMessage The message to format.
return string

Format() public method

Formats the specified message as JSON.
public Format ( IMessage message, TextWriter writer ) : void
message IMessage The message to format.
writer System.IO.TextWriter The TextWriter to write the formatted message to.
return void

JsonFormatter() public method

Creates a new formatted with the given settings.
public JsonFormatter ( Settings settings ) : System
settings Settings The settings.
return System

ToDiagnosticString() public static method

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 @value 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.

public static ToDiagnosticString ( IMessage message ) : string
message IMessage The message to format for diagnostic purposes.
return string