Class MessageParser<T>

public sealed class MessageParser<T> : MessageParser where T : IMessage<T>

A parser for a specific message type.

Inheritance

Object > MessageParser > MessageParser<T>

Namespace

Google.Protobuf

Assembly

Google.Protobuf.dll

Type Parameter

NameDescription
T

The type of message to be parsed.

Remarks

This delegates most behavior to the MergeFrom(CodedInputStream) implementation within the original type, but provides convenient overloads to parse from a variety of sources.

Most applications will never need to create their own instances of this type; instead, use the static Parser property of a generated message type to obtain a parser for that type.

Constructors

MessageParser(Func<T>)

public MessageParser(Func<T> factory)

Creates a new parser.

Parameter
TypeNameDescription
Func<T>factory

Function to invoke when a new, empty message is required.

Remarks

The factory method is effectively an optimization over using a generic constraint to require a parameterless constructor: delegates are significantly faster to execute.

Methods

ParseDelimitedFrom(Stream)

public T ParseDelimitedFrom(Stream input)

Parses a length-delimited message from the given stream.

Parameter
TypeNameDescription
Streaminput

The stream to parse.

Returns
TypeDescription
T

The parsed message.

Remarks

The stream is expected to contain a length and then the data. Only the amount of data specified by the length will be consumed.

ParseFrom(ByteString)

public T ParseFrom(ByteString data)

Parses a message from the given byte string.

Parameter
TypeNameDescription
ByteStringdata

The data to parse.

Returns
TypeDescription
T

The parsed message.

ParseFrom(CodedInputStream)

public T ParseFrom(CodedInputStream input)

Parses a message from the given coded input stream.

Parameter
TypeNameDescription
CodedInputStreaminput

The stream to parse.

Returns
TypeDescription
T

The parsed message.

ParseFrom(ReadOnlySequence<Byte>)

public T ParseFrom(ReadOnlySequence<byte> data)

Parses a message from the given sequence.

Parameter
TypeNameDescription
ReadOnlySequence<Byte>data

The data to parse.

Returns
TypeDescription
T

The parsed message.

ParseFrom(Stream)

public T ParseFrom(Stream input)

Parses a message from the given stream.

Parameter
TypeNameDescription
Streaminput

The stream to parse.

Returns
TypeDescription
T

The parsed message.

ParseFrom(Byte[])

public T ParseFrom(byte[] data)

Parses a message from a byte array.

Parameter
TypeNameDescription
Byte[]data

The byte array containing the message. Must not be null.

Returns
TypeDescription
T

The newly parsed message.

ParseFrom(Byte[], Int32, Int32)

public T ParseFrom(byte[] data, int offset, int length)

Parses a message from a byte array slice.

Parameters
TypeNameDescription
Byte[]data

The byte array containing the message. Must not be null.

Int32offset

The offset of the slice to parse.

Int32length

The length of the slice to parse.

Returns
TypeDescription
T

The newly parsed message.

ParseJson(String)

public T ParseJson(string json)

Parses a message from the given JSON.

Parameter
TypeNameDescription
Stringjson

The JSON to parse.

Returns
TypeDescription
T

The parsed message.

Exceptions
TypeCondition
InvalidJsonException

The JSON does not comply with RFC 7159

InvalidProtocolBufferException

The JSON does not represent a Protocol Buffers message correctly

WithDiscardUnknownFields(Boolean)

public MessageParser<T> WithDiscardUnknownFields(bool discardUnknownFields)

Creates a new message parser which optionally discards unknown fields when parsing.

Parameter
TypeNameDescription
BooleandiscardUnknownFields

Whether or not to discard unknown fields when parsing.

Returns
TypeDescription
MessageParser<T>

A newly configured message parser.

WithExtensionRegistry(ExtensionRegistry)

public MessageParser<T> WithExtensionRegistry(ExtensionRegistry registry)

Creates a new message parser which registers extensions from the specified registry upon creating the message instance

Parameter
TypeNameDescription
ExtensionRegistryregistry

The extensions to register

Returns
TypeDescription
MessageParser<T>

A newly configured message parser.