MCPcopy Create free account
hub / github.com/ChaiScript/ChaiScript / json_wrap

Class json_wrap

include/chaiscript/utility/json_wrap.hpp:8–151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6namespace chaiscript
7{
8 class json_wrap
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() ) {
27 case json::JSON::Class::Null:
28 return Boxed_Value();
29 case json::JSON::Class::Object:
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 }
40 case json::JSON::Class::Array:
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 }
51 case json::JSON::Class::String:
52 return Boxed_Value(t_json.to_string());
53 case json::JSON::Class::Floating:
54 return Boxed_Value(t_json.to_float());
55 case json::JSON::Class::Integral:
56 return Boxed_Value(t_json.to_int());
57 case json::JSON::Class::Boolean:
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 {

Callers

nothing calls this directly

Calls 2

funFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected