summaryrefslogtreecommitdiff
path: root/src/json.hh
blob: 5d5aa389f3bceea8751ddcc3989b4b453ebbd6d7 (plain)
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
#ifndef JSON_HH
#define JSON_HH

#include <memory>
#include <string>

namespace stuff {

class JsonArray;

class JsonObject {
public:
    virtual ~JsonObject() {}
    virtual void put(const std::string& name, const std::string& value) = 0;
    void put(const std::string& name, const char* value);
    virtual void put(const std::string& name, double value) = 0;
    virtual void put(const std::string& name, int64_t value) = 0;
    virtual void put(const std::string& name, bool value) = 0;
    virtual void put(const std::string& name, std::nullptr_t value) = 0;
    virtual void put(const std::string& name,
                     std::shared_ptr<JsonObject> obj) = 0;
    virtual void put(const std::string& name,
                     std::shared_ptr<JsonArray> arr) = 0;

    virtual bool erase(const std::string& name) = 0;
    virtual void clear() = 0;

    virtual bool contains(const std::string& name) const = 0;
    virtual bool is_null(const std::string& name) const = 0;
    virtual const std::string& get(const std::string& name,
                                   const std::string& fallback
                                   = std::string()) const = 0;
    virtual const char* get(const std::string& name,
                            const char* fallback) const = 0;
    virtual double get(const std::string& name,
                       double fallback = 0.0) const = 0;
    virtual int64_t get(const std::string& name,
                        int64_t fallback = 0) const = 0;
    virtual bool get(const std::string& name, bool fallback) const = 0;
    virtual bool get(const std::string& name,
                     std::shared_ptr<JsonObject>* obj) const = 0;
    virtual bool get(const std::string& name,
                     std::shared_ptr<JsonArray>* arr) const = 0;

    virtual std::string str() const = 0;

    static std::shared_ptr<JsonObject> create();

protected:
    JsonObject() {}
    JsonObject(const JsonObject&) = delete;
    JsonObject& operator=(const JsonObject&) = delete;
};

class JsonArray {
public:
    virtual ~JsonArray() {}

    virtual size_t size() const = 0;
    virtual void resize(size_t size) = 0;

    virtual void put(size_t index, const std::string& value) = 0;
    void put(size_t index, const char* value);
    virtual void put(size_t index, double value) = 0;
    virtual void put(size_t index, int64_t value) = 0;
    virtual void put(size_t index, bool value) = 0;
    virtual void put(size_t index, std::nullptr_t value) = 0;
    virtual void put(size_t index,
                     std::shared_ptr<JsonObject> obj) = 0;
    virtual void put(size_t index,
                     std::shared_ptr<JsonArray> arr) = 0;

    virtual void erase(size_t index) = 0;
    void clear();

    void add(const std::string& value);
    void add(const char* value);
    void add(double value);
    void add(int64_t value);
    void add(bool value);
    void add(std::nullptr_t value);
    void add(std::shared_ptr<JsonObject> obj);
    void add(std::shared_ptr<JsonArray> arr);

    virtual bool is_null(size_t index) const = 0;
    virtual const std::string& get(size_t index,
                                   const std::string& fallback
                                   = std::string()) const = 0;
    virtual const char* get(size_t index, const char* fallback) const = 0;
    virtual double get(size_t index,
                       double fallback = 0.0) const = 0;
    virtual int64_t get(size_t index,
                        int64_t fallback = 0) const = 0;
    virtual bool get(size_t index, bool fallback) const = 0;
    virtual bool get(size_t index,
                     std::shared_ptr<JsonObject>* obj) const = 0;
    virtual bool get(size_t index,
                     std::shared_ptr<JsonArray>* arr) const = 0;

    virtual std::string str() const = 0;

    static std::shared_ptr<JsonArray> create();

protected:
    JsonArray() {}
    JsonArray(const JsonArray&) = delete;
    JsonArray& operator=(const JsonArray&) = delete;
};

}  // namespace stuff

#endif /* JSON_HH */