GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
json_wrap.hpp
Go to the documentation of this file.
1#ifndef CHAISCRIPT_SIMPLEJSON_WRAP_HPP
2#define CHAISCRIPT_SIMPLEJSON_WRAP_HPP
3
4#include "json.hpp"
5
6namespace chaiscript
7{
9 {
10 public:
11
12 static Module& library(Module& m)
13 {
14
15 m.add(chaiscript::fun([](const std::string &t_str) { return from_json(t_str); }), "from_json");
16 m.add(chaiscript::fun(&json_wrap::to_json), "to_json");
17
18 return m;
19
20 }
21
22 private:
23
24 static Boxed_Value from_json(const json::JSON &t_json)
25 {
26 switch( t_json.JSONType() ) {
28 return Boxed_Value();
30 {
31 std::map<std::string, Boxed_Value> m;
32
33 for (const auto &p : t_json.object_range())
34 {
35 m.insert(std::make_pair(p.first, from_json(p.second)));
36 }
37
38 return Boxed_Value(m);
39 }
41 {
42 std::vector<Boxed_Value> vec;
43
44 for (const auto &p : t_json.array_range())
45 {
46 vec.emplace_back(from_json(p));
47 }
48
49 return Boxed_Value(vec);
50 }
52 return Boxed_Value(t_json.to_string());
54 return Boxed_Value(t_json.to_float());
56 return Boxed_Value(t_json.to_int());
58 return Boxed_Value(t_json.to_bool());
59 }
60
61 throw std::runtime_error("Unknown JSON type");
62 }
63
64 static Boxed_Value from_json(const std::string &t_json)
65 {
66 try {
67 return from_json( json::JSON::Load(t_json) );
68 } catch (const std::out_of_range& ) {
69 throw std::runtime_error("Unparsed JSON input");
70 }
71 }
72
73 static std::string to_json(const Boxed_Value &t_bv)
74 {
75 return to_json_object(t_bv).dump();
76 }
77
78 static json::JSON to_json_object(const Boxed_Value &t_bv)
79 {
80 try {
81 const std::map<std::string, Boxed_Value> m = chaiscript::boxed_cast<const std::map<std::string, Boxed_Value> &>(t_bv);
82
83 json::JSON obj(json::JSON::Class::Object);
84 for (const auto &o : m)
85 {
86 obj[o.first] = to_json_object(o.second);
87 }
88 return obj;
89 } catch (const chaiscript::exception::bad_boxed_cast &) {
90 // not a map
91 }
92
93 try {
94 const std::vector<Boxed_Value> v = chaiscript::boxed_cast<const std::vector<Boxed_Value> &>(t_bv);
95
96 json::JSON obj(json::JSON::Class::Array);
97 for (size_t i = 0; i < v.size(); ++i)
98 {
99 obj[i] = to_json_object(v[i]);
100 }
101 return obj;
102 } catch (const chaiscript::exception::bad_boxed_cast &) {
103 // not a vector
104 }
105
106
107 try {
108 Boxed_Number bn(t_bv);
110 {
111 return json::JSON(bn.get_as<double>());
112 } else {
113 return json::JSON(bn.get_as<std::int64_t>());
114 }
115 } catch (const chaiscript::detail::exception::bad_any_cast &) {
116 // not a number
117 }
118
119 try {
120 return json::JSON(boxed_cast<bool>(t_bv));
121 } catch (const chaiscript::exception::bad_boxed_cast &) {
122 // not a bool
123 }
124
125 try {
126 return json::JSON(boxed_cast<std::string>(t_bv));
127 } catch (const chaiscript::exception::bad_boxed_cast &) {
128 // not a string
129 }
130
131
132 try {
133 const chaiscript::dispatch::Dynamic_Object &o = boxed_cast<const dispatch::Dynamic_Object &>(t_bv);
134
135 json::JSON obj(json::JSON::Class::Object);
136 for (const auto &attr : o.get_attrs())
137 {
138 obj[attr.first] = to_json_object(attr.second);
139 }
140 return obj;
141 } catch (const chaiscript::exception::bad_boxed_cast &) {
142 // not a dynamic object
143 }
144
145 if (t_bv.is_null()) return json::JSON(); // a null value
146
147 throw std::runtime_error("Unknown object type to convert to JSON");
148 }
149
150
151 };
152
153
154}
155
156#endif
static bool is_floating_point(const Boxed_Value &t_bv)
Definition boxed_number.hpp:528
A wrapper for holding any valid C++ type. All types in ChaiScript are Boxed_Value objects.
Definition boxed_value.hpp:28
Holds a collection of ChaiScript settings which can be applied to the ChaiScript runtime....
Definition dispatchkit.hpp:150
Module & add(Type_Info ti, std::string name)
Definition dispatchkit.hpp:152
std::map< std::string, Boxed_Value > get_attrs() const
Definition dynamic_object.hpp:116
Definition json_wrap.hpp:9
static Module & library(Module &m)
Definition json_wrap.hpp:12
Definition json.hpp:37
std::string to_string() const
Definition json.hpp:326
double to_float() const
Definition json.hpp:332
Class JSONType() const
Definition json.hpp:321
JSONWrapper< QuickFlatMap > object_range()
Definition json.hpp:350
static JSON Load(const std::string &)
Definition json.hpp:667
@ String
Definition json.hpp:43
@ Boolean
Definition json.hpp:46
@ Array
Definition json.hpp:42
@ Object
Definition json.hpp:41
@ Integral
Definition json.hpp:45
@ Null
Definition json.hpp:40
@ Floating
Definition json.hpp:44
bool to_bool() const
Definition json.hpp:344
JSONWrapper< std::vector< JSON > > array_range()
Definition json.hpp:358
int64_t to_int() const
Definition json.hpp:338
Namespace chaiscript contains every API call that the average user will be concerned with.
decltype(auto) boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions=nullptr)
Function for extracting a value stored in a Boxed_Value object.
Definition boxed_cast.hpp:76
Proxy_Function fun(const T &t)
Creates a new Proxy_Function object from a free function, member function or data member.
Definition register_function.hpp:43