diff --git a/JsonListener.h b/JsonListener.h index 7cfb197..6e280e7 100644 --- a/JsonListener.h +++ b/JsonListener.h @@ -40,9 +40,9 @@ class JsonListener { virtual void startDocument() = 0; - virtual void key(String key) = 0; + virtual void key(const char *key) = 0; - virtual void value(String value) = 0; + virtual void value(const char *value) = 0; virtual void endArray() = 0; diff --git a/JsonStreamingParser.cpp b/JsonStreamingParser.cpp index 3ae687f..72c50c3 100644 --- a/JsonStreamingParser.cpp +++ b/JsonStreamingParser.cpp @@ -210,11 +210,11 @@ void JsonStreamingParser::endString() { stackPos--; if (popped == STACK_KEY) { buffer[bufferPos] = '\0'; - myListener->key(String(buffer)); + myListener->key(buffer); state = STATE_END_KEY; } else if (popped == STACK_STRING) { buffer[bufferPos] = '\0'; - myListener->value(String(buffer)); + myListener->value(buffer); state = STATE_AFTER_VALUE; } else { // throw new ParsingError($this->_line_number, $this->_char_number, @@ -404,7 +404,7 @@ void JsonStreamingParser::endUnicodeSurrogateInterstitial() { void JsonStreamingParser::endNumber() { buffer[bufferPos] = '\0'; - String value = String(buffer); + // String value = String(buffer); //float result = 0.0; //if (doesCharArrayContain(buffer, bufferPos, '.')) { // result = value.toFloat(); @@ -412,7 +412,7 @@ void JsonStreamingParser::endNumber() { // needed special treatment in php, maybe not in Java and c // result = value.toFloat(); //} - myListener->value(value.c_str()); + myListener->value(buffer); bufferPos = 0; state = STATE_AFTER_VALUE; } @@ -433,8 +433,8 @@ void JsonStreamingParser::endDocument() { void JsonStreamingParser::endTrue() { buffer[bufferPos] = '\0'; - String value = String(buffer); - if (value == "true") { + // String value = String(buffer); + if (strncmp(buffer, "true", 4) == 0) { myListener->value("true"); } else { // throw new ParsingError($this->_line_number, $this->_char_number, @@ -446,8 +446,8 @@ void JsonStreamingParser::endTrue() { void JsonStreamingParser::endFalse() { buffer[bufferPos] = '\0'; - String value = String(buffer); - if (value == "false") { + // String value = String(buffer); + if (strncmp(buffer, "false",5) == 0 ) { myListener->value("false"); } else { // throw new ParsingError($this->_line_number, $this->_char_number, @@ -459,8 +459,8 @@ void JsonStreamingParser::endFalse() { void JsonStreamingParser::endNull() { buffer[bufferPos] = '\0'; - String value = String(buffer); - if (value == "null") { + // String value = String(buffer); + if (strncmp(buffer, "null", 4) == 0) { myListener->value("null"); } else { // throw new ParsingError($this->_line_number, $this->_char_number, diff --git a/examples/JsonStreamingParser/ExampleParser.cpp b/examples/JsonStreamingParser/ExampleParser.cpp index 0656fdb..88ea096 100644 --- a/examples/JsonStreamingParser/ExampleParser.cpp +++ b/examples/JsonStreamingParser/ExampleParser.cpp @@ -10,12 +10,14 @@ void ExampleListener::startDocument() { Serial.println("start document"); } -void ExampleListener::key(String key) { - Serial.println("key: " + key); +void ExampleListener::key(const char *key) { + Serial.print("key: "); + Serial.println(key); } -void ExampleListener::value(String value) { - Serial.println("value: " + value); +void ExampleListener::value(const char *value) { + Serial.print("value: "); + Serial.print(value); } void ExampleListener::endArray() { diff --git a/examples/JsonStreamingParser/ExampleParser.h b/examples/JsonStreamingParser/ExampleParser.h index c5f6f79..a986491 100644 --- a/examples/JsonStreamingParser/ExampleParser.h +++ b/examples/JsonStreamingParser/ExampleParser.h @@ -9,9 +9,9 @@ class ExampleListener: public JsonListener { virtual void startDocument(); - virtual void key(String key); + virtual void key(const char *key); - virtual void value(String value); + virtual void value(const char *value); virtual void endArray(); diff --git a/examples/JsonStreamingParser/JsonStreamingParser.ino b/examples/JsonStreamingParser/JsonStreamingParser.ino index fd6222c..fa7585f 100644 --- a/examples/JsonStreamingParser/JsonStreamingParser.ino +++ b/examples/JsonStreamingParser/JsonStreamingParser.ino @@ -7,14 +7,18 @@ ExampleListener listener; void setup() { Serial.begin(115200); +#ifdef ARDUINO_ARCH_ESP8266 Serial.println(String(ESP.getFreeHeap())); +#endif parser.setListener(&listener); // put your setup code here, to run once: char json[] = "{\"a\":3, \"b\":{\"c\":\"d\"}}"; for (int i = 0; i < sizeof(json); i++) { parser.parse(json[i]); } +#ifdef ARDUINO_ARCH_ESP8266 Serial.println(String(ESP.getFreeHeap())); +#endif } void loop() {