Skip to content

Commit

Permalink
Removed all references to Arduino String objects and replaced with ch…
Browse files Browse the repository at this point in the history
…ar buffers to avoid heap fragmentation
  • Loading branch information
Alligator Descartes committed Dec 6, 2016
1 parent 47ee391 commit 127ee35
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions JsonListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 10 additions & 10 deletions JsonStreamingParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -404,15 +404,15 @@ 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();
//} else {
// 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;
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions examples/JsonStreamingParser/ExampleParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions examples/JsonStreamingParser/ExampleParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions examples/JsonStreamingParser/JsonStreamingParser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 127ee35

Please sign in to comment.