Skip to content

Commit

Permalink
Add support top level array parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Jul 12, 2020
1 parent 73dd12b commit 3e2f82d
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The Json Parser/Editor Arduino library.


The easiest Arduino library JSON parser, builder and editor, v 2.3.5.
The easiest Arduino library JSON parser, builder and editor, v 2.3.6.

FirebaseJson doesn't use the recursive call to parse or deserialize complex or nested JSON objects and arrays.

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
},
"frameworks": "arduino",
"platforms": "*",
"version": "2.3.5"
"version": "2.3.6"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=FirebaseJson

version=2.3.5
version=2.3.6

author=Mobizt

Expand Down
117 changes: 101 additions & 16 deletions src/FirebaseJson.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* FirebaseJson, version 2.3.5
* FirebaseJson, version 2.3.6
*
* The Easiest ESP8266/ESP32 Arduino library for parse, create and edit JSON object using a relative path.
*
* July 11, 2020
* July 12, 2020
*
* Features
* - None recursive operations
Expand Down Expand Up @@ -38,6 +38,7 @@

#ifndef FirebaseJson_CPP
#define FirebaseJson_CPP
#define JSMN_STRICT

#include "FirebaseJson.h"

Expand Down Expand Up @@ -65,6 +66,7 @@ FirebaseJson::FirebaseJson(std::string &data)
FirebaseJson::~FirebaseJson()
{
clear();
_topLevelTkType = JSMN_OBJECT;
_parser.reset();
_parser = nullptr;
_finalize();
Expand Down Expand Up @@ -132,6 +134,7 @@ FirebaseJson &FirebaseJson::_setJsonData(std::string &data)

FirebaseJson &FirebaseJson::setJsonData(const String &data)
{
_topLevelTkType = JSMN_OBJECT;
if (data.length() > 0)
{
int p1 = _strpos(data.c_str(), _brk1, 0);
Expand All @@ -140,6 +143,26 @@ FirebaseJson &FirebaseJson::setJsonData(const String &data)
p1 += 1;
if (p1 != -1 && p2 != -1)
_rawbuf = data.substring(p1, p2).c_str();
else
{
p1 = _strpos(data.c_str(), _brk3, 0);
p2 = _rstrpos(data.c_str(), _brk4, data.length() - 1);
if (p1 != -1)
p1 += 1;
if (p1 != -1 && p2 != -1)
{
char *_r = _getPGMString(FirebaseJson_STR_21);
_rawbuf = _r;
_rawbuf += data.c_str();
_delPtr(_r);
_topLevelTkType = JSMN_ARRAY;
}
else
{
_rawbuf = data.c_str();
_topLevelTkType = JSMN_PRIMITIVE;
}
}
}
else
_rawbuf.clear();
Expand All @@ -155,6 +178,7 @@ FirebaseJson &FirebaseJson::clear()
clearPathTk();
_tokens.reset();
_tokens = nullptr;
_topLevelTkType = JSMN_OBJECT;
return *this;
}

Expand Down Expand Up @@ -302,7 +326,7 @@ char *FirebaseJson::getFloatString(float value)
char *FirebaseJson::getIntString(int value)
{
char *buf = _newPtr(36);
itoa(value, buf, 10);
sprintf(buf, "%d", value);
return buf;
}

Expand Down Expand Up @@ -358,17 +382,24 @@ void FirebaseJson::_toStdString(std::string &s, bool isJson)
s.clear();
size_t bufSize = 10;
char *buf = _newPtr(bufSize);
if (isJson)
strcat(buf, _brk1);
else
strcat(buf, _brk3);
if (_topLevelTkType != JSMN_PRIMITIVE)
{
if (isJson)
strcat(buf, _brk1);
else
strcat(buf, _brk3);
}

s += buf;
s += _rawbuf;
buf = _newPtr(buf, bufSize);
if (isJson)
strcat(buf, _brk2);
else
strcat(buf, _brk4);
if (_topLevelTkType != JSMN_PRIMITIVE)
{
if (isJson)
strcat(buf, _brk2);
else
strcat(buf, _brk4);
}
s += buf;
_delPtr(buf);
}
Expand Down Expand Up @@ -1860,8 +1891,24 @@ void FirebaseJson::_trim(std::string &str, const std::string &chars)
void FirebaseJson::_parse(const char *path, PRINT_MODE printMode)
{
clearPathTk();
_strToTk(path, _pathTk, '/');
std::string _path;

if (_topLevelTkType == JSMN_ARRAY)
{
char *_root = _getPGMString(FirebaseJson_STR_26);
char *_slash = _getPGMString(FirebaseJson_STR_27);
_path = _root;
_path += _slash;
_path += path;
_delPtr(_root);
_delPtr(_slash);
}
else
_path = path;

_strToTk(_path.c_str(), _pathTk, '/');
_fbjs_parse();
std::string().swap(_path);
if (!_jsonData.success)
return;
_jsonData.success = false;
Expand Down Expand Up @@ -2185,8 +2232,24 @@ void FirebaseJson::_setArray(const std::string &path, FirebaseJsonArray *arr)
void FirebaseJson::_set(const char *path, const char *data)
{
clearPathTk();
_strToTk(path, _pathTk, '/');
std::string _path;

if (_topLevelTkType == JSMN_ARRAY)
{
char *_root = _getPGMString(FirebaseJson_STR_26);
char *_slash = _getPGMString(FirebaseJson_STR_27);
_path = _root;
_path += _slash;
_path += path;
_delPtr(_root);
_delPtr(_slash);
}
else
_path = path;

_strToTk(_path.c_str(), _pathTk, '/');
_fbjs_parse();
std::string().swap(_path);
if (!_jsonData.success)
return;
_jsonData.success = false;
Expand Down Expand Up @@ -2262,10 +2325,27 @@ void FirebaseJson::_set(const char *path, const char *data)
bool FirebaseJson::remove(const String &path)
{
clearPathTk();
_strToTk(path.c_str(), _pathTk, '/');
std::string _path;

if (_topLevelTkType == JSMN_ARRAY)
{
char *_root = _getPGMString(FirebaseJson_STR_26);
char *_slash = _getPGMString(FirebaseJson_STR_27);
_path = _root;
_path += _slash;
_path += path.c_str();
_delPtr(_root);
_delPtr(_slash);
}
else
_path = path.c_str();

_strToTk(_path.c_str(), _pathTk, '/');
_fbjs_parse();
std::string().swap(_path);
if (!_jsonData.success)
return false;

_jsonData.success = false;
char *nbuf = _newPtr(2);
int len = _pathTk.size();
Expand Down Expand Up @@ -2756,6 +2836,9 @@ int FirebaseJson::fbjs_parse(fbjs_parser *parser, const char *js, size_t len,
token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
token->start = parser->pos;
parser->toksuper = parser->toknext - 1;
if (parser->pos > 0)
if (js[parser->pos - 1] == '{' && js[parser->pos] == '[')
return JSMN_ERROR_INVAL;
break;
case '}':
case ']':
Expand Down Expand Up @@ -2873,6 +2956,7 @@ int FirebaseJson::fbjs_parse(fbjs_parser *parser, const char *js, size_t len,
case 't':
case 'f':
case 'n':

/* And they must not be keys of the object */
if (tokens != NULL && parser->toksuper != -1)
{
Expand All @@ -2887,6 +2971,7 @@ int FirebaseJson::fbjs_parse(fbjs_parser *parser, const char *js, size_t len,
/* In non-strict mode every unquoted value is a primitive */
default:
#endif

r = fbjs_parse_primitive(parser, js, len, tokens, num_tokens);
if (r < 0)
return r;
Expand Down Expand Up @@ -3272,7 +3357,7 @@ char *FirebaseJsonArray::getFloatString(float value)
char *FirebaseJsonArray::getIntString(int value)
{
char *buf = _newPtr(36);
itoa(value, buf, 10);
sprintf(buf, "%d", value);
return buf;
}

Expand Down Expand Up @@ -3344,7 +3429,7 @@ void FirebaseJsonArray::_set2(int index, const char *value, bool isStr)
{
char *tmp = _newPtr(50);
std::string path = _brk3;
itoa(index, tmp, 10);
sprintf(tmp, "%d", index);
path += tmp;
path += _brk4;
_set(path.c_str(), value, isStr);
Expand Down
5 changes: 3 additions & 2 deletions src/FirebaseJson.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* FirebaseJson, version 2.3.5
* FirebaseJson, version 2.3.6
*
* The Easiest ESP8266/ESP32 Arduino library for parse, create and edit JSON object using a relative path.
*
* July 11, 2020
* July 12, 2020
*
* Features
* - None recursive operations
Expand Down Expand Up @@ -645,6 +645,7 @@ class FirebaseJson
bool _paresRes = false;
bool _arrReplaced = false;
bool _arrInserted = false;
fbjs_type_t _topLevelTkType = JSMN_OBJECT;

char *_qt = nullptr;
char *_tab = nullptr;
Expand Down

0 comments on commit 3e2f82d

Please sign in to comment.