json.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2011-present 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. * Serialize and deserialize folly::dynamic values as JSON.
  19. *
  20. * Before you use this you should probably understand the basic
  21. * concepts in the JSON type system:
  22. *
  23. * Value : String | Bool | Null | Object | Array | Number
  24. * String : UTF-8 sequence
  25. * Object : (String, Value) pairs, with unique String keys
  26. * Array : ordered list of Values
  27. * Null : null
  28. * Bool : true | false
  29. * Number : (representation unspecified)
  30. *
  31. * ... That's about it. For more information see http://json.org or
  32. * look up RFC 4627.
  33. *
  34. * If your dynamic has anything illegal with regard to this type
  35. * system, the serializer will throw.
  36. *
  37. * @author Jordan DeLong <delong.j@fb.com>
  38. */
  39. #pragma once
  40. #include <iosfwd>
  41. #include <string>
  42. #include <folly/Function.h>
  43. #include <folly/Range.h>
  44. #include <folly/dynamic.h>
  45. namespace folly {
  46. //////////////////////////////////////////////////////////////////////
  47. namespace json {
  48. struct serialization_opts {
  49. explicit serialization_opts()
  50. : allow_non_string_keys(false),
  51. javascript_safe(false),
  52. pretty_formatting(false),
  53. encode_non_ascii(false),
  54. validate_utf8(false),
  55. allow_trailing_comma(false),
  56. sort_keys(false),
  57. skip_invalid_utf8(false),
  58. allow_nan_inf(false),
  59. double_mode(double_conversion::DoubleToStringConverter::SHORTEST),
  60. double_num_digits(0), // ignored when mode is SHORTEST
  61. double_fallback(false),
  62. parse_numbers_as_strings(false),
  63. recursion_limit(100),
  64. extra_ascii_to_escape_bitmap{{0, 0}} {}
  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
  67. // operator<<.
  68. bool allow_non_string_keys;
  69. /*
  70. * If true, refuse to serialize 64-bit numbers that cannot be
  71. * precisely represented by fit a double---instead, throws an
  72. * exception if the document contains this.
  73. */
  74. bool javascript_safe;
  75. // If true, the serialized json will contain space and newlines to
  76. // try to be minimally "pretty".
  77. bool pretty_formatting;
  78. // If true, non-ASCII utf8 characters would be encoded as \uXXXX:
  79. // - if the code point is in [U+0000..U+FFFF] => encode as a single \uXXXX
  80. // - if the code point is > U+FFFF => encode as 2 UTF-16 surrogate pairs.
  81. bool encode_non_ascii;
  82. // Check that strings are valid utf8
  83. bool validate_utf8;
  84. // Allow trailing comma in lists of values / items
  85. bool allow_trailing_comma;
  86. // Sort keys of all objects before printing out (potentially slow)
  87. // using dynamic::operator<.
  88. // Has no effect if sort_keys_by is set.
  89. bool sort_keys;
  90. // Sort keys of all objects before printing out (potentially slow)
  91. // using the provided less functor.
  92. Function<bool(dynamic const&, dynamic const&) const> sort_keys_by;
  93. // Replace invalid utf8 characters with U+FFFD and continue
  94. bool skip_invalid_utf8;
  95. // true to allow NaN or INF values
  96. bool allow_nan_inf;
  97. // Options for how to print floating point values. See Conv.h
  98. // toAppend implementation for floating point for more info
  99. double_conversion::DoubleToStringConverter::DtoaMode double_mode;
  100. unsigned int double_num_digits;
  101. // Fallback to double when a value that looks like integer is too big to
  102. // fit in an int64_t. Can result in loss a of precision.
  103. bool double_fallback;
  104. // Do not parse numbers. Instead, store them as strings and leave the
  105. // conversion up to the user.
  106. bool parse_numbers_as_strings;
  107. // Recursion limit when parsing.
  108. unsigned int recursion_limit;
  109. // Bitmap representing ASCII characters to escape with unicode
  110. // representations. The least significant bit of the first in the pair is
  111. // ASCII value 0; the most significant bit of the second in the pair is ASCII
  112. // value 127. Some specific characters in this range are always escaped
  113. // regardless of the bitmask - namely characters less than 0x20, \, and ".
  114. std::array<uint64_t, 2> extra_ascii_to_escape_bitmap;
  115. };
  116. /*
  117. * Generates a bitmap with bits set for each of the ASCII characters provided
  118. * for use in the serialization_opts extra_ascii_to_escape_bitmap option. If any
  119. * characters are not valid ASCII, they are ignored.
  120. */
  121. std::array<uint64_t, 2> buildExtraAsciiToEscapeBitmap(StringPiece chars);
  122. /*
  123. * Main JSON serialization routine taking folly::dynamic parameters.
  124. * For the most common use cases there are simpler functions in the
  125. * main folly namespace below.
  126. */
  127. std::string serialize(dynamic const&, serialization_opts const&);
  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. void escapeString(
  133. StringPiece input,
  134. std::string& out,
  135. const serialization_opts& opts);
  136. /*
  137. * Strip all C99-like comments (i.e. // and / * ... * /)
  138. */
  139. std::string stripComments(StringPiece jsonC);
  140. } // namespace json
  141. //////////////////////////////////////////////////////////////////////
  142. /*
  143. * Parse a json blob out of a range and produce a dynamic representing
  144. * it.
  145. */
  146. dynamic parseJson(StringPiece, json::serialization_opts const&);
  147. dynamic parseJson(StringPiece);
  148. /*
  149. * Serialize a dynamic into a json string.
  150. */
  151. std::string toJson(dynamic const&);
  152. /*
  153. * Same as the above, except format the json with some minimal
  154. * indentation.
  155. */
  156. std::string toPrettyJson(dynamic const&);
  157. /*
  158. * Printer for GTest.
  159. * Uppercase name to fill GTest's API, which calls this method through ADL.
  160. */
  161. void PrintTo(const dynamic&, std::ostream*);
  162. //////////////////////////////////////////////////////////////////////
  163. } // namespace folly