Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 6f1ec7d

Browse files
committed
Merge pull request #79 from ed7coyne/json-parsing
Include ArduinoJson directly in our library and parse json.
2 parents 7bad03f + b5f1e53 commit 6f1ec7d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+4331
-20
lines changed

examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
// create firebase client.
2323
Firebase fbase = Firebase("example.firebaseio.com")
2424
.auth("secret_or_token");
25-
2625
void setup() {
2726
Serial.begin(9600);
2827

@@ -46,7 +45,7 @@ void setup() {
4645
}
4746

4847
// print key.
49-
Serial.println(push.name());
48+
Serial.println("Name: " + push.name());
5049

5150
// get all entries.
5251
FirebaseGet get = fbase.get("/logs");
@@ -55,8 +54,9 @@ void setup() {
5554
Serial.println(push.error().message());
5655
return;
5756
}
58-
// print json.
59-
Serial.println(get.json());
57+
// Print written timestamp.
58+
String data = get.json()[push.name()];
59+
Serial.println("Timestamp:" + data);
6060
}
6161

6262
void loop() {

library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=firebase-arduino
2+
version=0.4
3+
author=
4+
maintainer=
5+
sentence=Fill in later
6+
paragraph=Fill in later
7+
category=Communication
8+
url=http://example.com/
9+
architectures=esp8266

Firebase.cpp renamed to src/Firebase.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,18 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
118118
}
119119
}
120120

121+
const JsonObject& FirebaseCall::json() {
122+
//TODO(edcoyne): This is not efficient, we should do something smarter with
123+
//the buffers.
124+
buffer_ = DynamicJsonBuffer();
125+
return buffer_.parseObject(response());
126+
}
127+
121128
// FirebaseGet
122129
FirebaseGet::FirebaseGet(const String& host, const String& auth,
123130
const String& path,
124131
HTTPClient* http)
125132
: FirebaseCall(host, auth, "GET", path, "", http) {
126-
if (!error()) {
127-
// TODO: parse json
128-
json_ = response();
129-
}
130133
}
131134

132135
// FirebaseSet
@@ -145,8 +148,7 @@ FirebasePush::FirebasePush(const String& host, const String& auth,
145148
HTTPClient* http)
146149
: FirebaseCall(host, auth, "POST", path, value, http) {
147150
if (!error()) {
148-
// TODO: parse name
149-
name_ = response();
151+
name_ = json()["name"].as<const char*>();
150152
}
151153
}
152154

Firebase.h renamed to src/Firebase.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <ESP8266WiFi.h>
2525
#include <WiFiClientSecure.h>
2626
#include <ESP8266HTTPClient.h>
27+
#include "third-party/arduino-json-5.1.1/include/ArduinoJson.h"
2728

2829
class FirebaseGet;
2930
class FirebaseSet;
@@ -81,24 +82,25 @@ class FirebaseCall {
8182
const FirebaseError& error() const {
8283
return error_;
8384
}
85+
8486
const String& response() {
8587
return response_;
8688
}
89+
90+
const JsonObject& json();
91+
8792
protected:
8893
HTTPClient* http_;
8994
FirebaseError error_;
9095
String response_;
96+
DynamicJsonBuffer buffer_;
9197
};
9298

9399
class FirebaseGet : public FirebaseCall {
94100
public:
95101
FirebaseGet() {}
96102
FirebaseGet(const String& host, const String& auth,
97103
const String& path, HTTPClient* http = NULL);
98-
99-
const String& json() const {
100-
return json_;
101-
}
102104

103105
private:
104106
String json_;
@@ -110,10 +112,6 @@ class FirebaseSet: public FirebaseCall {
110112
FirebaseSet(const String& host, const String& auth,
111113
const String& path, const String& value, HTTPClient* http = NULL);
112114

113-
const String& json() const {
114-
return json_;
115-
}
116-
117115
private:
118116
String json_;
119117
};
@@ -145,7 +143,7 @@ class FirebaseStream : public FirebaseCall {
145143
FirebaseStream() {}
146144
FirebaseStream(const String& host, const String& auth,
147145
const String& path, HTTPClient* http = NULL);
148-
146+
149147
// Return if there is any event available to read.
150148
bool available();
151149

@@ -162,7 +160,7 @@ class FirebaseStream : public FirebaseCall {
162160
const FirebaseError& error() const {
163161
return _error;
164162
}
165-
163+
166164
private:
167165
FirebaseError _error;
168166
};

src/third-party/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
These are packages that we depend upon. They may need to be trimmed, all .cpp
2+
files in them will be compiled by the arduino IDE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
ArduinoJson: change log
2+
=======================
3+
4+
v5.1.1
5+
------
6+
7+
* Removed `String` duplication when one replaces a value in a `JsonObject` (PR #232 by @ulion)
8+
9+
v5.1.0
10+
------
11+
12+
* Added support of `long long` (issue #171)
13+
* Moved all build settings to `ArduinoJson/Configuration.hpp`
14+
15+
**BREAKING CHANGE**:
16+
If you defined `ARDUINOJSON_ENABLE_STD_STREAM`, you now need to define it to `1`.
17+
18+
v5.0.8
19+
------
20+
21+
* Made the library compatible with [PlatformIO](http://platformio.org/) (issue #181)
22+
* Fixed `JsonVariant::is<bool>()` that was incorrectly returning false (issue #214)
23+
24+
v5.0.7
25+
------
26+
27+
* Made library easier to use from a CMake project: simply `add_subdirectory(ArduinoJson/src)`
28+
* Changed `String` to be a `typedef` of `std::string` (issues #142 and #161)
29+
30+
**BREAKING CHANGES**:
31+
- `JsonVariant(true).as<String>()` now returns `"true"` instead of `"1"`
32+
- `JsonVariant(false).as<String>()` now returns `"false"` instead of `"0"`
33+
34+
v5.0.6
35+
------
36+
37+
* Added parameter to `DynamicJsonBuffer` constructor to set initial size (issue #152)
38+
* Fixed warning about library category in Arduino 1.6.6 (issue #147)
39+
* Examples: Added a loop to wait for serial port to be ready (issue #156)
40+
41+
v5.0.5
42+
------
43+
44+
* Added overload `JsonObjectSuscript::set(value, decimals)` (issue #143)
45+
* Use `float` instead of `double` to reduce the size of `JsonVariant` (issue #134)
46+
47+
v5.0.4
48+
------
49+
50+
* Fixed ambiguous overload with `JsonArraySubscript` and `JsonObjectSubscript` (issue #122)
51+
52+
v5.0.3
53+
------
54+
55+
* Fixed `printTo(String)` which wrote numbers instead of strings (issue #120)
56+
* Fixed return type of `JsonArray::is<T>()` and some others (issue #121)
57+
58+
v5.0.2
59+
------
60+
61+
* Fixed segmentation fault in `parseObject(String)` and `parseArray(String)`, when the
62+
`StaticJsonBuffer` is too small to hold a copy of the string
63+
* Fixed Clang warning "register specifier is deprecated" (issue #102)
64+
* Fixed GCC warning "declaration shadows a member" (issue #103)
65+
* Fixed memory alignment, which made ESP8266 crash (issue #104)
66+
* Fixed compilation on Visual Studio 2010 and 2012 (issue #107)
67+
68+
v5.0.1
69+
------
70+
71+
* Fixed compilation with Arduino 1.0.6 (issue #99)
72+
73+
v5.0.0
74+
------
75+
76+
* Added support of `String` class (issues #55, #56, #70, #77)
77+
* Added `JsonBuffer::strdup()` to make a copy of a string (issues #10, #57)
78+
* Implicitly call `strdup()` for `String` but not for `char*` (issues #84, #87)
79+
* Added support of non standard JSON input (issue #44)
80+
* Added support of comments in JSON input (issue #88)
81+
* Added implicit cast between numerical types (issues #64, #69, #93)
82+
* Added ability to read number values as string (issue #90)
83+
* Redesigned `JsonVariant` to leverage converting constructors instead of assignment operators (issue #66)
84+
* Switched to new the library layout (requires Arduino 1.0.6 or above)
85+
86+
**BREAKING CHANGES**:
87+
- `JsonObject::add()` was renamed to `set()`
88+
- `JsonArray::at()` and `JsonObject::at()` were renamed to `get()`
89+
- Number of digits of floating point value are now set with `double_with_n_digits()`
90+
91+
**Personal note about the `String` class**:
92+
Support of the `String` class has been added to the library because many people use it in their programs.
93+
However, you should not see this as an invitation to use the `String` class.
94+
The `String` class is **bad** because it uses dynamic memory allocation.
95+
Compared to static allocation, it compiles to a bigger, slower program, and is less predictable.
96+
You certainly don't want that in an embedded environment!
97+
98+
v4.6
99+
----
100+
101+
* Fixed segmentation fault in `DynamicJsonBuffer` when memory allocation fails (issue #92)
102+
103+
v4.5
104+
----
105+
106+
* Fixed buffer overflow when input contains a backslash followed by a terminator (issue #81)
107+
108+
**Upgrading is recommended** since previous versions contain a potential security risk.
109+
110+
Special thanks to [Giancarlo Canales Barreto](https://github.com/gcanalesb) for finding this nasty bug.
111+
112+
v4.4
113+
----
114+
115+
* Added `JsonArray::measureLength()` and `JsonObject::measureLength()` (issue #75)
116+
117+
v4.3
118+
----
119+
120+
* Added `JsonArray::removeAt()` to remove an element of an array (issue #58)
121+
* Fixed stack-overflow in `DynamicJsonBuffer` when parsing huge JSON files (issue #65)
122+
* Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68)
123+
124+
v4.2
125+
----
126+
127+
* Switched back to old library layout (issues #39, #43 and #45)
128+
* Removed global new operator overload (issue #40, #45 and #46)
129+
* Added an example with EthernetServer
130+
131+
v4.1
132+
----
133+
134+
* Added DynamicJsonBuffer (issue #19)
135+
136+
v4.0
137+
----
138+
139+
* Unified parser and generator API (issue #23)
140+
* Updated library layout, now requires Arduino 1.0.6 or newer
141+
142+
**BREAKING CHANGE**: API changed significantly, see [Migrating code to the new API](https://github.com/bblanchon/ArduinoJson/wiki/Migrating-code-to-the-new-API).
143+
144+
145+
v3.4
146+
----
147+
148+
* Fixed escaped char parsing (issue #16)
149+
150+
151+
v3.3
152+
----
153+
154+
* Added indented output for the JSON generator (issue #11), see example bellow.
155+
* Added `IndentedPrint`, a decorator for `Print` to allow indented output
156+
157+
Example:
158+
159+
JsonOject<2> json;
160+
json["key"] = "value";
161+
json.prettyPrintTo(Serial);
162+
163+
v3.2
164+
----
165+
166+
* Fixed a bug when adding nested object in `JsonArray` (bug introduced in v3.1).
167+
168+
v3.1
169+
----
170+
171+
* Calling `Generator::JsonObject::add()` twice with the same `key` now replaces the `value`
172+
* Added `Generator::JsonObject::operator[]`, see bellow the new API
173+
* Added `Generator::JsonObject::remove()` (issue #9)
174+
175+
Old generator API:
176+
177+
JsonObject<3> root;
178+
root.add("sensor", "gps");
179+
root.add("time", 1351824120);
180+
root.add("data", array);
181+
182+
New generator API:
183+
184+
JsonObject<3> root;
185+
root["sensor"] = "gps";
186+
root["time"] = 1351824120;
187+
root["data"] = array;
188+
189+
v3.0
190+
----
191+
192+
* New parser API, see bellow
193+
* Renamed `JsonHashTable` into `JsonObject`
194+
* Added iterators for `JsonArray` and `JsonObject` (issue #4)
195+
196+
Old parser API:
197+
198+
JsonHashTable root = parser.parseHashTable(json);
199+
200+
char* sensor = root.getString("sensor");
201+
long time = root.getLong("time");
202+
double latitude = root.getArray("data").getDouble(0);
203+
double longitude = root.getArray("data").getDouble(1);
204+
205+
New parser API:
206+
207+
JsonObject root = parser.parse(json);
208+
209+
char* sensor = root["sensor"];
210+
long time = root["time"];
211+
double latitude = root["data"][0];
212+
double longitude = root["data"][1];
213+
214+
v2.1
215+
----
216+
217+
* Fixed case `#include "jsmn.cpp"` which caused an error in Linux (issue #6)
218+
* Fixed a buffer overrun in JSON Parser (issue #5)
219+
220+
v2.0
221+
----
222+
223+
* Added JSON encoding (issue #2)
224+
* Renamed the library `ArduinoJsonParser` becomes `ArduinoJson`
225+
226+
**Breaking change**: you need to add the following line at the top of your program.
227+
228+
using namespace ArduinoJson::Parser;
229+
230+
v1.2
231+
----
232+
233+
* Fixed error in JSON parser example (issue #1)
234+
235+
v1.1
236+
----
237+
238+
* Example: changed `char* json` into `char[] json` so that the bytes are not write protected
239+
* Fixed parsing bug when the JSON contains multi-dimensional arrays
240+
241+
v1.0
242+
----
243+
244+
Initial release
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The MIT License (MIT)
2+
---------------------
3+
4+
Copyright © 2014-2016 Benoit BLANCHON
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)