Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerializedValue.hpp Source File

SerializedValue.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Strings/StringAdapters.hpp>
00008 
00009 namespace ARDUINOJSON_NAMESPACE {
00010 
00011 // A special type of data that can be used to insert pregenerated JSON portions.
00012 template <typename T>
00013 class SerializedValue {
00014  public:
00015   explicit SerializedValue(T str) : _str(str) {}
00016   operator T() const {
00017     return _str;
00018   }
00019 
00020   const char* data() const {
00021     return _str.c_str();
00022   }
00023 
00024   size_t size() const {
00025     // CAUTION: the old Arduino String doesn't have size()
00026     return _str.length();
00027   }
00028 
00029  private:
00030   T _str;
00031 };
00032 
00033 template <typename TChar>
00034 class SerializedValue<TChar*> {
00035  public:
00036   explicit SerializedValue(TChar* p, size_t n) : _data(p), _size(n) {}
00037   operator TChar*() const {
00038     return _data;
00039   }
00040 
00041   TChar* data() const {
00042     return _data;
00043   }
00044 
00045   size_t size() const {
00046     return _size;
00047   }
00048 
00049  private:
00050   TChar* _data;
00051   size_t _size;
00052 };
00053 
00054 template <typename T>
00055 inline SerializedValue<T> serialized(T str) {
00056   return SerializedValue<T>(str);
00057 }
00058 
00059 template <typename TChar>
00060 inline SerializedValue<TChar*> serialized(TChar* p) {
00061   return SerializedValue<TChar*>(p, adaptString(p).size());
00062 }
00063 
00064 template <typename TChar>
00065 inline SerializedValue<TChar*> serialized(TChar* p, size_t n) {
00066   return SerializedValue<TChar*>(p, n);
00067 }
00068 }  // namespace ARDUINOJSON_NAMESPACE