diff --git a/JsonStreamingParser.cpp b/JsonStreamingParser.cpp index cfc71d1..1c70211 100644 --- a/JsonStreamingParser.cpp +++ b/JsonStreamingParser.cpp @@ -49,8 +49,8 @@ void JsonStreamingParser::parse(char c) { // http://stackoverflow.com/questions/16042274/definition-of-whitespace-in-json if ((c == ' ' || c == '\t' || c == '\n' || c == '\r') && !(state == STATE_IN_STRING || state == STATE_UNICODE || state == STATE_START_ESCAPE - || state == STATE_IN_NUMBER || state == STATE_START_DOCUMENT)) { - return; + || state == STATE_IN_NUMBER)) { + return; } switch (state) { case STATE_IN_STRING: @@ -68,6 +68,8 @@ void JsonStreamingParser::parse(char c) { case STATE_IN_ARRAY: if (c == ']') { endArray(); + } else if (c == '{') { + startObject(); } else { startValue(c); } diff --git a/examples/ComplexJsonFile/ComplexJsonFile.cpp b/examples/ComplexJsonFile/ComplexJsonFile.cpp new file mode 100644 index 0000000..3ae7af1 --- /dev/null +++ b/examples/ComplexJsonFile/ComplexJsonFile.cpp @@ -0,0 +1 @@ +#include "ComplexJsonFile.h" \ No newline at end of file diff --git a/examples/ComplexJsonFile/ComplexJsonFile.h b/examples/ComplexJsonFile/ComplexJsonFile.h new file mode 100644 index 0000000..617d513 --- /dev/null +++ b/examples/ComplexJsonFile/ComplexJsonFile.h @@ -0,0 +1,28 @@ +#include "sample.json.h" +#include "JsonStreamingParser.h" +#include "ExampleListener.h" +#include + +void setup() { + ExampleListener listener = ExampleListener(); + JsonStreamingParser parser = JsonStreamingParser(); + + parser.setListener(&listener); + + for (int i = 0; i < strlen(json_sample); i++) + { + parser.parse(json_sample[i]); + } +} + +void loop() { + +} + +#ifndef ARDUINO +int main() { + setup(); + loop(); + return 0; +} +#endif \ No newline at end of file diff --git a/examples/ComplexJsonFile/ComplexJsonFile.ino b/examples/ComplexJsonFile/ComplexJsonFile.ino new file mode 100644 index 0000000..3ae7af1 --- /dev/null +++ b/examples/ComplexJsonFile/ComplexJsonFile.ino @@ -0,0 +1 @@ +#include "ComplexJsonFile.h" \ No newline at end of file diff --git a/examples/ComplexJsonFile/ExampleListener.cpp b/examples/ComplexJsonFile/ExampleListener.cpp new file mode 100644 index 0000000..016aca4 --- /dev/null +++ b/examples/ComplexJsonFile/ExampleListener.cpp @@ -0,0 +1,47 @@ +#include "ExampleListener.h" +#include "JsonListener.h" + + +void json_print(String value) { + #ifdef ARDUINO + Serial.println(value.c_str()); + #else + printf("%s\n", value.c_str()); + #endif +} +void ExampleListener::whitespace(char c) { + json_print("whitespace"); +} + +void ExampleListener::startDocument() { + json_print("start document"); +} + +void ExampleListener::key(String key) { + json_print("key: " + key); +} + +void ExampleListener::value(String value) { + json_print("value: " + value); +} + +void ExampleListener::endArray() { + json_print("end array. "); +} + +void ExampleListener::endObject() { + json_print("end object. "); +} + +void ExampleListener::endDocument() { + json_print("end document. "); +} + +void ExampleListener::startArray() { + json_print("start array. "); +} + +void ExampleListener::startObject() { + json_print("start object. "); +} + diff --git a/examples/ComplexJsonFile/ExampleListener.h b/examples/ComplexJsonFile/ExampleListener.h new file mode 100644 index 0000000..c5f6f79 --- /dev/null +++ b/examples/ComplexJsonFile/ExampleListener.h @@ -0,0 +1,25 @@ +#pragma once + +#include "JsonListener.h" + +class ExampleListener: public JsonListener { + + public: + virtual void whitespace(char c); + + virtual void startDocument(); + + virtual void key(String key); + + virtual void value(String value); + + virtual void endArray(); + + virtual void endObject(); + + virtual void endDocument(); + + virtual void startArray(); + + virtual void startObject(); +}; diff --git a/examples/ComplexJsonFile/sample.json.h b/examples/ComplexJsonFile/sample.json.h new file mode 100644 index 0000000..6757918 --- /dev/null +++ b/examples/ComplexJsonFile/sample.json.h @@ -0,0 +1,23 @@ +const char* json_sample = R"JSON( +{ + "simpleValue": 1234, + "simpleFloatValue": 12.345, + "normalizedFloat": 1.345e-6, + "boolean": true, + "string": "str1234", + "arrayOfSimpleValues": [ + "text", + 6234, + false + ], + "arraOfObjects": [ + { + "utf8String": "éáőúüóóű", + "escapedString": "\"\u3fd4" + }, + { + "whatever": "lol", + "nullValue": null + } + ] +})JSON"; \ No newline at end of file