////
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;
}
}
//---------------------------------------------------------------------------