blob: 1718bfb528119fad916b0d934190ff7f3a89ef1a [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// Utility functions to convert between protobuf binary format and proto3 JSON
32// format.
33#ifndef GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
34#define GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
35
36#include <google/protobuf/util/type_resolver.h>
37#include <google/protobuf/stubs/bytestream.h>
38
39namespace google {
40namespace protobuf {
41namespace io {
42class ZeroCopyInputStream;
43class ZeroCopyOutputStream;
44} // namespace io
45namespace util {
46
47struct JsonOptions {
48 // Whether to add spaces, line breaks and indentation to make the JSON output
49 // easy to read.
50 bool add_whitespace;
51 // Whether to always print primitive fields. By default primitive fields with
52 // default values will be omitted in JSON joutput. For example, an int32 field
53 // set to 0 will be omitted. Set this flag to true will override the default
54 // behavior and print primitive fields regardless of their values.
55 bool always_print_primitive_fields;
56
57 JsonOptions() : add_whitespace(false),
58 always_print_primitive_fields(false) {
59 }
60};
61
62// Converts protobuf binary data to JSON.
63// The conversion will fail if:
64// 1. TypeResolver fails to resolve a type.
65// 2. input is not valid protobuf wire format, or conflicts with the type
66// information returned by TypeResolver.
67// Note that unknown fields will be discarded silently.
68util::Status BinaryToJsonStream(
69 TypeResolver* resolver,
70 const string& type_url,
71 io::ZeroCopyInputStream* binary_input,
72 io::ZeroCopyOutputStream* json_output,
73 const JsonOptions& options);
74
75inline util::Status BinaryToJsonStream(
76 TypeResolver* resolver, const string& type_url,
77 io::ZeroCopyInputStream* binary_input,
78 io::ZeroCopyOutputStream* json_output) {
79 return BinaryToJsonStream(resolver, type_url, binary_input, json_output,
80 JsonOptions());
81}
82
83LIBPROTOBUF_EXPORT util::Status BinaryToJsonString(
84 TypeResolver* resolver,
85 const string& type_url,
86 const string& binary_input,
87 string* json_output,
88 const JsonOptions& options);
89
90inline util::Status BinaryToJsonString(TypeResolver* resolver,
91 const string& type_url,
92 const string& binary_input,
93 string* json_output) {
94 return BinaryToJsonString(resolver, type_url, binary_input, json_output,
95 JsonOptions());
96}
97
98// Converts JSON data to protobuf binary format.
99// The conversion will fail if:
100// 1. TypeResolver fails to resolve a type.
101// 2. input is not valid JSON format, or conflicts with the type
102// information returned by TypeResolver.
103// 3. input has unknown fields.
104util::Status JsonToBinaryStream(
105 TypeResolver* resolver,
106 const string& type_url,
107 io::ZeroCopyInputStream* json_input,
108 io::ZeroCopyOutputStream* binary_output);
109
110LIBPROTOBUF_EXPORT util::Status JsonToBinaryString(
111 TypeResolver* resolver,
112 const string& type_url,
113 const string& json_input,
114 string* binary_output);
115
116namespace internal {
117// Internal helper class. Put in the header so we can write unit-tests for it.
118class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
119 public:
120 explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream)
121 : stream_(stream) {}
122
123 virtual void Append(const char* bytes, size_t len);
124
125 private:
126 io::ZeroCopyOutputStream* stream_;
127
128 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink);
129};
130} // namespace internal
131
132} // namespace util
133} // namespace protobuf
134
135} // namespace google
136#endif // GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__