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

Method to_json_object

include/chaiscript/utility/json_wrap.hpp:78–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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);
109 if (Boxed_Number::is_floating_point(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);

Callers

nothing calls this directly

Calls 4

JSONClass · 0.85
get_attrsMethod · 0.80
sizeMethod · 0.45
is_nullMethod · 0.45

Tested by

no test coverage detected