# Class MessageParser (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)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.MessageParser)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.MessageParser)

    public class MessageParser

A general message parser, typically used by reflection-based code as all the methods
return simple [IMessage](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage).

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> MessageParser

## 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.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Derived Types

[MessageParser\<T\>](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser-1)

## Namespace

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

## Assembly

Google.Protobuf.dll

## Methods

### ParseDelimitedFrom(Stream)

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | 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 IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The parsed message. |

### ParseFrom(CodedInputStream)

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The parsed message. |

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

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The parsed message. |

### ParseFrom(byte\[\])

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The newly parsed message. |

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

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The newly parsed message. |

### ParseFrom(Stream)

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The parsed message. |

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

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The parsed message. |

### ParseJson(string)

    public IMessage 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` | The parsed message. |

**Remarks** This method always uses the default JSON parser; it is not affected by [WithDiscardUnknownFields(bool)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser#Google_Protobuf_MessageParser_WithDiscardUnknownFields_System_Boolean_).
To ignore unknown fields when parsing JSON, create a [JsonParser](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser) using a [JsonParser.Settings](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser.Settings)
with [IgnoreUnknownFields](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser.Settings#Google_Protobuf_JsonParser_Settings_IgnoreUnknownFields) set to true and call [Parse\<T\>(string)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser#Google_Protobuf_JsonParser_Parse__1_System_String_) directly.

| **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 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` | A newly configured message parser. |

**Remarks** Note that this does not affect the behavior of [ParseJson(string)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser#Google_Protobuf_MessageParser_ParseJson_System_String_)
at all. To ignore unknown fields when parsing JSON, create a [JsonParser](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser) using a [JsonParser.Settings](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser.Settings)
with [IgnoreUnknownFields](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser.Settings#Google_Protobuf_JsonParser_Settings_IgnoreUnknownFields) set to true and call [Parse\<T\>(string)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser#Google_Protobuf_JsonParser_Parse__1_System_String_) directly.

### WithExtensionRegistry(ExtensionRegistry)

    public MessageParser 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` | A newly configured message parser. |