Klamp't  0.9.0
JSON.h
1 #ifndef IO_JSON_H
2 #define IO_JSON_H
3 
5 #include <KrisLibrary/utils/AnyCollection.h>
6 
7 namespace Klampt {
8 
10 template <class T>
11 void Convert(const T& x,AnyCollection& c) { c = x; }
13 template <class T>
14 bool Convert(const AnyCollection& c,T& x) { return c.as<T>(x); }
15 
17 template <class T>
18 void Convert(const std::vector<T>& v,AnyCollection& c)
19 {
20  c.resize(v.size());
21  for(size_t i=0;i<v.size();i++)
22  Convert(v[i],c[(int)i]);
23 }
24 
26 template <class T>
27 bool Convert(const AnyCollection& c,std::vector<T>& v)
28 {
29  v.resize(c.size());
30  for(size_t i=0;i<v.size();i++)
31  if(!Convert(c[(int)i],v[i])) return false;
32  return true;
33 }
34 
35 template <> void Convert(const Vector3& x,AnyCollection& c);
36 template <> void Convert(const Vector& x,AnyCollection& c);
37 template <> void Convert(const IKGoal& g,AnyCollection& c);
38 template <> void Convert(const Hold& h,AnyCollection& c);
39 template <> void Convert(const Grasp& g,AnyCollection& c);
40 template <> void Convert(const Stance& s,AnyCollection& c);
41 template <> bool Convert(const AnyCollection& c,Vector3& x);
42 template <> bool Convert(const AnyCollection& c,Vector& x);
43 template <> bool Convert(const AnyCollection& c,IKGoal& g);
44 template <> bool Convert(const AnyCollection& c,Hold& h);
45 template <> bool Convert(const AnyCollection& c,Grasp& g);
46 template <> bool Convert(const AnyCollection& c,Stance& s);
47 
48 template <class T>
49 void SaveJSON(std::ostream& out,const T& x) {
50  AnyCollection msg;
51  Convert(x,msg);
52  out<<msg;
53 }
54 
55 
56 template <class T>
57 bool LoadJSON(std::istream& in,const T& x) {
58  AnyCollection msg;
59  in >> msg;
60  if(!in) return false;
61  return Convert(msg,x);
62 }
63 
64 } //namespace Klampt
65 
66 #endif
void Convert(const LinearPath &in, MultiPath &out)
Exact, direct conversion from LinearPath to MultiPath.
Defines all Klampt Resource types, and packing / unpacking utilities.
Definition: ContactDistance.h:6