Show / Hide Table of Contents

Class MessageParser<T>

A parser for a specific message type.

Inheritance
System.Object
MessageParser
MessageParser<T>
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 MessageParser<T> : MessageParser where T : IMessage<T>
Type Parameters
Name Description
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>)

Creates a new parser.

Declaration
public MessageParser(Func<T> factory)
Parameters
Type Name Description
System.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)

Parses a length-delimited message from the given stream.

Declaration
public T ParseDelimitedFrom(Stream input)
Parameters
Type Name Description
System.IO.Stream input

The stream to parse.

Returns
Type Description
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)

Parses a message from the given byte string.

Declaration
public T ParseFrom(ByteString data)
Parameters
Type Name Description
ByteString data

The data to parse.

Returns
Type Description
T

The parsed message.

ParseFrom(CodedInputStream)

Parses a message from the given coded input stream.

Declaration
public T ParseFrom(CodedInputStream input)
Parameters
Type Name Description
CodedInputStream input

The stream to parse.

Returns
Type Description
T

The parsed message.

ParseFrom(Byte[])

Parses a message from a byte array.

Declaration
public T ParseFrom(byte[] data)
Parameters
Type Name Description
System.Byte[] data

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

Returns
Type Description
T

The newly parsed message.

ParseFrom(Byte[], Int32, Int32)

Parses a message from a byte array slice.

Declaration
public T ParseFrom(byte[] data, int offset, int length)
Parameters
Type Name Description
System.Byte[] data

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

System.Int32 offset

The offset of the slice to parse.

System.Int32 length

The length of the slice to parse.

Returns
Type Description
T

The newly parsed message.

ParseFrom(Stream)

Parses a message from the given stream.

Declaration
public T ParseFrom(Stream input)
Parameters
Type Name Description
System.IO.Stream input

The stream to parse.

Returns
Type Description
T

The parsed message.

ParseJson(String)

Parses a message from the given JSON.

Declaration
public T ParseJson(string json)
Parameters
Type Name Description
System.String json

The JSON to parse.

Returns
Type Description
T

The parsed message.

Exceptions
Type Condition
InvalidJsonException

The JSON does not comply with RFC 7159

InvalidProtocolBufferException

The JSON does not represent a Protocol Buffers message correctly

WithDiscardUnknownFields(Boolean)

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

Declaration
public MessageParser<T> WithDiscardUnknownFields(bool discardUnknownFields)
Parameters
Type Name Description
System.Boolean discardUnknownFields

Whether or not to discard unknown fields when parsing.

Returns
Type Description
MessageParser<T>

A newly configured message parser.

Back to top