1#ifndef CHAISCRIPT_SIMPLEJSON_WRAP_HPP
2#define CHAISCRIPT_SIMPLEJSON_WRAP_HPP
15 m.
add(
chaiscript::fun([](
const std::string &t_str) {
return from_json(t_str); }),
"from_json");
31 std::map<std::string, Boxed_Value> m;
35 m.insert(std::make_pair(p.first, from_json(p.second)));
38 return Boxed_Value(m);
42 std::vector<Boxed_Value> vec;
46 vec.emplace_back(from_json(p));
49 return Boxed_Value(vec);
54 return Boxed_Value(t_json.
to_float());
56 return Boxed_Value(t_json.
to_int());
58 return Boxed_Value(t_json.
to_bool());
61 throw std::runtime_error(
"Unknown JSON type");
64 static Boxed_Value from_json(
const std::string &t_json)
68 }
catch (
const std::out_of_range& ) {
69 throw std::runtime_error(
"Unparsed JSON input");
73 static std::string to_json(
const Boxed_Value &t_bv)
75 return to_json_object(t_bv).dump();
78 static json::JSON to_json_object(
const Boxed_Value &t_bv)
84 for (
const auto &o : m)
86 obj[o.first] = to_json_object(o.second);
89 }
catch (
const chaiscript::exception::bad_boxed_cast &) {
97 for (
size_t i = 0; i < v.size(); ++i)
99 obj[i] = to_json_object(v[i]);
102 }
catch (
const chaiscript::exception::bad_boxed_cast &) {
108 Boxed_Number bn(t_bv);
111 return json::JSON(bn.get_as<
double>());
113 return json::JSON(bn.get_as<std::int64_t>());
115 }
catch (
const chaiscript::detail::exception::bad_any_cast &) {
121 }
catch (
const chaiscript::exception::bad_boxed_cast &) {
127 }
catch (
const chaiscript::exception::bad_boxed_cast &) {
138 obj[attr.first] = to_json_object(attr.second);
141 }
catch (
const chaiscript::exception::bad_boxed_cast &) {
145 if (t_bv.is_null())
return json::JSON();
147 throw std::runtime_error(
"Unknown object type to convert to JSON");
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
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