-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonValue.h
87 lines (71 loc) · 2.21 KB
/
JsonValue.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
// Copyright 2015 Eric Millin
//
// 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 JSONVALUE_H
#define JSONVALUE_H
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <assert.h>
class JsonValue;
typedef std::map<std::string, JsonValue> JsonObject;
typedef std::vector<JsonValue> JsonArray;
enum ValType {
Empty = 0,
Bool,
Int,
String,
Object,
Array
};
class JsonValue
{
class ObjectPtr; // pimpl
public:
~JsonValue(){};
explicit JsonValue();
JsonValue(bool val);
JsonValue(int val);
JsonValue(const std::string& val);
JsonValue(const JsonObject& val);
JsonValue(const JsonArray& val);
JsonValue(const char* str);
JsonValue(std::shared_ptr<ObjectPtr> obj);
JsonValue(const JsonValue& other);
int asInt() const;
bool asBool() const;
std::string asStr() const;
::JsonObject& asObject();
::JsonArray& asArray();
bool isEmptyOrNull() const;
const ValType type() const { return _type; };
std::string stringify() const;
JsonValue& operator[](const std::string& key);
const JsonValue& operator[](const std::string& key) const;
static std::unique_ptr<JsonValue> create2(const std::string &json);
static JsonValue create(const std::string &json);
static std::string stringify(const JsonObject& obj);
static std::string stringify(const JsonArray& array);
JsonValue& operator=(const JsonValue& other);
JsonValue& operator=(JsonValue&& other);
private:
std::shared_ptr<ObjectPtr> _object;
ValType _type;
bool _boolVal = false;
int _intVal = -1;
std::string _strVal;
::JsonArray _jArray;
::JsonObject _jObject;
};
#endif /* JSONVALUE_H */