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 #include <xgboost/base.h>
8 
9 #include <vector>
10 #include <memory>
11 #include <string>
12 #include <utility>
13 #include <map>
14 #include <limits>
15 #include <sstream>
16 #include <locale>
17 #include <cinttypes>
18 
19 namespace xgboost {
20 /*
21  * \brief A json reader, currently error checking and utf-8 is not fully supported.
22  */
23 class JsonReader {
24  protected:
25  size_t constexpr static kMaxNumLength =
26  std::numeric_limits<double>::max_digits10 + 1;
27 
28  struct SourceLocation {
29  private:
30  size_t pos_ { 0 }; // current position in raw_str_
31 
32  public:
33  SourceLocation() = default;
34  size_t Pos() const { return pos_; }
35 
36  void Forward() {
37  pos_++;
38  }
39  void Forward(uint32_t n) {
40  pos_ += n;
41  }
42  } cursor_;
43 
45 
46  protected:
47  void SkipSpaces();
48 
49  char GetNextChar() {
50  if (cursor_.Pos() == raw_str_.size()) {
51  return -1;
52  }
53  char ch = raw_str_[cursor_.Pos()];
54  cursor_.Forward();
55  return ch;
56  }
57 
58  char PeekNextChar() {
59  if (cursor_.Pos() == raw_str_.size()) {
60  return -1;
61  }
62  char ch = raw_str_[cursor_.Pos()];
63  return ch;
64  }
65 
66  /* \brief Skip spaces and consume next character. */
68  SkipSpaces();
69  return GetNextChar();
70  }
71  /* \brief Consume next character without first skipping empty space, throw when the next
72  * character is not the expected one.
73  */
74  char GetConsecutiveChar(char expected_char) {
75  char result = GetNextChar();
76  if (XGBOOST_EXPECT(result != expected_char, false)) { Expect(expected_char, result); }
77  return result;
78  }
79 
80  void Error(std::string msg) const;
81 
82  // Report expected character
83  void Expect(char c, char got) {
84  std::string msg = "Expecting: \"";
85  msg += c;
86  msg += "\", got: \"";
87  if (got == -1) {
88  msg += "EOF\"";
89  } else if (got == 0) {
90  msg += "\\0\"";
91  } else {
92  msg += std::to_string(got) + " \"";
93  }
94  Error(msg);
95  }
96 
97  virtual Json ParseString();
98  virtual Json ParseObject();
99  virtual Json ParseArray();
100  virtual Json ParseNumber();
101  virtual Json ParseBoolean();
102  virtual Json ParseNull();
103 
104  Json Parse();
105 
106  public:
107  explicit JsonReader(StringView str) :
108  raw_str_{str} {}
109 
110  virtual ~JsonReader() = default;
111 
112  Json Load();
113 };
114 
115 class JsonWriter {
116  static constexpr size_t kIndentSize = 2;
117 
118  size_t n_spaces_;
119  std::vector<char>* stream_;
120 
121  public:
122  explicit JsonWriter(std::vector<char>* stream) :
123  n_spaces_{0}, stream_{stream} {}
124 
125  virtual ~JsonWriter() = default;
126 
127  void Save(Json json);
128 
129  virtual void Visit(JsonArray const* arr);
130  virtual void Visit(JsonObject const* obj);
131  virtual void Visit(JsonNumber const* num);
132  virtual void Visit(JsonInteger const* num);
133  virtual void Visit(JsonNull const* null);
134  virtual void Visit(JsonString const* str);
135  virtual void Visit(JsonBoolean const* boolean);
136 };
137 } // namespace xgboost
138 
139 #endif // XGBOOST_JSON_IO_H_
char PeekNextChar()
Definition: json_io.h:58
void Expect(char c, char got)
Definition: json_io.h:83
JsonWriter(std::vector< char > *stream)
Definition: json_io.h:122
Describes both true and false.
Definition: json.h:252
Definition: json.h:233
virtual Json ParseString()
Definition: json_io.h:23
StringView raw_str_
Definition: json_io.h:44
char GetNextNonSpaceChar()
Definition: json_io.h:67
Definition: json.h:96
virtual ~JsonReader()=default
size_t Pos() const
Definition: json_io.h:34
size_t size() const
Definition: json.h:297
Definition: json_io.h:28
Definition: json.h:189
namespace of xgboost
Definition: base.h:102
virtual Json ParseNull()
virtual Json ParseObject()
defines configuration macros of xgboost.
virtual Json ParseBoolean()
virtual Json ParseNumber()
Definition: json.h:125
char GetNextChar()
Definition: json_io.h:49
Definition: json.h:70
JsonReader(StringView str)
Definition: json_io.h:107
Definition: json_io.h:115
struct xgboost::JsonReader::SourceLocation cursor_
void Forward()
Definition: json_io.h:36
Definition: json.h:282
Data structure representing JSON format.
Definition: json.h:326
size_t static constexpr kMaxNumLength
Definition: json_io.h:25
#define XGBOOST_EXPECT(cond, ret)
Definition: base.h:75
void Error(std::string msg) const
Definition: json.h:152
void Forward(uint32_t n)
Definition: json_io.h:39
char GetConsecutiveChar(char expected_char)
Definition: json_io.h:74
virtual Json ParseArray()