Re: [Cppcms-users] json serialization issue
Brought to you by:
artyom-beilis
|
From: Lee E. <lee...@gm...> - 2013-01-10 08:20:00
|
ok allow me to elaborate
my map real code is like below - but the simple code i posted earlier
duplicates it as well
struct my_struct;
namespace cppcms {
namespace json {
struct traits<my_struct> {
static my_struct get(const value& val) {
...
}
static void set(value& v, const my_struct& m) {
...
}
};
template<typename V>
struct traits<std::map<std::string,V> > {
static std::map<std::string,V> get(const value& val) {
...
}
static void set(value& v, const std::map<std::string,V> m) {
auto o = v.object(); <-- attemp to make sure the
returned json is an object even if the map is empty
for(auto p : m)
o[p.first]=value(p.second);
}
}
};
} // namespace json
} // namespace cppcms
map<string, my_struct> m;
...
json::value v(m);
so i try to generate a new json object from my own map with json traits
defined for it
i get the exception on the o definition - and since v is an object i just
created is has to be undefined - i want the traits to change it to become
object
using the loop:
for(auto p : m)
v.set(p.first,p.second);
was my original code - this results in the same exception if there is some
data in the map, and returns an undefined object in case the map is empty
On Thu, Jan 10, 2013 at 2:42 AM, kpeo <sla...@ya...> wrote:
> P.S. Also, in general, as i understand it, your code doesn't change
> anything in "set" function.
> And you can also set default value in "get" function for passing undefined
> values:
>
>
> namespace cppcms {
> namespace json {
>
> template<typename V>
> struct traits<std::map<std::string,V> > {
> static std::map<std::string,V> get(const value& val) {
> std::map<std::string,V> m;
>
> for(auto p : val.object())
> m[p.first]=traits<V>::get(p.second,V()); // <-- set
> constructor as default for undefined values
>
>
> return m;
> }
> static void set(value& v, const std::map<std::string,V> m) {
> if(!v.is_undefined()){
>
> for(auto p : m)
> v.set(p.first,value(p.second)); // <-- set
> values with map ones (for example, so verify this code for your real app's
> needs)
>
> }
> }
> };
>
> } // namespace json
> } // namespace cppcms
>
> Regards,
> kpeo
>
> 10.01.2013, 02:48, "Lee Elenbaas" <lee...@gm...>:
>
> can anyone point me to what i did wrong in this code:
>
>
> namespace cppcms {
> namespace json {
>
> template<typename V>
> struct traits<std::map<std::string,V>> {
> static std::map<std::string,V> get(const value& val) {
> std::map<std::string,V> m;
>
> for(auto p : val.object())
> m[p.first]=traits<V>::get(p.second);
>
> return m;
> }
> static void set(value& v, const std::map<std::string,V> m) {
> auto o = v.object();
>
> for(auto p : m) {
> o[p.first]=value(p.second);
> }
> }
> };
>
> } // namespace json
> } // namespace cppcms
>
> when i try to use this:
>
> std::map<string, cppcms::json::value> m;
> ...
> cppcms::json::value v(m);
>
> i get the bad_value_cast exception
>
> thanks
> lee
>
> --
> --
> lee
> Lee Elenbaas
> lee...@gm...
> ,
>
>
> ------------------------------------------------------------------------------
> Master Java SE, Java EE, Eclipse, Spring, Hibernate, JavaScript, jQuery
> and much more. Keep your Java skills current with LearnJavaNow -
> 200+ hours of step-by-step video tutorials by Java experts.
> SALE $49.99 this month only -- learn more at:
> http://p.sf.net/sfu/learnmore_122612
> ,
>
> _______________________________________________
> Cppcms-users mailing list
> Cpp...@li...
> https://lists.sourceforge.net/lists/listinfo/cppcms-users
>
>
>
> ------------------------------------------------------------------------------
> Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
> MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
> with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
> MVPs and experts. ON SALE this month only -- learn more at:
> http://p.sf.net/sfu/learnmore_122712
> _______________________________________________
> Cppcms-users mailing list
> Cpp...@li...
> https://lists.sourceforge.net/lists/listinfo/cppcms-users
>
>
--
--
lee
Lee Elenbaas
lee...@gm...
|