1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9 
10 #include <boost/json.hpp>
11 
12 using namespace boost::json;
13 
14 value&
path(value & jv,std::size_t index)15 path( value& jv, std::size_t index)
16 {
17     array *arr;
18     if(jv.is_null())
19         arr = &jv.emplace_array();
20     else
21         arr = &jv.as_array();
22     if(arr->size() <= index)
23         arr->resize(index + 1);
24     return (*arr)[index];
25 }
26 
27 value&
path(value & jv,string_view key)28 path( value& jv, string_view key )
29 {
30     object* obj;
31     if(jv.is_null())
32         obj = &jv.emplace_object();
33     else
34         obj = &jv.as_object();
35     return (*obj)[key];
36 }
37 
38 template<class Arg0, class Arg1, class... Args>
39 value&
path(value & jv,Arg0 const & arg0,Arg1 const & arg1,Args const &...args)40 path( value& jv, Arg0 const& arg0, Arg1 const& arg1, Args const&... args )
41 {
42     return path( path( jv, arg0 ), arg1, args... );
43 }
44 
45 int
main(int,char **)46 main(int, char**)
47 {
48     value jv;
49     path( jv, "user:config", "authority", "router", 0, "users" ) = 42;
50     return EXIT_SUCCESS;
51 }
52