forked from bblanchon/ArduinoJson
Added JsonError
This commit is contained in:
@ -33,8 +33,8 @@ HEAD
|
|||||||
>
|
>
|
||||||
> ```c++
|
> ```c++
|
||||||
> DynamicJsonObject obj;
|
> DynamicJsonObject obj;
|
||||||
> bool success = deserializeJson(obj, json);
|
> JsonError error = deserializeJson(obj, json);
|
||||||
> if (success) {
|
> if (error) {
|
||||||
>
|
>
|
||||||
> }
|
> }
|
||||||
> ```
|
> ```
|
||||||
|
@ -35,9 +35,9 @@ void loadConfiguration(const char *filename, Config &config) {
|
|||||||
StaticJsonObject<512> root;
|
StaticJsonObject<512> root;
|
||||||
|
|
||||||
// Parse the root object
|
// Parse the root object
|
||||||
bool success = deserializeJson(root, file);
|
JsonError error = deserializeJson(root, file);
|
||||||
|
|
||||||
if (!success)
|
if (error)
|
||||||
Serial.println(F("Failed to read file, using default configuration"));
|
Serial.println(F("Failed to read file, using default configuration"));
|
||||||
|
|
||||||
// Copy values from the JsonObject to the Config
|
// Copy values from the JsonObject to the Config
|
||||||
|
@ -76,8 +76,8 @@ void setup() {
|
|||||||
DynamicJsonObject root(capacity);
|
DynamicJsonObject root(capacity);
|
||||||
|
|
||||||
// Parse JSON object
|
// Parse JSON object
|
||||||
bool success = deserializeJson(root, client);
|
JsonError error = deserializeJson(root, client);
|
||||||
if (!root.success()) {
|
if (error) {
|
||||||
Serial.println(F("Parsing failed!"));
|
Serial.println(F("Parsing failed!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,10 @@ void setup() {
|
|||||||
// It's a reference to the JsonObject, the actual bytes are inside the
|
// It's a reference to the JsonObject, the actual bytes are inside the
|
||||||
// JsonBuffer with all the other nodes of the object tree.
|
// JsonBuffer with all the other nodes of the object tree.
|
||||||
// Memory is freed when jsonBuffer goes out of scope.
|
// Memory is freed when jsonBuffer goes out of scope.
|
||||||
bool success = deserializeJson(root, json);
|
JsonError error = deserializeJson(root, json);
|
||||||
|
|
||||||
// Test if parsing succeeds.
|
// Test if parsing succeeds.
|
||||||
if (!success) {
|
if (error) {
|
||||||
Serial.println("parseObject() failed");
|
Serial.println("parseObject() failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,11 @@ class memstream : public std::istream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonVariant variant;
|
||||||
memstream json(data, size);
|
memstream json(data, size);
|
||||||
|
JsonError error = deserializeJson(variant, json);
|
||||||
JsonVariant variant = jsonBuffer.parse(json);
|
JsonVariant variant = jsonBuffer.parse(json);
|
||||||
if (variant.success()) {
|
if (!error) {
|
||||||
variant.as<std::string>(); // <- serialize to JSON
|
variant.as<std::string>(); // <- serialize to JSON
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../JsonBuffer.hpp"
|
#include "../JsonBuffer.hpp"
|
||||||
|
#include "../JsonError.hpp"
|
||||||
#include "../JsonVariant.hpp"
|
#include "../JsonVariant.hpp"
|
||||||
#include "../TypeTraits/IsConst.hpp"
|
#include "../TypeTraits/IsConst.hpp"
|
||||||
#include "StringWriter.hpp"
|
#include "StringWriter.hpp"
|
||||||
@ -24,9 +25,9 @@ class JsonParser {
|
|||||||
_reader(reader),
|
_reader(reader),
|
||||||
_writer(writer),
|
_writer(writer),
|
||||||
_nestingLimit(nestingLimit) {}
|
_nestingLimit(nestingLimit) {}
|
||||||
bool parse(JsonArray &destination);
|
JsonError parse(JsonArray &destination);
|
||||||
bool parse(JsonObject &destination);
|
JsonError parse(JsonObject &destination);
|
||||||
bool parse(JsonVariant &destination);
|
JsonError parse(JsonVariant &destination);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonParser &operator=(const JsonParser &); // non-copiable
|
JsonParser &operator=(const JsonParser &); // non-copiable
|
||||||
@ -37,12 +38,12 @@ class JsonParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *parseString();
|
const char *parseString();
|
||||||
bool parseAnythingTo(JsonVariant *destination);
|
JsonError parseAnythingTo(JsonVariant *destination);
|
||||||
FORCE_INLINE bool parseAnythingToUnsafe(JsonVariant *destination);
|
FORCE_INLINE JsonError parseAnythingToUnsafe(JsonVariant *destination);
|
||||||
|
|
||||||
inline bool parseArrayTo(JsonVariant *destination);
|
inline JsonError parseArrayTo(JsonVariant *destination);
|
||||||
inline bool parseObjectTo(JsonVariant *destination);
|
inline JsonError parseObjectTo(JsonVariant *destination);
|
||||||
inline bool parseStringTo(JsonVariant *destination);
|
inline JsonError parseStringTo(JsonVariant *destination);
|
||||||
|
|
||||||
static inline bool isBetween(char c, char min, char max) {
|
static inline bool isBetween(char c, char min, char max) {
|
||||||
return min <= c && c <= max;
|
return min <= c && c <= max;
|
||||||
|
@ -18,18 +18,18 @@ inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::eat(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool
|
inline ArduinoJson::JsonError
|
||||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingTo(
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingTo(
|
||||||
JsonVariant *destination) {
|
JsonVariant *destination) {
|
||||||
if (_nestingLimit == 0) return false;
|
if (_nestingLimit == 0) return JsonError::TooDeep;
|
||||||
_nestingLimit--;
|
_nestingLimit--;
|
||||||
bool success = parseAnythingToUnsafe(destination);
|
JsonError error = parseAnythingToUnsafe(destination);
|
||||||
_nestingLimit++;
|
_nestingLimit++;
|
||||||
return success;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool
|
inline ArduinoJson::JsonError
|
||||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingToUnsafe(
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingToUnsafe(
|
||||||
JsonVariant *destination) {
|
JsonVariant *destination) {
|
||||||
skipSpacesAndComments(_reader);
|
skipSpacesAndComments(_reader);
|
||||||
@ -47,92 +47,76 @@ ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingToUnsafe(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
inline ArduinoJson::JsonError
|
||||||
JsonArray &array) {
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(JsonArray &array) {
|
||||||
// Check opening braket
|
// Check opening braket
|
||||||
if (!eat('[')) goto ERROR_MISSING_BRACKET;
|
if (!eat('[')) return JsonError::OpeningBracketExpected;
|
||||||
if (eat(']')) goto SUCCESS_EMPTY_ARRAY;
|
if (eat(']')) return JsonError::Ok;
|
||||||
|
|
||||||
// Read each value
|
// Read each value
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// 1 - Parse value
|
// 1 - Parse value
|
||||||
JsonVariant value;
|
JsonVariant value;
|
||||||
if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE;
|
JsonError error = parseAnythingTo(&value);
|
||||||
if (!array.add(value)) goto ERROR_NO_MEMORY;
|
if (error != JsonError::Ok) return error;
|
||||||
|
if (!array.add(value)) return JsonError::NoMemory;
|
||||||
|
|
||||||
// 2 - More values?
|
// 2 - More values?
|
||||||
if (eat(']')) goto SUCCES_NON_EMPTY_ARRAY;
|
if (eat(']')) return JsonError::Ok;
|
||||||
if (!eat(',')) goto ERROR_MISSING_COMMA;
|
if (!eat(',')) return JsonError::ClosingBracketExpected;
|
||||||
}
|
}
|
||||||
|
|
||||||
SUCCESS_EMPTY_ARRAY:
|
|
||||||
SUCCES_NON_EMPTY_ARRAY:
|
|
||||||
return true;
|
|
||||||
|
|
||||||
ERROR_INVALID_VALUE:
|
|
||||||
ERROR_MISSING_BRACKET:
|
|
||||||
ERROR_MISSING_COMMA:
|
|
||||||
ERROR_NO_MEMORY:
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
inline ArduinoJson::JsonError
|
||||||
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
||||||
JsonObject &object) {
|
JsonObject &object) {
|
||||||
// Check opening brace
|
// Check opening brace
|
||||||
if (!eat('{')) goto ERROR_MISSING_BRACE;
|
if (!eat('{')) return JsonError::OpeningBraceExpected;
|
||||||
if (eat('}')) goto SUCCESS_EMPTY_OBJECT;
|
if (eat('}')) return JsonError::Ok;
|
||||||
|
|
||||||
// Read each key value pair
|
// Read each key value pair
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// 1 - Parse key
|
// 1 - Parse key
|
||||||
const char *key = parseString();
|
const char *key = parseString();
|
||||||
if (!key) goto ERROR_INVALID_KEY;
|
if (!key) return JsonError::NoMemory;
|
||||||
if (!eat(':')) goto ERROR_MISSING_COLON;
|
if (!eat(':')) return JsonError::ColonExpected;
|
||||||
|
|
||||||
// 2 - Parse value
|
// 2 - Parse value
|
||||||
JsonVariant value;
|
JsonVariant value;
|
||||||
if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE;
|
JsonError error = parseAnythingTo(&value);
|
||||||
if (!object.set(key, value)) goto ERROR_NO_MEMORY;
|
if (error != JsonError::Ok) return error;
|
||||||
|
if (!object.set(key, value)) return JsonError::NoMemory;
|
||||||
|
|
||||||
// 3 - More keys/values?
|
// 3 - More keys/values?
|
||||||
if (eat('}')) goto SUCCESS_NON_EMPTY_OBJECT;
|
if (eat('}')) return JsonError::Ok;
|
||||||
if (!eat(',')) goto ERROR_MISSING_COMMA;
|
if (!eat(',')) return JsonError::ClosingBraceExpected;
|
||||||
}
|
}
|
||||||
|
|
||||||
SUCCESS_EMPTY_OBJECT:
|
|
||||||
SUCCESS_NON_EMPTY_OBJECT:
|
|
||||||
return true;
|
|
||||||
|
|
||||||
ERROR_INVALID_KEY:
|
|
||||||
ERROR_INVALID_VALUE:
|
|
||||||
ERROR_MISSING_BRACE:
|
|
||||||
ERROR_MISSING_COLON:
|
|
||||||
ERROR_MISSING_COMMA:
|
|
||||||
ERROR_NO_MEMORY:
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
inline ArduinoJson::JsonError
|
||||||
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
||||||
JsonVariant &variant) {
|
JsonVariant &variant) {
|
||||||
return parseAnythingTo(&variant);
|
return parseAnythingTo(&variant);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArrayTo(
|
inline ArduinoJson::JsonError
|
||||||
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArrayTo(
|
||||||
JsonVariant *destination) {
|
JsonVariant *destination) {
|
||||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||||
if (!array) return false;
|
if (!array) return JsonError::NoMemory;
|
||||||
*destination = array;
|
*destination = array;
|
||||||
return parse(*array);
|
return parse(*array);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseObjectTo(
|
inline ArduinoJson::JsonError
|
||||||
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseObjectTo(
|
||||||
JsonVariant *destination) {
|
JsonVariant *destination) {
|
||||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||||
if (!object) return false;
|
if (!object) return JsonError::NoMemory;
|
||||||
*destination = object;
|
*destination = object;
|
||||||
return parse(*object);
|
return parse(*object);
|
||||||
}
|
}
|
||||||
@ -177,15 +161,16 @@ ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseString() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TReader, typename TWriter>
|
template <typename TReader, typename TWriter>
|
||||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseStringTo(
|
inline ArduinoJson::JsonError
|
||||||
|
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseStringTo(
|
||||||
JsonVariant *destination) {
|
JsonVariant *destination) {
|
||||||
bool hasQuotes = isQuote(_reader.current());
|
bool hasQuotes = isQuote(_reader.current());
|
||||||
const char *value = parseString();
|
const char *value = parseString();
|
||||||
if (value == NULL) return false;
|
if (value == NULL) return JsonError::NoMemory;
|
||||||
if (hasQuotes) {
|
if (hasQuotes) {
|
||||||
*destination = value;
|
*destination = value;
|
||||||
} else {
|
} else {
|
||||||
*destination = RawJson(value);
|
*destination = RawJson(value);
|
||||||
}
|
}
|
||||||
return true;
|
return JsonError::Ok;
|
||||||
}
|
}
|
||||||
|
83
src/ArduinoJson/JsonError.hpp
Normal file
83
src/ArduinoJson/JsonError.hpp
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
// ArduinoJson - arduinojson.org
|
||||||
|
// Copyright Benoit Blanchon 2014-2018
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ArduinoJson {
|
||||||
|
|
||||||
|
class JsonError {
|
||||||
|
public:
|
||||||
|
enum Code {
|
||||||
|
Ok,
|
||||||
|
OpeningBraceExpected,
|
||||||
|
ClosingBraceExpected,
|
||||||
|
OpeningBracketExpected,
|
||||||
|
ClosingBracketExpected,
|
||||||
|
ColonExpected,
|
||||||
|
TooDeep,
|
||||||
|
NoMemory
|
||||||
|
};
|
||||||
|
|
||||||
|
JsonError(Code code) : _code(code) {}
|
||||||
|
|
||||||
|
bool operator==(Code code) const {
|
||||||
|
return _code == code;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(Code code) const {
|
||||||
|
return _code != code;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator bool() const {
|
||||||
|
return _code != Ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* c_str() const {
|
||||||
|
return to_string(_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
friend const char* to_string(const JsonError err) {
|
||||||
|
return to_string(err._code);
|
||||||
|
}
|
||||||
|
|
||||||
|
friend const char* to_string(JsonError::Code code) {
|
||||||
|
switch (code) {
|
||||||
|
case Ok:
|
||||||
|
return "Ok";
|
||||||
|
case OpeningBraceExpected:
|
||||||
|
return "OpeningBraceExpected";
|
||||||
|
case ClosingBraceExpected:
|
||||||
|
return "ClosingBraceExpected";
|
||||||
|
case OpeningBracketExpected:
|
||||||
|
return "OpeningBracketExpected";
|
||||||
|
case ClosingBracketExpected:
|
||||||
|
return "ClosingBracketExpected";
|
||||||
|
case ColonExpected:
|
||||||
|
return "ColonExpected";
|
||||||
|
case TooDeep:
|
||||||
|
return "TooDeep";
|
||||||
|
case NoMemory:
|
||||||
|
return "NoMemory";
|
||||||
|
default:
|
||||||
|
return "???";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Code _code;
|
||||||
|
};
|
||||||
|
|
||||||
|
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||||
|
inline std::ostream& operator<<(std::ostream& s, const JsonError& e) {
|
||||||
|
s << to_string(e);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::ostream& operator<<(std::ostream& s, JsonError::Code e) {
|
||||||
|
s << to_string(e);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace ArduinoJson
|
@ -7,33 +7,36 @@
|
|||||||
#include "Deserialization/JsonParser.hpp"
|
#include "Deserialization/JsonParser.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
// bool deserializeJson(TDestination& destination, TString json);
|
// JsonError deserializeJson(TDestination& destination, TString json);
|
||||||
// TDestination = JsonArray, JsonObject, JsonVariant
|
// TDestination = JsonArray, JsonObject, JsonVariant
|
||||||
// TString = const std::string&, const String&
|
// TString = const std::string&, const String&
|
||||||
template <typename TDestination, typename TString>
|
template <typename TDestination, typename TString>
|
||||||
typename Internals::EnableIf<!Internals::IsArray<TString>::value, bool>::type
|
typename Internals::EnableIf<!Internals::IsArray<TString>::value,
|
||||||
|
JsonError>::type
|
||||||
deserializeJson(TDestination &destination, const TString &json,
|
deserializeJson(TDestination &destination, const TString &json,
|
||||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||||
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
||||||
.parse(destination);
|
.parse(destination);
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
// bool deserializeJson(TDestination& destination, TString json);
|
// JsonError deserializeJson(TDestination& destination, TString json);
|
||||||
// TDestination = JsonArray, JsonObject, JsonVariant
|
// TDestination = JsonArray, JsonObject, JsonVariant
|
||||||
// TString = const char*, const char[N], const FlashStringHelper*
|
// TString = const char*, const char[N], const FlashStringHelper*
|
||||||
template <typename TDestination, typename TString>
|
template <typename TDestination, typename TString>
|
||||||
bool deserializeJson(TDestination &destination, TString *json,
|
JsonError deserializeJson(
|
||||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
TDestination &destination, TString *json,
|
||||||
|
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||||
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
||||||
.parse(destination);
|
.parse(destination);
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
// bool deserializeJson(TDestination& destination, TString json);
|
// JsonError deserializeJson(TDestination& destination, TString json);
|
||||||
// TDestination = JsonArray, JsonObject, JsonVariant
|
// TDestination = JsonArray, JsonObject, JsonVariant
|
||||||
// TString = std::istream&, Stream&
|
// TString = std::istream&, Stream&
|
||||||
template <typename TDestination, typename TString>
|
template <typename TDestination, typename TString>
|
||||||
bool deserializeJson(TDestination &destination, TString &json,
|
JsonError deserializeJson(
|
||||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
TDestination &destination, TString &json,
|
||||||
|
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||||
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
||||||
.parse(destination);
|
.parse(destination);
|
||||||
}
|
}
|
||||||
|
@ -27,9 +27,9 @@ TEST_CASE("DynamicJsonBuffer no memory") {
|
|||||||
// char json[] = "[{}]";
|
// char json[] = "[{}]";
|
||||||
// DynamicJsonArray arr;
|
// DynamicJsonArray arr;
|
||||||
|
|
||||||
// bool success = deserializeJson(arr, json);
|
// JsonError err = deserializeJson(arr, json);
|
||||||
|
|
||||||
// REQUIRE(success == false);
|
// REQUIRE(err != JsonError::Ok);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// TODO: uncomment
|
// TODO: uncomment
|
||||||
@ -37,9 +37,9 @@ TEST_CASE("DynamicJsonBuffer no memory") {
|
|||||||
// char json[] = "{[]}";
|
// char json[] = "{[]}";
|
||||||
// DynamicJsonObject obj;
|
// DynamicJsonObject obj;
|
||||||
|
|
||||||
// bool success = deserializeJson(obj, json);
|
// JsonError err = deserializeJson(obj, json);
|
||||||
|
|
||||||
// REQUIRE(success == false);
|
// REQUIRE(err != JsonError::Ok);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
SECTION("startString()") {
|
SECTION("startString()") {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
TEST_CASE("Gbathree") {
|
TEST_CASE("Gbathree") {
|
||||||
DynamicJsonObject _object;
|
DynamicJsonObject _object;
|
||||||
|
|
||||||
bool success = deserializeJson(
|
JsonError error = deserializeJson(
|
||||||
_object,
|
_object,
|
||||||
"{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,"
|
"{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,"
|
||||||
"\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_"
|
"\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_"
|
||||||
@ -22,7 +22,7 @@ TEST_CASE("Gbathree") {
|
|||||||
"[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}");
|
"[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}");
|
||||||
|
|
||||||
SECTION("Success") {
|
SECTION("Success") {
|
||||||
REQUIRE(success == true);
|
REQUIRE(error == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ProtocolName") {
|
SECTION("ProtocolName") {
|
||||||
|
@ -10,8 +10,8 @@ TEST_CASE("JsonArray::copyTo()") {
|
|||||||
|
|
||||||
SECTION("BiggerOneDimensionIntegerArray") {
|
SECTION("BiggerOneDimensionIntegerArray") {
|
||||||
char json[] = "[1,2,3]";
|
char json[] = "[1,2,3]";
|
||||||
bool success = deserializeJson(array, json);
|
JsonError err = deserializeJson(array, json);
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
|
||||||
int destination[4] = {0};
|
int destination[4] = {0};
|
||||||
size_t result = array.copyTo(destination);
|
size_t result = array.copyTo(destination);
|
||||||
@ -25,8 +25,8 @@ TEST_CASE("JsonArray::copyTo()") {
|
|||||||
|
|
||||||
SECTION("SmallerOneDimensionIntegerArray") {
|
SECTION("SmallerOneDimensionIntegerArray") {
|
||||||
char json[] = "[1,2,3]";
|
char json[] = "[1,2,3]";
|
||||||
bool success = deserializeJson(array, json);
|
JsonError err = deserializeJson(array, json);
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
|
||||||
int destination[2] = {0};
|
int destination[2] = {0};
|
||||||
size_t result = array.copyTo(destination);
|
size_t result = array.copyTo(destination);
|
||||||
@ -39,8 +39,8 @@ TEST_CASE("JsonArray::copyTo()") {
|
|||||||
SECTION("TwoOneDimensionIntegerArray") {
|
SECTION("TwoOneDimensionIntegerArray") {
|
||||||
char json[] = "[[1,2],[3],[4]]";
|
char json[] = "[[1,2],[3],[4]]";
|
||||||
|
|
||||||
bool success = deserializeJson(array, json);
|
JsonError err = deserializeJson(array, json);
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
|
||||||
int destination[3][2] = {{0}};
|
int destination[3][2] = {{0}};
|
||||||
array.copyTo(destination);
|
array.copyTo(destination);
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
add_executable(JsonParserTests
|
add_executable(JsonParserTests
|
||||||
JsonArray.cpp
|
JsonArray.cpp
|
||||||
|
JsonError.cpp
|
||||||
JsonObject.cpp
|
JsonObject.cpp
|
||||||
JsonVariant.cpp
|
JsonVariant.cpp
|
||||||
nestingLimit.cpp
|
nestingLimit.cpp
|
||||||
|
@ -8,339 +8,346 @@
|
|||||||
TEST_CASE("deserializeJson(JsonArray&)") {
|
TEST_CASE("deserializeJson(JsonArray&)") {
|
||||||
DynamicJsonArray arr;
|
DynamicJsonArray arr;
|
||||||
|
|
||||||
SECTION("EmptyArray") {
|
SECTION("An empty array") {
|
||||||
bool success = deserializeJson(arr, "[]");
|
JsonError err = deserializeJson(arr, "[]");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(0 == arr.size());
|
REQUIRE(0 == arr.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("MissingOpeningBracket") {
|
SECTION("Spaces") {
|
||||||
bool success = deserializeJson(arr, "]");
|
SECTION("Before the opening bracket") {
|
||||||
REQUIRE_FALSE(success == true);
|
JsonError err = deserializeJson(arr, " []");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(0 == arr.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Before first value") {
|
||||||
|
JsonError err = deserializeJson(arr, "[ \t\r\n42]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("After first value") {
|
||||||
|
JsonError err = deserializeJson(arr, "[42 \t\r\n]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == 42);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ArrayWithNoEnd") {
|
SECTION("Values types") {
|
||||||
bool success = deserializeJson(arr, "[");
|
SECTION("On integer") {
|
||||||
REQUIRE_FALSE(success == true);
|
JsonError err = deserializeJson(arr, "[42]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Two integers") {
|
||||||
|
JsonError err = deserializeJson(arr, "[42,84]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == 42);
|
||||||
|
REQUIRE(arr[1] == 84);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Double") {
|
||||||
|
JsonError err = deserializeJson(arr, "[4.2,1e2]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == 4.2);
|
||||||
|
REQUIRE(arr[1] == 1e2);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Unsigned long") {
|
||||||
|
JsonError err = deserializeJson(arr, "[4294967295]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == 4294967295UL);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Boolean") {
|
||||||
|
JsonError err = deserializeJson(arr, "[true,false]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == true);
|
||||||
|
REQUIRE(arr[1] == false);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Null") {
|
||||||
|
JsonError err = deserializeJson(arr, "[null,null]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0].as<char *>() == 0);
|
||||||
|
REQUIRE(arr[1].as<char *>() == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("EmptyArrayWithLeadingSpaces") {
|
SECTION("Quotes") {
|
||||||
bool success = deserializeJson(arr, " []");
|
SECTION("Double quotes") {
|
||||||
|
JsonError err = deserializeJson(arr, "[ \"hello\" , \"world\" ]");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(0 == arr.size());
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
REQUIRE(arr[1] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Single quotes") {
|
||||||
|
JsonError err = deserializeJson(arr, "[ 'hello' , 'world' ]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
REQUIRE(arr[1] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("No quotes") {
|
||||||
|
JsonError err = deserializeJson(arr, "[ hello , world ]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
REQUIRE(arr[1] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Double quotes (empty strings)") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"\",\"\"]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "");
|
||||||
|
REQUIRE(arr[1] == "");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Single quotes (empty strings)") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\'\',\'\']");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "");
|
||||||
|
REQUIRE(arr[1] == "");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("No quotes (empty strings)") {
|
||||||
|
JsonError err = deserializeJson(arr, "[,]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "");
|
||||||
|
REQUIRE(arr[1] == "");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Closing single quotes missing") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"]");
|
||||||
|
|
||||||
|
REQUIRE(err != JsonError::Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Closing double quotes missing") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\']");
|
||||||
|
|
||||||
|
REQUIRE(err != JsonError::Ok);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Garbage") {
|
SECTION("Block comments") {
|
||||||
bool success = deserializeJson(arr, "%*$£¤");
|
SECTION("Before opening bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "/*COMMENT*/ [\"hello\"]");
|
||||||
|
|
||||||
REQUIRE_FALSE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("After opening bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "[/*COMMENT*/ \"hello\"]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Before closing bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\"/*COMMENT*/]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("After closing bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\"]/*COMMENT*/");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Before comma") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\"/*COMMENT*/,\"world\"]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
REQUIRE(arr[1] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("After comma") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\",/*COMMENT*/ \"world\"]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
REQUIRE(arr[1] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("/*/") {
|
||||||
|
JsonError err = deserializeJson(arr, "[/*/\n]");
|
||||||
|
REQUIRE(err != JsonError::Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Unfinished comment") {
|
||||||
|
JsonError err = deserializeJson(arr, "[/*COMMENT]");
|
||||||
|
REQUIRE(err != JsonError::Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Final slash missing") {
|
||||||
|
JsonError err = deserializeJson(arr, "[/*COMMENT*]");
|
||||||
|
REQUIRE(err != JsonError::Ok);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("OneInteger") {
|
SECTION("Line comments") {
|
||||||
bool success = deserializeJson(arr, "[42]");
|
SECTION("Before opening bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "//COMMENT\n\t[\"hello\"]");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(1 == arr.size());
|
REQUIRE(1 == arr.size());
|
||||||
REQUIRE(arr[0] == 42);
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("After opening bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "[//COMMENT\n\"hello\"]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Before closing bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\"//COMMENT\r\n]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("After closing bracket") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\"]//COMMENT\n");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Before comma") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\"//COMMENT\n,\"world\"]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
REQUIRE(arr[1] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("After comma") {
|
||||||
|
JsonError err = deserializeJson(arr, "[\"hello\",//COMMENT\n\"world\"]");
|
||||||
|
|
||||||
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
REQUIRE(2 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "hello");
|
||||||
|
REQUIRE(arr[1] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Invalid comment") {
|
||||||
|
JsonError err = deserializeJson(arr, "[/COMMENT\n]");
|
||||||
|
REQUIRE(err != JsonError::Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("End document with comment") {
|
||||||
|
JsonError err = deserializeJson(arr, "[//COMMENT");
|
||||||
|
REQUIRE(err != JsonError::Ok);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("OneIntegerWithSpacesBefore") {
|
SECTION("Misc") {
|
||||||
bool success = deserializeJson(arr, "[ \t\r\n42]");
|
SECTION("Garbage") {
|
||||||
|
JsonError err = deserializeJson(arr, "%*$£¤");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err != JsonError::Ok);
|
||||||
REQUIRE(1 == arr.size());
|
}
|
||||||
REQUIRE(arr[0] == 42);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("OneIntegerWithSpaceAfter") {
|
SECTION("The opening bracket is missing") {
|
||||||
bool success = deserializeJson(arr, "[42 \t\r\n]");
|
JsonError err = deserializeJson(arr, "]");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err != JsonError::Ok); // TODO
|
||||||
REQUIRE(1 == arr.size());
|
}
|
||||||
REQUIRE(arr[0] == 42);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("TwoIntegers") {
|
SECTION("The closing bracket is missing") {
|
||||||
bool success = deserializeJson(arr, "[42,84]");
|
JsonError err = deserializeJson(arr, "[");
|
||||||
|
REQUIRE(err != JsonError::Ok); // TODO
|
||||||
|
}
|
||||||
|
|
||||||
REQUIRE(success == true);
|
SECTION("Escape sequences") {
|
||||||
REQUIRE(2 == arr.size());
|
JsonError err =
|
||||||
REQUIRE(arr[0] == 42);
|
deserializeJson(arr, "[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
|
||||||
REQUIRE(arr[1] == 84);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("TwoDoubles") {
|
REQUIRE(err == JsonError::Ok);
|
||||||
bool success = deserializeJson(arr, "[4.2,1e2]");
|
REQUIRE(1 == arr.size());
|
||||||
|
REQUIRE(arr[0] == "1\"2\\3/4\b5\f6\n7\r8\t9");
|
||||||
|
}
|
||||||
|
|
||||||
REQUIRE(success == true);
|
SECTION("Unterminated escape sequence") {
|
||||||
REQUIRE(2 == arr.size());
|
JsonError err = deserializeJson(arr, "\"\\\0\"", 4);
|
||||||
REQUIRE(arr[0] == 4.2);
|
REQUIRE(err != JsonError::Ok);
|
||||||
REQUIRE(arr[1] == 1e2);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("UnsignedLong") {
|
SECTION("Nested objects") {
|
||||||
bool success = deserializeJson(arr, "[4294967295]");
|
char jsonString[] =
|
||||||
|
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||||
|
|
||||||
REQUIRE(success == true);
|
JsonError err = deserializeJson(arr, jsonString);
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == 4294967295UL);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("TwoBooleans") {
|
JsonObject &object1 = arr[0];
|
||||||
bool success = deserializeJson(arr, "[true,false]");
|
const JsonObject &object2 = arr[1];
|
||||||
|
JsonObject &object3 = arr[2];
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == true);
|
|
||||||
REQUIRE(arr[1] == false);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("TwoNulls") {
|
REQUIRE(true == object1.success());
|
||||||
bool success = deserializeJson(arr, "[null,null]");
|
REQUIRE(true == object2.success());
|
||||||
|
REQUIRE(false == object3.success());
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(2 == object1.size());
|
||||||
REQUIRE(2 == arr.size());
|
REQUIRE(2 == object2.size());
|
||||||
REQUIRE(arr[0].as<char *>() == 0);
|
REQUIRE(0 == object3.size());
|
||||||
REQUIRE(arr[1].as<char *>() == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("TwoStringsDoubleQuotes") {
|
REQUIRE(1 == object1["a"].as<int>());
|
||||||
bool success = deserializeJson(arr, "[ \"hello\" , \"world\" ]");
|
REQUIRE(2 == object1["b"].as<int>());
|
||||||
|
REQUIRE(3 == object2["c"].as<int>());
|
||||||
REQUIRE(success == true);
|
REQUIRE(4 == object2["d"].as<int>());
|
||||||
REQUIRE(2 == arr.size());
|
REQUIRE(0 == object3["e"].as<int>());
|
||||||
REQUIRE(arr[0] == "hello");
|
}
|
||||||
REQUIRE(arr[1] == "world");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("TwoStringsSingleQuotes") {
|
|
||||||
bool success = deserializeJson(arr, "[ 'hello' , 'world' ]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
REQUIRE(arr[1] == "world");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("TwoStringsNoQuotes") {
|
|
||||||
bool success = deserializeJson(arr, "[ hello , world ]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
REQUIRE(arr[1] == "world");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("EmptyStringsDoubleQuotes") {
|
|
||||||
bool success = deserializeJson(arr, "[\"\",\"\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "");
|
|
||||||
REQUIRE(arr[1] == "");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("EmptyStringSingleQuotes") {
|
|
||||||
bool success = deserializeJson(arr, "[\'\',\'\']");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "");
|
|
||||||
REQUIRE(arr[1] == "");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("EmptyStringNoQuotes") {
|
|
||||||
bool success = deserializeJson(arr, "[,]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "");
|
|
||||||
REQUIRE(arr[1] == "");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("ClosingDoubleQuoteMissing") {
|
|
||||||
bool success = deserializeJson(arr, "[\"]");
|
|
||||||
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("ClosingSignleQuoteMissing") {
|
|
||||||
bool success = deserializeJson(arr, "[\']");
|
|
||||||
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("StringWithEscapedChars") {
|
|
||||||
bool success =
|
|
||||||
deserializeJson(arr, "[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "1\"2\\3/4\b5\f6\n7\r8\t9");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("StringWithUnterminatedEscapeSequence") {
|
|
||||||
bool success = deserializeJson(arr, "\"\\\0\"", 4);
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CCommentBeforeOpeningBracket") {
|
|
||||||
bool success = deserializeJson(arr, "/*COMMENT*/ [\"hello\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CCommentAfterOpeningBracket") {
|
|
||||||
bool success = deserializeJson(arr, "[/*COMMENT*/ \"hello\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CCommentBeforeClosingBracket") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\"/*COMMENT*/]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CCommentAfterClosingBracket") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\"]/*COMMENT*/");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CCommentBeforeComma") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\"/*COMMENT*/,\"world\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
REQUIRE(arr[1] == "world");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CCommentAfterComma") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\",/*COMMENT*/ \"world\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
REQUIRE(arr[1] == "world");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CppCommentBeforeOpeningBracket") {
|
|
||||||
bool success = deserializeJson(arr, "//COMMENT\n\t[\"hello\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CppCommentAfterOpeningBracket") {
|
|
||||||
bool success = deserializeJson(arr, "[//COMMENT\n\"hello\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CppCommentBeforeClosingBracket") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\"//COMMENT\r\n]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CppCommentAfterClosingBracket") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\"]//COMMENT\n");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(1 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CppCommentBeforeComma") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\"//COMMENT\n,\"world\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
REQUIRE(arr[1] == "world");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("CppCommentAfterComma") {
|
|
||||||
bool success = deserializeJson(arr, "[\"hello\",//COMMENT\n\"world\"]");
|
|
||||||
|
|
||||||
REQUIRE(success == true);
|
|
||||||
REQUIRE(2 == arr.size());
|
|
||||||
REQUIRE(arr[0] == "hello");
|
|
||||||
REQUIRE(arr[1] == "world");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("InvalidCppComment") {
|
|
||||||
bool success = deserializeJson(arr, "[/COMMENT\n]");
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("InvalidComment") {
|
|
||||||
bool success = deserializeJson(arr, "[/*/\n]");
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("UnfinishedCComment") {
|
|
||||||
bool success = deserializeJson(arr, "[/*COMMENT]");
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("EndsInCppComment") {
|
|
||||||
bool success = deserializeJson(arr, "[//COMMENT");
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("AfterClosingStar") {
|
|
||||||
bool success = deserializeJson(arr, "[/*COMMENT*");
|
|
||||||
REQUIRE_FALSE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("DeeplyNested") {
|
|
||||||
bool success = deserializeJson(
|
|
||||||
arr, "[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
|
|
||||||
REQUIRE(success == true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("ObjectNestedInArray") {
|
|
||||||
char jsonString[] =
|
|
||||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
|
||||||
|
|
||||||
bool success = deserializeJson(arr, jsonString);
|
|
||||||
|
|
||||||
JsonObject &object1 = arr[0];
|
|
||||||
const JsonObject &object2 = arr[1];
|
|
||||||
JsonObject &object3 = arr[2];
|
|
||||||
|
|
||||||
REQUIRE(true == success);
|
|
||||||
|
|
||||||
REQUIRE(true == object1.success());
|
|
||||||
REQUIRE(true == object2.success());
|
|
||||||
REQUIRE(false == object3.success());
|
|
||||||
|
|
||||||
REQUIRE(2 == object1.size());
|
|
||||||
REQUIRE(2 == object2.size());
|
|
||||||
REQUIRE(0 == object3.size());
|
|
||||||
|
|
||||||
REQUIRE(1 == object1["a"].as<int>());
|
|
||||||
REQUIRE(2 == object1["b"].as<int>());
|
|
||||||
REQUIRE(3 == object2["c"].as<int>());
|
|
||||||
REQUIRE(4 == object2["d"].as<int>());
|
|
||||||
REQUIRE(0 == object3["e"].as<int>());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
44
test/JsonParser/JsonError.cpp
Normal file
44
test/JsonParser/JsonError.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// ArduinoJson - arduinojson.org
|
||||||
|
// Copyright Benoit Blanchon 2014-2018
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
void testStringification(JsonError error, std::string expected) {
|
||||||
|
REQUIRE(error.c_str() == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testBoolification(JsonError error, bool expected) {
|
||||||
|
CHECK(error == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST_STRINGIFICATION(symbol) \
|
||||||
|
testStringification(JsonError::symbol, #symbol)
|
||||||
|
|
||||||
|
#define TEST_BOOLIFICATION(symbol, expected) \
|
||||||
|
testBoolification(JsonError::symbol, expected)
|
||||||
|
|
||||||
|
TEST_CASE("JsonError") {
|
||||||
|
SECTION("c_str()") {
|
||||||
|
TEST_STRINGIFICATION(Ok);
|
||||||
|
TEST_STRINGIFICATION(OpeningBraceExpected);
|
||||||
|
TEST_STRINGIFICATION(ClosingBraceExpected);
|
||||||
|
TEST_STRINGIFICATION(OpeningBracketExpected);
|
||||||
|
TEST_STRINGIFICATION(ClosingBracketExpected);
|
||||||
|
TEST_STRINGIFICATION(ColonExpected);
|
||||||
|
TEST_STRINGIFICATION(TooDeep);
|
||||||
|
TEST_STRINGIFICATION(NoMemory);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("as boolean") {
|
||||||
|
TEST_BOOLIFICATION(Ok, false);
|
||||||
|
TEST_BOOLIFICATION(OpeningBraceExpected, true);
|
||||||
|
TEST_BOOLIFICATION(ClosingBraceExpected, true);
|
||||||
|
TEST_BOOLIFICATION(OpeningBracketExpected, true);
|
||||||
|
TEST_BOOLIFICATION(ClosingBracketExpected, true);
|
||||||
|
TEST_BOOLIFICATION(ColonExpected, true);
|
||||||
|
TEST_BOOLIFICATION(TooDeep, true);
|
||||||
|
TEST_BOOLIFICATION(NoMemory, true);
|
||||||
|
}
|
||||||
|
}
|
@ -9,36 +9,36 @@ TEST_CASE("deserializeJson(JsonObject&)") {
|
|||||||
DynamicJsonObject obj;
|
DynamicJsonObject obj;
|
||||||
|
|
||||||
SECTION("An empty object") {
|
SECTION("An empty object") {
|
||||||
bool success = deserializeJson(obj, "{}");
|
JsonError err = deserializeJson(obj, "{}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 0);
|
REQUIRE(obj.size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Quotes") {
|
SECTION("Quotes") {
|
||||||
SECTION("Double quotes") {
|
SECTION("Double quotes") {
|
||||||
bool success = deserializeJson(obj, "{\"key\":\"value\"}");
|
JsonError err = deserializeJson(obj, "{\"key\":\"value\"}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["key"] == "value");
|
REQUIRE(obj["key"] == "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Single quotes") {
|
SECTION("Single quotes") {
|
||||||
bool success = deserializeJson(obj, "{'key':'value'}");
|
JsonError err = deserializeJson(obj, "{'key':'value'}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["key"] == "value");
|
REQUIRE(obj["key"] == "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("No quotes") {
|
SECTION("No quotes") {
|
||||||
bool success = deserializeJson(obj, "{key:value}");
|
JsonError err = deserializeJson(obj, "{key:value}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["key"] == "value");
|
REQUIRE(obj["key"] == "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("No quotes, allow underscore in key") {
|
SECTION("No quotes, allow underscore in key") {
|
||||||
bool success = deserializeJson(obj, "{_k_e_y_:42}");
|
JsonError err = deserializeJson(obj, "{_k_e_y_:42}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["_k_e_y_"] == 42);
|
REQUIRE(obj["_k_e_y_"] == 42);
|
||||||
}
|
}
|
||||||
@ -46,46 +46,46 @@ TEST_CASE("deserializeJson(JsonObject&)") {
|
|||||||
|
|
||||||
SECTION("Spaces") {
|
SECTION("Spaces") {
|
||||||
SECTION("Before the key") {
|
SECTION("Before the key") {
|
||||||
bool success = deserializeJson(obj, "{ \"key\":\"value\"}");
|
JsonError err = deserializeJson(obj, "{ \"key\":\"value\"}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["key"] == "value");
|
REQUIRE(obj["key"] == "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("After the key") {
|
SECTION("After the key") {
|
||||||
bool success = deserializeJson(obj, "{\"key\" :\"value\"}");
|
JsonError err = deserializeJson(obj, "{\"key\" :\"value\"}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["key"] == "value");
|
REQUIRE(obj["key"] == "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Before the value") {
|
SECTION("Before the value") {
|
||||||
bool success = deserializeJson(obj, "{\"key\": \"value\"}");
|
JsonError err = deserializeJson(obj, "{\"key\": \"value\"}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["key"] == "value");
|
REQUIRE(obj["key"] == "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("After the value") {
|
SECTION("After the value") {
|
||||||
bool success = deserializeJson(obj, "{\"key\":\"value\" }");
|
JsonError err = deserializeJson(obj, "{\"key\":\"value\" }");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 1);
|
REQUIRE(obj.size() == 1);
|
||||||
REQUIRE(obj["key"] == "value");
|
REQUIRE(obj["key"] == "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Before the colon") {
|
SECTION("Before the colon") {
|
||||||
bool success =
|
JsonError err =
|
||||||
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 2);
|
REQUIRE(obj.size() == 2);
|
||||||
REQUIRE(obj["key1"] == "value1");
|
REQUIRE(obj["key1"] == "value1");
|
||||||
REQUIRE(obj["key2"] == "value2");
|
REQUIRE(obj["key2"] == "value2");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("After the colon") {
|
SECTION("After the colon") {
|
||||||
bool success =
|
JsonError err =
|
||||||
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
deserializeJson(obj, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 2);
|
REQUIRE(obj.size() == 2);
|
||||||
REQUIRE(obj["key1"] == "value1");
|
REQUIRE(obj["key1"] == "value1");
|
||||||
REQUIRE(obj["key2"] == "value2");
|
REQUIRE(obj["key2"] == "value2");
|
||||||
@ -94,41 +94,41 @@ TEST_CASE("deserializeJson(JsonObject&)") {
|
|||||||
|
|
||||||
SECTION("Values types") {
|
SECTION("Values types") {
|
||||||
SECTION("String") {
|
SECTION("String") {
|
||||||
bool success =
|
JsonError err =
|
||||||
deserializeJson(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
deserializeJson(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 2);
|
REQUIRE(obj.size() == 2);
|
||||||
REQUIRE(obj["key1"] == "value1");
|
REQUIRE(obj["key1"] == "value1");
|
||||||
REQUIRE(obj["key2"] == "value2");
|
REQUIRE(obj["key2"] == "value2");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Integer") {
|
SECTION("Integer") {
|
||||||
bool success = deserializeJson(obj, "{\"key1\":42,\"key2\":-42}");
|
JsonError err = deserializeJson(obj, "{\"key1\":42,\"key2\":-42}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 2);
|
REQUIRE(obj.size() == 2);
|
||||||
REQUIRE(obj["key1"] == 42);
|
REQUIRE(obj["key1"] == 42);
|
||||||
REQUIRE(obj["key2"] == -42);
|
REQUIRE(obj["key2"] == -42);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Double") {
|
SECTION("Double") {
|
||||||
bool success = deserializeJson(obj, "{\"key1\":12.345,\"key2\":-7E89}");
|
JsonError err = deserializeJson(obj, "{\"key1\":12.345,\"key2\":-7E89}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 2);
|
REQUIRE(obj.size() == 2);
|
||||||
REQUIRE(obj["key1"] == 12.345);
|
REQUIRE(obj["key1"] == 12.345);
|
||||||
REQUIRE(obj["key2"] == -7E89);
|
REQUIRE(obj["key2"] == -7E89);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Booleans") {
|
SECTION("Booleans") {
|
||||||
bool success = deserializeJson(obj, "{\"key1\":true,\"key2\":false}");
|
JsonError err = deserializeJson(obj, "{\"key1\":true,\"key2\":false}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 2);
|
REQUIRE(obj.size() == 2);
|
||||||
REQUIRE(obj["key1"] == true);
|
REQUIRE(obj["key1"] == true);
|
||||||
REQUIRE(obj["key2"] == false);
|
REQUIRE(obj["key2"] == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Null") {
|
SECTION("Null") {
|
||||||
bool success = deserializeJson(obj, "{\"key1\":null,\"key2\":null}");
|
JsonError err = deserializeJson(obj, "{\"key1\":null,\"key2\":null}");
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(obj.size() == 2);
|
REQUIRE(obj.size() == 2);
|
||||||
REQUIRE(obj["key1"].as<char *>() == 0);
|
REQUIRE(obj["key1"].as<char *>() == 0);
|
||||||
REQUIRE(obj["key2"].as<char *>() == 0);
|
REQUIRE(obj["key2"].as<char *>() == 0);
|
||||||
@ -137,13 +137,13 @@ TEST_CASE("deserializeJson(JsonObject&)") {
|
|||||||
SECTION("Array") {
|
SECTION("Array") {
|
||||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||||
|
|
||||||
bool success = deserializeJson(obj, jsonString);
|
JsonError err = deserializeJson(obj, jsonString);
|
||||||
|
|
||||||
JsonArray &array1 = obj["ab"];
|
JsonArray &array1 = obj["ab"];
|
||||||
const JsonArray &array2 = obj["cd"];
|
const JsonArray &array2 = obj["cd"];
|
||||||
JsonArray &array3 = obj["ef"];
|
JsonArray &array3 = obj["ef"];
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
|
|
||||||
REQUIRE(true == array1.success());
|
REQUIRE(true == array1.success());
|
||||||
REQUIRE(true == array2.success());
|
REQUIRE(true == array2.success());
|
||||||
@ -165,33 +165,33 @@ TEST_CASE("deserializeJson(JsonObject&)") {
|
|||||||
|
|
||||||
SECTION("Misc") {
|
SECTION("Misc") {
|
||||||
SECTION("The opening brace is missing") {
|
SECTION("The opening brace is missing") {
|
||||||
bool success = deserializeJson(obj, "}");
|
JsonError err = deserializeJson(obj, "}");
|
||||||
REQUIRE(success == false);
|
REQUIRE(err == JsonError::OpeningBraceExpected);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("The closing brace is missing") {
|
SECTION("The closing brace is missing") {
|
||||||
bool success = deserializeJson(obj, "{");
|
JsonError err = deserializeJson(obj, "{\"hello\":\"world\"");
|
||||||
REQUIRE(success == false);
|
REQUIRE(err == JsonError::ClosingBraceExpected);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("A quoted key without value") {
|
SECTION("A quoted key without value") {
|
||||||
bool success = deserializeJson(obj, "{\"key\"}");
|
JsonError err = deserializeJson(obj, "{\"key\"}");
|
||||||
REQUIRE(success == false);
|
REQUIRE(err == JsonError::ColonExpected);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("A non-quoted key without value") {
|
SECTION("A non-quoted key without value") {
|
||||||
bool success = deserializeJson(obj, "{key}");
|
JsonError err = deserializeJson(obj, "{key}");
|
||||||
REQUIRE(success == false);
|
REQUIRE(err == JsonError::ColonExpected);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("A dangling comma") {
|
SECTION("A dangling comma") {
|
||||||
bool success = deserializeJson(obj, "{\"key1\":\"value1\",}");
|
JsonError err = deserializeJson(obj, "{\"key1\":\"value1\",}");
|
||||||
REQUIRE(success == false);
|
REQUIRE(err == JsonError::ColonExpected);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("null as a key") {
|
SECTION("null as a key") {
|
||||||
bool success = deserializeJson(obj, "null:\"value\"}");
|
JsonError err = deserializeJson(obj, "{null:\"value\"}");
|
||||||
REQUIRE(success == false);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,79 +11,79 @@ TEST_CASE("deserializeJson(JsonVariant&)") {
|
|||||||
DynamicJsonVariant variant;
|
DynamicJsonVariant variant;
|
||||||
|
|
||||||
SECTION("EmptyObject") {
|
SECTION("EmptyObject") {
|
||||||
bool success = deserializeJson(variant, "{}");
|
JsonError err = deserializeJson(variant, "{}");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<JsonObject>());
|
REQUIRE(variant.is<JsonObject>());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("EmptyArray") {
|
SECTION("EmptyArray") {
|
||||||
bool success = deserializeJson(variant, "[]");
|
JsonError err = deserializeJson(variant, "[]");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<JsonArray>());
|
REQUIRE(variant.is<JsonArray>());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Integer") {
|
SECTION("Integer") {
|
||||||
bool success = deserializeJson(variant, "-42");
|
JsonError err = deserializeJson(variant, "-42");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<int>());
|
REQUIRE(variant.is<int>());
|
||||||
REQUIRE_FALSE(variant.is<bool>());
|
REQUIRE_FALSE(variant.is<bool>());
|
||||||
REQUIRE(variant == -42);
|
REQUIRE(variant == -42);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Double") {
|
SECTION("Double") {
|
||||||
bool success = deserializeJson(variant, "-1.23e+4");
|
JsonError err = deserializeJson(variant, "-1.23e+4");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE_FALSE(variant.is<int>());
|
REQUIRE_FALSE(variant.is<int>());
|
||||||
REQUIRE(variant.is<double>());
|
REQUIRE(variant.is<double>());
|
||||||
REQUIRE(variant.as<double>() == Approx(-1.23e+4));
|
REQUIRE(variant.as<double>() == Approx(-1.23e+4));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Double quoted string") {
|
SECTION("Double quoted string") {
|
||||||
bool success = deserializeJson(variant, "\"hello world\"");
|
JsonError err = deserializeJson(variant, "\"hello world\"");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<char*>());
|
REQUIRE(variant.is<char*>());
|
||||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Single quoted string") {
|
SECTION("Single quoted string") {
|
||||||
bool success = deserializeJson(variant, "\'hello world\'");
|
JsonError err = deserializeJson(variant, "\'hello world\'");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<char*>());
|
REQUIRE(variant.is<char*>());
|
||||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("True") {
|
SECTION("True") {
|
||||||
bool success = deserializeJson(variant, "true");
|
JsonError err = deserializeJson(variant, "true");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<bool>());
|
REQUIRE(variant.is<bool>());
|
||||||
REQUIRE(variant == true);
|
REQUIRE(variant == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("False") {
|
SECTION("False") {
|
||||||
bool success = deserializeJson(variant, "false");
|
JsonError err = deserializeJson(variant, "false");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<bool>());
|
REQUIRE(variant.is<bool>());
|
||||||
REQUIRE(variant == false);
|
REQUIRE(variant == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("OpenBrace") {
|
SECTION("OpenBrace") {
|
||||||
bool success = deserializeJson(variant, "{");
|
JsonError err = deserializeJson(variant, "{");
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Incomplete string") {
|
SECTION("Incomplete string") {
|
||||||
bool success = deserializeJson(variant, "\"hello");
|
JsonError err = deserializeJson(variant, "\"hello");
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(variant.is<char*>());
|
REQUIRE(variant.is<char*>());
|
||||||
REQUIRE_THAT(variant.as<char*>(), Equals("hello"));
|
REQUIRE_THAT(variant.as<char*>(), Equals("hello"));
|
||||||
}
|
}
|
||||||
|
@ -10,61 +10,61 @@ TEST_CASE("deserializeJson(StaticJsonArray&)") {
|
|||||||
StaticJsonArray<JSON_ARRAY_SIZE(0)> arr;
|
StaticJsonArray<JSON_ARRAY_SIZE(0)> arr;
|
||||||
char input[] = "[]";
|
char input[] = "[]";
|
||||||
|
|
||||||
bool success = deserializeJson(arr, input);
|
JsonError err = deserializeJson(arr, input);
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("TooSmallBufferForArrayWithOneValue") {
|
SECTION("TooSmallBufferForArrayWithOneValue") {
|
||||||
StaticJsonArray<JSON_ARRAY_SIZE(1) - 1> arr;
|
StaticJsonArray<JSON_ARRAY_SIZE(1) - 1> arr;
|
||||||
char input[] = "[1]";
|
char input[] = "[1]";
|
||||||
|
|
||||||
bool success = deserializeJson(arr, input);
|
JsonError err = deserializeJson(arr, input);
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
||||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||||
char input[] = "[1]";
|
char input[] = "[1]";
|
||||||
|
|
||||||
bool success = deserializeJson(arr, input);
|
JsonError err = deserializeJson(arr, input);
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
||||||
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> arr;
|
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> arr;
|
||||||
char input[] = "[{}]";
|
char input[] = "[{}]";
|
||||||
|
|
||||||
bool success = deserializeJson(arr, input);
|
JsonError err = deserializeJson(arr, input);
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
||||||
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> arr;
|
StaticJsonArray<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> arr;
|
||||||
char input[] = "[{}]";
|
char input[] = "[{}]";
|
||||||
|
|
||||||
bool success = deserializeJson(arr, input);
|
JsonError err = deserializeJson(arr, input);
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("CharPtrNull") {
|
SECTION("CharPtrNull") {
|
||||||
StaticJsonArray<100> arr;
|
StaticJsonArray<100> arr;
|
||||||
|
|
||||||
bool success = deserializeJson(arr, static_cast<char*>(0));
|
JsonError err = deserializeJson(arr, static_cast<char*>(0));
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ConstCharPtrNull") {
|
SECTION("ConstCharPtrNull") {
|
||||||
StaticJsonArray<100> arr;
|
StaticJsonArray<100> arr;
|
||||||
|
|
||||||
bool success = deserializeJson(arr, static_cast<const char*>(0));
|
JsonError err = deserializeJson(arr, static_cast<const char*>(0));
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("CopyStringNotSpaces") {
|
SECTION("CopyStringNotSpaces") {
|
||||||
|
@ -10,60 +10,60 @@ TEST_CASE("deserializeJson(StaticJsonObject&)") {
|
|||||||
StaticJsonObject<JSON_OBJECT_SIZE(0)> obj;
|
StaticJsonObject<JSON_OBJECT_SIZE(0)> obj;
|
||||||
char input[] = "{}";
|
char input[] = "{}";
|
||||||
|
|
||||||
bool success = deserializeJson(obj, input);
|
JsonError err = deserializeJson(obj, input);
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("TooSmallBufferForObjectWithOneValue") {
|
SECTION("TooSmallBufferForObjectWithOneValue") {
|
||||||
StaticJsonObject<JSON_OBJECT_SIZE(1) - 1> obj;
|
StaticJsonObject<JSON_OBJECT_SIZE(1) - 1> obj;
|
||||||
char input[] = "{\"a\":1}";
|
char input[] = "{\"a\":1}";
|
||||||
|
|
||||||
bool success = deserializeJson(obj, input);
|
JsonError err = deserializeJson(obj, input);
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
||||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||||
char input[] = "{\"a\":1}";
|
char input[] = "{\"a\":1}";
|
||||||
|
|
||||||
bool success = deserializeJson(obj, input);
|
JsonError err = deserializeJson(obj, input);
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
||||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> obj;
|
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> obj;
|
||||||
char input[] = "{\"a\":[]}";
|
char input[] = "{\"a\":[]}";
|
||||||
|
|
||||||
bool success = deserializeJson(obj, input);
|
JsonError err = deserializeJson(obj, input);
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
||||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> obj;
|
StaticJsonObject<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> obj;
|
||||||
char input[] = "{\"a\":[]}";
|
char input[] = "{\"a\":[]}";
|
||||||
|
|
||||||
bool success = deserializeJson(obj, input);
|
JsonError err = deserializeJson(obj, input);
|
||||||
|
|
||||||
REQUIRE(success == true);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("CharPtrNull") {
|
SECTION("CharPtrNull") {
|
||||||
StaticJsonObject<100> obj;
|
StaticJsonObject<100> obj;
|
||||||
|
|
||||||
bool success = deserializeJson(obj, static_cast<char*>(0));
|
JsonError err = deserializeJson(obj, static_cast<char*>(0));
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ConstCharPtrNull") {
|
SECTION("ConstCharPtrNull") {
|
||||||
StaticJsonObject<100> obj;
|
StaticJsonObject<100> obj;
|
||||||
|
|
||||||
bool success = deserializeJson(obj, static_cast<const char*>(0));
|
JsonError err = deserializeJson(obj, static_cast<const char*>(0));
|
||||||
|
|
||||||
REQUIRE(success == false);
|
REQUIRE(err != JsonError::Ok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,44 +5,45 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
bool tryParseArray(const char *json, uint8_t nestingLimit) {
|
JsonError tryParseArray(const char *json, uint8_t nestingLimit) {
|
||||||
DynamicJsonArray array;
|
DynamicJsonArray array;
|
||||||
return deserializeJson(array, json, nestingLimit);
|
return deserializeJson(array, json, nestingLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tryParseObject(const char *json, uint8_t nestingLimit) {
|
JsonError tryParseObject(const char *json, uint8_t nestingLimit) {
|
||||||
DynamicJsonObject obj;
|
DynamicJsonObject obj;
|
||||||
return deserializeJson(obj, json, nestingLimit);
|
return deserializeJson(obj, json, nestingLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("JsonParser nestingLimit") {
|
TEST_CASE("JsonParser nestingLimit") {
|
||||||
SECTION("ParseArrayWithNestingLimit0") {
|
SECTION("ParseArrayWithNestingLimit0") {
|
||||||
REQUIRE(true == tryParseArray("[]", 0));
|
REQUIRE(tryParseArray("[]", 0) == JsonError::Ok);
|
||||||
REQUIRE(false == tryParseArray("[[]]", 0));
|
REQUIRE(tryParseArray("[[]]", 0) == JsonError::TooDeep);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ParseArrayWithNestingLimit1") {
|
SECTION("ParseArrayWithNestingLimit1") {
|
||||||
REQUIRE(true == tryParseArray("[[]]", 1));
|
REQUIRE(tryParseArray("[[]]", 1) == JsonError::Ok);
|
||||||
REQUIRE(false == tryParseArray("[[[]]]", 1));
|
REQUIRE(tryParseArray("[[[]]]", 1) == JsonError::TooDeep);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ParseArrayWithNestingLimit2") {
|
SECTION("ParseArrayWithNestingLimit2") {
|
||||||
REQUIRE(true == tryParseArray("[[[]]]", 2));
|
REQUIRE(tryParseArray("[[[]]]", 2) == JsonError::Ok);
|
||||||
REQUIRE(false == tryParseArray("[[[[]]]]", 2));
|
REQUIRE(tryParseArray("[[[[]]]]", 2) == JsonError::TooDeep);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ParseObjectWithNestingLimit0") {
|
SECTION("ParseObjectWithNestingLimit0") {
|
||||||
REQUIRE(true == tryParseObject("{}", 0));
|
REQUIRE(tryParseObject("{}", 0) == JsonError::Ok);
|
||||||
REQUIRE(false == tryParseObject("{\"key\":{}}", 0));
|
REQUIRE(tryParseObject("{\"key\":{}}", 0) == JsonError::TooDeep);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ParseObjectWithNestingLimit1") {
|
SECTION("ParseObjectWithNestingLimit1") {
|
||||||
REQUIRE(true == tryParseObject("{\"key\":{}}", 1));
|
REQUIRE(tryParseObject("{\"key\":{}}", 1) == JsonError::Ok);
|
||||||
REQUIRE(false == tryParseObject("{\"key\":{\"key\":{}}}", 1));
|
REQUIRE(tryParseObject("{\"key\":{\"key\":{}}}", 1) == JsonError::TooDeep);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ParseObjectWithNestingLimit2") {
|
SECTION("ParseObjectWithNestingLimit2") {
|
||||||
REQUIRE(true == tryParseObject("{\"key\":{\"key\":{}}}", 2));
|
REQUIRE(tryParseObject("{\"key\":{\"key\":{}}}", 2) == JsonError::Ok);
|
||||||
REQUIRE(false == tryParseObject("{\"key\":{\"key\":{\"key\":{}}}}", 2));
|
REQUIRE(tryParseObject("{\"key\":{\"key\":{\"key\":{}}}}", 2) ==
|
||||||
|
JsonError::TooDeep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,9 +56,9 @@ TEST_CASE("std::stream") {
|
|||||||
SECTION("ParseArray") {
|
SECTION("ParseArray") {
|
||||||
std::istringstream json(" [ 42 /* comment */ ] ");
|
std::istringstream json(" [ 42 /* comment */ ] ");
|
||||||
DynamicJsonArray arr;
|
DynamicJsonArray arr;
|
||||||
bool success = deserializeJson(arr, json);
|
JsonError err = deserializeJson(arr, json);
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(1 == arr.size());
|
REQUIRE(1 == arr.size());
|
||||||
REQUIRE(42 == arr[0]);
|
REQUIRE(42 == arr[0]);
|
||||||
}
|
}
|
||||||
@ -66,9 +66,9 @@ TEST_CASE("std::stream") {
|
|||||||
SECTION("ParseObject") {
|
SECTION("ParseObject") {
|
||||||
std::istringstream json(" { hello : world // comment\n }");
|
std::istringstream json(" { hello : world // comment\n }");
|
||||||
DynamicJsonObject obj;
|
DynamicJsonObject obj;
|
||||||
bool success = deserializeJson(obj, json);
|
JsonError err = deserializeJson(obj, json);
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(1 == obj.size());
|
REQUIRE(1 == obj.size());
|
||||||
REQUIRE(std::string("world") == obj["hello"]);
|
REQUIRE(std::string("world") == obj["hello"]);
|
||||||
}
|
}
|
||||||
|
@ -19,10 +19,10 @@ TEST_CASE("std::string") {
|
|||||||
SECTION("deserializeJson") {
|
SECTION("deserializeJson") {
|
||||||
std::string json("[\"hello\"]");
|
std::string json("[\"hello\"]");
|
||||||
|
|
||||||
bool success = deserializeJson(array, json);
|
JsonError err = deserializeJson(array, json);
|
||||||
eraseString(json);
|
eraseString(json);
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(std::string("hello") == array[0]);
|
REQUIRE(std::string("hello") == array[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,10 +72,10 @@ TEST_CASE("std::string") {
|
|||||||
SECTION("deserializeJson()") {
|
SECTION("deserializeJson()") {
|
||||||
std::string json("{\"hello\":\"world\"}");
|
std::string json("{\"hello\":\"world\"}");
|
||||||
|
|
||||||
bool success = deserializeJson(object, json);
|
JsonError err = deserializeJson(object, json);
|
||||||
eraseString(json);
|
eraseString(json);
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
REQUIRE(std::string("world") == object["hello"]);
|
REQUIRE(std::string("world") == object["hello"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,18 +14,18 @@ TEST_CASE("unsigned char string") {
|
|||||||
unsigned char json[] = "[42]";
|
unsigned char json[] = "[42]";
|
||||||
|
|
||||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||||
bool success = deserializeJson(arr, json);
|
JsonError err = deserializeJson(arr, json);
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("JsonBuffer::parseObject") {
|
SECTION("JsonBuffer::parseObject") {
|
||||||
unsigned char json[] = "{\"a\":42}";
|
unsigned char json[] = "{\"a\":42}";
|
||||||
|
|
||||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||||
bool success = deserializeJson(obj, json);
|
JsonError err = deserializeJson(obj, json);
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("JsonVariant constructor") {
|
SECTION("JsonVariant constructor") {
|
||||||
|
@ -23,9 +23,9 @@ TEST_CASE("Variable Length Array") {
|
|||||||
strcpy(vla, "[42]");
|
strcpy(vla, "[42]");
|
||||||
|
|
||||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||||
bool success = deserializeJson(arr, vla);
|
JsonError err = deserializeJson(arr, vla);
|
||||||
|
|
||||||
REQUIRE(true == success);
|
REQUIRE(err == JsonError::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ParseObject") {
|
SECTION("ParseObject") {
|
||||||
|
Reference in New Issue
Block a user