Files
ArduinoJson/JsonParser/JsonToken.h

100 lines
2.5 KiB
C
Raw Normal View History

2014-07-18 15:43:20 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "jsmn.h"
namespace ArduinoJson
{
2014-07-19 12:44:27 +02:00
namespace Parser
2014-07-18 15:43:20 +02:00
{
2014-07-22 20:14:25 +02:00
// A pointer to a JSON token
2014-07-18 15:43:20 +02:00
class JsonToken
{
public:
2014-07-22 20:14:25 +02:00
// Create a "null" pointer
2014-07-19 12:44:27 +02:00
JsonToken()
: token(0)
2014-07-18 15:43:20 +02:00
{
}
2014-07-22 20:14:25 +02:00
// Create a pointer to the specified JSON token
2014-07-19 12:44:27 +02:00
JsonToken(char* json, jsmntok_t* token)
2014-07-22 20:14:25 +02:00
: json(json), token(token)
2014-07-19 12:44:27 +02:00
{
}
2014-07-22 20:14:25 +02:00
// Get content of the JSON token
2014-07-19 12:44:27 +02:00
char* getText()
2014-07-18 15:43:20 +02:00
{
json[token->end] = 0;
return json + token->start;
}
2014-07-22 20:14:25 +02:00
// Get the number of children tokens
int childrenCount()
{
return token->size;
}
// Get a pointer to the first child of the current token
JsonToken firstChild() const
2014-07-18 15:43:20 +02:00
{
2014-07-19 12:44:27 +02:00
return JsonToken(json, token + 1);
2014-07-18 15:43:20 +02:00
}
2014-07-22 20:14:25 +02:00
// Get a pointer to the next sibling token (ie skiping the children tokens)
JsonToken nextSibling() const;
2014-07-18 15:43:20 +02:00
2014-07-22 20:14:25 +02:00
// Test equality
bool operator!=(const JsonToken& other) const
2014-07-18 15:43:20 +02:00
{
return token != other.token;
}
2014-07-22 20:14:25 +02:00
// Tell if the pointer is "null"
2014-07-18 15:43:20 +02:00
bool isValid()
{
return token != 0;
}
2014-07-22 20:14:25 +02:00
// Tell if the JSON token is a JSON object
2014-07-18 15:43:20 +02:00
bool isObject()
{
return token != 0 && token->type == JSMN_OBJECT;
}
2014-07-22 20:14:25 +02:00
// Tell if the JSON token is a JSON array
2014-07-18 15:43:20 +02:00
bool isArray()
{
return token != 0 && token->type == JSMN_ARRAY;
}
2014-07-22 20:14:25 +02:00
// Tell if the JSON token is a primitive
2014-07-18 15:43:20 +02:00
bool isPrimitive()
{
return token != 0 && token->type == JSMN_PRIMITIVE;
}
2014-07-22 20:14:25 +02:00
// Tell if the JSON token is a string
2014-07-18 15:43:20 +02:00
bool isString()
{
return token != 0 && token->type == JSMN_STRING;
}
2014-07-22 20:14:25 +02:00
// Explicit wait to create a "null" JsonToken
static JsonToken null()
2014-07-18 15:43:20 +02:00
{
2014-07-22 20:14:25 +02:00
return JsonToken();
2014-07-18 15:43:20 +02:00
}
private:
2014-07-19 12:44:27 +02:00
char* json;
2014-07-18 15:43:20 +02:00
jsmntok_t* token;
};
}
2014-07-22 20:14:25 +02:00
}