mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-01-01 03:12:23 +08:00
In to_json(), maintain the "type" of empty maps and vectors
- fix issue #399. - make to_json() with an empty Map, Vector or Dynamic_Object return a similar/compatible type (JSON object or array), rather than "null". - include the fix for #381 (to_json() support for null values), so that can also be unit tested.
This commit is contained in:
parent
136539867c
commit
783b8b7361
@ -80,7 +80,7 @@ namespace chaiscript
|
||||
try {
|
||||
const std::map<std::string, Boxed_Value> m = chaiscript::boxed_cast<const std::map<std::string, Boxed_Value> &>(t_bv);
|
||||
|
||||
json::JSON obj;
|
||||
json::JSON obj(json::JSON::Class::Object);
|
||||
for (const auto &o : m)
|
||||
{
|
||||
obj[o.first] = to_json_object(o.second);
|
||||
@ -93,7 +93,7 @@ namespace chaiscript
|
||||
try {
|
||||
const std::vector<Boxed_Value> v = chaiscript::boxed_cast<const std::vector<Boxed_Value> &>(t_bv);
|
||||
|
||||
json::JSON obj;
|
||||
json::JSON obj(json::JSON::Class::Array);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
{
|
||||
obj[i] = to_json_object(v[i]);
|
||||
@ -132,7 +132,7 @@ namespace chaiscript
|
||||
try {
|
||||
const chaiscript::dispatch::Dynamic_Object &o = boxed_cast<const dispatch::Dynamic_Object &>(t_bv);
|
||||
|
||||
json::JSON obj;
|
||||
json::JSON obj(json::JSON::Class::Object);
|
||||
for (const auto &attr : o.get_attrs())
|
||||
{
|
||||
obj[attr.first] = to_json_object(attr.second);
|
||||
@ -142,6 +142,8 @@ namespace chaiscript
|
||||
// not a dynamic object
|
||||
}
|
||||
|
||||
if (t_bv.is_null()) return json::JSON(); // a null value
|
||||
|
||||
throw std::runtime_error("Unknown object type to convert to JSON");
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user