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
44 StringView raw_str_;
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. */
67 char GetNextNonSpaceChar() {
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_
xgboost::JsonWriter::Visit
virtual void Visit(JsonArray const *arr)
xgboost::JsonReader::Parse
Json Parse()
xgboost::JsonReader::GetNextChar
char GetNextChar()
Definition: json_io.h:49
xgboost::JsonReader::ParseBoolean
virtual Json ParseBoolean()
XGBOOST_EXPECT
#define XGBOOST_EXPECT(cond, ret)
Definition: base.h:75
xgboost::JsonReader::GetConsecutiveChar
char GetConsecutiveChar(char expected_char)
Definition: json_io.h:74
xgboost::JsonReader::~JsonReader
virtual ~JsonReader()=default
xgboost::JsonReader::ParseNull
virtual Json ParseNull()
xgboost::StringView
Definition: json.h:301
xgboost::JsonWriter::Save
void Save(Json json)
xgboost::JsonReader::Expect
void Expect(char c, char got)
Definition: json_io.h:83
xgboost::JsonReader::kMaxNumLength
constexpr static size_t kMaxNumLength
Definition: json_io.h:25
base.h
defines configuration macros of xgboost.
xgboost::JsonReader::SourceLocation
Definition: json_io.h:28
xgboost::JsonReader::ParseObject
virtual Json ParseObject()
xgboost::JsonReader
Definition: json_io.h:23
xgboost::JsonReader::ParseString
virtual Json ParseString()
xgboost::JsonReader::raw_str_
StringView raw_str_
Definition: json_io.h:44
xgboost::JsonReader::GetNextNonSpaceChar
char GetNextNonSpaceChar()
Definition: json_io.h:67
xgboost::JsonReader::ParseArray
virtual Json ParseArray()
xgboost::JsonReader::Load
Json Load()
xgboost::JsonReader::SkipSpaces
void SkipSpaces()
xgboost::JsonReader::SourceLocation::SourceLocation
SourceLocation()=default
xgboost::JsonReader::cursor_
struct xgboost::JsonReader::SourceLocation cursor_
xgboost::JsonReader::SourceLocation::Pos
size_t Pos() const
Definition: json_io.h:34
xgboost::JsonReader::SourceLocation::Forward
void Forward()
Definition: json_io.h:36
xgboost::JsonWriter::~JsonWriter
virtual ~JsonWriter()=default
json.h
xgboost::JsonWriter::JsonWriter
JsonWriter(std::vector< char > *stream)
Definition: json_io.h:122
xgboost::JsonReader::SourceLocation::Forward
void Forward(uint32_t n)
Definition: json_io.h:39
xgboost::JsonWriter
Definition: json_io.h:115
xgboost::JsonReader::ParseNumber
virtual Json ParseNumber()
xgboost::JsonReader::JsonReader
JsonReader(StringView str)
Definition: json_io.h:107
xgboost::Json
Data structure representing JSON format.
Definition: json.h:355
xgboost::StringView::size
size_t size() const
Definition: json.h:319
xgboost::JsonReader::Error
void Error(std::string msg) const
xgboost::JsonReader::PeekNextChar
char PeekNextChar()
Definition: json_io.h:58
xgboost
namespace of xgboost
Definition: base.h:110