1 #ifndef CHAISCRIPT_SIMPLEJSON_WRAP_HPP 2 #define CHAISCRIPT_SIMPLEJSON_WRAP_HPP 10 m.add(
chaiscript::fun([](
const std::string &t_str) {
return from_json(t_str); }),
"from_json");
18 switch (t_json.JSONType()) {
19 case json::JSON::Class::Null:
21 case json::JSON::Class::Object: {
22 std::map<std::string, Boxed_Value> m;
24 for (
const auto &p : t_json.object_range()) {
25 m.insert(std::make_pair(p.first, from_json(p.second)));
30 case json::JSON::Class::Array: {
31 std::vector<Boxed_Value> vec;
33 for (
const auto &p : t_json.array_range()) {
34 vec.emplace_back(from_json(p));
39 case json::JSON::Class::String:
41 case json::JSON::Class::Floating:
43 case json::JSON::Class::Integral:
45 case json::JSON::Class::Boolean:
49 throw std::runtime_error(
"Unknown JSON type");
52 static Boxed_Value from_json(
const std::string &t_json) {
54 return from_json(json::JSON::Load(t_json));
55 }
catch (
const std::out_of_range &) {
56 throw std::runtime_error(
"Unparsed JSON input");
60 static std::string to_json(
const Boxed_Value &t_bv) {
return to_json_object(t_bv).dump(); }
64 const std::map<std::string, Boxed_Value> m =
chaiscript::boxed_cast<
const std::map<std::string, Boxed_Value> &>(t_bv);
67 for (
const auto &o : m) {
68 obj[o.first] = to_json_object(o.second);
79 for (
size_t i = 0; i < v.size(); ++i) {
80 obj[i] = to_json_object(v[i]);
89 if (Boxed_Number::is_floating_point(t_bv)) {
105 return json::JSON(boxed_cast<std::string>(t_bv));
114 for (
const auto &attr : o.get_attrs()) {
115 obj[attr.first] = to_json_object(attr.second);
125 throw std::runtime_error(
"Unknown object type to convert to JSON");
Holds a collection of ChaiScript settings which can be applied to the ChaiScript runtime.
Definition: dispatchkit.hpp:128
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:71
Definition: json_wrap.hpp:7
Namespace chaiscript contains every API call that the average user will be concerned with...
A wrapper for holding any valid C++ type.
Definition: boxed_value.hpp:24
Thrown in the event that a Boxed_Value cannot be cast to the desired type.
Definition: bad_boxed_cast.hpp:31
Definition: dynamic_object.hpp:38
Thrown in the event that an Any cannot be cast to the desired type.
Definition: any.hpp:20
Proxy_Function fun(T &&t)
Creates a new Proxy_Function object from a free function, member function or data member...
Definition: register_function.hpp:81
Represents any numeric type, generically. Used internally for generic operations between POD values...
Definition: boxed_number.hpp:60