WPILibC++  unspecified
json_serializer.h
1 /*----------------------------------------------------------------------------*/
2 /* Modifications Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 /*
8  __ _____ _____ _____
9  __| | __| | | | JSON for Modern C++
10 | | |__ | | | | | | version 3.1.2
11 |_____|_____|_____|_|___| https://github.com/nlohmann/json
12 
13 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 Copyright (c) 2013-2018 Niels Lohmann <http://nlohmann.me>.
15 
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
22 
23 The above copyright notice and this permission notice shall be included in all
24 copies or substantial portions of the Software.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 SOFTWARE.
33 */
34 #include "wpi/json.h"
35 
36 #include <clocale> // lconv, localeconv
37 #include <cmath> // labs, isfinite, isnan, signbit, ldexp
38 #include <locale> // locale
39 
40 #include "wpi/raw_ostream.h"
41 
42 namespace wpi {
43 
45 {
46  static constexpr uint8_t UTF8_ACCEPT = 0;
47  static constexpr uint8_t UTF8_REJECT = 1;
48 
49  public:
54  serializer(raw_ostream& s, const char ichar)
55  : o(s), loc(std::localeconv()),
56  thousands_sep(loc->thousands_sep == nullptr ? '\0' : * (loc->thousands_sep)),
57  decimal_point(loc->decimal_point == nullptr ? '\0' : * (loc->decimal_point)),
58  indent_char(ichar), indent_string(512, indent_char)
59  {}
60 
61  // delete because of pointer members
62  serializer(const serializer&) = delete;
63  serializer& operator=(const serializer&) = delete;
64 
82  void dump(const json& val, const bool pretty_print,
83  const bool ensure_ascii,
84  const unsigned int indent_step,
85  const unsigned int current_indent = 0);
86 
87  private:
102  void dump_escaped(StringRef s, const bool ensure_ascii);
103 
113  template<typename NumberType, detail::enable_if_t<
114  std::is_same<NumberType, uint64_t>::value or
115  std::is_same<NumberType, int64_t>::value,
116  int> = 0>
117  void dump_integer(NumberType x)
118  {
119  // special case for "0"
120  if (x == 0)
121  {
122  o << '0';
123  return;
124  }
125 
126  const bool is_negative = (x <= 0) and (x != 0); // see issue #755
127  std::size_t i = 0;
128 
129  while (x != 0)
130  {
131  // spare 1 byte for '\0'
132  assert(i < number_buffer.size() - 1);
133 
134  const auto digit = std::labs(static_cast<long>(x % 10));
135  number_buffer[i++] = static_cast<char>('0' + digit);
136  x /= 10;
137  }
138 
139  if (is_negative)
140  {
141  // make sure there is capacity for the '-'
142  assert(i < number_buffer.size() - 2);
143  number_buffer[i++] = '-';
144  }
145 
146  std::reverse(number_buffer.begin(), number_buffer.begin() + i);
147  o.write(number_buffer.data(), i);
148  }
149 
158  void dump_float(double x);
159 
181  static uint8_t decode(uint8_t& state, uint32_t& codep, const uint8_t byte) noexcept;
182 
183  private:
185  raw_ostream& o;
186 
188  std::array<char, 64> number_buffer{{}};
189 
191  const std::lconv* loc = nullptr;
193  const char thousands_sep = '\0';
195  const char decimal_point = '\0';
196 
198  std::array<char, 512> string_buffer{{}};
199 
201  const char indent_char;
203  std::string indent_string;
204 };
205 
206 } // namespace wpi
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Definition: SmallVector.h:946
namespace to hold default to_json function
Definition: json_binary_writer.cpp:39
Definition: json_serializer.h:44
a class to store JSON values
Definition: json.h:2714
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
serializer(raw_ostream &s, const char ichar)
Definition: json_serializer.h:54
void dump(const json &val, const bool pretty_print, const bool ensure_ascii, const unsigned int indent_step, const unsigned int current_indent=0)
internal implementation of the serialization function
Definition: json_serializer.cpp:1130