Re: [Cppcms-users] json serialization issue
Brought to you by:
artyom-beilis
|
From: kpeo <sla...@ya...> - 2013-01-10 00:42:24
|
<div>P.S. Also, in general, as i understand it, your code doesn't change anything in "set" function. <br />And you can also set default value in "get" function for passing undefined values:<br /><br />namespace cppcms {<br /> namespace json {<br /><br /> template<typename V><br /> struct traits<std::map<std::string,V> > {<br /> static std::map<std::string,V> get(const value& val) {<br /> std::map<std::string,V> m;<br /> <br /> for(auto p : val.object())<br /> m[p.first]=traits<V>::get(p.second,V()); // <-- set constructor as default for undefined values<br /> <br /> return m;<br /> }<br /> static void set(value& v, const std::map<std::string,V> m) {<br /> if(!v.is_undefined()){<br /><br /> for(auto p : m)<br /> v.set(p.first,value(p.second)); // <-- set values with map ones (for example, so verify this code for your real app's needs)<br /> }<br /> }<br /> };<br /><br /> } // namespace json<br />} // namespace cppcms</div><div><br />Regards,<br />kpeo<br /><br /></div><div>10.01.2013, 02:48, "Lee Elenbaas" <lee...@gm...>:</div><blockquote><div dir="ltr"><div>can anyone point me to what i did wrong in this code:<br /><br />namespace cppcms {<br /> namespace json {<br /><br /> template<typename V><br /> struct traits<std::map<std::string,V>> {<br /> static std::map<std::string,V> get(const value& val) {<br /> std::map<std::string,V> m;<br /> <br /> for(auto p : val.object())<br /> m[p.first]=traits<V>::get(p.second);<br /> <br /> return m;<br /> }<br /> static void set(value& v, const std::map<std::string,V> m) {<br /> auto o = v.object();<br /><br /> for(auto p : m) {<br /> o[p.first]=value(p.second);<br /> }<br /> }<br /> };<br /><br /> } // namespace json<br />} // namespace cppcms<br /><br /></div><div>when i try to use this:<br /><br /></div><div>std::map<string, cppcms::json::value> m;<br /> ...</div><div>cppcms::json::value v(m);<br /><br /></div><div>i get the bad_value_cast exception<br /><br /></div>thanks<br />lee<br clear="all" /><div><div><br />-- <br />--<br />lee<br />Lee Elenbaas<br /><a href="mailto:lee...@gm...">lee...@gm...</a></div></div></div>,<p>------------------------------------------------------------------------------<br />Master Java SE, Java EE, Eclipse, Spring, Hibernate, JavaScript, jQuery<br />and much more. Keep your Java skills current with LearnJavaNow -<br />200+ hours of step-by-step video tutorials by Java experts.<br />SALE $49.99 this month only -- learn more at:<br /><a href="http://p.sf.net/sfu/learnmore_122612">http://p.sf.net/sfu/learnmore_122612</a> </p>,<p>_______________________________________________<br />Cppcms-users mailing list<br /><a href="mailto:Cpp...@li...">Cpp...@li...</a><br /><a href="https://lists.sourceforge.net/lists/listinfo/cppcms-users">https://lists.sourceforge.net/lists/listinfo/cppcms-users</a></p></blockquote> |