Package google :: Package protobuf :: Module json_format
[frames] | no frames]

Module json_format

source code

Contains routines for printing protocol messages in JSON format.

Simple usage example:

  # Create a proto object and serialize it to a json format string.
  message = my_proto_pb2.MyMessage(foo='bar')
  json_string = json_format.MessageToJson(message)

  # Parse a json format string to proto object.
  message = json_format.Parse(json_string, my_proto_pb2.MyMessage())


Author: jieluo@google.com (Jie Luo)

Classes
  Error
Top-level module error for json_format.
  SerializeToJsonError
Thrown if serialization to JSON fails.
  ParseError
Thrown in case of parsing error.
Functions
 
MessageToJson(message, including_default_value_fields=False)
Converts protobuf message to JSON format.
source code
 
Parse(text, message, ignore_unknown_fields=False)
Parses a JSON representation of a protocol message into a message.
source code
Function Details

MessageToJson(message, including_default_value_fields=False)

source code 
Converts protobuf message to JSON format.

Args:
  message: The protocol buffers message instance to serialize.
  including_default_value_fields: If True, singular primitive fields,
      repeated fields, and map fields will always be serialized.  If
      False, only serialize non-empty fields.  Singular message fields
      and oneof fields are not affected by this option.

Returns:
  A string containing the JSON formatted protocol buffer message.

Parse(text, message, ignore_unknown_fields=False)

source code 
Parses a JSON representation of a protocol message into a message.

Args:
  text: Message JSON representation.
  message: A protocol beffer message to merge into.
  ignore_unknown_fields: If True, do not raise errors for unknown fields.

Returns:
  The same message passed as argument.

Raises::
  ParseError: On JSON parsing problems.