Skip to content

Commit 26954d8

Browse files
committed
Initial functionality for objects
1 parent fe0fa21 commit 26954d8

File tree

5 files changed

+269
-24
lines changed

5 files changed

+269
-24
lines changed

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#######################################
66

77
JSON KEYWORD1
8+
JSONVar KEYWORD1
9+
var KEYWORD1
810

911
#######################################
1012
# Methods and Functions

src/JSON.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,21 @@
1717
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#include "cjson/cJSON.h"
21-
2220
#include "JSON.h"
2321

24-
JSON::JSON()
25-
{
26-
}
27-
28-
JSON::~JSON()
22+
JSONVar JSONClass::parse(const char* s)
2923
{
24+
return JSONVar::parse(s);
3025
}
3126

32-
JSON JSON::parse(const char* s)
27+
JSONVar JSONClass::parse(const String& s)
3328
{
34-
return JSON();
29+
return JSONVar::parse(s);
3530
}
3631

37-
JSON JSON::parse(const String& s)
32+
String JSONClass::stringify(const JSONVar& value)
3833
{
39-
return parse(s.c_str());
34+
return JSONVar::stringify(value);
4035
}
4136

42-
String JSON::stringify()
43-
{
44-
return "";
45-
}
37+
JSONClass JSON;

src/JSON.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222

2323
#include <Arduino.h>
2424

25-
class JSON {
26-
public:
27-
JSON();
28-
virtual ~JSON();
29-
30-
static JSON parse(const char* s);
31-
static JSON parse(const String& s);
25+
#include "JSONVar.h"
3226

33-
String stringify();
27+
class JSONClass {
28+
public:
29+
JSONVar parse(const char* s);
30+
JSONVar parse(const String& s);
3431

35-
private:
32+
String stringify(const JSONVar& value);
3633
};
3734

35+
extern JSONClass JSON;
36+
3837
#endif

src/JSONVar.cpp

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
This file is part of the JSON library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "cjson/cJSON.h"
21+
22+
#include "JSONVar.h"
23+
24+
JSONVar::JSONVar(struct cJSON* json, struct cJSON* parent) :
25+
_json(json),
26+
_parent(parent)
27+
{
28+
}
29+
30+
JSONVar::JSONVar(const JSONVar& v)
31+
{
32+
_json = cJSON_Duplicate(v._json, true);
33+
_parent = NULL;
34+
}
35+
36+
JSONVar::JSONVar() :
37+
JSONVar(NULL, NULL)
38+
{
39+
}
40+
41+
JSONVar::~JSONVar()
42+
{
43+
if (_json != NULL && _parent == NULL) {
44+
cJSON_Delete(_json);
45+
46+
_json = NULL;
47+
}
48+
}
49+
50+
size_t JSONVar::printTo(Print& p) const
51+
{
52+
if (_json == NULL) {
53+
return 0;
54+
}
55+
56+
char* s = cJSON_PrintUnformatted(_json);
57+
58+
size_t writen = p.print(s);
59+
60+
cJSON_free(s);
61+
62+
return writen;
63+
}
64+
65+
JSONVar::operator bool()
66+
{
67+
return cJSON_IsBool(_json) && cJSON_IsTrue(_json);
68+
}
69+
70+
JSONVar::operator int()
71+
{
72+
return cJSON_IsNumber(_json) ? _json->valueint : 0;
73+
}
74+
75+
JSONVar::operator double()
76+
{
77+
return cJSON_IsNumber(_json) ? _json->valuedouble : NAN;
78+
}
79+
80+
JSONVar::operator const char*()
81+
{
82+
if (cJSON_IsString(_json)) {
83+
return _json->valuestring;
84+
}
85+
86+
return NULL;
87+
}
88+
89+
void JSONVar::operator= (const JSONVar& v)
90+
{
91+
replaceJson(cJSON_Duplicate(v._json, true));
92+
_parent = NULL;
93+
}
94+
95+
void JSONVar::operator=(bool b)
96+
{
97+
replaceJson(b ? cJSON_CreateTrue() : cJSON_CreateFalse());
98+
}
99+
100+
void JSONVar::operator=(int i)
101+
{
102+
replaceJson(cJSON_CreateNumber(i));
103+
}
104+
105+
void JSONVar::operator=(double d)
106+
{
107+
replaceJson(cJSON_CreateNumber(d));
108+
}
109+
110+
void JSONVar::operator=(const char* s)
111+
{
112+
replaceJson(cJSON_CreateString(s));
113+
}
114+
115+
void JSONVar::operator=(const String& s)
116+
{
117+
*this = s.c_str();
118+
}
119+
120+
JSONVar JSONVar::operator[](const char* key)
121+
{
122+
cJSON* json = cJSON_GetObjectItemCaseSensitive(_json, key);
123+
124+
if (json == NULL) {
125+
json = cJSON_AddNullToObject(_json, key);
126+
}
127+
128+
return JSONVar(json, _json);
129+
}
130+
131+
JSONVar JSONVar::operator[](int index)
132+
{
133+
cJSON* json = cJSON_GetArrayItem(_json, index);
134+
135+
// TODO: create NULL cJSON if null?
136+
137+
return JSONVar(json, _json);
138+
}
139+
140+
JSONVar JSONVar::parse(const char* s)
141+
{
142+
cJSON* json = cJSON_Parse(s);
143+
144+
return JSONVar(json, NULL);
145+
}
146+
147+
JSONVar JSONVar::parse(const String& s)
148+
{
149+
return parse(s.c_str());
150+
}
151+
152+
String JSONVar::stringify(const JSONVar& value)
153+
{
154+
if (value._json == NULL) {
155+
return String((const char *)NULL);
156+
}
157+
158+
char* s = cJSON_PrintUnformatted(value._json);
159+
160+
String str = s;
161+
162+
cJSON_free(s);
163+
164+
return str;
165+
}
166+
167+
void JSONVar::replaceJson(struct cJSON* json)
168+
{
169+
cJSON* old = _json;
170+
171+
_json = json;
172+
173+
if (old) {
174+
if (_parent) {
175+
if (cJSON_IsObject(_parent)) {
176+
cJSON_ReplaceItemInObjectCaseSensitive(_parent, old->string, _json);
177+
} else if (cJSON_IsArray(_parent)) {
178+
cJSON_ReplaceItemViaPointer(_parent, old, _json);
179+
}
180+
} else {
181+
cJSON_Delete(old);
182+
}
183+
}
184+
}

src/JSONVar.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
This file is part of the JSON library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _JSON_VAR_H_
21+
#define _JSON_VAR_H_
22+
23+
#include <Arduino.h>
24+
25+
struct cJSON;
26+
27+
class JSONVar : public Printable {
28+
public:
29+
JSONVar();
30+
JSONVar(const JSONVar& v);
31+
virtual ~JSONVar();
32+
33+
virtual size_t printTo(Print& p) const;
34+
35+
operator bool();
36+
operator int();
37+
operator double();
38+
operator const char*();
39+
40+
void operator=(const JSONVar& v);
41+
void operator=(bool b);
42+
void operator=(int i);
43+
void operator=(double d);
44+
void operator=(const char* s);
45+
void operator=(const String& s);
46+
47+
JSONVar operator[](const char* key);
48+
JSONVar operator[](int index);
49+
50+
static JSONVar parse(const char* s);
51+
static JSONVar parse(const String& s);
52+
static String stringify(const JSONVar& value);
53+
54+
private:
55+
JSONVar(struct cJSON* json, struct cJSON* parent);
56+
57+
void replaceJson(struct cJSON* json);
58+
59+
private:
60+
struct cJSON* _json;
61+
struct cJSON* _parent;
62+
};
63+
64+
#ifndef var
65+
typedef JSONVar var;
66+
#endif
67+
68+
#endif

0 commit comments

Comments
 (0)