Răsfoiți Sursa

Splitted in several files

Benoit Blanchon 12 ani în urmă
părinte
comite
e924bef409
10 a modificat fișierele cu 325 adăugiri și 259 ștergeri
  1. 0 90
      ArduinoJsonParser.cpp
  2. 0 169
      ArduinoJsonParser.h
  3. 30 0
      JsonArray.cpp
  4. 51 0
      JsonArray.h
  5. 45 0
      JsonHashTable.cpp
  6. 45 0
      JsonHashTable.h
  7. 19 0
      JsonObjectBase.cpp
  8. 42 0
      JsonObjectBase.h
  9. 23 0
      JsonParser.cpp
  10. 70 0
      JsonParser.h

+ 0 - 90
ArduinoJsonParser.cpp

@@ -1,90 +0,0 @@
-/*
- * malloc-free JSON parser for Arduino
- * Benoit Blanchon 2014
- * MIT License
- */
-
-#include "ArduinoJsonParser.h"
-
-int JsonObjectBase::getNestedTokenCounts(int tokenIndex)
-{
-	int count = 0;
-
-	for (int i = 0; i < tokens[tokenIndex].size; i++)
-	{
-		count += 1 + getNestedTokenCounts(tokenIndex + 1 + i);
-	}
-
-	return count;
-}
-
-bool JsonParserBase::parse(char* jsonString)
-{
-	buffer = jsonString;
-
-	if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount))
-		return false;
-
-	// Add null termination to each token
-	for (int i = 1; i < parser.toknext; i++)
-	{
-		buffer[tokens[i].end] = 0;
-	}
-
-	return true;
-}
-
-jsmntok_t* JsonHashTable::getToken(char* name)
-{	
-	// skip first token, it's the whole object
-	int currentToken = 1;
-
-	// Scan each keys
-	for (int i = 0; i < tokens[0].size / 2 ; i++)
-	{
-		// Get key token string
-		char* key = json + tokens[currentToken].start;
-
-		// Compare with desired name
-		if (strcmp(name, key) == 0)
-		{
-			return &tokens[currentToken + 1];
-		}
-
-		// move forward: key + value + nested tokens
-		currentToken += 2 + getNestedTokenCounts(currentToken + 1);
-	}
-
-	return NULL;
-}
-
-JsonArray JsonHashTable::getArray(char* key)
-{
-	jsmntok_t* token = getToken(key);
-	return JsonArray(json, token);
-}
-
-jsmntok_t* JsonArray::getToken(int index)
-{
-	if (json == NULL) return NULL;
-	if (tokens == NULL) return NULL;
-	if (index < 0) return NULL;
-	if (index >= tokens[0].size) return NULL;
-
-	// skip first token, it's the whole object
-	int currentToken = 1;
-
-	for (int i = 0; i < index; i++)
-	{
-		// move forward: current + nested tokens
-		currentToken += 1 + getNestedTokenCounts(currentToken);
-	}
-
-	return &tokens[currentToken];
-}
-
-JsonArray JsonArray::getArray(int index)
-{
-	jsmntok_t* token = getToken(index);
-	return JsonArray(json, token);
-}

+ 0 - 169
ArduinoJsonParser.h

@@ -1,169 +0,0 @@
-/*
- * malloc-free JSON parser for Arduino
- * Benoit Blanchon 2014
- * MIT License
- */
-
-#ifndef __ARDUINOJSONPARSER_H
-#define __ARDUINOJSONPARSER_H
-
-#include <Arduino.h>
-#include "utility/jsmn.h"
-
-class JsonObjectBase
-{
-public:
-
-	JsonObjectBase()
-	{
-		json = NULL;
-		tokens = NULL;
-	}
-
-	bool success() 
-	{ 
-		return json != NULL && tokens != NULL;
-	}
-
-protected:
-	
-	JsonObjectBase(char* json, jsmntok_t* tokens)
-	{
-		this->json = json;
-		this->tokens = tokens;
-	}
-		
-	int getNestedTokenCounts(int tokenIndex);
-
-	char* json;
-	jsmntok_t* tokens;
-};
-
-class JsonArray;
-
-class JsonHashTable : public JsonObjectBase
-{
-	friend class JsonParserBase;
-
-public:
-
-	JsonHashTable()
-	{
-
-	}
-
-	char* getString(char* key)
-	{
-		jsmntok_t* token = getToken(key);
-		return token != NULL ? json + token->start : NULL;
-	}
-
-	JsonArray getArray(char* key);
-
-private:
-
-	JsonHashTable(char* json, jsmntok_t* tokens)
-		: JsonObjectBase(json, tokens)
-	{
-
-	}
-
-	jsmntok_t* getToken(char* key);
-};
-
-class JsonArray : public JsonObjectBase
-{
-	friend class JsonParserBase;
-	friend class JsonHashTable;
-
-public:
-
-public:
-
-	JsonArray()
-	{
-
-	}
-
-	JsonArray getArray(int index);
-
-	char* getString(int index)
-	{
-		jsmntok_t* token = getToken(index);
-		return token != NULL ? json + token->start : NULL;
-	}
-	
-	int getLength()
-	{
-		return tokens != NULL ? tokens[0].size : 0;
-	}
-
-private:
-
-	JsonArray(char* json, jsmntok_t* tokens)
-		: JsonObjectBase(json, tokens)
-	{
-
-	}
-
-	jsmntok_t* getToken(int index);
-};
-
-class JsonParserBase
-{
-public:
-
-	JsonArray parseArray(char* json)
-	{
-		if (!parse(json) || tokens[0].type != JSMN_ARRAY)
-			return JsonArray();
-
-		return JsonArray(json, tokens);
-	}
-
-	JsonHashTable parseHashTable(char* json)
-	{
-		if (!parse(json) || tokens[0].type != JSMN_OBJECT)
-			return JsonHashTable();
-
-		return JsonHashTable(json, tokens);
-	}
-	
-protected:
-
-	JsonParserBase(jsmntok_t* tokens, int maxTokenCount)
-	{
-		this->maxTokenCount = maxTokenCount;
-		this->tokens = tokens;
-
-		jsmn_init(&parser);
-	}	
-	
-	bool parse(char* json);
-
-private:
-
-	char* buffer;
-	jsmn_parser parser;
-	int maxTokenCount;
-	jsmntok_t* tokens;
-};
-
-template <int N>
-class JsonParser : public JsonParserBase
-{
-public:
-
-	JsonParser()
-		: JsonParserBase(tokens, N)
-	{
-
-	}
-
-private:
-	
-	jsmntok_t tokens[N];
-};
-
-#endif
-

+ 30 - 0
JsonArray.cpp

@@ -0,0 +1,30 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#include "JsonArray.h"
+
+jsmntok_t* JsonArray::getToken(int index)
+{
+	if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
+		return 0;
+
+	// skip first token, it's the whole object
+	int currentToken = 1;
+
+	for (int i = 0; i < index; i++)
+	{
+		// move forward: current + nested tokens
+		currentToken += 1 + getNestedTokenCounts(currentToken);
+	}
+
+	return &tokens[currentToken];
+}
+
+JsonArray JsonArray::getArray(int index)
+{
+	jsmntok_t* token = getToken(index);
+	return JsonArray(json, token);
+}

+ 51 - 0
JsonArray.h

@@ -0,0 +1,51 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#ifndef __JSONARRAY_H
+#define __JSONARRAY_H
+
+#include "JsonObjectBase.h"
+
+class JsonArray : public JsonObjectBase
+{
+	friend class JsonParserBase;
+	friend class JsonHashTable;
+
+public:
+
+public:
+
+	JsonArray()
+	{
+
+	}
+
+	JsonArray getArray(int index);
+
+	char* getString(int index)
+	{
+		jsmntok_t* token = getToken(index);
+		return token != 0 ? json + token->start : 0;
+	}
+	
+	int getLength()
+	{
+		return tokens != 0 ? tokens[0].size : 0;
+	}
+
+private:
+
+	JsonArray(char* json, jsmntok_t* tokens)
+		: JsonObjectBase(json, tokens)
+	{
+
+	}
+
+	jsmntok_t* getToken(int index);
+};
+
+#endif
+

+ 45 - 0
JsonHashTable.cpp

@@ -0,0 +1,45 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#include "JsonArray.h"
+#include "JsonHashTable.h"
+
+#include <string.h> // for strcmp()
+
+jsmntok_t* JsonHashTable::getToken(char* name)
+{	
+	// sanity check
+	if (json == 0 || tokens == 0 || name == 0)
+		return 0;
+
+	// skip first token, it's the whole object
+	int currentToken = 1;
+
+	// scan each keys
+	for (int i = 0; i < tokens[0].size / 2 ; i++)
+	{
+		// Get key token string
+		char* key = json + tokens[currentToken].start;
+
+		// Compare with desired name
+		if (strcmp(name, key) == 0)
+		{
+			return &tokens[currentToken + 1];
+		}
+
+		// move forward: key + value + nested tokens
+		currentToken += 2 + getNestedTokenCounts(currentToken + 1);
+	}
+
+	// nothing found, return NULL
+	return 0; 
+}
+
+JsonArray JsonHashTable::getArray(char* key)
+{
+	jsmntok_t* token = getToken(key);
+	return JsonArray(json, token);
+}

+ 45 - 0
JsonHashTable.h

@@ -0,0 +1,45 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#ifndef __JSONHASHTABLE_H
+#define __JSONHASHTABLE_H
+
+#include "JsonObjectBase.h"
+
+class JsonArray;
+
+class JsonHashTable : public JsonObjectBase
+{
+	friend class JsonParserBase;
+
+public:
+
+	JsonHashTable()
+	{
+
+	}
+
+	char* getString(char* key)
+	{
+		jsmntok_t* token = getToken(key);
+		return token != 0 ? json + token->start : 0;
+	}
+
+	JsonArray getArray(char* key);
+
+private:
+
+	JsonHashTable(char* json, jsmntok_t* tokens)
+		: JsonObjectBase(json, tokens)
+	{
+
+	}
+
+	jsmntok_t* getToken(char* key);
+};
+
+#endif
+

+ 19 - 0
JsonObjectBase.cpp

@@ -0,0 +1,19 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#include "JsonObjectBase.h"
+
+int JsonObjectBase::getNestedTokenCounts(int tokenIndex)
+{
+	int count = 0;
+
+	for (int i = 0; i < tokens[tokenIndex].size; i++)
+	{
+		count += 1 + getNestedTokenCounts(tokenIndex + 1 + i);
+	}
+
+	return count;
+}

+ 42 - 0
JsonObjectBase.h

@@ -0,0 +1,42 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#ifndef __JSONOBJECTBASE_H
+#define __JSONOBJECTBASE_H
+
+#include "utility/jsmn.h"
+
+class JsonObjectBase
+{
+public:
+
+	JsonObjectBase()
+	{
+		json = 0;
+		tokens = 0;
+	}
+
+	bool success() 
+	{ 
+		return json != 0 && tokens != 0;
+	}
+
+protected:
+	
+	JsonObjectBase(char* json, jsmntok_t* tokens)
+	{
+		this->json = json;
+		this->tokens = tokens;
+	}
+		
+	int getNestedTokenCounts(int tokenIndex);
+
+	char* json;
+	jsmntok_t* tokens;
+};
+
+#endif
+

+ 23 - 0
JsonParser.cpp

@@ -0,0 +1,23 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#include "JsonParser.h"
+
+bool JsonParserBase::parse(char* jsonString)
+{
+	buffer = jsonString;
+
+	if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount))
+		return false;
+
+	// Add null termination to each token
+	for (int i = 1; i < parser.toknext; i++)
+	{
+		buffer[tokens[i].end] = 0;
+	}
+
+	return true;
+}

+ 70 - 0
JsonParser.h

@@ -0,0 +1,70 @@
+/*
+ * malloc-free JSON parser for Arduino
+ * Benoit Blanchon 2014
+ * MIT License
+ */
+
+#ifndef __JSONPARSER_H
+#define __JSONPARSER_H
+
+#include "JsonHashTable.h"
+#include "JsonArray.h"
+
+class JsonParserBase
+{
+public:
+
+	JsonArray parseArray(char* json)
+	{
+		if (!parse(json) || tokens[0].type != JSMN_ARRAY)
+			return JsonArray();
+
+		return JsonArray(json, tokens);
+	}
+
+	JsonHashTable parseHashTable(char* json)
+	{
+		if (!parse(json) || tokens[0].type != JSMN_OBJECT)
+			return JsonHashTable();
+
+		return JsonHashTable(json, tokens);
+	}
+	
+protected:
+
+	JsonParserBase(jsmntok_t* tokens, int maxTokenCount)
+	{
+		this->maxTokenCount = maxTokenCount;
+		this->tokens = tokens;
+
+		jsmn_init(&parser);
+	}	
+	
+	bool parse(char* json);
+
+private:
+
+	char* buffer;
+	jsmn_parser parser;
+	int maxTokenCount;
+	jsmntok_t* tokens;
+};
+
+template <int N>
+class JsonParser : public JsonParserBase
+{
+public:
+
+	JsonParser()
+		: JsonParserBase(tokens, N)
+	{
+
+	}
+
+private:
+	
+	jsmntok_t tokens[N];
+};
+
+#endif
+