xgboost
json_io.h
Go to the documentation of this file.
1 
4 #ifndef XGBOOST_JSON_IO_H_
5 #define XGBOOST_JSON_IO_H_
6 #include <xgboost/json.h>
7 
8 #include <memory>
9 #include <string>
10 #include <cinttypes>
11 #include <utility>
12 #include <map>
13 #include <limits>
14 #include <sstream>
15 #include <locale>
16 
17 namespace xgboost {
18 
19 template <typename Allocator>
20 class FixedPrecisionStreamContainer : public std::basic_stringstream<
21  char, std::char_traits<char>, Allocator> {
22  public:
24  this->precision(std::numeric_limits<double>::max_digits10);
25  this->imbue(std::locale("C"));
26  this->setf(std::ios::scientific);
27  }
28 };
29 
31 
32 /*
33  * \brief A json reader, currently error checking and utf-8 is not fully supported.
34  */
35 class JsonReader {
36  protected:
37  size_t constexpr static kMaxNumLength =
38  std::numeric_limits<double>::max_digits10 + 1;
39 
40  struct SourceLocation {
41  size_t pos_; // current position in raw_str_
42 
43  public:
44  SourceLocation() : pos_(0) {}
45  size_t Pos() const { return pos_; }
46 
48  pos_++;
49  return *this;
50  }
51  SourceLocation& Forward(uint32_t n) {
52  pos_ += n;
53  return *this;
54  }
55  } cursor_;
56 
58 
59  protected:
60  void SkipSpaces();
61 
62  char GetNextChar() {
63  if (cursor_.Pos() == raw_str_.size()) {
64  return -1;
65  }
66  char ch = raw_str_[cursor_.Pos()];
67  cursor_.Forward();
68  return ch;
69  }
70 
71  char PeekNextChar() {
72  if (cursor_.Pos() == raw_str_.size()) {
73  return -1;
74  }
75  char ch = raw_str_[cursor_.Pos()];
76  return ch;
77  }
78 
80  SkipSpaces();
81  return GetNextChar();
82  }
83 
84  char GetChar(char c) {
85  char result = GetNextNonSpaceChar();
86  if (result != c) { Expect(c, result); }
87  return result;
88  }
89 
90  void Error(std::string msg) const;
91 
92  // Report expected character
93  void Expect(char c, char got) {
94  std::string msg = "Expecting: \"";
95  msg += c;
96  msg += "\", got: \"";
97  msg += std::string {got} + " \"";
98  Error(msg);
99  }
100 
101  virtual Json ParseString();
102  virtual Json ParseObject();
103  virtual Json ParseArray();
104  virtual Json ParseNumber();
105  virtual Json ParseBoolean();
106  virtual Json ParseNull();
107 
108  Json Parse();
109 
110  public:
111  explicit JsonReader(StringView str) :
112  raw_str_{str} {}
113 
114  virtual ~JsonReader() = default;
115 
116  Json Load();
117 };
118 
119 class JsonWriter {
120  static constexpr size_t kIndentSize = 2;
121  FixedPrecisionStream convertor_;
122 
123  size_t n_spaces_;
124  std::ostream* stream_;
125  bool pretty_;
126 
127  public:
128  JsonWriter(std::ostream* stream, bool pretty) :
129  n_spaces_{0}, stream_{stream}, pretty_{pretty} {}
130 
131  virtual ~JsonWriter() = default;
132 
133  void NewLine() {
134  if (pretty_) {
135  *stream_ << u8"\n" << std::string(n_spaces_, ' ');
136  }
137  }
138 
139  void BeginIndent() {
140  n_spaces_ += kIndentSize;
141  }
142  void EndIndent() {
143  n_spaces_ -= kIndentSize;
144  }
145 
146  void Write(std::string str) {
147  *stream_ << str;
148  }
149  void Write(StringView str) {
150  stream_->write(str.c_str(), str.size());
151  }
152 
153  void Save(Json json);
154 
155  virtual void Visit(JsonArray const* arr);
156  virtual void Visit(JsonObject const* obj);
157  virtual void Visit(JsonNumber const* num);
158  virtual void Visit(JsonInteger const* num);
159  virtual void Visit(JsonNull const* null);
160  virtual void Visit(JsonString const* str);
161  virtual void Visit(JsonBoolean const* boolean);
162 };
163 } // namespace xgboost
164 
165 #endif // XGBOOST_JSON_IO_H_
char PeekNextChar()
Definition: json_io.h:71
size_t pos_
Definition: json_io.h:41
void Expect(char c, char got)
Definition: json_io.h:93
void BeginIndent()
Definition: json_io.h:139
void Write(StringView str)
Definition: json_io.h:149
Describes both true and false.
Definition: json.h:252
Definition: json.h:233
JsonWriter(std::ostream *stream, bool pretty)
Definition: json_io.h:128
Definition: json_io.h:35
StringView raw_str_
Definition: json_io.h:57
char GetNextNonSpaceChar()
Definition: json_io.h:79
Definition: json.h:96
void Write(std::string str)
Definition: json_io.h:146
size_t Pos() const
Definition: json_io.h:45
void NewLine()
Definition: json_io.h:133
size_t size() const
Definition: json.h:296
SourceLocation & Forward()
Definition: json_io.h:47
Definition: json_io.h:40
Definition: json.h:189
namespace of xgboost
Definition: base.h:102
SourceLocation & Forward(uint32_t n)
Definition: json_io.h:51
Definition: json.h:125
SourceLocation()
Definition: json_io.h:44
char GetNextChar()
Definition: json_io.h:62
Definition: json.h:70
JsonReader(StringView str)
Definition: json_io.h:111
Definition: json_io.h:119
Definition: json.h:282
Data structure representing JSON format.
Definition: json.h:325
FixedPrecisionStreamContainer()
Definition: json_io.h:23
char const * c_str() const
Definition: json.h:305
Definition: json.h:152
char GetChar(char c)
Definition: json_io.h:84
void EndIndent()
Definition: json_io.h:142