JSMN iterator fix

This commit is contained in:
Stephan Hadinger 2020-09-24 18:45:01 +02:00
parent cac00a7efd
commit a16b2f01ae
2 changed files with 11 additions and 4 deletions

View File

@ -178,7 +178,7 @@ JsonParserArray::const_iterator::const_iterator(const JsonParserArray t): tok(t)
}
JsonParserArray::const_iterator JsonParserArray::const_iterator::const_iterator::operator++() {
if (remaining == 0) { tok.t = nullptr; }
if (remaining <= 1) { tok.t = nullptr; }
else {
remaining--;
tok.skipToken(); // munch value
@ -234,7 +234,7 @@ JsonParserObject::const_iterator::const_iterator(const JsonParserObject t): tok(
}
JsonParserObject::const_iterator JsonParserObject::const_iterator::operator++() {
if (remaining == 0) { tok.t = nullptr; }
if (remaining <= 1) { tok.t = nullptr; }
else {
remaining--;
tok.nextOne(); // munch key
@ -336,7 +336,7 @@ float JsonParserToken::getFloat(float val) const {
const char * JsonParserToken::getStr(const char * val) const {
if (t->type == JSMN_INVALID) { return val; }
if (t->type == JSMN_NULL) return "";
return (t->type >= JSMN_STRING) ? &k_current_json_buffer[t->start] : "";
return (t->type >= JSMN_STRING) ? &k_current_json_buffer[t->start] : val;
}
@ -467,6 +467,9 @@ JsonParserToken JsonParserObject::operator[](const char * needle) const {
return JsonParserToken(&token_bad);
}
JsonParserToken JsonParserObject::operator[](const String & needle) const {
return (*this)[needle.c_str()];
}
JsonParserToken JsonParserObject::findStartsWith(const char * needle) const {
// key can be in PROGMEM

View File

@ -23,6 +23,7 @@
#include "jsmn.h"
#include <string.h>
#include <stdlib.h>
#include <Arduino.h>
// #define strcmp_P(x, y) strcmp(x,y)
// #define strcasecmp_P(x,y) strcasecmp(x,y)
@ -146,10 +147,12 @@ public:
class JsonParserObject : public JsonParserToken {
public:
JsonParserObject(const jsmntok_t * token);
explicit JsonParserObject(const JsonParserToken token);
JsonParserObject(const JsonParserToken token);
JsonParserObject() : JsonParserToken() { }
// find key with name, case-insensitive, '?' matches any key. Returns Invalid Token if not found
JsonParserToken operator[](const char * needle) const;
JsonParserToken operator[](const String & needle) const;
// find a key starting with `needle`, case insensitive
JsonParserToken findStartsWith(const char * needle) const;
// find a key, case-insensitive, return nullptr if not found (instead of "")
@ -190,6 +193,7 @@ class JsonParserArray : public JsonParserToken {
public:
JsonParserArray(const jsmntok_t * token);
JsonParserArray(const JsonParserToken token);
JsonParserArray() : JsonParserToken() { }
// get the element if index `i` from 0 to `size() - 1`
JsonParserToken operator[](int32_t i) const;