-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathjson_data.h
115 lines (99 loc) · 3.69 KB
/
json_data.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (C) 2021 Duowan Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __X_PACK_JSON_DATA_H
#define __X_PACK_JSON_DATA_H
#include "rapidjson_custom.h"
#include "rapidjson/document.h"
#include "util.h"
#include "traits.h"
namespace xpack {
// wrapper for rapidjson::Value.
// If we use other json parser someday, users won't have to modify the code.
// Most of the code is copied from rapidjson.
enum JsonType {
kNullType = 0, //!< null
kFalseType = 1, //!< false
kTrueType = 2, //!< true
kObjectType = 3, //!< object
kArrayType = 4, //!< array
kStringType = 5, //!< string
kNumberType = 6 //!< number
};
class JsonData {
friend class JsonNode;
friend class JsonWriter;
public:
JsonData():current(NULL){}
// check is valid JsonData
operator bool() const {
return NULL != current;
}
JsonType Type() const {
if (NULL != current) {
return static_cast<JsonType>(current->GetType());
}
return kNullType; // ?
}
bool IsNull() const { return NULL==current||current->IsNull(); }
bool IsBool() const { return NULL!=current && current->IsBool(); }
bool IsObject() const { return NULL!=current && current->IsObject(); }
bool IsArray() const { return NULL!=current && current->IsArray(); }
bool IsNumber() const { return NULL!=current && current->IsNumber(); }
bool IsDouble() const { return NULL!=current && current->IsDouble(); }
bool IsString() const { return NULL!=current && current->IsString(); }
size_t Size() const {return (size_t)current->Size();}
JsonData operator [](const char *key) const {
if (NULL != current && current->IsObject()) {
rapidjson::Value::ConstMemberIterator iter = current->FindMember(key);
if (iter != current->MemberEnd()) {
return JsonData(&iter->value);
} // no exception ?
}
return JsonData();
}
JsonData operator [](size_t index) const {
if (NULL != current && current->IsArray() && index < (size_t)current->Size()) {
return JsonData(&(*current)[(rapidjson::SizeType)index]);
}
return JsonData();
}
std::string String() const; // Implementation in json_encoder.h
template <typename T> // For base byte(integer/float/double/bool/std::string), implementation in json_decoder.h
T Get() const;
template <typename T> // struct, implementation in json_decoder.h
bool Get(T &val) const;
private:
JsonData(const rapidjson::Value*v):current(v){}
void reset(const rapidjson::Value *v) {
node.reset();
allocator.reset();
current = NULL;
if (NULL != v) {
if (v->IsNull()) {
node.reset(new rapidjson::Value());
} else {
allocator.reset(new rapidjson::MemoryPoolAllocator<>());
node.reset(new rapidjson::Value(*v, *allocator, true));
}
current = node.get();
}
}
x_shared_ptr<rapidjson::MemoryPoolAllocator<> > allocator;
x_shared_ptr<rapidjson::Value> node; // release node first
const rapidjson::Value* current;
};
}
#endif