diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2024-02-07 13:22:59 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2024-02-07 15:02:57 +0200 |
commit | adc9a34ee759e31dc1f7f9b98629042be3819815 (patch) | |
tree | 7feef9c4305fda1020bc9b915dde721a29b69d29 | |
parent | fcc239ecdbd1467a4ac8b17a353e1b0ae7fd63a0 (diff) |
Use reverse to fundamental types semantics in $json.member_value()
Feels like this is an equivalent context to subscript/iteration.
-rw-r--r-- | libbuild2/functions-json.cxx | 24 | ||||
-rw-r--r-- | libbuild2/variable.cxx | 5 | ||||
-rw-r--r-- | tests/function/json/testscript | 2 |
3 files changed, 26 insertions, 5 deletions
diff --git a/libbuild2/functions-json.cxx b/libbuild2/functions-json.cxx index 7551fa1..d7265ba 100644 --- a/libbuild2/functions-json.cxx +++ b/libbuild2/functions-json.cxx @@ -70,7 +70,29 @@ namespace build2 // for details). // if (v.type == json_type::object && v.object.size () == 1) - return move (v.object.front ().value); + { + // Reverse simple JSON values to the corresponding fundamental type + // values for consistency with subscript/iteration (see + // json_subscript_impl() for background). + // + json_value& jr (v.object.front ().value); + + switch (jr.type) + { +#if 0 + case json_type::null: return value (names {}); +#else + case json_type::null: return value (); +#endif + case json_type::boolean: return value (jr.boolean); + case json_type::signed_number: return value (jr.signed_number); + case json_type::unsigned_number: + case json_type::hexadecimal_number: return value (jr.unsigned_number); + case json_type::string: return value (move (jr.string)); + case json_type::array: + case json_type::object: return value (move (jr)); + } + } fail << "json object member expected instead of " << v.type << endf; }; diff --git a/libbuild2/variable.cxx b/libbuild2/variable.cxx index 1c899f9..3db0fd0 100644 --- a/libbuild2/variable.cxx +++ b/libbuild2/variable.cxx @@ -1993,11 +1993,11 @@ namespace build2 // opposed to reverse() where it is used to build up values and thus // needs things to be fully reversible). Once we add type hints, then // this should become unnecessary and we should be able to just always - // return json_value. + // return json_value. See also $json.member_value() where we do the + // same thing. // // @@ TODO: split this function into two (index/name) once get rid of this. // -#if 1 value r; switch (jr.type) { @@ -2018,7 +2018,6 @@ namespace build2 case json_type::array: case json_type::object: r = value (move (jr)); break; } -#endif return make_pair (move (r), true); } diff --git a/tests/function/json/testscript b/tests/function/json/testscript index f5fc671..fdfc9ab 100644 --- a/tests/function/json/testscript +++ b/tests/function/json/testscript @@ -198,7 +198,7 @@ EOO for m: $j print $member_name($m) $member_value($m) EOI - str "abc" + str abc num 123 arr [1,2,3] obj {"one":1,"two":2,"three":3} |