summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/third_party/rapidjson/example/archiver/archiver.h
blob: 72d0cdd65cbef82dbfdc171fafb8fe657f0dfb57 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef ARCHIVER_H_
#define ARCHIVER_H_

#include <cstddef>
#include <string>

/**
\class Archiver
\brief Archiver concept

Archiver can be a reader or writer for serialization or deserialization respectively.

class Archiver {
public:
    /// \returns true if the archiver is in normal state. false if it has errors.
    operator bool() const;

    /// Starts an object
    Archiver& StartObject();

    /// After calling StartObject(), assign a member with a name
    Archiver& Member(const char* name);

    /// After calling StartObject(), check if a member presents
    bool HasMember(const char* name) const;

    /// Ends an object
    Archiver& EndObject();

    /// Starts an array
    /// \param size If Archiver::IsReader is true, the size of array is written.
    Archiver& StartArray(size_t* size = 0);

    /// Ends an array
    Archiver& EndArray();

    /// Read/Write primitive types.
    Archiver& operator&(bool& b);
    Archiver& operator&(unsigned& u);
    Archiver& operator&(int& i);
    Archiver& operator&(double& d);
    Archiver& operator&(std::string& s);

    /// Write primitive types.
    Archiver& SetNull();

    //! Whether it is a reader.
    static const bool IsReader;

    //! Whether it is a writer.
    static const bool IsWriter;
};
*/

/// Represents a JSON reader which implements Archiver concept.
class JsonReader {
public:
    /// Constructor.
    /**
        \param json A non-const source json string for in-situ parsing.
        \note in-situ means the source JSON string will be modified after parsing.
    */
    JsonReader(const char* json);

    /// Destructor.
    ~JsonReader();

    // Archive concept

    operator bool() const { return !mError; }

    JsonReader& StartObject();
    JsonReader& Member(const char* name);
    bool HasMember(const char* name) const;
    JsonReader& EndObject();

    JsonReader& StartArray(size_t* size = 0);
    JsonReader& EndArray();

    JsonReader& operator&(bool& b);
    JsonReader& operator&(unsigned& u);
    JsonReader& operator&(int& i);
    JsonReader& operator&(double& d);
    JsonReader& operator&(std::string& s);

    JsonReader& SetNull();

    static const bool IsReader = true;
    static const bool IsWriter = !IsReader;

private:
    JsonReader(const JsonReader&);
    JsonReader& operator=(const JsonReader&);

    void Next();

    // PIMPL
    void* mDocument;              ///< DOM result of parsing.
    void* mStack;                 ///< Stack for iterating the DOM
    bool mError;                  ///< Whether an error has occurred.
};

class JsonWriter {
public:
    /// Constructor.
    JsonWriter();

    /// Destructor.
    ~JsonWriter();

    /// Obtains the serialized JSON string.
    const char* GetString() const;

    // Archive concept

    operator bool() const { return true; }

    JsonWriter& StartObject();
    JsonWriter& Member(const char* name);
    bool HasMember(const char* name) const;
    JsonWriter& EndObject();

    JsonWriter& StartArray(size_t* size = 0);
    JsonWriter& EndArray();

    JsonWriter& operator&(bool& b);
    JsonWriter& operator&(unsigned& u);
    JsonWriter& operator&(int& i);
    JsonWriter& operator&(double& d);
    JsonWriter& operator&(std::string& s);
    JsonWriter& SetNull();

    static const bool IsReader = false;
    static const bool IsWriter = !IsReader;

private:
    JsonWriter(const JsonWriter&);
    JsonWriter& operator=(const JsonWriter&);

    // PIMPL idiom
    void* mWriter;      ///< JSON writer.
    void* mStream;      ///< Stream buffer.
};

#endif // ARCHIVER_H__