mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-02 00:38:26 +01:00
Renamed function RawJson() to serialized()
This commit is contained in:
@@ -7,14 +7,14 @@
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
class DummyPrint {
|
||||
class DummyWriter {
|
||||
public:
|
||||
size_t print(char) {
|
||||
size_t write(uint8_t) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t print(const char* s) {
|
||||
return strlen(s);
|
||||
size_t write(const uint8_t*, size_t n) {
|
||||
return n;
|
||||
}
|
||||
};
|
||||
} // namespace Internals
|
||||
@@ -1,35 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Strings/StringTraits.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// A Print implementation that allows to write in a String
|
||||
template <typename TString>
|
||||
class DynamicStringBuilder {
|
||||
public:
|
||||
DynamicStringBuilder(TString &str) : _str(str) {}
|
||||
|
||||
size_t print(char c) {
|
||||
StringTraits<TString>::append(_str, c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t print(const char *s) {
|
||||
size_t initialLen = _str.length();
|
||||
StringTraits<TString>::append(_str, s);
|
||||
return _str.length() - initialLen;
|
||||
}
|
||||
|
||||
private:
|
||||
DynamicStringBuilder &operator=(const DynamicStringBuilder &);
|
||||
|
||||
TString &_str;
|
||||
};
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
81
src/ArduinoJson/Serialization/DynamicStringWriter.hpp
Normal file
81
src/ArduinoJson/Serialization/DynamicStringWriter.hpp
Normal file
@@ -0,0 +1,81 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
|
||||
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
|
||||
#include <WString.h>
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STRING
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
template <typename>
|
||||
struct IsWriteableString : false_type {};
|
||||
|
||||
// A Print implementation that allows to write in a String
|
||||
template <typename TString>
|
||||
class DynamicStringWriter {};
|
||||
|
||||
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
|
||||
template <>
|
||||
struct IsWriteableString<String> : true_type {};
|
||||
|
||||
template <>
|
||||
class DynamicStringWriter<String> {
|
||||
public:
|
||||
DynamicStringWriter(String &str) : _str(&str) {}
|
||||
|
||||
size_t write(uint8_t c) {
|
||||
_str->operator+=(static_cast<char>(c));
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t write(const uint8_t *s, size_t n) {
|
||||
// CAUTION: Arduino String doesn't have append()
|
||||
// and old version doesn't have size() either
|
||||
_str->reserve(_str->length() + n);
|
||||
while (n > 0) {
|
||||
_str->operator+=(static_cast<char>(*s++));
|
||||
n--;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
private:
|
||||
String *_str;
|
||||
};
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STRING
|
||||
template <>
|
||||
struct IsWriteableString<std::string> : true_type {};
|
||||
|
||||
template <>
|
||||
class DynamicStringWriter<std::string> {
|
||||
public:
|
||||
DynamicStringWriter(std::string &str) : _str(&str) {}
|
||||
|
||||
size_t write(uint8_t c) {
|
||||
_str->operator+=(static_cast<char>(c));
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t write(const uint8_t *s, size_t n) {
|
||||
_str->append(reinterpret_cast<const char *>(s), n);
|
||||
return n;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string *_str;
|
||||
};
|
||||
#endif
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
@@ -8,22 +8,25 @@ namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// A Print implementation that allows to write in a char[]
|
||||
class StaticStringBuilder {
|
||||
class StaticStringWriter {
|
||||
public:
|
||||
StaticStringBuilder(char *buf, size_t size) : end(buf + size - 1), p(buf) {
|
||||
StaticStringWriter(char *buf, size_t size) : end(buf + size - 1), p(buf) {
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
size_t print(char c) {
|
||||
size_t write(uint8_t c) {
|
||||
if (p >= end) return 0;
|
||||
*p++ = c;
|
||||
*p++ = static_cast<char>(c);
|
||||
*p = '\0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t print(const char *s) {
|
||||
size_t write(const uint8_t *s, size_t n) {
|
||||
char *begin = p;
|
||||
while (p < end && *s) *p++ = *s++;
|
||||
while (p < end && n > 0) {
|
||||
*p++ = static_cast<char>(*s++);
|
||||
n--;
|
||||
}
|
||||
*p = '\0';
|
||||
return size_t(p - begin);
|
||||
}
|
||||
@@ -13,27 +13,28 @@
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
class StreamPrintAdapter {
|
||||
class StreamWriter {
|
||||
public:
|
||||
explicit StreamPrintAdapter(std::ostream& os) : _os(os) {}
|
||||
explicit StreamWriter(std::ostream& os) : _os(os) {}
|
||||
|
||||
size_t print(char c) {
|
||||
size_t write(uint8_t c) {
|
||||
_os << c;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t print(const char* s) {
|
||||
_os << s;
|
||||
return strlen(s);
|
||||
size_t write(const uint8_t* s, size_t n) {
|
||||
_os.write(reinterpret_cast<const char*>(s),
|
||||
static_cast<std::streamsize>(n));
|
||||
return n;
|
||||
}
|
||||
|
||||
private:
|
||||
// cannot be assigned
|
||||
StreamPrintAdapter& operator=(const StreamPrintAdapter&);
|
||||
StreamWriter& operator=(const StreamWriter&);
|
||||
|
||||
std::ostream& _os;
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
||||
#endif // ARDUINOJSON_ENABLE_STD_STREAM
|
||||
@@ -4,15 +4,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "./DummyPrint.hpp"
|
||||
#include "./DummyWriter.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
template <template <typename> class TSerializer, typename TSource>
|
||||
size_t measure(const TSource &source) {
|
||||
DummyPrint dp;
|
||||
TSerializer<DummyPrint> serializer(dp);
|
||||
DummyWriter dp;
|
||||
TSerializer<DummyWriter> serializer(dp);
|
||||
source.visit(serializer);
|
||||
return serializer.bytesWritten();
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "./DynamicStringBuilder.hpp"
|
||||
#include "./StaticStringBuilder.hpp"
|
||||
#include "./DynamicStringWriter.hpp"
|
||||
#include "./StaticStringWriter.hpp"
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include "./StreamPrintAdapter.hpp"
|
||||
#include "./StreamWriter.hpp"
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
@@ -16,7 +16,7 @@ namespace Internals {
|
||||
|
||||
template <template <typename> class TSerializer, typename TSource,
|
||||
typename TPrint>
|
||||
typename enable_if<!StringTraits<TPrint>::has_append, size_t>::type serialize(
|
||||
typename enable_if<!IsWriteableString<TPrint>::value, size_t>::type serialize(
|
||||
const TSource &source, TPrint &destination) {
|
||||
TSerializer<TPrint> serializer(destination);
|
||||
source.visit(serializer);
|
||||
@@ -26,29 +26,29 @@ typename enable_if<!StringTraits<TPrint>::has_append, size_t>::type serialize(
|
||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||
template <template <typename> class TSerializer, typename TSource>
|
||||
size_t serialize(const TSource &source, std::ostream &os) {
|
||||
StreamPrintAdapter adapter(os);
|
||||
return serialize<TSerializer>(source, adapter);
|
||||
StreamWriter writer(os);
|
||||
return serialize<TSerializer>(source, writer);
|
||||
}
|
||||
#endif
|
||||
|
||||
template <template <typename> class TSerializer, typename TSource>
|
||||
size_t serialize(const TSource &source, char *buffer, size_t bufferSize) {
|
||||
StaticStringBuilder sb(buffer, bufferSize);
|
||||
return serialize<TSerializer>(source, sb);
|
||||
StaticStringWriter writer(buffer, bufferSize);
|
||||
return serialize<TSerializer>(source, writer);
|
||||
}
|
||||
|
||||
template <template <typename> class TSerializer, typename TSource, size_t N>
|
||||
size_t serialize(const TSource &source, char (&buffer)[N]) {
|
||||
StaticStringBuilder sb(buffer, N);
|
||||
return serialize<TSerializer>(source, sb);
|
||||
StaticStringWriter writer(buffer, N);
|
||||
return serialize<TSerializer>(source, writer);
|
||||
}
|
||||
|
||||
template <template <typename> class TSerializer, typename TSource,
|
||||
typename TString>
|
||||
typename enable_if<StringTraits<TString>::has_append, size_t>::type serialize(
|
||||
typename enable_if<IsWriteableString<TString>::value, size_t>::type serialize(
|
||||
const TSource &source, TString &str) {
|
||||
DynamicStringBuilder<TString> sb(str);
|
||||
return serialize<TSerializer>(source, sb);
|
||||
DynamicStringWriter<TString> writer(str);
|
||||
return serialize<TSerializer>(source, writer);
|
||||
}
|
||||
|
||||
} // namespace Internals
|
||||
|
||||
Reference in New Issue
Block a user