#pragma once #include using namespace std::literals; #include "BSTTree.hpp" template 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 JSONEncoder & Set (tKeyType Key, const std::vector & arValue); template JSONEncoder & Set (tKeyType Key, const std::list & arValue); template JSONEncoder & Set (tKeyType Key, const std::initializer_list & arValue); public: template JSONEncoder & Encode (tKeyType Key, const TV & value) { Set (Key, value); return (*this); } template JSONEncoder & Encode (tKeyType Key, const std::initializer_list & 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 inline Proxy & operator = (const std::vector & arValue) { m_Encoder.Set (m_Key, arValue); return (*this); } template inline Proxy & operator = (const std::list & arValue) { m_Encoder.Set (m_Key, arValue); return (*this); } template inline Proxy & operator = (const std::initializer_list & 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 JSONEncoder ::JSONEncoder () { m_JsonRoot = new ECOM::Utility::BSTTree; } template JSONEncoder ::~JSONEncoder () { delete m_JsonRoot; } template void JSONEncoder ::Set (tKeyType Key, const T & Value) { m_JsonRoot->Put (Key, std::string_view {Value}); } template void JSONEncoder ::Set (tKeyType Key, CV_String Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, LPCTSTR Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, __int64 Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, unsigned __int64 Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, int Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, unsigned int Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, unsigned long Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, float Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, double Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, bool Value) { m_JsonRoot->Put (Key, Value); } template void JSONEncoder ::Set (tKeyType Key, const JSONEncoder & child) { const auto & tree { *child.m_JsonRoot }; m_JsonRoot->Add (Key, tree); } template bool JSONEncoder ::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 T JSONEncoder ::ToString () const { return T { ECOM::Utility::BSTJSON::ToString (*m_JsonRoot)}; } template template JSONEncoder & JSONEncoder ::Set (tKeyType Key, const std::vector & 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 template JSONEncoder & JSONEncoder ::Set (tKeyType Key, const std::list & 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 template JSONEncoder & JSONEncoder ::Set (tKeyType Key, const std::initializer_list & 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); }