在BOOST 1.75 尚未出来之前要使用JSON 常常需要使用PROPERTY tree 来进行转换,代码冗长。而使用BOOST::JSON 则大大简化。
//使用BOOST PROPERTYTREE
m_jsTitleBlock.put(L"Title", m_sTitle);
m_jsTitleBlock.put(L"DataType", m_sDataType);
m_jsTitleBlock.put(L"Version", m_sVersion);
m_jsTitleBlock.put(L"Author", m_sAuthor);
m_jsTitleBlock.put(L"CopyRight", m_sCopyRight);
m_jsTitleBlock.put(L"CreateDate", m_sCreateDate);
m_jsTitleBlock.put(L"LastModfiyDate", m_sLastModfiyDate);
m_jsTitleBlock.put(L"ModifyDate", m_sModifyDate);
WPTREE_A historyInfo,historyFather;
for (auto v : m_sHistoryInfo) //vector<wstring> m_sHistoryInfo
{
historyInfo.put(L"", v);
historyFather.push_back(make_pair(L"", historyInfo));
}
m_jsTitleBlock.put_child(L"History", historyFather);
使用BOOST 1.75 BOOST::json
inline int tag_invoke(const value_from_tag&, value& jv, const CTitleViewHistorySerial& title)
{
jv = {
{ "Title", title.m_sTitle },
{ "DataType", title.m_sDataType },
{ "Author", title.m_sAuthor },
{ "Version", title.m_sVersion },
{ "CopyRight",title.m_sCopyRight},
{ "CreateDate",title.m_sCreateDate},
{ "LastModfiyDate",title.m_sLastModfiyDate},
{ "ModifyDate",title.m_sModifyDate},
{ "History",title.m_sHistoryInfo} //vector<wstring> m_sHistoryInfo
};
return 0;
}
非常强大的JSON 转换能力。
template<class T>
CTitleViewHistorySerial tag_invoke(const value_to_tag<T>&, const value& jv)
{
std::wstring a = value_to< std::wstring >(jv.get_object().at("Title"));
std::wstring b = value_to< std::wstring >(jv.get_object().at("DataType"));
std::wstring c = value_to< std::wstring >(jv.get_object().at("Version"));
std::vector<std::wstring> d=value_to< vector<wstring> >(jv.get_object().at("History"));// 直接转换vector 为变量
return T(a, b, c,d);
}