Added support of non standard JSON input (issue #44)

This commit is contained in:
Benoit Blanchon
2015-07-10 22:11:26 +02:00
parent 78ae0b8aee
commit 92e687303d
30 changed files with 406 additions and 398 deletions

View File

@ -18,4 +18,4 @@ add_executable(ArduinoJsonTests
target_link_libraries(ArduinoJsonTests ArduinoJson)
add_test(ArduinoJsonTests ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ArduinoJsonTests)
add_test(ArduinoJsonTests ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ArduinoJsonTests)

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#define ARDUINOJSON_ENABLE_STD_STREAM
#include <ArduinoJson.h>
#include "Printers.hpp"
class GbathreeBug : public testing::Test {
public:

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#define ARDUINOJSON_ENABLE_STD_STREAM
#include <ArduinoJson.h>
#include "Printers.hpp"
class JsonObject_Iterator_Test : public testing::Test {
public:

View File

@ -9,14 +9,19 @@
class JsonParser_Array_Tests : public testing::Test {
protected:
void whenInputIs(const char *json) {
strcpy(_jsonString, json);
_array = &_jsonBuffer.parseArray(_jsonString);
void whenInputIs(const char *json) { strcpy(_jsonString, json); }
void whenInputIs(const char *json, size_t len) {
memcpy(_jsonString, json, len);
}
void parseMustSucceed() { EXPECT_TRUE(_array->success()); }
void parseMustSucceed() {
_array = &_jsonBuffer.parseArray(_jsonString);
EXPECT_TRUE(_array->success());
}
void parseMustFail() {
_array = &_jsonBuffer.parseArray(_jsonString);
EXPECT_FALSE(_array->success());
EXPECT_EQ(0, _array->size());
}
@ -154,6 +159,11 @@ TEST_F(JsonParser_Array_Tests, IncompleteFalse) {
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, MixedTrueFalse) {
whenInputIs("[trufalse]");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, TwoStrings) {
whenInputIs("[\"hello\",\"world\"]");
@ -162,3 +172,55 @@ TEST_F(JsonParser_Array_Tests, TwoStrings) {
firstElementMustBe("hello");
secondElementMustBe("world");
}
TEST_F(JsonParser_Array_Tests, EmptyStringsDoubleQuotes) {
whenInputIs("[\"\",\"\"]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("");
secondElementMustBe("");
}
TEST_F(JsonParser_Array_Tests, EmptyStringSingleQuotes) {
whenInputIs("[\'\',\'\']");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("");
secondElementMustBe("");
}
TEST_F(JsonParser_Array_Tests, EmptyStringNoQuotes) {
whenInputIs("[,]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("");
secondElementMustBe("");
}
TEST_F(JsonParser_Array_Tests, ClosingDoubleQuoteMissing) {
whenInputIs("[\"]");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, ClosingSignleQuoteMissing) {
whenInputIs("[\']");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, StringWithEscapedChars) {
whenInputIs("[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
}
TEST_F(JsonParser_Array_Tests, StringWithUnterminatedEscapeSequence) {
whenInputIs("\"\\\0\"", 4);
parseMustFail();
}

View File

@ -75,6 +75,13 @@ TEST_F(JsonParser_Object_Test, OneStringSingleQuotes) {
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringNoQuotes) {
whenInputIs("{key:value}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey) {
whenInputIs("{ \"key\":\"value\"}");
parseMustSucceed();

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#define ARDUINOJSON_ENABLE_STD_STREAM
#include <ArduinoJson/JsonVariant.hpp>
#include "Printers.hpp"
using namespace ArduinoJson;

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#define ARDUINOJSON_ENABLE_STD_STREAM
#include <ArduinoJson.h>
#include "Printers.hpp"
class JsonVariant_Undefined_Tests : public ::testing::Test {
protected:

View File

@ -6,16 +6,18 @@
#include <gtest/gtest.h>
#include <ArduinoJson/Internals/QuotedString.hpp>
#include <ArduinoJson/Internals/JsonWriter.hpp>
#include <ArduinoJson/Internals/StringBuilder.hpp>
using namespace ArduinoJson::Internals;
class QuotedString_PrintTo_Tests : public testing::Test {
class JsonWriter_WriteString_Tests : public testing::Test {
protected:
void whenInputIs(const char *input) {
StringBuilder sb(buffer, sizeof(buffer));
returnValue = QuotedString::printTo(input, sb);
JsonWriter writer(sb);
writer.writeString(input);
returnValue = writer.bytesWritten();
}
void outputMustBe(const char *expected) {
@ -28,52 +30,52 @@ class QuotedString_PrintTo_Tests : public testing::Test {
size_t returnValue;
};
TEST_F(QuotedString_PrintTo_Tests, Null) {
TEST_F(JsonWriter_WriteString_Tests, Null) {
whenInputIs(0);
outputMustBe("null");
}
TEST_F(QuotedString_PrintTo_Tests, EmptyString) {
TEST_F(JsonWriter_WriteString_Tests, EmptyString) {
whenInputIs("");
outputMustBe("\"\"");
}
TEST_F(QuotedString_PrintTo_Tests, QuotationMark) {
TEST_F(JsonWriter_WriteString_Tests, QuotationMark) {
whenInputIs("\"");
outputMustBe("\"\\\"\"");
}
TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus) {
TEST_F(JsonWriter_WriteString_Tests, ReverseSolidus) {
whenInputIs("\\");
outputMustBe("\"\\\\\"");
}
TEST_F(QuotedString_PrintTo_Tests, Solidus) {
TEST_F(JsonWriter_WriteString_Tests, Solidus) {
whenInputIs("/");
outputMustBe("\"/\""); // but the JSON format allows \/
}
TEST_F(QuotedString_PrintTo_Tests, Backspace) {
TEST_F(JsonWriter_WriteString_Tests, Backspace) {
whenInputIs("\b");
outputMustBe("\"\\b\"");
}
TEST_F(QuotedString_PrintTo_Tests, Formfeed) {
TEST_F(JsonWriter_WriteString_Tests, Formfeed) {
whenInputIs("\f");
outputMustBe("\"\\f\"");
}
TEST_F(QuotedString_PrintTo_Tests, Newline) {
TEST_F(JsonWriter_WriteString_Tests, Newline) {
whenInputIs("\n");
outputMustBe("\"\\n\"");
}
TEST_F(QuotedString_PrintTo_Tests, CarriageReturn) {
TEST_F(JsonWriter_WriteString_Tests, CarriageReturn) {
whenInputIs("\r");
outputMustBe("\"\\r\"");
}
TEST_F(QuotedString_PrintTo_Tests, HorizontalTab) {
TEST_F(JsonWriter_WriteString_Tests, HorizontalTab) {
whenInputIs("\t");
outputMustBe("\"\\t\"");
}

View File

@ -1,50 +0,0 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "Printers.hpp"
#include <ArduinoJson/JsonArray.hpp>
class StreamPrintAdapter : public Print {
public:
explicit StreamPrintAdapter(std::ostream& os) : _os(os) {}
virtual size_t write(uint8_t c) {
_os << static_cast<char>(c);
return 1;
}
private:
// cannot be assigned
StreamPrintAdapter& operator=(const StreamPrintAdapter&);
std::ostream& _os;
};
std::ostream& ArduinoJson::operator<<(std::ostream& os,
const ArduinoJson::JsonVariant& v) {
StreamPrintAdapter adapter(os);
v.printTo(adapter);
return os;
}
std::ostream& ArduinoJson::operator<<(std::ostream& os,
const ArduinoJson::JsonArray& v) {
StreamPrintAdapter adapter(os);
v.printTo(adapter);
return os;
}
std::ostream& ArduinoJson::operator<<(
std::ostream& os, const ArduinoJson::JsonObjectSubscript& v) {
JsonVariant value = v;
return os << value;
}
std::ostream& ArduinoJson::operator<<(
std::ostream& os, const ArduinoJson::JsonArraySubscript& v) {
JsonVariant value = v;
return os << value;
}

View File

@ -1,17 +0,0 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include <ArduinoJson.h>
#include <ostream>
namespace ArduinoJson {
std::ostream& operator<<(std::ostream& os, const JsonVariant& v);
std::ostream& operator<<(std::ostream& os, const JsonArray& v);
std::ostream& operator<<(std::ostream& os, const JsonObjectSubscript& v);
std::ostream& operator<<(std::ostream& os, const JsonArraySubscript& v);
}

View File

@ -1,136 +0,0 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#include <ArduinoJson/Internals/QuotedString.hpp>
using namespace ArduinoJson::Internals;
class QuotedString_ExtractFrom_Tests : public testing::Test {
protected:
void whenInputIs(const char *json) {
strcpy(_jsonString, json);
_result = QuotedString::extractFrom(_jsonString, &_trailing);
}
void resultMustBe(const char *expected) { EXPECT_STREQ(expected, _result); }
void trailingMustBe(const char *expected) {
EXPECT_STREQ(expected, _trailing);
}
private:
char _jsonString[256];
char *_result;
char *_trailing;
};
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString) {
whenInputIs("\"\"");
resultMustBe("");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, NoQuotes) {
whenInputIs("hello world");
resultMustBe(0);
}
TEST_F(QuotedString_ExtractFrom_Tests, MissingClosingQuote) {
whenInputIs("\"hello world");
resultMustBe(0);
}
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString) {
whenInputIs("''");
resultMustBe("");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString) {
whenInputIs("\"hello world\"");
resultMustBe("hello world");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing) {
whenInputIs("\"hello\" world");
resultMustBe("hello");
trailingMustBe(" world");
}
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing) {
whenInputIs("'hello' world");
resultMustBe("hello");
trailingMustBe(" world");
}
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces) {
whenInputIs("\"{hello:world}\"");
resultMustBe("{hello:world}");
}
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets) {
whenInputIs("\"[hello,world]\"");
resultMustBe("[hello,world]");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote) {
whenInputIs("\"hello \\\"world\\\"\"");
resultMustBe("hello \"world\"");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote) {
whenInputIs("\"hello \\\'world\\\'\"");
resultMustBe("hello 'world'");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus) {
whenInputIs("\"hello \\/world\\/\"");
resultMustBe("hello /world/");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus) {
whenInputIs("\"hello \\\\world\\\\\"");
resultMustBe("hello \\world\\");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace) {
whenInputIs("\"hello \\bworld\\b\"");
resultMustBe("hello \bworld\b");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed) {
whenInputIs("\"hello \\fworld\\f\"");
resultMustBe("hello \fworld\f");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline) {
whenInputIs("\"hello \\nworld\\n\"");
resultMustBe("hello \nworld\n");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn) {
whenInputIs("\"hello \\rworld\\r\"");
resultMustBe("hello \rworld\r");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab) {
whenInputIs("\"hello \\tworld\\t\"");
resultMustBe("hello \tworld\t");
}
TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether) {
whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
resultMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
}

62
test/StdStream.cpp Normal file
View File

@ -0,0 +1,62 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <sstream>
#include <gtest/gtest.h>
#define ARDUINOJSON_ENABLE_STD_STREAM
#include <ArduinoJson.h>
TEST(StdStream, JsonVariantFalse) {
std::ostringstream os;
JsonVariant variant = false;
os << variant;
ASSERT_EQ("false", os.str());
}
TEST(StdStream, JsonVariantString) {
std::ostringstream os;
JsonVariant variant = "coucou";
os << variant;
ASSERT_EQ("\"coucou\"", os.str());
}
TEST(StdStream, JsonObject) {
std::ostringstream os;
DynamicJsonBuffer jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object["key"] = "value";
os << object;
ASSERT_EQ("{\"key\":\"value\"}", os.str());
}
TEST(StdStream, JsonObjectSubscript) {
std::ostringstream os;
DynamicJsonBuffer jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object["key"] = "value";
os << object["key"];
ASSERT_EQ("\"value\"", os.str());
}
TEST(StdStream, JsonArray) {
std::ostringstream os;
DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.add("value");
os << array;
ASSERT_EQ("[\"value\"]", os.str());
}
TEST(StdStream, JsonArraySubscript) {
std::ostringstream os;
DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.createArray();
array.add("value");
os << array[0];
ASSERT_EQ("\"value\"", os.str());
}