forked from bblanchon/ArduinoJson
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
9f07cdcabf | |||
41b5bba939 | |||
7ac4a91591 | |||
5534635feb | |||
2e5b959e8b | |||
9ee3dc638d | |||
ccb97fc6e0 | |||
39c185ae67 | |||
cf1324c09b | |||
0b087b7bdb | |||
dde5a2510b | |||
a42b03ec26 | |||
2f98e59fc6 | |||
f58a8b0ca5 |
14
CHANGELOG.md
14
CHANGELOG.md
@ -1,10 +1,16 @@
|
|||||||
Arduino JSON library: change log
|
Arduino JSON: change log
|
||||||
================================
|
========================
|
||||||
|
|
||||||
|
v2.1
|
||||||
|
----
|
||||||
|
|
||||||
|
* Fixed case `#include "jsmn.cpp"` which caused an error in Linux (issue #6)
|
||||||
|
* Fixed a buffer overrun in JSON Parser (issue #5)
|
||||||
|
|
||||||
v2.0
|
v2.0
|
||||||
----
|
----
|
||||||
|
|
||||||
* Added JSON encoding.
|
* Added JSON encoding (issue #2)
|
||||||
* Renamed the library `ArduinoJsonParser` becomes `ArduinoJson`
|
* Renamed the library `ArduinoJsonParser` becomes `ArduinoJson`
|
||||||
|
|
||||||
**Breaking change**: you need to add the following line at the top of your program.
|
**Breaking change**: you need to add the following line at the top of your program.
|
||||||
@ -14,7 +20,7 @@ v2.0
|
|||||||
v1.2
|
v1.2
|
||||||
----
|
----
|
||||||
|
|
||||||
* Example: changed `char[] json` into `char json[]`. Damn it C# !
|
* Fixed error in JSON parser example (issue #1)
|
||||||
|
|
||||||
v1.1
|
v1.1
|
||||||
----
|
----
|
||||||
|
@ -43,7 +43,7 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonValue* items;
|
Internals::JsonValue* items;
|
||||||
int count, capacity;
|
int capacity, count;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -55,8 +55,7 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
KeyValuePair* items;
|
KeyValuePair* items;
|
||||||
int count;
|
int capacity, count;
|
||||||
int capacity;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,4 +8,5 @@
|
|||||||
#include "JsonParser/JsonArray.cpp"
|
#include "JsonParser/JsonArray.cpp"
|
||||||
#include "JsonParser/JsonHashTable.cpp"
|
#include "JsonParser/JsonHashTable.cpp"
|
||||||
#include "JsonParser/JsonObjectBase.cpp"
|
#include "JsonParser/JsonObjectBase.cpp"
|
||||||
#include "JsonParser/Jsmn.cpp"
|
#include "JsonParser/JsonParserBase.cpp"
|
||||||
|
#include "JsonParser/jsmn.cpp"
|
@ -15,9 +15,7 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
class JsonArray : public JsonObjectBase
|
class JsonArray : public JsonObjectBase
|
||||||
{
|
{
|
||||||
template <int N>
|
friend class JsonParserBase;
|
||||||
friend class JsonParser;
|
|
||||||
|
|
||||||
friend class JsonHashTable;
|
friend class JsonHashTable;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -15,9 +15,7 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
class JsonHashTable : public JsonObjectBase
|
class JsonHashTable : public JsonObjectBase
|
||||||
{
|
{
|
||||||
template <int N>
|
friend class JsonParserBase;
|
||||||
friend class JsonParser;
|
|
||||||
|
|
||||||
friend class JsonArray;
|
friend class JsonArray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -10,58 +10,58 @@ using namespace ArduinoJson::Parser;
|
|||||||
|
|
||||||
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
|
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
|
||||||
{
|
{
|
||||||
int end = token->end;
|
int tokensToVisit = token->size;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
token++;
|
while (tokensToVisit)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
token++;
|
||||||
|
tokensToVisit--;
|
||||||
|
tokensToVisit += token->size;
|
||||||
|
}
|
||||||
|
|
||||||
while (token->start < end)
|
return count;
|
||||||
{
|
|
||||||
token++;
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JsonObjectBase::getBoolFromToken(jsmntok_t* token)
|
bool JsonObjectBase::getBoolFromToken(jsmntok_t* token)
|
||||||
{
|
{
|
||||||
if (token->type != JSMN_PRIMITIVE) return 0;
|
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
|
||||||
|
|
||||||
// "true"
|
// "true"
|
||||||
if (json[token->start] == 't') return true;
|
if (json[token->start] == 't') return true;
|
||||||
|
|
||||||
// "false"
|
// "false"
|
||||||
if (json[token->start] == 'f') return false;
|
if (json[token->start] == 'f') return false;
|
||||||
|
|
||||||
// "null"
|
// "null"
|
||||||
if (json[token->start] == 'n') return false;
|
if (json[token->start] == 'n') return false;
|
||||||
|
|
||||||
// number
|
// number
|
||||||
return strtol(json + token->start, 0, 0) != 0;
|
return strtol(json + token->start, 0, 0) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
double JsonObjectBase::getDoubleFromToken(jsmntok_t* token)
|
double JsonObjectBase::getDoubleFromToken(jsmntok_t* token)
|
||||||
{
|
{
|
||||||
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
|
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
|
||||||
|
|
||||||
return strtod(json + token->start, 0);
|
return strtod(json + token->start, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
long JsonObjectBase::getLongFromToken(jsmntok_t* token)
|
long JsonObjectBase::getLongFromToken(jsmntok_t* token)
|
||||||
{
|
{
|
||||||
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
|
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
|
||||||
|
|
||||||
return strtol(json + token->start, 0, 0);
|
return strtol(json + token->start, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
|
char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
|
||||||
{
|
{
|
||||||
if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
|
if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// add null terminator to the string
|
// add null terminator to the string
|
||||||
json[token->end] = 0;
|
json[token->end] = 0;
|
||||||
|
|
||||||
return json + token->start;
|
return json + token->start;
|
||||||
}
|
}
|
@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonHashTable.h"
|
#include "JsonParserBase.h"
|
||||||
#include "JsonArray.h"
|
|
||||||
|
|
||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
@ -26,45 +25,15 @@ namespace ArduinoJson
|
|||||||
* longer life that the JsonParser.
|
* longer life that the JsonParser.
|
||||||
*/
|
*/
|
||||||
template <int MAX_TOKENS>
|
template <int MAX_TOKENS>
|
||||||
class JsonParser
|
class JsonParser : public JsonParserBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
JsonParser()
|
||||||
/*
|
: JsonParserBase(tokens, MAX_TOKENS)
|
||||||
* 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:
|
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];
|
jsmntok_t tokens[MAX_TOKENS];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
19
JsonParser/JsonParserBase.cpp
Normal file
19
JsonParser/JsonParserBase.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "JsonParserBase.h"
|
||||||
|
|
||||||
|
using namespace ArduinoJson::Parser;
|
||||||
|
|
||||||
|
jsmntok_t* JsonParserBase::parse(char* json)
|
||||||
|
{
|
||||||
|
jsmn_parser parser;
|
||||||
|
jsmn_init(&parser);
|
||||||
|
|
||||||
|
if (JSMN_SUCCESS != jsmn_parse(&parser, json, tokens, maxTokens))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
}
|
53
JsonParser/JsonParserBase.h
Normal file
53
JsonParser/JsonParserBase.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "JsonHashTable.h"
|
||||||
|
#include "JsonArray.h"
|
||||||
|
|
||||||
|
namespace ArduinoJson
|
||||||
|
{
|
||||||
|
namespace Parser
|
||||||
|
{
|
||||||
|
class JsonParserBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
JsonParserBase(jsmntok_t* tokens, int maxTokens)
|
||||||
|
: tokens(tokens), maxTokens(maxTokens)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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* tokens;
|
||||||
|
int maxTokens;
|
||||||
|
|
||||||
|
jsmntok_t* parse(char* json);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -210,7 +210,7 @@ This table is for an 8-bit Arduino, types would be bigger on a 32-bit processor.
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Parser<N></td>
|
<td>Parser<N></td>
|
||||||
<td>8 x N</td>
|
<td>4 + 8 x N</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>JsonArray</td>
|
<td>JsonArray</td>
|
||||||
|
@ -13,7 +13,7 @@ using namespace ArduinoJson::Parser;
|
|||||||
|
|
||||||
namespace ArduinoJsonParserTests
|
namespace ArduinoJsonParserTests
|
||||||
{
|
{
|
||||||
TEST_CLASS(TestGbathreeSample1)
|
TEST_CLASS(GbathreeBug)
|
||||||
{
|
{
|
||||||
char json[1024];
|
char json[1024];
|
||||||
JsonParser<200> parser;
|
JsonParser<200> parser;
|
195
JsonParserTests/JsonArrayTests.cpp
Normal file
195
JsonParserTests/JsonArrayTests.cpp
Normal file
@ -0,0 +1,195 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "CppUnitTest.h"
|
||||||
|
#include "JsonParser.h"
|
||||||
|
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
using namespace ArduinoJson::Parser;
|
||||||
|
|
||||||
|
namespace ArduinoJsonParserTests
|
||||||
|
{
|
||||||
|
TEST_CLASS(JsonArrayTests)
|
||||||
|
{
|
||||||
|
JsonArray array;
|
||||||
|
char json[256];
|
||||||
|
jsmntok_t tokens[32];
|
||||||
|
JsonParserBase parser = JsonParserBase(tokens, 32);
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
TEST_METHOD(EmptyString)
|
||||||
|
{
|
||||||
|
whenInputIs("");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TooFewClosingBrackets)
|
||||||
|
{
|
||||||
|
whenInputIs("[[]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TooManyClosingBrackets)
|
||||||
|
{
|
||||||
|
whenInputIs("[]]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(EmptyArray)
|
||||||
|
{
|
||||||
|
whenInputIs("[]");
|
||||||
|
parseMustSucceed();
|
||||||
|
lengthMustBe(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(NotEnoughTokens)
|
||||||
|
{
|
||||||
|
setTokenCountTo(2);
|
||||||
|
|
||||||
|
whenInputIs("[1,2]");
|
||||||
|
|
||||||
|
parseMustFail();
|
||||||
|
itemMustNotExist(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoIntegers)
|
||||||
|
{
|
||||||
|
setTokenCountTo(3);
|
||||||
|
|
||||||
|
whenInputIs("[1,2]");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
lengthMustBe(2);
|
||||||
|
itemMustBe(0, 1L);
|
||||||
|
itemMustBe(1, 2L);
|
||||||
|
itemMustNotExist(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoBooleans)
|
||||||
|
{
|
||||||
|
setTokenCountTo(3);
|
||||||
|
|
||||||
|
whenInputIs("[true,false]");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
lengthMustBe(2);
|
||||||
|
itemMustBe(0, true);
|
||||||
|
itemMustBe(1, false);
|
||||||
|
itemMustNotExist(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoStrings)
|
||||||
|
{
|
||||||
|
setTokenCountTo(3);
|
||||||
|
|
||||||
|
whenInputIs("[\"hello\",\"world\"]");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
lengthMustBe(2);
|
||||||
|
itemMustBe(0, "hello");
|
||||||
|
itemMustBe(1, "world");
|
||||||
|
itemMustNotExist(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoDimensionsArray)
|
||||||
|
{
|
||||||
|
setTokenCountTo(7);
|
||||||
|
|
||||||
|
whenInputIs("[[1,2],[3,4]]");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
lengthMustBe(2);
|
||||||
|
itemMustBe(0, 0, 1L);
|
||||||
|
itemMustBe(0, 1, 2L);
|
||||||
|
itemMustBe(1, 0, 3L);
|
||||||
|
itemMustBe(1, 1, 4L);
|
||||||
|
itemMustNotExist(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(ThreeDimensionsArray)
|
||||||
|
{
|
||||||
|
setTokenCountTo(15);
|
||||||
|
|
||||||
|
whenInputIs("[[[1,2],[3,4]],[[5,6],[7,8]]]");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
lengthMustBe(2);
|
||||||
|
itemMustBe(0, 0, 0, 1L);
|
||||||
|
itemMustBe(0, 0, 1, 2L);
|
||||||
|
itemMustBe(0, 1, 0, 3L);
|
||||||
|
itemMustBe(0, 1, 1, 4L);
|
||||||
|
itemMustBe(1, 0, 0, 5L);
|
||||||
|
itemMustBe(1, 0, 1, 6L);
|
||||||
|
itemMustBe(1, 1, 0, 7L);
|
||||||
|
itemMustBe(1, 1, 1, 8L);
|
||||||
|
itemMustNotExist(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void setTokenCountTo(int n)
|
||||||
|
{
|
||||||
|
parser = JsonParserBase(tokens, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void whenInputIs(const char* input)
|
||||||
|
{
|
||||||
|
strcpy(json, input);
|
||||||
|
array = parser.parseArray(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseMustFail()
|
||||||
|
{
|
||||||
|
Assert::IsFalse(array.success());
|
||||||
|
lengthMustBe(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseMustSucceed()
|
||||||
|
{
|
||||||
|
Assert::IsTrue(array.success());
|
||||||
|
}
|
||||||
|
|
||||||
|
void lengthMustBe(int expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, array.getLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(int index, long expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, array.getLong(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(int index, bool expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, array.getBool(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(int index, const char* expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, array.getString(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(int index0, int index1, long expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, array.getArray(index0).getLong(index1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(int index0, int index1, int index2, long expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, array.getArray(index0).getArray(index1).getLong(index2));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustNotExist(int index)
|
||||||
|
{
|
||||||
|
Assert::IsFalse(array.getHashTable(index).success());
|
||||||
|
Assert::IsFalse(array.getArray(index).success());
|
||||||
|
Assert::IsFalse(array.getBool(index));
|
||||||
|
Assert::AreEqual(0.0, array.getDouble(index));
|
||||||
|
Assert::AreEqual(0L, array.getLong(index));
|
||||||
|
Assert::IsNull(array.getString(index));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
171
JsonParserTests/JsonHashTableTests.cpp
Normal file
171
JsonParserTests/JsonHashTableTests.cpp
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "CppUnitTest.h"
|
||||||
|
#include "JsonParser.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
using namespace ArduinoJson::Parser;
|
||||||
|
|
||||||
|
namespace ArduinoJsonParserTests
|
||||||
|
{
|
||||||
|
TEST_CLASS(JsonHashTableTests)
|
||||||
|
{
|
||||||
|
JsonHashTable hashTable;
|
||||||
|
JsonArray nestedArray;
|
||||||
|
char json[256];
|
||||||
|
jsmntok_t tokens[32];
|
||||||
|
JsonParserBase parser = JsonParserBase(tokens, 32);
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
TEST_METHOD(EmptyString)
|
||||||
|
{
|
||||||
|
whenInputIs("");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(EmptyHashTable)
|
||||||
|
{
|
||||||
|
whenInputIs("{}");
|
||||||
|
parseMustSucceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(NotEnoughTokens)
|
||||||
|
{
|
||||||
|
setTokenCountTo(2);
|
||||||
|
|
||||||
|
whenInputIs("{\"key\":0}");
|
||||||
|
|
||||||
|
parseMustFail();
|
||||||
|
itemMustNotExist("key");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoIntegers)
|
||||||
|
{
|
||||||
|
setTokenCountTo(5);
|
||||||
|
|
||||||
|
whenInputIs("{\"key1\":1,\"key2\":2}");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
itemMustBe("key1", 1L);
|
||||||
|
itemMustBe("key2", 2L);
|
||||||
|
itemMustNotExist("key3");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoBooleans)
|
||||||
|
{
|
||||||
|
setTokenCountTo(5);
|
||||||
|
|
||||||
|
whenInputIs("{\"key1\":true,\"key2\":false}");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
itemMustBe("key1", true);
|
||||||
|
itemMustBe("key2", false);
|
||||||
|
itemMustNotExist("key3");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoStrings)
|
||||||
|
{
|
||||||
|
setTokenCountTo(5);
|
||||||
|
|
||||||
|
whenInputIs("{\"key1\":\"hello\",\"key2\":\"world\"}");
|
||||||
|
|
||||||
|
parseMustSucceed();
|
||||||
|
itemMustBe("key1", "hello");
|
||||||
|
itemMustBe("key2", "world");
|
||||||
|
itemMustNotExist("key3");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(TwoNestedArrays)
|
||||||
|
{
|
||||||
|
setTokenCountTo(9);
|
||||||
|
|
||||||
|
whenInputIs("{\"key1\":[1,2],\"key2\":[3,4]}");
|
||||||
|
parseMustSucceed();
|
||||||
|
|
||||||
|
itemMustBeAnArray("key1");
|
||||||
|
arrayLengthMustBe(2);
|
||||||
|
arrayItemMustBe(0, 1L);
|
||||||
|
arrayItemMustBe(1, 2L);
|
||||||
|
arrayItemMustBe(2, 0L);
|
||||||
|
|
||||||
|
itemMustBeAnArray("key2");
|
||||||
|
arrayLengthMustBe(2);
|
||||||
|
arrayItemMustBe(0, 3L);
|
||||||
|
arrayItemMustBe(1, 4L);
|
||||||
|
arrayItemMustBe(2, 0L);
|
||||||
|
|
||||||
|
itemMustNotExist("key3");
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void setTokenCountTo(int n)
|
||||||
|
{
|
||||||
|
parser = JsonParserBase(tokens, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void whenInputIs(const char* input)
|
||||||
|
{
|
||||||
|
strcpy(json, input);
|
||||||
|
hashTable = parser.parseHashTable(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseMustFail()
|
||||||
|
{
|
||||||
|
Assert::IsFalse(hashTable.success());
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseMustSucceed()
|
||||||
|
{
|
||||||
|
Assert::IsTrue(hashTable.success());
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(const char* key, long expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, hashTable.getLong(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(const char* key, bool expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, hashTable.getBool(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBe(const char* key, const char* expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, hashTable.getString(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustNotExist(const char* key)
|
||||||
|
{
|
||||||
|
Assert::IsFalse(hashTable.containsKey(key));
|
||||||
|
Assert::IsFalse(hashTable.getHashTable(key).success());
|
||||||
|
Assert::IsFalse(hashTable.getArray(key).success());
|
||||||
|
Assert::IsFalse(hashTable.getBool(key));
|
||||||
|
Assert::AreEqual(0.0, hashTable.getDouble(key));
|
||||||
|
Assert::AreEqual(0L, hashTable.getLong(key));
|
||||||
|
Assert::IsNull(hashTable.getString(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
void itemMustBeAnArray(const char* key)
|
||||||
|
{
|
||||||
|
nestedArray = hashTable.getArray(key);
|
||||||
|
Assert::IsTrue(nestedArray.success());
|
||||||
|
}
|
||||||
|
|
||||||
|
void arrayLengthMustBe(int expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, nestedArray.getLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
void arrayItemMustBe(int index, long expected)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected, nestedArray.getLong(index));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -89,10 +89,10 @@
|
|||||||
<ClCompile Include="..\JsonParser\JsonArray.cpp" />
|
<ClCompile Include="..\JsonParser\JsonArray.cpp" />
|
||||||
<ClCompile Include="..\JsonParser\JsonHashTable.cpp" />
|
<ClCompile Include="..\JsonParser\JsonHashTable.cpp" />
|
||||||
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp" />
|
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp" />
|
||||||
<ClCompile Include="TestArrayExample.cpp" />
|
<ClCompile Include="..\JsonParser\JsonParserBase.cpp" />
|
||||||
<ClCompile Include="TestArrays.cpp" />
|
<ClCompile Include="JsonArrayTests.cpp" />
|
||||||
<ClCompile Include="TestHashTableExample.cpp" />
|
<ClCompile Include="JsonHashTableTests.cpp" />
|
||||||
<ClCompile Include="TestGbathreeStrings.cpp" />
|
<ClCompile Include="GbathreeBug.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\JsonParser\jsmn.h" />
|
<ClInclude Include="..\JsonParser\jsmn.h" />
|
||||||
@ -100,6 +100,7 @@
|
|||||||
<ClInclude Include="..\JsonParser\JsonHashTable.h" />
|
<ClInclude Include="..\JsonParser\JsonHashTable.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonObjectBase.h" />
|
<ClInclude Include="..\JsonParser\JsonObjectBase.h" />
|
||||||
<ClInclude Include="..\JsonParser\JsonParser.h" />
|
<ClInclude Include="..\JsonParser\JsonParser.h" />
|
||||||
|
<ClInclude Include="..\JsonParser\JsonParserBase.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -15,18 +15,6 @@
|
|||||||
</Filter>
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="TestHashTableExample.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="TestArrayExample.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="TestGbathreeStrings.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="TestArrays.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="..\JsonParser\jsmn.cpp">
|
<ClCompile Include="..\JsonParser\jsmn.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -39,6 +27,18 @@
|
|||||||
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp">
|
<ClCompile Include="..\JsonParser\JsonObjectBase.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="JsonArrayTests.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="GbathreeBug.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="JsonHashTableTests.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\JsonParser\JsonParserBase.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\JsonParser\jsmn.h">
|
<ClInclude Include="..\JsonParser\jsmn.h">
|
||||||
@ -56,5 +56,8 @@
|
|||||||
<ClInclude Include="..\JsonParser\JsonParser.h">
|
<ClInclude Include="..\JsonParser\JsonParser.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\JsonParser\JsonParserBase.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
* Arduino JSON library
|
|
||||||
* Benoit Blanchon 2014 - MIT License
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "CppUnitTest.h"
|
|
||||||
#include "JsonParser.h"
|
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
|
||||||
using namespace ArduinoJson::Parser;
|
|
||||||
|
|
||||||
namespace ArduinoJsonParserTests
|
|
||||||
{
|
|
||||||
TEST_CLASS(TestArrayExample)
|
|
||||||
{
|
|
||||||
char json[128];
|
|
||||||
JsonParser<32> parser;
|
|
||||||
JsonArray array;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
TEST_METHOD_INITIALIZE(Initialize)
|
|
||||||
{
|
|
||||||
strcpy(json, "[[1.2,3.4],[5.6,7.8]]");
|
|
||||||
array = parser.parseArray(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(Array_Success_ReturnsTrue)
|
|
||||||
{
|
|
||||||
Assert::IsTrue(array.success());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(Array_GetLength_Returns2)
|
|
||||||
{
|
|
||||||
Assert::AreEqual(2, array.getLength());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(Array_GetArray0_ReturnsInnerArray0)
|
|
||||||
{
|
|
||||||
JsonArray innerArray = array.getArray(0);
|
|
||||||
|
|
||||||
Assert::AreEqual(2, innerArray.getLength());
|
|
||||||
Assert::AreEqual(1.2, innerArray.getDouble(0));
|
|
||||||
Assert::AreEqual(3.4, innerArray.getDouble(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(Array_GetArray1_ReturnsInnerArray1)
|
|
||||||
{
|
|
||||||
JsonArray innerArray = array.getArray(1);
|
|
||||||
|
|
||||||
Assert::AreEqual(2, innerArray.getLength());
|
|
||||||
Assert::AreEqual(5.6, innerArray.getDouble(0));
|
|
||||||
Assert::AreEqual(7.8, innerArray.getDouble(1));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
@ -1,147 +0,0 @@
|
|||||||
/*
|
|
||||||
* Arduino JSON library
|
|
||||||
* Benoit Blanchon 2014 - MIT License
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "CppUnitTest.h"
|
|
||||||
#include "JsonParser.h"
|
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
|
||||||
using namespace ArduinoJson::Parser;
|
|
||||||
|
|
||||||
namespace ArduinoJsonParserTests
|
|
||||||
{
|
|
||||||
TEST_CLASS(TestArrays)
|
|
||||||
{
|
|
||||||
JsonParser<32> parser;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
TEST_METHOD(EmptyString)
|
|
||||||
{
|
|
||||||
char json[] = "";
|
|
||||||
|
|
||||||
JsonArray array = parser.parseArray(json);
|
|
||||||
Assert::IsFalse(array.success());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(EmptyArray)
|
|
||||||
{
|
|
||||||
char json[] = "[]";
|
|
||||||
|
|
||||||
JsonArray array = parser.parseArray(json);
|
|
||||||
Assert::IsTrue(array.success());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(TooFewClosingBrackets)
|
|
||||||
{
|
|
||||||
char json[] = "[[]";
|
|
||||||
|
|
||||||
JsonArray array = parser.parseArray(json);
|
|
||||||
Assert::IsFalse(array.success());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(TooManyClosingBrackets)
|
|
||||||
{
|
|
||||||
char json[] = "[]]";
|
|
||||||
|
|
||||||
JsonArray array = parser.parseArray(json);
|
|
||||||
Assert::IsFalse(array.success());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(OneDimensionArray)
|
|
||||||
{
|
|
||||||
char json [] = "[0,0]";
|
|
||||||
|
|
||||||
JsonArray array = parser.parseArray(json);
|
|
||||||
Assert::IsTrue(array.success());
|
|
||||||
Assert::AreEqual(2, array.getLength());
|
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++)
|
|
||||||
{
|
|
||||||
Assert::AreEqual(0L, array.getLong(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(TwoDimensionsArray)
|
|
||||||
{
|
|
||||||
char json[] = "[[0,0],[0,0]]";
|
|
||||||
|
|
||||||
JsonArray array1 = parser.parseArray(json);
|
|
||||||
Assert::IsTrue(array1.success());
|
|
||||||
Assert::AreEqual(2, array1.getLength());
|
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++)
|
|
||||||
{
|
|
||||||
JsonArray array2 = array1.getArray(i);
|
|
||||||
|
|
||||||
Assert::AreEqual(2, array2.getLength());
|
|
||||||
|
|
||||||
for (int j = 0; j < 2; j++)
|
|
||||||
{
|
|
||||||
Assert::AreEqual(0L, array2.getLong(j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(TreeDimensionsArray)
|
|
||||||
{
|
|
||||||
char json[] = "[[[0,0],[0,0]],[[0,0],[0,0]]]";
|
|
||||||
|
|
||||||
JsonArray array1 = parser.parseArray(json);
|
|
||||||
Assert::IsTrue(array1.success());
|
|
||||||
Assert::AreEqual(2, array1.getLength());
|
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++)
|
|
||||||
{
|
|
||||||
JsonArray array2 = array1.getArray(i);
|
|
||||||
|
|
||||||
Assert::AreEqual(2, array2.getLength());
|
|
||||||
|
|
||||||
for (int j = 0; j < 2; j++)
|
|
||||||
{
|
|
||||||
JsonArray array3 = array2.getArray(j);
|
|
||||||
|
|
||||||
Assert::AreEqual(2, array3.getLength());
|
|
||||||
|
|
||||||
for (int k = 0; k < 2; k++)
|
|
||||||
{
|
|
||||||
Assert::AreEqual(0L, array3.getLong(k));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(OneDimensionArrayInHashTable)
|
|
||||||
{
|
|
||||||
char json[] = "{a:[0,0],b:[0,0]}";
|
|
||||||
|
|
||||||
JsonHashTable root = parser.parseHashTable(json);
|
|
||||||
Assert::IsTrue(root.success());
|
|
||||||
|
|
||||||
JsonArray arrayA = root.getArray("a");
|
|
||||||
Assert::IsTrue(arrayA.success());
|
|
||||||
Assert::AreEqual(2, arrayA.getLength());
|
|
||||||
|
|
||||||
JsonArray arrayB = root.getArray("b");
|
|
||||||
Assert::IsTrue(arrayB.success());
|
|
||||||
Assert::AreEqual(2, arrayB.getLength());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(TwoDimensionsArrayInHashTable)
|
|
||||||
{
|
|
||||||
char json[] = "{a:[[0],[0]],b:[[0],[0]]}";
|
|
||||||
|
|
||||||
JsonHashTable root = parser.parseHashTable(json);
|
|
||||||
Assert::IsTrue(root.success());
|
|
||||||
|
|
||||||
JsonArray arrayA = root.getArray("a");
|
|
||||||
Assert::IsTrue(arrayA.success());
|
|
||||||
Assert::AreEqual(2, arrayA.getLength());
|
|
||||||
|
|
||||||
JsonArray arrayB = root.getArray("b");
|
|
||||||
Assert::IsTrue(arrayB.success());
|
|
||||||
Assert::AreEqual(2, arrayB.getLength());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
/*
|
|
||||||
* Arduino JSON library
|
|
||||||
* Benoit Blanchon 2014 - MIT License
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "CppUnitTest.h"
|
|
||||||
#include "JsonParser.h"
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
|
||||||
using namespace ArduinoJson::Parser;
|
|
||||||
|
|
||||||
namespace ArduinoJsonParserTests
|
|
||||||
{
|
|
||||||
TEST_CLASS(TestHashTableExample)
|
|
||||||
{
|
|
||||||
char json[128];
|
|
||||||
JsonParser<32> parser;
|
|
||||||
JsonHashTable hashTable;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
TEST_METHOD_INITIALIZE(Initialize)
|
|
||||||
{
|
|
||||||
strcpy(json, "{\"Name\":\"Blanchon\",\"Skills\":[\"C\",\"C++\",\"C#\"],\"Age\":32,\"Online\":true}");
|
|
||||||
hashTable = parser.parseHashTable(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(HashTable_Success_ReturnsTrue)
|
|
||||||
{
|
|
||||||
Assert::IsTrue(hashTable.success());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(HashTable_GetString_ReturnsExpectedValue)
|
|
||||||
{
|
|
||||||
string name = hashTable.getString("Name");
|
|
||||||
Assert::AreEqual(name, string("Blanchon"));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(HashTable_GetArray_ReturnsExpectedValue)
|
|
||||||
{
|
|
||||||
JsonArray skills = hashTable.getArray("Skills");
|
|
||||||
|
|
||||||
string skill0 = skills.getString(0);
|
|
||||||
Assert::AreEqual(skill0, string("C"));
|
|
||||||
|
|
||||||
string skill1 = skills.getString(1);
|
|
||||||
Assert::AreEqual(skill1, string("C++"));
|
|
||||||
|
|
||||||
string skill2 = skills.getString(2);
|
|
||||||
Assert::AreEqual(skill2, string("C#"));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(HashTable_GetLong_ReturnsExpectedValue)
|
|
||||||
{
|
|
||||||
int age = hashTable.getLong("Age");
|
|
||||||
Assert::AreEqual(32, age);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_METHOD(HashTable_GetBool_ReturnsExpectedValue)
|
|
||||||
{
|
|
||||||
bool online = hashTable.getBool("Online");
|
|
||||||
Assert::AreEqual(true, online);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
Reference in New Issue
Block a user