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

    public sealed class JsonFormatter

Reflection-based converter from messages to JSON.

## Inheritance

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

## 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

## Remarks


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


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

### JsonFormatter(Settings)

    public JsonFormatter(JsonFormatter.Settings settings)

Creates a new formatted with the given settings.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `settings` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatterhttps://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter.Settings` The settings. |

## Properties

### Default

    public static JsonFormatter Default { get; }

Returns a formatter using the default settings.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter` |   |

## Methods

### Format(IMessage)

    public string Format(IMessage message)

Formats the specified message as JSON.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `message` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` The message to format. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The formatted message. |

**Remarks** This method delegates to `Format(IMessage, int)` with `indentationLevel =
0`.

### Format(IMessage, TextWriter)

    public void Format(IMessage message, TextWriter writer)

Formats the specified message as JSON.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `message` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` The message to format. |
| `writer` | `https://learn.microsoft.com/dotnet/api/system.io.textwriter` The TextWriter to write the formatted message to. |

**Remarks** This method delegates to `Format(IMessage, TextWriter, int)` with
`indentationLevel = 0`.

### Format(IMessage, TextWriter, int)

    public void Format(IMessage message, TextWriter writer, int indentationLevel)

Formats the specified message as JSON. When [Indentation](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter.Settings#Google_Protobuf_JsonFormatter_Settings_Indentation) is not null,
start indenting at the specified `indentationLevel`.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `message` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` The message to format. |
| `writer` | `https://learn.microsoft.com/dotnet/api/system.io.textwriter` The TextWriter to write the formatted message to. |
| `indentationLevel` | `https://learn.microsoft.com/dotnet/api/system.int32` Indentation level to start at. |

**Remarks** To keep consistent indentation when embedding a message inside another JSON string,
set `indentationLevel`.

### Format(IMessage, int)

    public string Format(IMessage message, int indentationLevel)

Formats the specified message as JSON.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `message` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` The message to format. |
| `indentationLevel` | `https://learn.microsoft.com/dotnet/api/system.int32` Indentation level to start at. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The formatted message. |

**Remarks** To keep consistent indentation when embedding a message inside another JSON string,
set `indentationLevel`. E.g:

    var response = $@"{{
                                                                                         ""data"": { Format(message, indentationLevel: 1) }
                                                                                       }}"

<br />

### ToDiagnosticString(IMessage)

    public static string ToDiagnosticString(IMessage message)

Converts a message to JSON for diagnostic purposes with no extra context.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `message` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage` The message to format for diagnostic purposes. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The diagnostic-only JSON representation of the message |

**Remarks**
This differs from calling [Format(IMessage)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_) on the default JSON
formatter in its handling of [Any](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Any). As no type registry is available
in [ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring) calls, the normal way of resolving the type of
an `Any` message cannot be applied. Instead, a JSON property named `@value`
is included with the base64 data from the [Value](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Any#Google_Protobuf_WellKnownTypes_Any_Value) property of the message.

The value returned by this method is only designed to be used for diagnostic
purposes. It may not be parsable by [JsonParser](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser), and may not be parsable
by other Protocol Buffer implementations.

### WriteValue(TextWriter, object)

    public void WriteValue(TextWriter writer, object value)

Writes a single value to the given writer as JSON. Only types understood by
Protocol Buffers can be written in this way. This method is only exposed for
advanced use cases; most users should be using [Format(IMessage)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_)
or [Format(IMessage, TextWriter)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_System_IO_TextWriter_).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `writer` | `https://learn.microsoft.com/dotnet/api/system.io.textwriter` The writer to write the value to. Must not be null. |
| `value` | `https://learn.microsoft.com/dotnet/api/system.object` The value to write. May be null. |

**Remarks** Delegates to `WriteValue(TextWriter, object, int)` with `indentationLevel =
0`.

### WriteValue(TextWriter, object, int)

    public void WriteValue(TextWriter writer, object value, int indentationLevel)

Writes a single value to the given writer as JSON. Only types understood by
Protocol Buffers can be written in this way. This method is only exposed for
advanced use cases; most users should be using [Format(IMessage)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_)
or [Format(IMessage, TextWriter)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_System_IO_TextWriter_).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `writer` | `https://learn.microsoft.com/dotnet/api/system.io.textwriter` The writer to write the value to. Must not be null. |
| `value` | `https://learn.microsoft.com/dotnet/api/system.object` The value to write. May be null. |
| `indentationLevel` | `https://learn.microsoft.com/dotnet/api/system.int32` The current indentationLevel. Not used when [Indentation](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter.Settings#Google_Protobuf_JsonFormatter_Settings_Indentation) is null. |