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)
The diagnostic-only JSON representation of the message
Remarks
This differs from calling Format(IMessage) on the default JSON
formatter in its handling of Any. As no type registry is available
in 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 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, 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)
or Format(IMessage, TextWriter).
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)
or Format(IMessage, TextWriter).
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003e\u003ccode\u003eJsonFormatter\u003c/code\u003e is a reflection-based converter for transforming messages into JSON format, suitable for use cases where simplicity is prioritized over performance optimization.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eJsonFormatter\u003c/code\u003e class is thread-safe due to its lack of mutable state, ensuring safe concurrent usage.\u003c/p\u003e\n"],["\u003cp\u003eThe class provides methods like \u003ccode\u003eFormat\u003c/code\u003e for formatting \u003ccode\u003eIMessage\u003c/code\u003e instances into JSON strings and allows control over indentation for nested JSON structures.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eToDiagnosticString\u003c/code\u003e method is specifically designed for creating JSON representations for diagnostic purposes, which may not be fully parsable but provides a clear view of the message content.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eWriteValue\u003c/code\u003e method is exposed for advanced scenarios to write single values as JSON, and is primarily for advanced usage rather than the standard \u003ccode\u003eFormat\u003c/code\u003e method.\u003c/p\u003e\n"]]],[],null,["# Class JsonFormatter (3.27.1)\n\nVersion latestkeyboard_arrow_down\n\n- [3.27.1 (latest)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter)\n- [3.23.0](/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.JsonFormatter)\n- [3.15.8](/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.JsonFormatter) \n\n public sealed class JsonFormatter\n\nReflection-based converter from messages to JSON. \n\nInheritance\n-----------\n\n[object](https://learn.microsoft.com/dotnet/api/system.object) \\\u003e JsonFormatter \n\nInherited Members\n-----------------\n\n[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) \n[object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) \n[object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) \n[object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) \n[object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) \n[object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)\n\nNamespace\n---------\n\n[Google.Protobuf](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf)\n\nAssembly\n--------\n\nGoogle.Protobuf.dll\n\nRemarks\n-------\n\n\nInstances of this class are thread-safe, with no mutable state.\n\n\nThis is a simple start to get JSON formatting working. As it's reflection-based,\nit's not as quick as baking calls into generated messages - but is a simpler implementation.\n(This code is generally not heavily optimized.)\n\nConstructors\n------------\n\n### JsonFormatter(Settings)\n\n public JsonFormatter(JsonFormatter.Settings settings)\n\nCreates a new formatted with the given settings.\n\nProperties\n----------\n\n### Default\n\n public static JsonFormatter Default { get; }\n\nReturns a formatter using the default settings.\n\nMethods\n-------\n\n### Format(IMessage)\n\n public string Format(IMessage message)\n\nFormats the specified message as JSON.\n\n**Remarks** \nThis method delegates to `Format(IMessage, int)` with `indentationLevel =\n0`.\n\n### Format(IMessage, TextWriter)\n\n public void Format(IMessage message, TextWriter writer)\n\nFormats the specified message as JSON.\n\n**Remarks** \nThis method delegates to `Format(IMessage, TextWriter, int)` with\n`indentationLevel = 0`.\n\n### Format(IMessage, TextWriter, int)\n\n public void Format(IMessage message, TextWriter writer, int indentationLevel)\n\nFormats the specified message as JSON. When [Indentation](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter.Settings#Google_Protobuf_JsonFormatter_Settings_Indentation) is not null,\nstart indenting at the specified `indentationLevel`.\n\n**Remarks** \nTo keep consistent indentation when embedding a message inside another JSON string,\nset `indentationLevel`.\n\n### Format(IMessage, int)\n\n public string Format(IMessage message, int indentationLevel)\n\nFormats the specified message as JSON.\n\n**Remarks** \nTo keep consistent indentation when embedding a message inside another JSON string,\nset `indentationLevel`. E.g: \n\n var response = $@\"{{\n \"\"data\"\": { Format(message, indentationLevel: 1) }\n }}\"\n\n\u003cbr /\u003e\n\n### ToDiagnosticString(IMessage)\n\n public static string ToDiagnosticString(IMessage message)\n\nConverts a message to JSON for diagnostic purposes with no extra context.\n\n**Remarks** \n\nThis differs from calling [Format(IMessage)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_) on the default JSON\nformatter in its handling of [Any](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Any). As no type registry is available\nin [ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring) calls, the normal way of resolving the type of\nan `Any` message cannot be applied. Instead, a JSON property named `@value`\nis included with the base64 data from the [Value](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Any#Google_Protobuf_WellKnownTypes_Any_Value) property of the message.\n\nThe value returned by this method is only designed to be used for diagnostic\npurposes. It may not be parsable by [JsonParser](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonParser), and may not be parsable\nby other Protocol Buffer implementations.\n\n### WriteValue(TextWriter, object)\n\n public void WriteValue(TextWriter writer, object value)\n\nWrites a single value to the given writer as JSON. Only types understood by\nProtocol Buffers can be written in this way. This method is only exposed for\nadvanced use cases; most users should be using [Format(IMessage)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_)\nor [Format(IMessage, TextWriter)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_System_IO_TextWriter_).\n\n**Remarks** \nDelegates to `WriteValue(TextWriter, object, int)` with `indentationLevel =\n0`.\n\n### WriteValue(TextWriter, object, int)\n\n public void WriteValue(TextWriter writer, object value, int indentationLevel)\n\nWrites a single value to the given writer as JSON. Only types understood by\nProtocol Buffers can be written in this way. This method is only exposed for\nadvanced use cases; most users should be using [Format(IMessage)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_)\nor [Format(IMessage, TextWriter)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_Format_Google_Protobuf_IMessage_System_IO_TextWriter_)."]]