Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#pragma once
2
#include <Arduino.h>
3
#include <vector>
4
#include <stdint.h>
5
extern "C" {
6
#include "json/cJSON.h"
7
}
8
 
9
namespace CJSON {
10
class Json {
11
public:
12
    Json();
13
    ~Json();
14
 
15
    bool parse(const String& text);
16
    String serialize(bool pretty=false) const;
17
 
18
    // Construction helpers for nested structures
19
    // Initialize the root as an empty object or array
20
    bool createObject();
21
    bool createArray();
22
    // Append a child to the root array
23
    bool add(const Json& child);
24
    // Set a nested child under a key in the root object
25
    bool set(const String& key, const Json& child);
26
 
27
    bool hasObject(const String& key) const;
28
    void ensureObject(const String& key);
29
 
30
    // Top-level key helpers
31
    bool hasKey(const String& key) const;
32
    bool setString(const String& key, const String& value);
33
    bool setNumber(const String& key, double value);
34
    bool setBool(const String& key, bool value);
35
    bool setArray(const String& key, const std::vector<String>& values);
36
    bool getString(const String& key, String& out) const;
37
    bool getBool(const String& key, bool& out) const;
38
    bool getNumber(const String& key, double& out) const;
39
 
40
    // Object-scoped key helpers
41
    bool hasKey(const String& obj, const String& key) const;
42
    bool setString(const String& obj, const String& key, const String& value);
43
    bool setNumber(const String& obj, const String& key, double value);
44
    bool setBool(const String& obj, const String& key, bool value);
45
    bool setArray(const String& obj, const String& key, const std::vector<String>& values);
46
    bool getString(const String& obj, const String& key, String& out) const;
47
    bool getBool(const String& obj, const String& key, bool& out) const;
48
    bool getNumber(const String& obj, const String& key, double& out) const;
49
 
50
    // Low-level accessor: expose underlying cJSON root for advanced operations
51
    // (e.g. iterating arrays/objects in higher-level helpers).
52
    // Caller must NOT free or modify the returned pointer directly.
53
    cJSON* getRoot() { return root; }
54
    const cJSON* getRoot() const { return root; }
55
 
56
private:
57
    cJSON* root;
58
};
59
}