Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>json-streaming-parser</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
131 changes: 131 additions & 0 deletions ElementPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**The MIT License (MIT)

Contributors:
Stefano Chizzolini

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <Arduino.h>
#include "ElementPath.h"

int ElementSelector::getIndex() {
return index;
}

const char* ElementSelector::getKey() {
return key;
}

bool ElementSelector::isObject() {
return index < 0;
}

void ElementSelector::reset() {
index = -1;
key[0] = '\0';
}

void ElementSelector::set(int index) {
this->index = index;
this->key[0] = '\0';
}

void ElementSelector::set(char* key) {
strcpy(this->key, key);
this->index = -1;
}

void ElementSelector::step() {
index++;
}

void ElementSelector::toString(char* buffer) {
if (index >= 0) {
sprintf(buffer, "%s[%d]", buffer, index);
} else {
strcat(buffer, key);
}
}

ElementSelector* ElementPath::get(int index) {
if (index >= count
|| (index < 0 && (index += count - 1) < 0))
return NULL;

return &selectors[index];
}

int ElementPath::getCount() {
return count;
}

ElementSelector* ElementPath::getCurrent() {
return current;
}

int ElementPath::getIndex() {
return getIndex(current);
}

int ElementPath::getIndex(int index) {
return getIndex(get(index));
}

int ElementPath::getIndex(ElementSelector* selector) {
return selector != NULL ? selector->index : -1;
}

const char* ElementPath::getKey() {
return current != NULL ? current->key : "\0";
}

const char* ElementPath::getKey(int index) {
return getKey(get(index));
}

const char* ElementPath::getKey(ElementSelector* selector) {
return selector != NULL ? selector->key : "\0";
}

ElementSelector* ElementPath::getParent() {
return get(-1);
}

void ElementPath::pop() {
if(count > 0) {
current = --count > 0 ? &selectors[count - 1] : NULL;
}
}

void ElementPath::push() {
(current = &selectors[count++])->reset();
}

void ElementPath::toString(char* buffer) {
if (count <= 0)
return;

for(int index = 0; index < count; index++) {
if(index > 0 && selectors[index].isObject()) {
strcat(buffer, ".");
}
selectors[index].toString(buffer);
}
}
134 changes: 134 additions & 0 deletions ElementPath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**The MIT License (MIT)

Contributors:
Stefano Chizzolini

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
Unified element selector.
Represents the handle associated to an element within either
an object (key) or an array (index).
*/
class ElementSelector {
friend class ElementPath;
friend class JsonStreamingParser;

private:
int index;
char key[20];

public:
int getIndex();

const char* getKey();

bool isObject();

/*
Builds the string representation of this node position within
its parent.
*/
void toString(char* buffer);

private:
void reset();

void set(int index);

void set(char* key);

/*
Advances to next index.
*/
void step();
};

/*
Hierarchical path to currently parsed element.
It eases element filtering, keeping track of the current node
position.
*/
class ElementPath {
friend class JsonStreamingParser;

private:
int count = 0;
ElementSelector* current;
ElementSelector selectors[20];

public:
/*
Gets the element selector at the given level.
*/
ElementSelector* get(int index);

int getCount();

/*
Gets current element selector.
*/
ElementSelector* getCurrent();

/*
Gets current element's index (in case of array).
*/
int getIndex();

int getIndex(int index);

/*
Gets current element's key (in case of object).
*/
const char* getKey();

const char* getKey(int index);

/*
Gets parent element selector.
*/
ElementSelector* getParent();

/*
Builds the full path corresponding to the current node position.

For example, "weather[0].id" corresponds to a 3-level hierarchy:
{
"weather" : [
{
"id" : ..., <===== HERE IT IS
... : ...
},
{ ... }
],
...
}
*/
void toString(char* buffer);

private:
int getIndex(ElementSelector* selector);

const char* getKey(ElementSelector* selector);

void pop();

void push();
};
Empty file added ElementValue.cpp
Empty file.
102 changes: 102 additions & 0 deletions ElementValue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <Arduino.h>

union Variant {
bool boolValue;
float numValue;
const char* stringValue;
};

struct ElementValue {
private:
static const int Type_Null = 0;
static const int Type_Int = 1;
static const int Type_Float = 2;
static const int Type_String = 3;
static const int Type_Bool = 4;

Variant data;
int type;

public:
ElementValue with(float value) {
data.numValue = value;
type = Type_Float;
return *this;
}

ElementValue with(long value) {
data.numValue = value;
type = Type_Int;
return *this;
}

ElementValue with(bool value) {
data.boolValue = value;
type = Type_Bool;
return *this;
}

ElementValue with(const char* value) {
data.stringValue = value;
type = Type_String;
return *this;
}

ElementValue with() {
type = Type_Null;
return *this;
}

bool getBool() {
return data.boolValue;
}

const char* getString() {
return data.stringValue;
}

float getFloat() {
return data.numValue;
}

long getInt() {
return (long)data.numValue;
}

bool isInt() {
return type == Type_Int;
}

bool isFloat() {
return type == Type_Float;
}

bool isString() {
return type == Type_String;
}

bool isBool() {
return type == Type_Bool;
}

bool isNull() {
return type == Type_Null;
}

char* toString(char* buffer) {
if(isInt()) {
sprintf(buffer, "%d", getInt());
} else if(isFloat()) {
sprintf(buffer, "%f", getFloat());
} else if(isString()) {
sprintf(buffer, "\"%s\"", getString());
} else if(isBool()) {
strcpy(buffer, getBool() ? "true" : "false");
} else if(isNull()) {
strcpy(buffer, "null");
} else {
strcpy(buffer, "?");
}
return buffer;
}
};
1 change: 0 additions & 1 deletion JsonListener.cpp
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

Loading