Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
JsonSerializer.hpp
00001 // ArduinoJson - arduinojson.org 00002 // Copyright Benoit Blanchon 2014-2021 00003 // MIT License 00004 00005 #pragma once 00006 00007 #include <ArduinoJson/Json/TextFormatter.hpp> 00008 #include <ArduinoJson/Misc/Visitable.hpp> 00009 #include <ArduinoJson/Serialization/measure.hpp> 00010 #include <ArduinoJson/Serialization/serialize.hpp> 00011 00012 namespace ARDUINOJSON_NAMESPACE { 00013 00014 template <typename TWriter> 00015 class JsonSerializer : public Visitor<size_t> { 00016 public: 00017 JsonSerializer(TWriter writer) : _formatter(writer) {} 00018 00019 FORCE_INLINE size_t visitArray(const CollectionData &array) { 00020 write('['); 00021 00022 VariantSlot *slot = array.head(); 00023 00024 while (slot != 0) { 00025 slot->data()->accept(*this); 00026 00027 slot = slot->next(); 00028 if (slot == 0) 00029 break; 00030 00031 write(','); 00032 } 00033 00034 write(']'); 00035 return bytesWritten(); 00036 } 00037 00038 size_t visitObject(const CollectionData &object) { 00039 write('{'); 00040 00041 VariantSlot *slot = object.head(); 00042 00043 while (slot != 0) { 00044 _formatter.writeString(slot->key()); 00045 write(':'); 00046 slot->data()->accept(*this); 00047 00048 slot = slot->next(); 00049 if (slot == 0) 00050 break; 00051 00052 write(','); 00053 } 00054 00055 write('}'); 00056 return bytesWritten(); 00057 } 00058 00059 size_t visitFloat(Float value) { 00060 _formatter.writeFloat(value); 00061 return bytesWritten(); 00062 } 00063 00064 size_t visitString(const char *value) { 00065 _formatter.writeString(value); 00066 return bytesWritten(); 00067 } 00068 00069 size_t visitRawJson(const char *data, size_t n) { 00070 _formatter.writeRaw(data, n); 00071 return bytesWritten(); 00072 } 00073 00074 size_t visitNegativeInteger(UInt value) { 00075 _formatter.writeNegativeInteger(value); 00076 return bytesWritten(); 00077 } 00078 00079 size_t visitPositiveInteger(UInt value) { 00080 _formatter.writePositiveInteger(value); 00081 return bytesWritten(); 00082 } 00083 00084 size_t visitBoolean(bool value) { 00085 _formatter.writeBoolean(value); 00086 return bytesWritten(); 00087 } 00088 00089 size_t visitNull() { 00090 _formatter.writeRaw("null"); 00091 return bytesWritten(); 00092 } 00093 00094 protected: 00095 size_t bytesWritten() const { 00096 return _formatter.bytesWritten(); 00097 } 00098 00099 void write(char c) { 00100 _formatter.writeRaw(c); 00101 } 00102 00103 void write(const char *s) { 00104 _formatter.writeRaw(s); 00105 } 00106 00107 private: 00108 TextFormatter<TWriter> _formatter; 00109 }; 00110 00111 template <typename TSource, typename TDestination> 00112 size_t serializeJson(const TSource &source, TDestination &destination) { 00113 return serialize<JsonSerializer>(source, destination); 00114 } 00115 00116 template <typename TSource> 00117 size_t serializeJson(const TSource &source, void *buffer, size_t bufferSize) { 00118 return serialize<JsonSerializer>(source, buffer, bufferSize); 00119 } 00120 00121 template <typename TSource> 00122 size_t measureJson(const TSource &source) { 00123 return measure<JsonSerializer>(source); 00124 } 00125 00126 #if ARDUINOJSON_ENABLE_STD_STREAM 00127 template <typename T> 00128 inline typename enable_if<IsVisitable<T>::value, std::ostream &>::type 00129 operator<<(std::ostream &os, const T &source) { 00130 serializeJson(source, os); 00131 return os; 00132 } 00133 #endif 00134 00135 } // namespace ARDUINOJSON_NAMESPACE
Generated on Wed Jul 13 2022 01:10:36 by
1.7.2