From 3d8b31b1ecd72255b3f370fe47fe27b626f2afa7 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 24 Jun 2014 12:53:22 +0200 Subject: [PATCH] Moved all JsonParser code in a sub-folder. --- JsonParser.cpp | 11 +++ JsonParser.h | 65 +---------------- JsonArray.cpp => JsonParser/JsonArray.cpp | 0 JsonArray.h => JsonParser/JsonArray.h | 0 .../JsonHashTable.cpp | 0 JsonHashTable.h => JsonParser/JsonHashTable.h | 0 .../JsonObjectBase.cpp | 0 .../JsonObjectBase.h | 2 +- JsonParser/JsonParser.h | 69 +++++++++++++++++++ README.md => JsonParser/README.md | 0 {utility => JsonParser}/jsmn.cpp | 0 {utility => JsonParser}/jsmn.h | 0 {tests => JsonParserTests}/.gitignore | 0 .../ArduinoJsonParserTests.sln | 0 .../ArduinoJsonParserTests.vcxproj | 22 +++--- .../ArduinoJsonParserTests.vcxproj.filters | 58 ++++++++-------- .../TestArrayExample.cpp | 0 {tests => JsonParserTests}/TestArrays.cpp | 0 .../TestGbathreeStrings.cpp | 0 JsonParserTests/TestHashGenerator.cpp | 25 +++++++ .../TestHashTableExample.cpp | 0 21 files changed, 147 insertions(+), 105 deletions(-) create mode 100644 JsonParser.cpp rename JsonArray.cpp => JsonParser/JsonArray.cpp (100%) rename JsonArray.h => JsonParser/JsonArray.h (100%) rename JsonHashTable.cpp => JsonParser/JsonHashTable.cpp (100%) rename JsonHashTable.h => JsonParser/JsonHashTable.h (100%) rename JsonObjectBase.cpp => JsonParser/JsonObjectBase.cpp (100%) rename JsonObjectBase.h => JsonParser/JsonObjectBase.h (90%) create mode 100644 JsonParser/JsonParser.h rename README.md => JsonParser/README.md (100%) rename {utility => JsonParser}/jsmn.cpp (100%) rename {utility => JsonParser}/jsmn.h (100%) rename {tests => JsonParserTests}/.gitignore (100%) rename {tests => JsonParserTests}/ArduinoJsonParserTests.sln (100%) rename {tests => JsonParserTests}/ArduinoJsonParserTests.vcxproj (89%) rename {tests => JsonParserTests}/ArduinoJsonParserTests.vcxproj.filters (76%) rename {tests => JsonParserTests}/TestArrayExample.cpp (100%) rename {tests => JsonParserTests}/TestArrays.cpp (100%) rename {tests => JsonParserTests}/TestGbathreeStrings.cpp (100%) create mode 100644 JsonParserTests/TestHashGenerator.cpp rename {tests => JsonParserTests}/TestHashTableExample.cpp (100%) diff --git a/JsonParser.cpp b/JsonParser.cpp new file mode 100644 index 00000000..718acdd9 --- /dev/null +++ b/JsonParser.cpp @@ -0,0 +1,11 @@ +/* +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ + +// This file is here to help the Arduino IDE find the .cpp files + +#include "JsonParser/JsonArray.cpp" +#include "JsonParser/JsonHashTable.cpp" +#include "JsonParser/JsonObjectBase.cpp" +#include "JsonParser/Jsmn.cpp" \ No newline at end of file diff --git a/JsonParser.h b/JsonParser.h index b977befd..52a28741 100644 --- a/JsonParser.h +++ b/JsonParser.h @@ -3,67 +3,4 @@ * Benoit Blanchon 2014 - MIT License */ -#ifndef __JSONPARSER_H -#define __JSONPARSER_H - -#include "JsonHashTable.h" -#include "JsonArray.h" - -/* -* The JSON parser. -* -* You need to specifiy the number of token to be allocated for that parser. -* Values from 16 to 32 are recommended. -* The parser size will be MAX_TOKEN*8 bytes. -* Don't forget that the memory size of standard Arduino board is only 2KB -* -* CAUTION: JsonArray and JsonHashTable contain pointers to tokens of the -* JsonParser, so they need the JsonParser to be in memory to work. -* As a result, you must not create JsonArray and JsonHashTable that have a -* longer life that the JsonParser. -*/ -template -class JsonParser -{ -public: - - /* - * Parse the JSON string and return a array. - * - * The content of the string may be altered to add '\0' at the - * end of string tokens - */ - JsonArray parseArray(char* json) - { - return JsonArray(json, parse(json)); - } - - /* - * Parse the JSON string and return a array. - * - * The content of the string may be altered to add '\0' at the - * end of string tokens - */ - JsonHashTable parseHashTable(char* json) - { - return JsonHashTable(json, parse(json)); - } - -private: - - jsmntok_t* parse(char* json) - { - jsmn_parser parser; - jsmn_init(&parser); - - if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, MAX_TOKENS)) - return 0; - - return tokens; - } - - jsmntok_t tokens[MAX_TOKENS]; -}; - -#endif - +#include "JsonParser/JsonParser.h" \ No newline at end of file diff --git a/JsonArray.cpp b/JsonParser/JsonArray.cpp similarity index 100% rename from JsonArray.cpp rename to JsonParser/JsonArray.cpp diff --git a/JsonArray.h b/JsonParser/JsonArray.h similarity index 100% rename from JsonArray.h rename to JsonParser/JsonArray.h diff --git a/JsonHashTable.cpp b/JsonParser/JsonHashTable.cpp similarity index 100% rename from JsonHashTable.cpp rename to JsonParser/JsonHashTable.cpp diff --git a/JsonHashTable.h b/JsonParser/JsonHashTable.h similarity index 100% rename from JsonHashTable.h rename to JsonParser/JsonHashTable.h diff --git a/JsonObjectBase.cpp b/JsonParser/JsonObjectBase.cpp similarity index 100% rename from JsonObjectBase.cpp rename to JsonParser/JsonObjectBase.cpp diff --git a/JsonObjectBase.h b/JsonParser/JsonObjectBase.h similarity index 90% rename from JsonObjectBase.h rename to JsonParser/JsonObjectBase.h index 091a968d..348a4e99 100644 --- a/JsonObjectBase.h +++ b/JsonParser/JsonObjectBase.h @@ -7,7 +7,7 @@ #ifndef __JSONOBJECTBASE_H #define __JSONOBJECTBASE_H -#include "utility/jsmn.h" +#include "jsmn.h" class JsonObjectBase { diff --git a/JsonParser/JsonParser.h b/JsonParser/JsonParser.h new file mode 100644 index 00000000..b977befd --- /dev/null +++ b/JsonParser/JsonParser.h @@ -0,0 +1,69 @@ +/* +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ + +#ifndef __JSONPARSER_H +#define __JSONPARSER_H + +#include "JsonHashTable.h" +#include "JsonArray.h" + +/* +* The JSON parser. +* +* You need to specifiy the number of token to be allocated for that parser. +* Values from 16 to 32 are recommended. +* The parser size will be MAX_TOKEN*8 bytes. +* Don't forget that the memory size of standard Arduino board is only 2KB +* +* CAUTION: JsonArray and JsonHashTable contain pointers to tokens of the +* JsonParser, so they need the JsonParser to be in memory to work. +* As a result, you must not create JsonArray and JsonHashTable that have a +* longer life that the JsonParser. +*/ +template +class JsonParser +{ +public: + + /* + * Parse the JSON string and return a array. + * + * The content of the string may be altered to add '\0' at the + * end of string tokens + */ + JsonArray parseArray(char* json) + { + return JsonArray(json, parse(json)); + } + + /* + * Parse the JSON string and return a array. + * + * The content of the string may be altered to add '\0' at the + * end of string tokens + */ + JsonHashTable parseHashTable(char* json) + { + return JsonHashTable(json, parse(json)); + } + +private: + + jsmntok_t* parse(char* json) + { + jsmn_parser parser; + jsmn_init(&parser); + + if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, MAX_TOKENS)) + return 0; + + return tokens; + } + + jsmntok_t tokens[MAX_TOKENS]; +}; + +#endif + diff --git a/README.md b/JsonParser/README.md similarity index 100% rename from README.md rename to JsonParser/README.md diff --git a/utility/jsmn.cpp b/JsonParser/jsmn.cpp similarity index 100% rename from utility/jsmn.cpp rename to JsonParser/jsmn.cpp diff --git a/utility/jsmn.h b/JsonParser/jsmn.h similarity index 100% rename from utility/jsmn.h rename to JsonParser/jsmn.h diff --git a/tests/.gitignore b/JsonParserTests/.gitignore similarity index 100% rename from tests/.gitignore rename to JsonParserTests/.gitignore diff --git a/tests/ArduinoJsonParserTests.sln b/JsonParserTests/ArduinoJsonParserTests.sln similarity index 100% rename from tests/ArduinoJsonParserTests.sln rename to JsonParserTests/ArduinoJsonParserTests.sln diff --git a/tests/ArduinoJsonParserTests.vcxproj b/JsonParserTests/ArduinoJsonParserTests.vcxproj similarity index 89% rename from tests/ArduinoJsonParserTests.vcxproj rename to JsonParserTests/ArduinoJsonParserTests.vcxproj index 6569011b..db741777 100644 --- a/tests/ArduinoJsonParserTests.vcxproj +++ b/JsonParserTests/ArduinoJsonParserTests.vcxproj @@ -84,22 +84,22 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/tests/ArduinoJsonParserTests.vcxproj.filters b/JsonParserTests/ArduinoJsonParserTests.vcxproj.filters similarity index 76% rename from tests/ArduinoJsonParserTests.vcxproj.filters rename to JsonParserTests/ArduinoJsonParserTests.vcxproj.filters index 4fc06abc..95c497e2 100644 --- a/tests/ArduinoJsonParserTests.vcxproj.filters +++ b/JsonParserTests/ArduinoJsonParserTests.vcxproj.filters @@ -15,35 +15,6 @@ - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - - - Source Files - - - Source Files - - - Source Files - - - Source Files - Source Files @@ -56,5 +27,34 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/tests/TestArrayExample.cpp b/JsonParserTests/TestArrayExample.cpp similarity index 100% rename from tests/TestArrayExample.cpp rename to JsonParserTests/TestArrayExample.cpp diff --git a/tests/TestArrays.cpp b/JsonParserTests/TestArrays.cpp similarity index 100% rename from tests/TestArrays.cpp rename to JsonParserTests/TestArrays.cpp diff --git a/tests/TestGbathreeStrings.cpp b/JsonParserTests/TestGbathreeStrings.cpp similarity index 100% rename from tests/TestGbathreeStrings.cpp rename to JsonParserTests/TestGbathreeStrings.cpp diff --git a/JsonParserTests/TestHashGenerator.cpp b/JsonParserTests/TestHashGenerator.cpp new file mode 100644 index 00000000..754e2119 --- /dev/null +++ b/JsonParserTests/TestHashGenerator.cpp @@ -0,0 +1,25 @@ +#include "stdafx.h" +#include "CppUnitTest.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace ArduinoJsonParserTests +{ + TEST_CLASS(TestHashGenerator) + { + public: + + TEST_METHOD(TestMethod1) + { + JsonArray<5> arr; + arr.Add(1); + arr.Add("Hi!"); + + JsonHashTable<4> hash; + hash.Add("key1", 1); + hash.Add("key2", "Hello!"); + hash.Add("key3", arr); + } + + }; +} \ No newline at end of file diff --git a/tests/TestHashTableExample.cpp b/JsonParserTests/TestHashTableExample.cpp similarity index 100% rename from tests/TestHashTableExample.cpp rename to JsonParserTests/TestHashTableExample.cpp