mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	serializeMsgPack(doc, p, n) doesn't add terminator anymore (fixes #1545)
This commit is contained in:
		| @@ -14,6 +14,8 @@ namespace ARDUINOJSON_NAMESPACE { | ||||
| template <typename TWriter> | ||||
| class JsonSerializer : public Visitor<size_t> { | ||||
|  public: | ||||
|   static const bool producesText = true; | ||||
|  | ||||
|   JsonSerializer(TWriter writer) : _formatter(writer) {} | ||||
|  | ||||
|   FORCE_INLINE size_t visitArray(const CollectionData &array) { | ||||
|   | ||||
| @@ -16,7 +16,7 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> { | ||||
|   typedef JsonSerializer<TWriter> base; | ||||
|  | ||||
|  public: | ||||
|   PrettyJsonSerializer(TWriter &writer) : base(writer), _nesting(0) {} | ||||
|   PrettyJsonSerializer(TWriter writer) : base(writer), _nesting(0) {} | ||||
|  | ||||
|   size_t visitArray(const CollectionData &array) { | ||||
|     VariantSlot *slot = array.head(); | ||||
|   | ||||
| @@ -17,6 +17,8 @@ namespace ARDUINOJSON_NAMESPACE { | ||||
| template <typename TWriter> | ||||
| class MsgPackSerializer : public Visitor<size_t> { | ||||
|  public: | ||||
|   static const bool producesText = false; | ||||
|  | ||||
|   MsgPackSerializer(TWriter writer) : _writer(writer) {} | ||||
|  | ||||
|   template <typename T> | ||||
|   | ||||
| @@ -8,18 +8,14 @@ | ||||
|  | ||||
| namespace ARDUINOJSON_NAMESPACE { | ||||
|  | ||||
| // A Print implementation that allows to write in a char[] | ||||
| class StaticStringWriter { | ||||
|  public: | ||||
|   StaticStringWriter(char *buf, size_t size) : end(buf + size - 1), p(buf) { | ||||
|     *p = '\0'; | ||||
|   } | ||||
|   StaticStringWriter(char *buf, size_t size) : end(buf + size), p(buf) {} | ||||
|  | ||||
|   size_t write(uint8_t c) { | ||||
|     if (p >= end) | ||||
|       return 0; | ||||
|     *p++ = static_cast<char>(c); | ||||
|     *p = '\0'; | ||||
|     return 1; | ||||
|   } | ||||
|  | ||||
| @@ -29,7 +25,6 @@ class StaticStringWriter { | ||||
|       *p++ = static_cast<char>(*s++); | ||||
|       n--; | ||||
|     } | ||||
|     *p = '\0'; | ||||
|     return size_t(p - begin); | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -23,11 +23,23 @@ size_t serialize(const TSource &source, TDestination &destination) { | ||||
| } | ||||
|  | ||||
| template <template <typename> class TSerializer, typename TSource> | ||||
| size_t serialize(const TSource &source, void *buffer, size_t bufferSize) { | ||||
| typename enable_if<!TSerializer<StaticStringWriter>::producesText, size_t>::type | ||||
| serialize(const TSource &source, void *buffer, size_t bufferSize) { | ||||
|   StaticStringWriter writer(reinterpret_cast<char *>(buffer), bufferSize); | ||||
|   return doSerialize<TSerializer>(source, writer); | ||||
| } | ||||
|  | ||||
| template <template <typename> class TSerializer, typename TSource> | ||||
| typename enable_if<TSerializer<StaticStringWriter>::producesText, size_t>::type | ||||
| serialize(const TSource &source, void *buffer, size_t bufferSize) { | ||||
|   StaticStringWriter writer(reinterpret_cast<char *>(buffer), bufferSize); | ||||
|   size_t n = doSerialize<TSerializer>(source, writer); | ||||
|   // add null-terminator for text output (not counted in the size) | ||||
|   if (n < bufferSize) | ||||
|     reinterpret_cast<char *>(buffer)[n] = 0; | ||||
|   return n; | ||||
| } | ||||
|  | ||||
| template <template <typename> class TSerializer, typename TSource, | ||||
|           typename TChar, size_t N> | ||||
| #if defined _MSC_VER && _MSC_VER < 1900 | ||||
| @@ -36,8 +48,7 @@ typename enable_if<sizeof(remove_reference<TChar>::type) == 1, size_t>::type | ||||
| typename enable_if<sizeof(TChar) == 1, size_t>::type | ||||
| #endif | ||||
| serialize(const TSource &source, TChar (&buffer)[N]) { | ||||
|   StaticStringWriter writer(reinterpret_cast<char *>(buffer), N); | ||||
|   return doSerialize<TSerializer>(source, writer); | ||||
|   return serialize<TSerializer>(source, buffer, N); | ||||
| } | ||||
|  | ||||
| }  // namespace ARDUINOJSON_NAMESPACE | ||||
|   | ||||
		Reference in New Issue
	
	Block a user