Author: h.mohri
First declare with Pascal to use the converter and generics.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//// unit Unit2; interface uses System.JSON.Converters, System.JSON.Serializers, System.Generics.Collections; type TListString = TList<String>; TDictionaryStrStr = class(TDictionary<String, String>) constructor Create; overload; end; TTJsonListConverterString = TJsonListConverter<String>; TJsonDictionaryConverterStrStr = class(TJsonStringDictionaryConverter<String>) end; |
This is because the use of generics in C++Builder. C++Builder used Win64.
1 2 3 4 5 6 |
//// #include <System.JSON.Converters.hpp> #include <System.JSON.Serializers.hpp> #include <System.Generics.Collections.hpp> #include <System.JSON.Writers.hpp> #include <memory> |
Include each file.
This time I tried TJsonListConverter and TJsonStringDictionaryConverter.
TJsonListConverter converts TList<T> to JSON. TJsonStringDictionaryConverter converts TDictionary<String, T> to JSON.
placed two buttons on the TForm.
It outputs a json string of after-converted to TMemo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
//// template <typename T1, typename T2> struct jCollections { UnicodeString _property_name{L""}; std::unique_ptr<TStringWriter> _string_write{std::make_unique<TStringWriter>()}; std::unique_ptr<TJsonSerializer> _j_serializer{std::make_unique<TJsonSerializer>()}; std::unique_ptr<TJsonTextWriter> _json_writer{std::make_unique<TJsonTextWriter>(_string_write.get())}; jCollections(UnicodeString lname) { ///Constructor. ///Determine the property name. _property_name = lname; } String listtoj(T1 l_list) { ///Start of object and setting property name. _json_writer->WriteStartObject(); _json_writer->WritePropertyName(_property_name); ///TJsonListConverter__1 or TJsonDictionaryConverterStrStr. std::unique_ptr<T2 > l_json_conv{std::make_unique<T2 >()}; ///Here convert the value of the input list. l_json_conv->WriteJson(_json_writer.get(), TValue::_op_Implicit(l_list), _j_serializer.get()); _json_writer->WriteEndObject(); ///returns a string. return _string_write->ToString(); } }; void __fastcall TForm1::Button1Click(TObject *Sender) { TList__1<String>* l_list= new TList__1<String>(); try { jCollections<TList__1<String>*, TJsonListConverter__1<String> > _jCollections{L"language_List"}; l_list->Add("Python"); l_list->Add("Delphi"); l_list->Add("C++"); l_list->Add("JAVA"); l_list->Add("ひまわり"); Memo1->Lines->Append(_jCollections.listtoj(l_list) ); } __finally { delete l_list; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { TDictionaryStrStr* l_dic = new TDictionaryStrStr(); try { jCollections<TDictionaryStrStr*, TJsonDictionaryConverterStrStr > _jCollections{L"2017_MotoGP_standings"}; l_dic->Add("Maverick Vinales", "Movistar Yamaha"); l_dic->Add("Andrea Dovizioso", "Ducati"); l_dic->Add("Valentino Rossi", "Movistar Yamaha"); l_dic->Add("Marc Marquez", "Repsol Honda Team"); l_dic->Add("Dani Pedrosa", "Repsol Honda Team"); Memo1->Lines->Append(_jCollections.listtoj(l_dic)); } __finally { delete l_dic; } } //--------------------------------------------------------------------------- |
Button1 is TList<String> to JSON Convert. Button2 is TDictionary<String, String>to JSON.
It will be like this when executed.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition