Show / Hide Table of Contents

Class JsonParser

Reflection-based converter from JSON to messages.

Inheritance
System.Object
JsonParser
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: Google.Protobuf
Assembly: Google.Protobuf.dll
Syntax
public sealed class JsonParser
Remarks

Instances of this class are thread-safe, with no mutable state.

This is a simple start to get JSON parsing 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

JsonParser(JsonParser.Settings)

Creates a new formatted with the given settings.

Declaration
public JsonParser(JsonParser.Settings settings)
Parameters
Type Name Description
JsonParser.Settings settings

The settings.

Properties

Default

Returns a formatter using the default settings.

Declaration
public static JsonParser Default { get; }
Property Value
Type Description
JsonParser

Methods

Parse(TextReader, MessageDescriptor)

Parses JSON read from jsonReader into a new message.

Declaration
public IMessage Parse(TextReader jsonReader, MessageDescriptor descriptor)
Parameters
Type Name Description
System.IO.TextReader jsonReader

Reader providing the JSON to parse.

MessageDescriptor descriptor

Descriptor of message type to parse.

Returns
Type Description
IMessage
Exceptions
Type Condition
InvalidJsonException

The JSON does not comply with RFC 7159

InvalidProtocolBufferException

The JSON does not represent a Protocol Buffers message correctly

Parse(String, MessageDescriptor)

Parses json into a new message.

Declaration
public IMessage Parse(string json, MessageDescriptor descriptor)
Parameters
Type Name Description
System.String json

The JSON to parse.

MessageDescriptor descriptor

Descriptor of message type to parse.

Returns
Type Description
IMessage
Exceptions
Type Condition
InvalidJsonException

The JSON does not comply with RFC 7159

InvalidProtocolBufferException

The JSON does not represent a Protocol Buffers message correctly

Parse<T>(TextReader)

Parses JSON read from jsonReader into a new message.

Declaration
public T Parse<T>(TextReader jsonReader)
    where T : IMessage, new()
Parameters
Type Name Description
System.IO.TextReader jsonReader

Reader providing the JSON to parse.

Returns
Type Description
T
Type Parameters
Name Description
T

The type of message to create.

Exceptions
Type Condition
InvalidJsonException

The JSON does not comply with RFC 7159

InvalidProtocolBufferException

The JSON does not represent a Protocol Buffers message correctly

Parse<T>(String)

Parses json into a new message.

Declaration
public T Parse<T>(string json)
    where T : IMessage, new()
Parameters
Type Name Description
System.String json

The JSON to parse.

Returns
Type Description
T
Type Parameters
Name Description
T

The type of message to create.

Exceptions
Type Condition
InvalidJsonException

The JSON does not comply with RFC 7159

InvalidProtocolBufferException

The JSON does not represent a Protocol Buffers message correctly

Back to top