Update SSLContext to use folly::Random and discrete_distribution
[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 <folly/dynamic.h>
45 #include <folly/FBString.h>
46 #include <folly/Range.h>
47
48 namespace folly {
49
50 //////////////////////////////////////////////////////////////////////
51
52 namespace json {
53
54   struct serialization_opts {
55     explicit serialization_opts()
56       : allow_non_string_keys(false)
57       , javascript_safe(false)
58       , pretty_formatting(false)
59       , encode_non_ascii(false)
60       , validate_utf8(false)
61       , allow_trailing_comma(false)
62       , sort_keys(false)
63       , skip_invalid_utf8(false)
64       , allow_nan_inf(false)
65       , double_mode(double_conversion::DoubleToStringConverter::SHORTEST)
66       , double_num_digits(0) // ignored when mode is SHORTEST
67       , double_fallback(false)
68       , parse_numbers_as_strings(false)
69     {}
70
71     // If true, keys in an object can be non-strings.  (In strict
72     // JSON, object keys must be strings.)  This is used by dynamic's
73     // operator<<.
74     bool allow_non_string_keys;
75
76     /*
77      * If true, refuse to serialize 64-bit numbers that cannot be
78      * precisely represented by fit a double---instead, throws an
79      * exception if the document contains this.
80      */
81     bool javascript_safe;
82
83     // If true, the serialized json will contain space and newlines to
84     // try to be minimally "pretty".
85     bool pretty_formatting;
86
87     // If true, non-ASCII utf8 characters would be encoded as \uXXXX.
88     bool encode_non_ascii;
89
90     // Check that strings are valid utf8
91     bool validate_utf8;
92
93     // Allow trailing comma in lists of values / items
94     bool allow_trailing_comma;
95
96     // Sort keys of all objects before printing out (potentially slow)
97     bool sort_keys;
98
99     // Replace invalid utf8 characters with U+FFFD and continue
100     bool skip_invalid_utf8;
101
102     // true to allow NaN or INF values
103     bool allow_nan_inf;
104
105     // Options for how to print floating point values.  See Conv.h
106     // toAppend implementation for floating point for more info
107     double_conversion::DoubleToStringConverter::DtoaMode double_mode;
108     unsigned int double_num_digits;
109
110     // Fallback to double when a value that looks like integer is too big to
111     // fit in an int64_t. Can result in loss a of precision.
112     bool double_fallback;
113
114     // Do not parse numbers. Instead, store them as strings and leave the
115     // conversion up to the user.
116     bool parse_numbers_as_strings;
117   };
118
119   /*
120    * Main JSON serialization routine taking folly::dynamic parameters.
121    * For the most common use cases there are simpler functions in the
122    * main folly namespace below.
123    */
124   fbstring serialize(dynamic const&, serialization_opts const&);
125
126   /*
127    * Escape a string so that it is legal to print it in JSON text and
128    * append the result to out.
129    */
130
131   void escapeString(StringPiece input,
132                     fbstring& out,
133                     const serialization_opts& opts);
134
135   /*
136    * Strip all C99-like comments (i.e. // and / * ... * /)
137    */
138   fbstring stripComments(StringPiece jsonC);
139 }
140
141 //////////////////////////////////////////////////////////////////////
142
143 /*
144  * Parse a json blob out of a range and produce a dynamic representing
145  * it.
146  */
147 dynamic parseJson(StringPiece, json::serialization_opts const&);
148 dynamic parseJson(StringPiece);
149
150 /*
151  * Serialize a dynamic into a json string.
152  */
153 fbstring toJson(dynamic const&);
154
155 /*
156  * Same as the above, except format the json with some minimal
157  * indentation.
158  */
159 fbstring toPrettyJson(dynamic const&);
160
161 //////////////////////////////////////////////////////////////////////
162
163 }
164
165 #endif