webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
json_util.h
Go to the documentation of this file.
1 // 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 
38 
39 namespace google {
40 namespace protobuf {
41 namespace io {
42 class ZeroCopyInputStream;
43 class ZeroCopyOutputStream;
44 } // namespace io
45 namespace util {
46 
47 struct JsonOptions {
48  // Whether to add spaces, line breaks and indentation to make the JSON output
49  // easy to read.
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.
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.
69  TypeResolver* resolver,
70  const string& type_url,
71  io::ZeroCopyInputStream* binary_input,
72  io::ZeroCopyOutputStream* json_output,
73  const JsonOptions& options);
74 
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 
84  TypeResolver* resolver,
85  const string& type_url,
86  const string& binary_input,
87  string* json_output,
88  const JsonOptions& options);
89 
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.
105  TypeResolver* resolver,
106  const string& type_url,
107  io::ZeroCopyInputStream* json_input,
108  io::ZeroCopyOutputStream* binary_output);
109 
111  TypeResolver* resolver,
112  const string& type_url,
113  const string& json_input,
114  string* binary_output);
115 
116 namespace internal {
117 // Internal helper class. Put in the header so we can write unit-tests for it.
118 class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
119  public:
121  : stream_(stream) {}
122 
123  virtual void Append(const char* bytes, size_t len);
124 
125  private:
126  io::ZeroCopyOutputStream* stream_;
127 
129 };
130 } // namespace internal
131 
132 } // namespace util
133 } // namespace protobuf
134 
135 } // namespace google
136 #endif // GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
util::Status JsonToBinaryString(TypeResolver *resolver, const string &type_url, const string &json_input, string *binary_output)
Definition: json_util.cc:165
Definition: util.py:1
EGLStreamKHR stream
Definition: eglext.h:340
ZeroCopyStreamByteSink(io::ZeroCopyOutputStream *stream)
Definition: json_util.h:120
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
string * type_url
Definition: conformance_cpp.cc:61
options
Definition: CodeGeneratorReplayInputs.py:1046
OPENSSL_EXPORT const ASN1_OBJECT int const unsigned char * bytes
Definition: x509.h:1053
util::Status BinaryToJsonString(TypeResolver *resolver, const string &type_url, const string &binary_input, string *json_output)
Definition: json_util.h:90
bool add_whitespace
Definition: json_util.h:50
Definition: json_util.h:47
Definition: type_resolver.h:54
util::Status JsonToBinaryStream(TypeResolver *resolver, const string &type_url, io::ZeroCopyInputStream *json_input, io::ZeroCopyOutputStream *binary_output)
Definition: json_util.cc:141
OPENSSL_EXPORT const ASN1_OBJECT int const unsigned char int len
Definition: x509.h:1053
Definition: zero_copy_stream.h:181
Definition: __init__.py:1
Definition: document.h:393
#define false
Definition: float-mm.c:5
Definition: zero_copy_stream.h:124
#define LIBPROTOBUF_EXPORT
Definition: port.h:97
Definition: gflags_completions.h:115
util::Status BinaryToJsonStream(TypeResolver *resolver, const string &type_url, io::ZeroCopyInputStream *binary_input, io::ZeroCopyOutputStream *json_output)
Definition: json_util.h:75
JsonOptions()
Definition: json_util.h:57
bool always_print_primitive_fields
Definition: json_util.h:55
Definition: status.h:69