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  private:
42  size_t pos_ { 0 }; // current position in raw_str_
43 
44  public:
45  SourceLocation() = default;
46  size_t Pos() const { return pos_; }
47 
49  pos_++;
50  return *this;
51  }
52  SourceLocation& Forward(uint32_t n) {
53  pos_ += n;
54  return *this;
55  }
56  } cursor_;
57 
59 
60  protected:
61  void SkipSpaces();
62 
63  char GetNextChar() {
64  if (cursor_.Pos() == raw_str_.size()) {
65  return -1;
66  }
67  char ch = raw_str_[cursor_.Pos()];
68  cursor_.Forward();
69  return ch;
70  }
71 
72  char PeekNextChar() {
73  if (cursor_.Pos() == raw_str_.size()) {
74  return -1;
75  }
76  char ch = raw_str_[cursor_.Pos()];
77  return ch;
78  }
79 
81  SkipSpaces();
82  return GetNextChar();
83  }
84 
85  char GetChar(char c) {
86  char result = GetNextNonSpaceChar();
87  if (result != c) { Expect(c, result); }
88  return result;
89  }
90 
91  void Error(std::string msg) const;
92 
93  // Report expected character
94  void Expect(char c, char got) {
95  std::string msg = "Expecting: \"";
96  msg += c;
97  msg += "\", got: \"";
98  msg += std::string {got} + " \"";
99  Error(msg);
100  }
101 
102  virtual Json ParseString();
103  virtual Json ParseObject();
104  virtual Json ParseArray();
105  virtual Json ParseNumber();
106  virtual Json ParseBoolean();
107  virtual Json ParseNull();
108 
109  Json Parse();
110 
111  public:
112  explicit JsonReader(StringView str) :
113  raw_str_{str} {}
114 
115  virtual ~JsonReader() = default;
116 
117  Json Load();
118 };
119 
120 class JsonWriter {
121  static constexpr size_t kIndentSize = 2;
122  FixedPrecisionStream convertor_;
123 
124  size_t n_spaces_;
125  std::ostream* stream_;
126  bool pretty_;
127 
128  public:
129  JsonWriter(std::ostream* stream, bool pretty) :
130  n_spaces_{0}, stream_{stream}, pretty_{pretty} {}
131 
132  virtual ~JsonWriter() = default;
133 
134  void NewLine() {
135  if (pretty_) {
136  *stream_ << u8"\n" << std::string(n_spaces_, ' ');
137  }
138  }
139 
140  void BeginIndent() {
141  n_spaces_ += kIndentSize;
142  }
143  void EndIndent() {
144  n_spaces_ -= kIndentSize;
145  }
146 
147  void Write(std::string str) {
148  *stream_ << str;
149  }
150  void Write(StringView str) {
151  stream_->write(str.c_str(), str.size());
152  }
153 
154  void Save(Json json);
155 
156  virtual void Visit(JsonArray const* arr);
157  virtual void Visit(JsonObject const* obj);
158  virtual void Visit(JsonNumber const* num);
159  virtual void Visit(JsonInteger const* num);
160  virtual void Visit(JsonNull const* null);
161  virtual void Visit(JsonString const* str);
162  virtual void Visit(JsonBoolean const* boolean);
163 };
164 } // namespace xgboost
165 
166 #endif // XGBOOST_JSON_IO_H_
char PeekNextChar()
Definition: json_io.h:72
void Expect(char c, char got)
Definition: json_io.h:94
void BeginIndent()
Definition: json_io.h:140
void Write(StringView str)
Definition: json_io.h:150
Describes both true and false.
Definition: json.h:252
Definition: json.h:233
JsonWriter(std::ostream *stream, bool pretty)
Definition: json_io.h:129
Definition: json_io.h:35
StringView raw_str_
Definition: json_io.h:58
char GetNextNonSpaceChar()
Definition: json_io.h:80
Definition: json.h:96
void Write(std::string str)
Definition: json_io.h:147
size_t Pos() const
Definition: json_io.h:46
void NewLine()
Definition: json_io.h:134
size_t size() const
Definition: json.h:297
SourceLocation & Forward()
Definition: json_io.h:48
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:52
Definition: json.h:125
char GetNextChar()
Definition: json_io.h:63
Definition: json.h:70
JsonReader(StringView str)
Definition: json_io.h:112
Definition: json_io.h:120
Definition: json.h:282
Data structure representing JSON format.
Definition: json.h:326
FixedPrecisionStreamContainer()
Definition: json_io.h:23
char const * c_str() const
Definition: json.h:306
Definition: json.h:152
char GetChar(char c)
Definition: json_io.h:85
void EndIndent()
Definition: json_io.h:143