123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- #pragma once
- #include <string>
- using namespace std::literals;
- #include "BSTTree.hpp"
- template <typename T>
- class JSONEncoder
- {
- using CV_String = const std::string_view;
- using tKeyType = const std::string_view &;
- public:
- JSONEncoder ();
- virtual ~JSONEncoder ();
- public:
- void Set (tKeyType Key, const T & Value);
- void Set (tKeyType Key, LPCSTR Value);
- void Set (tKeyType Key, CV_String Value);
- void Set (tKeyType Key, __int64 Value);
- void Set (tKeyType Key, unsigned __int64 Value);
- void Set (tKeyType Key, int Value);
- void Set (tKeyType Key, unsigned int Value);
- void Set (tKeyType Key, unsigned long Value);
- void Set (tKeyType Key, bool Value);
- void Set (tKeyType Key, float Value);
- void Set (tKeyType Key, double Value);
- void Set (tKeyType Key, const JSONEncoder & Element); // 作为嵌入对象
- public:
- template <typename TV>
- JSONEncoder & Set (tKeyType Key, const std::vector <TV> & arValue);
- template <typename TV>
- JSONEncoder & Set (tKeyType Key, const std::list <TV> & arValue);
- template <typename TV>
- JSONEncoder & Set (tKeyType Key, const std::initializer_list <TV> & arValue);
- public:
- template <typename TV>
- JSONEncoder & Encode (tKeyType Key, const TV & value)
- {
- Set (Key, value);
- return (*this);
- }
- template <typename TV>
- JSONEncoder & Encode (tKeyType Key, const std::initializer_list <TV> & value)
- {
- Set (Key, value);
- return (*this);
- }
- public:
- bool Recode (CV_String String);
- T ToString () const;
- operator T () const { return ToString (); }
- // 我没有拷贝构造函数
- private:
- JSONEncoder (const JSONEncoder &);
- JSONEncoder & operator = (JSONEncoder & Value);
- protected:
- ECOM::Utility::BSTTree * m_JsonRoot;
- protected:
- class Proxy
- {
- public:
- inline Proxy (JSONEncoder & Encoder, tKeyType Key) :
- m_Encoder (Encoder),
- m_Key (Key)
- {
- }
-
- inline Proxy & operator = (const T & Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (LPCSTR Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (CV_String Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (__int64 Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (unsigned __int64 Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (int Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (unsigned int Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (unsigned long Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (bool Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (float Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (double Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- template <typename TV>
- inline Proxy & operator = (const std::vector <TV> & arValue)
- {
- m_Encoder.Set (m_Key, arValue);
- return (*this);
- }
- template <typename TV>
- inline Proxy & operator = (const std::list <TV> & arValue)
- {
- m_Encoder.Set (m_Key, arValue);
- return (*this);
- }
- template <typename TV>
- inline Proxy & operator = (const std::initializer_list <TV> & arValue)
- {
- m_Encoder.Set (m_Key, arValue);
- return (*this);
- }
- inline Proxy & operator = (JSONEncoder & Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- protected:
- tKeyType m_Key;
- JSONEncoder & m_Encoder;
- };
- public:
- Proxy operator [] (CV_String & Key)
- {
- return Proxy (*this, Key);
- }
- Proxy operator () (CV_String & Key)
- {
- return Proxy (*this, Key);
- }
- };
- template <typename T>
- JSONEncoder <T>::JSONEncoder ()
- {
- m_JsonRoot = new ECOM::Utility::BSTTree;
- }
- template <typename T>
- JSONEncoder <T>::~JSONEncoder ()
- {
- delete m_JsonRoot;
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, const T & Value)
- {
- m_JsonRoot->Put (Key, std::string_view {Value});
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, CV_String Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, LPCTSTR Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, __int64 Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, unsigned __int64 Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, int Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, unsigned int Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, unsigned long Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, float Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, double Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, bool Value)
- {
- m_JsonRoot->Put (Key, Value);
- }
- template <typename T>
- void JSONEncoder <T>::Set (tKeyType Key, const JSONEncoder & child)
- {
- const auto & tree { *child.m_JsonRoot };
- m_JsonRoot->Add (Key, tree);
- }
- template <typename T>
- bool JSONEncoder <T>::Recode (CV_String String)
- {
- delete m_JsonRoot;
- m_JsonRoot = new ECOM::Utility::BSTTree (std::move (ECOM::Utility::BSTJSON::FromString (String)));
- return ! m_JsonRoot->IsEmpty ();
- }
- template <typename T>
- T JSONEncoder <T>::ToString () const
- {
- return T { ECOM::Utility::BSTJSON::ToString (*m_JsonRoot)};
- }
- template <typename T>
- template <typename TV>
- JSONEncoder <T> & JSONEncoder <T>::Set (tKeyType Key, const std::vector <TV> & arValue)
- {
- T emp;
- auto CHArray = m_JsonRoot->AddChildArray (Key);
- for (const auto & Item : arValue)
- {
- ECOM::Utility::BSTTree CH;
- CH.Put (emp, Item);
- CHArray.Add (std::move (CH));
- }
- return (*this);
- }
- template <typename T>
- template <typename TV>
- JSONEncoder <T> & JSONEncoder <T>::Set (tKeyType Key, const std::list <TV> & arValue)
- {
- T emp;
- auto CHArray = m_JsonRoot->AddChildArray (Key);
- for (const auto & Item : arValue)
- {
- ECOM::Utility::BSTTree CH;
- CH.Put (emp, Item);
- CHArray.Add (std::move (CH));
- }
- return (*this);
- }
- template <typename T>
- template <typename TV>
- JSONEncoder <T> & JSONEncoder <T>::Set (tKeyType Key, const std::initializer_list <TV> & arValue)
- {
- T emp;
- auto CHArray = m_JsonRoot->AddChildArray (Key);
- for (const auto & Item : arValue)
- {
- ECOM::Utility::BSTTree CH;
- CH.Put (emp, Item);
- CHArray.Add (std::move (CH));
- }
- return (*this);
- }
|