folly: add bser encode/decode for dynamic
[folly.git] / folly / json.h
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  *
19  * Serialize and deserialize folly::dynamic values as JSON.
20  *
21  * Before you use this you should probably understand the basic
22  * concepts in the JSON type system:
23  *
24  *    Value  : String | Bool | Null | Object | Array | Number
25  *    String : UTF-8 sequence
26  *    Object : (String, Value) pairs, with unique String keys
27  *    Array  : ordered list of Values
28  *    Null   : null
29  *    Bool   : true | false
30  *    Number : (representation unspecified)
31  *
32  * ... That's about it.  For more information see http://json.org or
33  * look up RFC 4627.
34  *
35  * If your dynamic has anything illegal with regard to this type
36  * system, the serializer will throw.
37  *
38  * @author Jordan DeLong <delong.j@fb.com>
39  */
40
41 #ifndef FOLLY_JSON_H_
42 #define FOLLY_JSON_H_
43
44 #include <iosfwd>
45
46 #include <folly/dynamic.h>
47 #include <folly/FBString.h>
48 #include <folly/Range.h>
49
50 namespace folly {
51
52 //////////////////////////////////////////////////////////////////////
53
54 namespace json {
55
56   struct serialization_opts {
57     explicit serialization_opts()
58       : allow_non_string_keys(false)
59       , javascript_safe(false)
60       , pretty_formatting(false)
61       , encode_non_ascii(false)
62       , validate_utf8(false)
63       , allow_trailing_comma(false)
64       , sort_keys(false)
65       , skip_invalid_utf8(false)
66       , allow_nan_inf(false)
67       , double_mode(double_conversion::DoubleToStringConverter::SHORTEST)
68       , double_num_digits(0) // ignored when mode is SHORTEST
69       , double_fallback(false)
70       , parse_numbers_as_strings(false)
71     {}
72
73     // If true, keys in an object can be non-strings.  (In strict
74     // JSON, object keys must be strings.)  This is used by dynamic's
75     // operator<<.
76     bool allow_non_string_keys;
77
78     /*
79      * If true, refuse to serialize 64-bit numbers that cannot be
80      * precisely represented by fit a double---instead, throws an
81      * exception if the document contains this.
82      */
83     bool javascript_safe;
84
85     // If true, the serialized json will contain space and newlines to
86     // try to be minimally "pretty".
87     bool pretty_formatting;
88
89     // If true, non-ASCII utf8 characters would be encoded as \uXXXX.
90     bool encode_non_ascii;
91
92     // Check that strings are valid utf8
93     bool validate_utf8;
94
95     // Allow trailing comma in lists of values / items
96     bool allow_trailing_comma;
97
98     // Sort keys of all objects before printing out (potentially slow)
99     bool sort_keys;
100
101     // Replace invalid utf8 characters with U+FFFD and continue
102     bool skip_invalid_utf8;
103
104     // true to allow NaN or INF values
105     bool allow_nan_inf;
106
107     // Options for how to print floating point values.  See Conv.h
108     // toAppend implementation for floating point for more info
109     double_conversion::DoubleToStringConverter::DtoaMode double_mode;
110     unsigned int double_num_digits;
111
112     // Fallback to double when a value that looks like integer is too big to
113     // fit in an int64_t. Can result in loss a of precision.
114     bool double_fallback;
115
116     // Do not parse numbers. Instead, store them as strings and leave the
117     // conversion up to the user.
118     bool parse_numbers_as_strings;
119   };
120
121   /*
122    * Main JSON serialization routine taking folly::dynamic parameters.
123    * For the most common use cases there are simpler functions in the
124    * main folly namespace below.
125    */
126   fbstring serialize(dynamic const&, serialization_opts const&);
127
128   /*
129    * Escape a string so that it is legal to print it in JSON text and
130    * append the result to out.
131    */
132
133   void escapeString(StringPiece input,
134                     fbstring& out,
135                     const serialization_opts& opts);
136
137   /*
138    * Strip all C99-like comments (i.e. // and / * ... * /)
139    */
140   fbstring stripComments(StringPiece jsonC);
141 }
142
143 //////////////////////////////////////////////////////////////////////
144
145 /*
146  * Parse a json blob out of a range and produce a dynamic representing
147  * it.
148  */
149 dynamic parseJson(StringPiece, json::serialization_opts const&);
150 dynamic parseJson(StringPiece);
151
152 /*
153  * Serialize a dynamic into a json string.
154  */
155 fbstring toJson(dynamic const&);
156
157 /*
158  * Same as the above, except format the json with some minimal
159  * indentation.
160  */
161 fbstring toPrettyJson(dynamic const&);
162
163 /*
164  * Printer for GTest.
165  * Uppercase name to fill GTest's API, which calls this method through ADL.
166  */
167 void PrintTo(const dynamic&, std::ostream*);
168 //////////////////////////////////////////////////////////////////////
169
170 }
171
172 #endif