Files
ArduinoJson/ArduinoJsonParser.h

64 lines
906 B
C
Raw Normal View History

2014-01-10 16:55:12 +01:00
/*
* malloc-free JSON parser for Arduino
* Benoit Blanchon 2014
* MIT License
*/
#ifndef __ARDUINOJSONPARSER_H
#define __ARDUINOJSONPARSER_H
#include <Arduino.h>
#include "utility/jsmn.h"
2014-01-10 20:13:18 +01:00
class JsonParserBase
2014-01-10 16:55:12 +01:00
{
2014-01-10 20:13:18 +01:00
protected:
2014-01-10 20:19:58 +01:00
JsonParserBase(jsmntok_t* tokens, int tokenCount)
2014-01-10 16:55:12 +01:00
{
2014-01-10 20:19:58 +01:00
this->tokenCount = tokenCount;
this->tokens = tokens;
2014-01-10 16:55:12 +01:00
2014-01-10 20:19:58 +01:00
jsmn_init(&parser);
2014-01-10 16:55:12 +01:00
}
2014-01-10 20:19:58 +01:00
bool parseTokens(char* jsonString);
char* getValueByKey(char* name);
2014-01-10 16:55:12 +01:00
private:
char* buffer;
jsmn_parser parser;
2014-01-10 20:19:58 +01:00
int tokenCount;
jsmntok_t* tokens;
2014-01-10 20:13:18 +01:00
};
template <int N>
class ArduinoJsonParser : JsonParserBase
{
public:
2014-01-10 20:19:58 +01:00
ArduinoJsonParser()
: JsonParserBase(tokens, N * 2 + 1)
{
}
2014-01-10 20:13:18 +01:00
bool parse(char* json)
{
2014-01-10 20:19:58 +01:00
return parseTokens(json);
2014-01-10 20:13:18 +01:00
}
char* getValue(char* name)
{
2014-01-10 20:19:58 +01:00
return getValueByKey(name);
2014-01-10 20:13:18 +01:00
}
private:
2014-01-10 16:55:12 +01:00
jsmntok_t tokens[N * 2 + 1];
};
#endif