2 * Copyright 2013 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * Serialize and deserialize folly::dynamic values as JSON.
21 * Before you use this you should probably understand the basic
22 * concepts in the JSON type system:
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
30 * Number : (representation unspecified)
32 * ... That's about it. For more information see http://json.org or
35 * If your dynamic has anything illegal with regard to this type
36 * system, the serializer will throw.
38 * @author Jordan DeLong <delong.j@fb.com>
44 #include "folly/dynamic.h"
45 #include "folly/FBString.h"
46 #include "folly/Range.h"
50 //////////////////////////////////////////////////////////////////////
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)
65 // If true, keys in an object can be non-strings. (In strict
66 // JSON, object keys must be strings.) This is used by dynamic's
68 bool allow_non_string_keys;
71 * If true, refuse to serialize 64-bit numbers that cannot be
72 * precisely represented by fit a double---instead, throws an
73 * exception if the document contains this.
77 // If true, the serialized json will contain space and newlines to
78 // try to be minimally "pretty".
79 bool pretty_formatting;
81 // If true, non-ASCII utf8 characters would be encoded as \uXXXX.
82 bool encode_non_ascii;
84 // Check that strings are valid utf8
87 // Allow trailing comma in lists of values / items
88 bool allow_trailing_comma;
90 // Sort keys of all objects before printing out (potentially slow)
95 * Main JSON serialization routine taking folly::dynamic parameters.
96 * For the most common use cases there are simpler functions in the
97 * main folly namespace below.
99 fbstring serialize(dynamic const&, serialization_opts const&);
102 * Escape a string so that it is legal to print it in JSON text and
103 * append the result to out.
106 void escapeString(StringPiece input,
108 const serialization_opts& opts);
111 //////////////////////////////////////////////////////////////////////
114 * Parse a json blob out of a range and produce a dynamic representing
117 dynamic parseJson(StringPiece, json::serialization_opts const&);
118 dynamic parseJson(StringPiece);
121 * Serialize a dynamic into a json string.
123 fbstring toJson(dynamic const&);
126 * Same as the above, except format the json with some minimal
129 fbstring toPrettyJson(dynamic const&);
131 //////////////////////////////////////////////////////////////////////