Replaced printTo() with serializeJson()

* Added `serializeJson()` and `serializeJsonPretty()`
* Added `measureJson()` and `measureJsonPretty()`
* Removed `printTo()` and `prettyPrintTo()`
* Removed `measureLength()` and `measurePrettyLength()`
This commit is contained in:
Benoit Blanchon
2018-03-01 09:24:58 +01:00
parent 7a2a64803a
commit 83d73c93f7
38 changed files with 238 additions and 233 deletions

View File

@ -4,12 +4,16 @@ ArduinoJson: change log
HEAD HEAD
---- ----
* Added DynamicJsonArray and StaticJsonArray * Added `DynamicJsonArray` and `StaticJsonArray`
* Added DynamicJsonObject and StaticJsonObject * Added `DynamicJsonObject` and `StaticJsonObject`
* Added DynamicJsonVariant and StaticJsonVariant * Added `DynamicJsonVariant` and `StaticJsonVariant`
* Added deserializeJson() * Added `deserializeJson()`
* Removed JsonBuffer::parseArray(), parseObject() and parse() * Added `serializeJson()` and `serializeJsonPretty()`
* Removed JsonBuffer::createArray() and createObject() * Added `measureJson()` and `measureJsonPretty()`
* Removed `JsonBuffer::parseArray()`, `parseObject()` and `parse()`
* Removed `JsonBuffer::createArray()` and `createObject()`
* Removed `printTo()` and `prettyPrintTo()`
* Removed `measureLength()` and `measurePrettyLength()`
> ### BREAKING CHANGES > ### BREAKING CHANGES
> >
@ -51,7 +55,7 @@ HEAD
> ```c++ > ```c++
> DynamicJsonObject obj; > DynamicJsonObject obj;
> obj["key"] = "value"; > obj["key"] = "value";
> obj.printTo(Serial); > serializeJson(obj, Serial);
> ``` > ```
v5.13.1 v5.13.1

View File

@ -60,7 +60,7 @@ Here is a program that parses a JSON document with ArduinoJson.
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
DynamicJsonObject root; DynamicJsonObject root;
derserializeJson(root, json); deserializeJson(root, json);
const char* sensor = root["sensor"]; const char* sensor = root["sensor"];
long time = root["time"]; long time = root["time"];
@ -83,7 +83,7 @@ JsonArray& data = root.createNestedArray("data");
data.add(48.756080); data.add(48.756080);
data.add(2.302038); data.add(2.302038);
root.printTo(Serial); serializeJson(root, Serial);
// This prints: // This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
``` ```

View File

@ -72,7 +72,7 @@ void saveConfiguration(const char *filename, const Config &config) {
root["port"] = config.port; root["port"] = config.port;
// Serialize JSON to file // Serialize JSON to file
if (root.printTo(file) == 0) { if (serializeJson(root, file) == 0) {
Serial.println(F("Failed to write to file")); Serial.println(F("Failed to write to file"));
} }

View File

@ -38,13 +38,13 @@ void setup() {
data.add(48.756080); data.add(48.756080);
data.add(2.302038); data.add(2.302038);
root.printTo(Serial); serializeJson(root, Serial);
// This prints: // This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
Serial.println(); Serial.println();
root.prettyPrintTo(Serial); serializeJsonPretty(root, Serial);
// This prints: // This prints:
// { // {
// "sensor": "gps", // "sensor": "gps",

View File

@ -76,7 +76,7 @@ void loop() {
} }
Serial.print(F("Sending: ")); Serial.print(F("Sending: "));
root.printTo(Serial); serializeJson(root, Serial);
Serial.println(); Serial.println();
// Write response headers // Write response headers
@ -86,7 +86,7 @@ void loop() {
client.println(); client.println();
// Write JSON document // Write JSON document
root.prettyPrintTo(client); serializeJsonPretty(root, client);
// Disconnect // Disconnect
client.stop(); client.stop();

View File

@ -72,11 +72,11 @@ void loop() {
Serial.print(remoteIp); Serial.print(remoteIp);
Serial.print(F(" on port ")); Serial.print(F(" on port "));
Serial.println(remotePort); Serial.println(remotePort);
root.printTo(Serial); serializeJson(root, Serial);
// Send UDP packet // Send UDP packet
udp.beginPacket(remoteIp, remotePort); udp.beginPacket(remoteIp, remotePort);
root.printTo(udp); serializeJson(root, udp);
udp.println(); udp.println();
udp.endPacket(); udp.endPacket();

View File

@ -54,7 +54,7 @@ void setup() {
// Lastly, you can print the resulting JSON to a String // Lastly, you can print the resulting JSON to a String
String output; String output;
root.printTo(output); serializeJson(root, output);
} }
void loop() { void loop() {

View File

@ -1,13 +1,18 @@
add KEYWORD2
as KEYWORD2
createNestedArray KEYWORD2
createNestedObject KEYWORD2
deserializeJson KEYWORD2
DynamicJsonArray KEYWORD1
DynamicJsonObject KEYWORD1
DynamicJsonVariant KEYWORD1
get KEYWORD2
JsonArray KEYWORD1 JsonArray KEYWORD1
JsonObject KEYWORD1 JsonObject KEYWORD1
JsonVariant KEYWORD1 JsonVariant KEYWORD1
StaticJsonBuffer KEYWORD1 serializeJson KEYWORD2
DynamicJsonBuffer KEYWORD1 serializeJsonPretty KEYWORD2
add KEYWORD2 set KEYWORD2
createNestedArray KEYWORD2 StaticJsonArray KEYWORD1
createNestedObject KEYWORD2 StaticJsonObject KEYWORD1
parseArray KEYWORD2 StaticJsonVariant KEYWORD1
parseObject KEYWORD2
prettyPrintTo KEYWORD2
printTo KEYWORD2
success KEYWORD2

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include "../Data/Encoding.hpp"
#include "Comments.hpp" #include "Comments.hpp"
#include "JsonParser.hpp" #include "JsonParser.hpp"

View File

@ -9,7 +9,6 @@
#include "Data/ReferenceType.hpp" #include "Data/ReferenceType.hpp"
#include "Data/ValueSaver.hpp" #include "Data/ValueSaver.hpp"
#include "JsonVariant.hpp" #include "JsonVariant.hpp"
#include "Serialization/JsonPrintable.hpp"
#include "StringTraits/StringTraits.hpp" #include "StringTraits/StringTraits.hpp"
#include "TypeTraits/EnableIf.hpp" #include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsArray.hpp" #include "TypeTraits/IsArray.hpp"
@ -30,8 +29,7 @@ namespace Internals {
class JsonArraySubscript; class JsonArraySubscript;
} }
class JsonArray : public Internals::JsonPrintable<JsonArray>, class JsonArray : public Internals::ReferenceType,
public Internals::ReferenceType,
public Internals::NonCopyable, public Internals::NonCopyable,
public Internals::List<JsonVariant>, public Internals::List<JsonVariant>,
public Internals::JsonBufferAllocated { public Internals::JsonBufferAllocated {

View File

@ -98,14 +98,7 @@ inline const JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[](
size_t index) const { size_t index) const {
return impl()->template as<JsonArray>()[index]; return impl()->template as<JsonArray>()[index];
} }
} // namespace Internals
#if ARDUINOJSON_ENABLE_STD_STREAM
inline std::ostream& operator<<(std::ostream& os,
const JsonArraySubscript& source) {
return source.printTo(os);
}
#endif
}
inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) { inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) {
return Internals::JsonArraySubscript(*this, index); return Internals::JsonArraySubscript(*this, index);
@ -115,7 +108,7 @@ inline const Internals::JsonArraySubscript JsonArray::operator[](
size_t index) const { size_t index) const {
return Internals::JsonArraySubscript(*const_cast<JsonArray*>(this), index); return Internals::JsonArraySubscript(*const_cast<JsonArray*>(this), index);
} }
} } // namespace ArduinoJson
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(pop) #pragma warning(pop)

View File

@ -9,7 +9,6 @@
#include "Data/ReferenceType.hpp" #include "Data/ReferenceType.hpp"
#include "Data/ValueSaver.hpp" #include "Data/ValueSaver.hpp"
#include "JsonPair.hpp" #include "JsonPair.hpp"
#include "Serialization/JsonPrintable.hpp"
#include "StringTraits/StringTraits.hpp" #include "StringTraits/StringTraits.hpp"
#include "TypeTraits/EnableIf.hpp" #include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsArray.hpp" #include "TypeTraits/IsArray.hpp"
@ -31,8 +30,7 @@ template <typename>
class JsonObjectSubscript; class JsonObjectSubscript;
} }
class JsonObject : public Internals::JsonPrintable<JsonObject>, class JsonObject : public Internals::ReferenceType,
public Internals::ReferenceType,
public Internals::NonCopyable, public Internals::NonCopyable,
public Internals::List<JsonPair>, public Internals::List<JsonPair>,
public Internals::JsonBufferAllocated { public Internals::JsonBufferAllocated {

View File

@ -94,16 +94,8 @@ class JsonObjectSubscript
JsonObject& _object; JsonObject& _object;
TStringRef _key; TStringRef _key;
}; };
} // namespace Internals
#if ARDUINOJSON_ENABLE_STD_STREAM } // namespace ArduinoJson
template <typename TStringRef>
inline std::ostream& operator<<(std::ostream& os,
const JsonObjectSubscript<TStringRef>& source) {
return source.printTo(os);
}
#endif
}
}
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(pop) #pragma warning(pop)

View File

@ -12,7 +12,6 @@
#include "Data/JsonVariantType.hpp" #include "Data/JsonVariantType.hpp"
#include "JsonVariantBase.hpp" #include "JsonVariantBase.hpp"
#include "RawJson.hpp" #include "RawJson.hpp"
#include "Serialization/JsonPrintable.hpp"
#include "TypeTraits/EnableIf.hpp" #include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsChar.hpp" #include "TypeTraits/IsChar.hpp"
#include "TypeTraits/IsFloatingPoint.hpp" #include "TypeTraits/IsFloatingPoint.hpp"
@ -28,6 +27,10 @@ namespace ArduinoJson {
// Forward declarations. // Forward declarations.
class JsonArray; class JsonArray;
class JsonObject; class JsonObject;
namespace Internals {
template <typename Print>
class JsonSerializer;
}
// A variant that can be a any value serializable to a JSON value. // A variant that can be a any value serializable to a JSON value.
// //
@ -194,7 +197,7 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
const char *cstr = variantAsString(); const char *cstr = variantAsString();
if (cstr) return T(cstr); if (cstr) return T(cstr);
T s; T s;
printTo(s); serializeJson(*this, s);
return s; return s;
} }
// //

View File

@ -8,17 +8,15 @@
#include "JsonVariantComparisons.hpp" #include "JsonVariantComparisons.hpp"
#include "JsonVariantOr.hpp" #include "JsonVariantOr.hpp"
#include "JsonVariantSubscripts.hpp" #include "JsonVariantSubscripts.hpp"
#include "Serialization/JsonPrintable.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
template <typename TImpl> template <typename TImpl>
class JsonVariantBase : public JsonPrintable<TImpl>, class JsonVariantBase : public JsonVariantCasts<TImpl>,
public JsonVariantCasts<TImpl>,
public JsonVariantComparisons<TImpl>, public JsonVariantComparisons<TImpl>,
public JsonVariantOr<TImpl>, public JsonVariantOr<TImpl>,
public JsonVariantSubscripts<TImpl>, public JsonVariantSubscripts<TImpl>,
public JsonVariantTag {}; public JsonVariantTag {};
} } // namespace Internals
} } // namespace ArduinoJson

View File

@ -117,10 +117,4 @@ inline bool JsonVariant::variantIsFloat() const {
(_type == JSON_UNPARSED && isFloat(_content.asString)); (_type == JSON_UNPARSED && isFloat(_content.asString));
} }
#if ARDUINOJSON_ENABLE_STD_STREAM
inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) {
return source.printTo(os);
}
#endif
} // namespace ArduinoJson } // namespace ArduinoJson

View File

@ -7,7 +7,7 @@
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
// A dummy Print implementation used in JsonPrintable::measureLength() // A dummy Print implementation used in measureJson()
class DummyPrint { class DummyPrint {
public: public:
size_t print(char) { size_t print(char) {
@ -18,5 +18,5 @@ class DummyPrint {
return strlen(s); return strlen(s);
} }
}; };
} } // namespace Internals
} } // namespace ArduinoJson

View File

@ -8,7 +8,7 @@ namespace ArduinoJson {
namespace Internals { namespace Internals {
// Decorator on top of Print to allow indented output. // Decorator on top of Print to allow indented output.
// This class is used by JsonPrintable::prettyPrintTo() but can also be used // This class is used by serializeJsonPretty() but can also be used
// for your own purpose, like logging. // for your own purpose, like logging.
template <typename Print> template <typename Print>
class IndentedPrint { class IndentedPrint {
@ -64,5 +64,5 @@ class IndentedPrint {
static const int MAX_LEVEL = 15; // because it's only 4 bits static const int MAX_LEVEL = 15; // because it's only 4 bits
static const int MAX_TAB_SIZE = 7; // because it's only 3 bits static const int MAX_TAB_SIZE = 7; // because it's only 3 bits
}; };
} } // namespace Internals
} } // namespace ArduinoJson

View File

@ -1,117 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Configuration.hpp"
#include "../TypeTraits/EnableIf.hpp"
#include "DummyPrint.hpp"
#include "DynamicStringBuilder.hpp"
#include "IndentedPrint.hpp"
#include "JsonSerializer.hpp"
#include "JsonWriter.hpp"
#include "Prettyfier.hpp"
#include "StaticStringBuilder.hpp"
#if ARDUINOJSON_ENABLE_STD_STREAM
#include "StreamPrintAdapter.hpp"
#endif
namespace ArduinoJson {
namespace Internals {
// Implements all the overloads of printTo() and prettyPrintTo()
// Caution: this class use a template parameter to avoid virtual methods.
// This is a bit curious but allows to reduce the size of JsonVariant, JsonArray
// and JsonObject.
template <typename T>
class JsonPrintable {
public:
template <typename Print>
typename EnableIf<!StringTraits<Print>::has_append, size_t>::type printTo(
Print &print) const {
JsonWriter<Print> writer(print);
JsonSerializer<JsonWriter<Print> >::serialize(downcast(), writer);
return writer.bytesWritten();
}
#if ARDUINOJSON_ENABLE_STD_STREAM
std::ostream &printTo(std::ostream &os) const {
StreamPrintAdapter adapter(os);
printTo(adapter);
return os;
}
#endif
size_t printTo(char *buffer, size_t bufferSize) const {
StaticStringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
template <size_t N>
size_t printTo(char (&buffer)[N]) const {
return printTo(buffer, N);
}
template <typename TString>
typename EnableIf<StringTraits<TString>::has_append, size_t>::type printTo(
TString &str) const {
DynamicStringBuilder<TString> sb(str);
return printTo(sb);
}
template <typename Print>
size_t prettyPrintTo(IndentedPrint<Print> &print) const {
Prettyfier<Print> p(print);
return printTo(p);
}
size_t prettyPrintTo(char *buffer, size_t bufferSize) const {
StaticStringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb);
}
template <size_t N>
size_t prettyPrintTo(char (&buffer)[N]) const {
return prettyPrintTo(buffer, N);
}
template <typename Print>
typename EnableIf<!StringTraits<Print>::has_append, size_t>::type
prettyPrintTo(Print &print) const {
IndentedPrint<Print> indentedPrint(print);
return prettyPrintTo(indentedPrint);
}
template <typename TString>
typename EnableIf<StringTraits<TString>::has_append, size_t>::type
prettyPrintTo(TString &str) const {
DynamicStringBuilder<TString> sb(str);
return prettyPrintTo(sb);
}
size_t measureLength() const {
DummyPrint dp;
return printTo(dp);
}
size_t measurePrettyLength() const {
DummyPrint dp;
return prettyPrintTo(dp);
}
private:
const T &downcast() const {
return *static_cast<const T *>(this);
}
};
#if ARDUINOJSON_ENABLE_STD_STREAM
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const JsonPrintable<T> &v) {
return v.printTo(os);
}
#endif
}
}

View File

@ -4,7 +4,16 @@
#pragma once #pragma once
#include "DummyPrint.hpp"
#include "DynamicStringBuilder.hpp"
#include "IndentedPrint.hpp"
#include "JsonWriter.hpp" #include "JsonWriter.hpp"
#include "Prettyfier.hpp"
#include "StaticStringBuilder.hpp"
#if ARDUINOJSON_ENABLE_STD_STREAM
#include "StreamPrintAdapter.hpp"
#endif
namespace ArduinoJson { namespace ArduinoJson {
@ -28,5 +37,122 @@ class JsonSerializer {
static void serialize(const JsonObjectSubscript<TKey> &, Writer &); static void serialize(const JsonObjectSubscript<TKey> &, Writer &);
static void serialize(const JsonVariant &, Writer &); static void serialize(const JsonVariant &, Writer &);
}; };
} // namespace Internals
template <typename TSource, typename TDestination>
typename Internals::EnableIf<!Internals::StringTraits<TDestination>::has_append,
size_t>::type
serializeJson(const TSource &source, TDestination &destination) {
Internals::JsonWriter<TDestination> writer(destination);
Internals::JsonSerializer<Internals::JsonWriter<TDestination> >::serialize(
source, writer);
return writer.bytesWritten();
} }
#if ARDUINOJSON_ENABLE_STD_STREAM
template <typename TSource>
std::ostream &serializeJson(const TSource &source, std::ostream &os) {
Internals::StreamPrintAdapter adapter(os);
serializeJson(source, adapter);
return os;
} }
#endif
template <typename TSource>
size_t serializeJson(const TSource &source, char *buffer, size_t bufferSize) {
Internals::StaticStringBuilder sb(buffer, bufferSize);
return serializeJson(source, sb);
}
template <typename TSource, size_t N>
size_t serializeJson(const TSource &source, char (&buffer)[N]) {
return serializeJson(source, buffer, N);
}
template <typename TSource, typename TDestination>
typename Internals::EnableIf<Internals::StringTraits<TDestination>::has_append,
size_t>::type
serializeJson(const TSource &source, TDestination &str) {
Internals::DynamicStringBuilder<TDestination> sb(str);
return serializeJson(source, sb);
}
template <typename TSource, typename TDestination>
size_t serializeJsonPretty(const TSource &source,
Internals::IndentedPrint<TDestination> &print) {
Internals::Prettyfier<TDestination> p(print);
return serializeJson(source, p);
}
template <typename TSource>
size_t serializeJsonPretty(const TSource &source, char *buffer,
size_t bufferSize) {
Internals::StaticStringBuilder sb(buffer, bufferSize);
return serializeJsonPretty(source, sb);
}
template <typename TSource, size_t N>
size_t serializeJsonPretty(const TSource &source, char (&buffer)[N]) {
return serializeJsonPretty(source, buffer, N);
}
template <typename TSource, typename TDestination>
typename Internals::EnableIf<!Internals::StringTraits<TDestination>::has_append,
size_t>::type
serializeJsonPretty(const TSource &source, TDestination &print) {
Internals::IndentedPrint<TDestination> indentedPrint(print);
return serializeJsonPretty(source, indentedPrint);
}
template <typename TSource, typename TDestination>
typename Internals::EnableIf<Internals::StringTraits<TDestination>::has_append,
size_t>::type
serializeJsonPretty(const TSource &source, TDestination &str) {
Internals::DynamicStringBuilder<TDestination> sb(str);
return serializeJsonPretty(source, sb);
}
template <typename TSource>
size_t measureJson(const TSource &source) {
Internals::DummyPrint dp;
return serializeJson(source, dp);
}
template <typename TSource>
size_t measureJsonPretty(const TSource &source) {
Internals::DummyPrint dp;
return serializeJsonPretty(source, dp);
}
#if ARDUINOJSON_ENABLE_STD_STREAM
inline std::ostream &operator<<(std::ostream &os, const JsonArray &source) {
serializeJson(source, os);
return os;
}
inline std::ostream &operator<<(std::ostream &os, const JsonObject &source) {
serializeJson(source, os);
return os;
}
inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) {
serializeJson(source, os);
return os;
}
namespace Internals {
inline std::ostream &operator<<(std::ostream &os,
const JsonArraySubscript &source) {
serializeJson(source, os);
return os;
}
template <typename TKey>
inline std::ostream &operator<<(std::ostream &os,
const JsonObjectSubscript<TKey> &source) {
serializeJson(source, os);
return os;
}
} // namespace Internals
#endif
} // namespace ArduinoJson

View File

@ -26,8 +26,6 @@ class JsonWriter {
explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {} explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {}
// Returns the number of bytes sent to the Print implementation. // Returns the number of bytes sent to the Print implementation.
// This is very handy for implementations of printTo() that must return the
// number of bytes written.
size_t bytesWritten() const { size_t bytesWritten() const {
return _length; return _length;
} }
@ -151,5 +149,5 @@ class JsonWriter {
private: private:
JsonWriter &operator=(const JsonWriter &); // cannot be assigned JsonWriter &operator=(const JsonWriter &); // cannot be assigned
}; };
} } // namespace Internals
} } // namespace ArduinoJson

View File

@ -69,6 +69,7 @@ add_subdirectory(IntegrationTests)
add_subdirectory(JsonArray) add_subdirectory(JsonArray)
add_subdirectory(JsonObject) add_subdirectory(JsonObject)
add_subdirectory(JsonParser) add_subdirectory(JsonParser)
add_subdirectory(JsonSerializer)
add_subdirectory(JsonVariant) add_subdirectory(JsonVariant)
add_subdirectory(JsonWriter) add_subdirectory(JsonWriter)
add_subdirectory(Misc) add_subdirectory(Misc)

View File

@ -18,7 +18,8 @@ std::stringstream allocatorLog;
struct SpyingAllocator : DefaultAllocator { struct SpyingAllocator : DefaultAllocator {
void* allocate(size_t n) { void* allocate(size_t n) {
allocatorLog << "A" << (n - DynamicJsonBuffer::EmptyBlockSize); allocatorLog << static_cast<const char*>("A")
<< (n - DynamicJsonBuffer::EmptyBlockSize);
return DefaultAllocator::allocate(n); return DefaultAllocator::allocate(n);
} }
void deallocate(void* p) { void deallocate(void* p) {

View File

@ -10,11 +10,11 @@ void check(std::string originalJson) {
std::string prettyJson; std::string prettyJson;
deserializeJson(obj, originalJson); deserializeJson(obj, originalJson);
obj.prettyPrintTo(prettyJson); serializeJsonPretty(obj, prettyJson);
std::string finalJson; std::string finalJson;
deserializeJson(obj, originalJson); deserializeJson(obj, originalJson);
obj.printTo(finalJson); serializeJson(obj, finalJson);
REQUIRE(originalJson == finalJson); REQUIRE(originalJson == finalJson);
} }

View File

@ -9,8 +9,6 @@ add_executable(JsonArrayTests
copyTo.cpp copyTo.cpp
invalid.cpp invalid.cpp
iterator.cpp iterator.cpp
prettyPrintTo.cpp
printTo.cpp
remove.cpp remove.cpp
set.cpp set.cpp
size.cpp size.cpp

View File

@ -14,7 +14,7 @@ TEST_CASE("JsonArray::copyFrom()") {
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
REQUIRE(ok); REQUIRE(ok);
array.printTo(json, sizeof(json)); serializeJson(array, json, sizeof(json));
REQUIRE(std::string("[1,2,3]") == json); REQUIRE(std::string("[1,2,3]") == json);
} }
@ -27,7 +27,7 @@ TEST_CASE("JsonArray::copyFrom()") {
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
REQUIRE_FALSE(ok); REQUIRE_FALSE(ok);
array.printTo(json, sizeof(json)); serializeJson(array, json, sizeof(json));
REQUIRE(std::string("[1,2]") == json); REQUIRE(std::string("[1,2]") == json);
} }
@ -39,7 +39,7 @@ TEST_CASE("JsonArray::copyFrom()") {
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
REQUIRE(ok); REQUIRE(ok);
array.printTo(json, sizeof(json)); serializeJson(array, json, sizeof(json));
REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
} }
@ -53,7 +53,7 @@ TEST_CASE("JsonArray::copyFrom()") {
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
REQUIRE_FALSE(ok); REQUIRE_FALSE(ok);
array.printTo(json, sizeof(json)); serializeJson(array, json, sizeof(json));
REQUIRE(std::string("[[1,2,3],[4,5]]") == json); REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
} }
} }

View File

@ -28,7 +28,7 @@ TEST_CASE("JsonArray::invalid()") {
SECTION("PrintToWritesBrackets") { SECTION("PrintToWritesBrackets") {
char buffer[32]; char buffer[32];
JsonArray::invalid().printTo(buffer, sizeof(buffer)); serializeJson(JsonArray::invalid(), buffer, sizeof(buffer));
REQUIRE_THAT(buffer, Equals("[]")); REQUIRE_THAT(buffer, Equals("[]"));
} }
} }

View File

@ -8,8 +8,6 @@ add_executable(JsonObjectTests
get.cpp get.cpp
invalid.cpp invalid.cpp
iterator.cpp iterator.cpp
prettyPrintTo.cpp
printTo.cpp
remove.cpp remove.cpp
set.cpp set.cpp
size.cpp size.cpp

View File

@ -29,7 +29,7 @@ TEST_CASE("JsonObject::invalid()") {
SECTION("PrintToWritesBraces") { SECTION("PrintToWritesBraces") {
char buffer[32]; char buffer[32];
obj.printTo(buffer, sizeof(buffer)); serializeJson(obj, buffer, sizeof(buffer));
REQUIRE_THAT(buffer, Equals("{}")); REQUIRE_THAT(buffer, Equals("{}"));
} }
} }

View File

@ -33,7 +33,7 @@ TEST_CASE("JsonObject::remove()") {
} }
std::string result; std::string result;
obj.printTo(result); serializeJson(obj, result);
REQUIRE("{\"a\":0,\"c\":2}" == result); REQUIRE("{\"a\":0,\"c\":2}" == result);
} }
} }

View File

@ -0,0 +1,14 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2018
# MIT License
add_executable(JsonSerializerTests
JsonArray.cpp
JsonArrayPretty.cpp
JsonObject.cpp
JsonObjectPretty.cpp
JsonVariant.cpp
)
target_link_libraries(JsonSerializerTests catch)
add_test(JsonSerializer JsonSerializerTests)

View File

@ -7,14 +7,14 @@
static void check(JsonArray &array, std::string expected) { static void check(JsonArray &array, std::string expected) {
std::string actual; std::string actual;
size_t actualLen = array.printTo(actual); size_t actualLen = serializeJson(array, actual);
REQUIRE(expected == actual); REQUIRE(expected == actual);
REQUIRE(actualLen == expected.size()); REQUIRE(actualLen == expected.size());
size_t measuredLen = array.measureLength(); size_t measuredLen = measureJson(array);
REQUIRE(measuredLen == expected.size()); REQUIRE(measuredLen == expected.size());
} }
TEST_CASE("JsonArray::printTo()") { TEST_CASE("serializeJson(JsonArray)") {
StaticJsonArray<JSON_ARRAY_SIZE(2)> array; StaticJsonArray<JSON_ARRAY_SIZE(2)> array;
SECTION("Empty") { SECTION("Empty") {

View File

@ -7,14 +7,14 @@
static void check(JsonArray& array, std::string expected) { static void check(JsonArray& array, std::string expected) {
std::string actual; std::string actual;
size_t actualLen = array.prettyPrintTo(actual); size_t actualLen = serializeJsonPretty(array, actual);
size_t measuredLen = array.measurePrettyLength(); size_t measuredLen = measureJsonPretty(array);
CHECK(actualLen == expected.size()); CHECK(actualLen == expected.size());
CHECK(measuredLen == expected.size()); CHECK(measuredLen == expected.size());
REQUIRE(expected == actual); REQUIRE(expected == actual);
} }
TEST_CASE("JsonArray::prettyPrintTo()") { TEST_CASE("serializeJsonPretty(JsonArray)") {
DynamicJsonArray array; DynamicJsonArray array;
SECTION("Empty") { SECTION("Empty") {

View File

@ -8,14 +8,15 @@
void check(const JsonObject &obj, const std::string &expected) { void check(const JsonObject &obj, const std::string &expected) {
char actual[256]; char actual[256];
size_t actualLen = obj.printTo(actual); size_t actualLen = serializeJson(obj, actual);
size_t measuredLen = obj.measureLength(); size_t measuredLen = measureJson(obj);
REQUIRE(expected == actual); REQUIRE(expected == actual);
REQUIRE(expected.size() == actualLen); REQUIRE(expected.size() == actualLen);
REQUIRE(expected.size() == measuredLen); REQUIRE(expected.size() == measuredLen);
} }
TEST_CASE("JsonObject::printTo()") {
TEST_CASE("serializeJson(JsonObject)") {
DynamicJsonObject obj; DynamicJsonObject obj;
SECTION("EmptyObject") { SECTION("EmptyObject") {

View File

@ -9,15 +9,15 @@
void check(const JsonObject &obj, const std::string expected) { void check(const JsonObject &obj, const std::string expected) {
char json[256]; char json[256];
size_t actualLen = obj.prettyPrintTo(json); size_t actualLen = serializeJsonPretty(obj, json);
size_t measuredLen = obj.measurePrettyLength(); size_t measuredLen = measureJsonPretty(obj);
REQUIRE(json == expected); REQUIRE(json == expected);
REQUIRE(expected.size() == actualLen); REQUIRE(expected.size() == actualLen);
REQUIRE(expected.size() == measuredLen); REQUIRE(expected.size() == measuredLen);
} }
TEST_CASE("JsonObject::prettyPrintTo()") { TEST_CASE("serializeJsonPretty(JsonObject)") {
DynamicJsonObject obj; DynamicJsonObject obj;
SECTION("EmptyObject") { SECTION("EmptyObject") {

View File

@ -8,12 +8,12 @@
void check(JsonVariant variant, const std::string &expected) { void check(JsonVariant variant, const std::string &expected) {
char buffer[256] = ""; char buffer[256] = "";
size_t returnValue = variant.printTo(buffer, sizeof(buffer)); size_t returnValue = serializeJson(variant, buffer, sizeof(buffer));
REQUIRE(expected == buffer); REQUIRE(expected == buffer);
REQUIRE(expected.size() == returnValue); REQUIRE(expected.size() == returnValue);
} }
TEST_CASE("JsonVariant::printTo()") { TEST_CASE("serializeJson(JsonVariant)") {
SECTION("Empty") { SECTION("Empty") {
check(JsonVariant(), ""); check(JsonVariant(), "");
} }

View File

@ -2,13 +2,12 @@
# Copyright Benoit Blanchon 2014-2018 # Copyright Benoit Blanchon 2014-2018
# MIT License # MIT License
add_executable(JsonVariantTests add_executable(JsonVariantTests
as.cpp as.cpp
compare.cpp compare.cpp
copy.cpp copy.cpp
is.cpp is.cpp
or.cpp or.cpp
printTo.cpp
set_get.cpp set_get.cpp
subscript.cpp subscript.cpp
success.cpp success.cpp

View File

@ -49,19 +49,19 @@ TEST_CASE("std::string") {
REQUIRE(std::string("world") == array[0]); REQUIRE(std::string("world") == array[0]);
} }
SECTION("printTo()") { SECTION("serializeJson()") {
array.add(4); array.add(4);
array.add(2); array.add(2);
std::string json; std::string json;
array.printTo(json); serializeJson(array, json);
REQUIRE(std::string("[4,2]") == json); REQUIRE(std::string("[4,2]") == json);
} }
SECTION("prettyPrintTo") { SECTION("serializeJsonPretty()") {
array.add(4); array.add(4);
array.add(2); array.add(2);
std::string json; std::string json;
array.prettyPrintTo(json); serializeJsonPretty(array, json);
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json); REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
} }
} }
@ -69,7 +69,7 @@ TEST_CASE("std::string") {
SECTION("JsonObject") { SECTION("JsonObject") {
DynamicJsonObject object; DynamicJsonObject object;
SECTION("deserializeJson") { SECTION("deserializeJson()") {
std::string json("{\"hello\":\"world\"}"); std::string json("{\"hello\":\"world\"}");
bool success = deserializeJson(object, json); bool success = deserializeJson(object, json);
@ -156,7 +156,7 @@ TEST_CASE("std::string") {
char json[64]; char json[64];
object.createNestedObject(key); object.createNestedObject(key);
eraseString(key); eraseString(key);
object.printTo(json, sizeof(json)); serializeJson(object, json, sizeof(json));
REQUIRE(std::string("{\"key\":{}}") == json); REQUIRE(std::string("{\"key\":{}}") == json);
} }
@ -165,7 +165,7 @@ TEST_CASE("std::string") {
char json[64]; char json[64];
object.createNestedArray(key); object.createNestedArray(key);
eraseString(key); eraseString(key);
object.printTo(json, sizeof(json)); serializeJson(object, json, sizeof(json));
REQUIRE(std::string("{\"key\":[]}") == json); REQUIRE(std::string("{\"key\":[]}") == json);
} }
@ -197,17 +197,17 @@ TEST_CASE("std::string") {
REQUIRE(std::string("world") == object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
SECTION("printTo") { SECTION("serializeJson()") {
object["key"] = "value"; object["key"] = "value";
std::string json; std::string json;
object.printTo(json); serializeJson(object, json);
REQUIRE(std::string("{\"key\":\"value\"}") == json); REQUIRE(std::string("{\"key\":\"value\"}") == json);
} }
SECTION("prettyPrintTo") { SECTION("serializeJsonPretty()") {
object["key"] = "value"; object["key"] = "value";
std::string json; std::string json;
object.prettyPrintTo(json); serializeJsonPretty(object, json);
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json); REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
} }