json_util.h

This section contains reference documentation for working with protocol buffer classes in C++.

#include <google/protobuf/util/json_util.h>
namespace google::protobuf::util

Utility functions to convert between protobuf binary format and proto3 JSON format.

Classes in this file

File Members

These definitions are not part of any class.
typedef
JsonPrintOptions JsonOptions
DEPRECATED. Use JsonPrintOptions instead.
util::Status
MessageToJsonString(const Message & message, std::string * output, const JsonOptions & options)
Converts from protobuf message to JSON and appends it to |output|. more...
util::Status
MessageToJsonString(const Message & message, std::string * output)
util::Status
JsonStringToMessage(StringPiece input, Message * message, const JsonParseOptions & options)
Converts from JSON to protobuf message. more...
util::Status
JsonStringToMessage(StringPiece input, Message * message)
util::Status
BinaryToJsonStream(TypeResolver * resolver, const std::string & type_url, io::ZeroCopyInputStream * binary_input, io::ZeroCopyOutputStream * json_output, const JsonPrintOptions & options)
Converts protobuf binary data to JSON. more...
util::Status
BinaryToJsonStream(TypeResolver * resolver, const std::string & type_url, io::ZeroCopyInputStream * binary_input, io::ZeroCopyOutputStream * json_output)
util::Status
BinaryToJsonString(TypeResolver * resolver, const std::string & type_url, const std::string & binary_input, std::string * json_output, const JsonPrintOptions & options)
util::Status
BinaryToJsonString(TypeResolver * resolver, const std::string & type_url, const std::string & binary_input, std::string * json_output)
util::Status
JsonToBinaryStream(TypeResolver * resolver, const std::string & type_url, io::ZeroCopyInputStream * json_input, io::ZeroCopyOutputStream * binary_output, const JsonParseOptions & options)
Converts JSON data to protobuf binary format. more...
util::Status
JsonToBinaryStream(TypeResolver * resolver, const std::string & type_url, io::ZeroCopyInputStream * json_input, io::ZeroCopyOutputStream * binary_output)
util::Status
JsonToBinaryString(TypeResolver * resolver, const std::string & type_url, StringPiece json_input, std::string * binary_output, const JsonParseOptions & options)
util::Status
JsonToBinaryString(TypeResolver * resolver, const std::string & type_url, StringPiece json_input, std::string * binary_output)

util::Status util::MessageToJsonString(
        const Message & message,
        std::string * output,
        const JsonOptions & options)

Converts from protobuf message to JSON and appends it to |output|.

This is a simple wrapper of BinaryToJsonString(). It will use the DescriptorPool of the passed-in message to resolve Any types.


util::Status util::JsonStringToMessage(
        StringPiece input,
        Message * message,
        const JsonParseOptions & options)

Converts from JSON to protobuf message.

This is a simple wrapper of JsonStringToBinary(). It will use the DescriptorPool of the passed-in message to resolve Any types.


util::Status util::BinaryToJsonStream(
        TypeResolver * resolver,
        const std::string & type_url,
        io::ZeroCopyInputStream * binary_input,
        io::ZeroCopyOutputStream * json_output,
        const JsonPrintOptions & options)

Converts protobuf binary data to JSON.

The conversion will fail if:

1. TypeResolver fails to resolve a type.
2. input is not valid protobuf wire format, or conflicts with the type
   information returned by TypeResolver.

Note that unknown fields will be discarded silently.


util::Status util::JsonToBinaryStream(
        TypeResolver * resolver,
        const std::string & type_url,
        io::ZeroCopyInputStream * json_input,
        io::ZeroCopyOutputStream * binary_output,
        const JsonParseOptions & options)

Converts JSON data to protobuf binary format.

The conversion will fail if:

1. TypeResolver fails to resolve a type.
2. input is not valid JSON format, or conflicts with the type
   information returned by TypeResolver.

struct JsonParseOptions

#include <google/protobuf/util/json_util.h>
namespace google::protobuf::util

Members

bool
ignore_unknown_fields
Whether to ignore unknown JSON fields during parsing.
bool
case_insensitive_enum_parsing
If true, when a lowercase enum value fails to parse, try convert it to UPPER_CASE and see if it matches a valid enum. more...
JsonParseOptions()

bool JsonParseOptions::case_insensitive_enum_parsing

If true, when a lowercase enum value fails to parse, try convert it to UPPER_CASE and see if it matches a valid enum.

WARNING: This option exists only to preserve legacy behavior. Avoid using this option. If your enum needs to support different casing, consider using allow_alias instead.

struct JsonPrintOptions

#include <google/protobuf/util/json_util.h>
namespace google::protobuf::util

Members

bool
add_whitespace
Whether to add spaces, line breaks and indentation to make the JSON output easy to read.
bool
always_print_primitive_fields
Whether to always print primitive fields. more...
bool
always_print_enums_as_ints
Whether to always print enums as ints. more...
bool
preserve_proto_field_names
Whether to preserve proto field names.
JsonPrintOptions()

bool JsonPrintOptions::always_print_primitive_fields

Whether to always print primitive fields.

By default proto3 primitive fields with default values will be omitted in JSON output. For example, an int32 field set to 0 will be omitted. Set this flag to true will override the default behavior and print primitive fields regardless of their values.


bool JsonPrintOptions::always_print_enums_as_ints

Whether to always print enums as ints.

By default they are rendered as strings.