# Class MessageParser&lt;T&gt; (3.27.1)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [3.27.1 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser-1)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.MessageParser-1)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.MessageParser-1)

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

A parser for a specific message type.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> [MessageParser](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser) \> MessageParser\<T\>

## Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Protobuf](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf)

## Assembly

Google.Protobuf.dll

| ## Type Parameter ||
|---|---|
| **Name** | **Description** |
| `T` | The type of message to be parsed. |

## Remarks


This delegates most behavior to the
[MergeFrom(CodedInputStream)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage#Google_Protobuf_IMessage_MergeFrom_Google_Protobuf_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** ||
|---|---|
| **Name** | **Description** |
| `factory` | `https://learn.microsoft.com/dotnet/api/system.func-1` 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** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://learn.microsoft.com/dotnet/api/system.io.stream` 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)

    public T ParseFrom(ByteString data)

Parses a message from the given byte string.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `data` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` The data to parse. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The parsed message. |

### ParseFrom(CodedInputStream)

    public T ParseFrom(CodedInputStream input)

Parses a message from the given coded input stream.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream` The stream to parse. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The parsed message. |

### ParseFrom(ReadOnlySequence\<byte\>)

    public T ParseFrom(ReadOnlySequence<byte> data)

Parses a message from the given sequence.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `data` | `https://learn.microsoft.com/dotnet/api/system.buffers.readonlysequence-1https://learn.microsoft.com/dotnet/api/system.byte` The data to parse. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The parsed message. |

### ParseFrom(byte\[\])

    public T ParseFrom(byte[] data)

Parses a message from a byte array.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `data` | `https://learn.microsoft.com/dotnet/api/system.byte` The byte array containing the message. Must not be null. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The newly parsed message. |

### ParseFrom(byte\[\], int, int)

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

Parses a message from a byte array slice.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `data` | `https://learn.microsoft.com/dotnet/api/system.byte` The byte array containing the message. Must not be null. |
| `offset` | `https://learn.microsoft.com/dotnet/api/system.int32` The offset of the slice to parse. |
| `length` | `https://learn.microsoft.com/dotnet/api/system.int32` The length of the slice to parse. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The newly parsed message. |

### ParseFrom(Stream)

    public T ParseFrom(Stream input)

Parses a message from the given stream.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://learn.microsoft.com/dotnet/api/system.io.stream` The stream to parse. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The parsed message. |

### ParseFrom(ReadOnlySpan\<byte\>)

    public T ParseFrom(ReadOnlySpan<byte> data)

Parses a message from the given span.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `data` | `https://learn.microsoft.com/dotnet/api/system.readonlyspan-1https://learn.microsoft.com/dotnet/api/system.byte` The data to parse. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The parsed message. |

### ParseJson(string)

    public T ParseJson(string json)

Parses a message from the given JSON.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `json` | `https://learn.microsoft.com/dotnet/api/system.string` The JSON to parse. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The parsed message. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.InvalidJsonException` | The JSON does not comply with RFC 7159 |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.InvalidProtocolBufferException` | The JSON does not represent a Protocol Buffers message correctly |

### WithDiscardUnknownFields(bool)

    public MessageParser<T> WithDiscardUnknownFields(bool discardUnknownFields)

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

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `discardUnknownFields` | `https://learn.microsoft.com/dotnet/api/system.boolean` Whether or not to discard unknown fields when parsing. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser-1` | 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** ||
|---|---|
| **Name** | **Description** |
| `registry` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ExtensionRegistry` The extensions to register |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser-1` | A newly configured message parser. |