Commit 5bb7112
Json updates (#37)
* Provide json_encode for const char*
* json_encode for std::vector and std::map: propagate errors
* Implement json_encode for pointers and std::array
Testing revealed difficulties regarding extending json_encode with
user-defined types. In the following chain of calls:
struct S { ... };
json_encode(std::string, std::vector<std::array<S, 6>>); // 1
json_encode(std::ostream, std::vector<std::array<S, 6>>); // 2
json_encode(std::ostream, std::array<S, 6>*, ...); // 3
json_encode(std::ostream, std::array<S, 6>); // 4
json_encode(std::ostream, S*); // 5
json_encode(std::ostream, S); // 6, implemented in the user code
the compiler might only be able to find the user function if it was
called with unqualified names and argument-dependent lookup took
action. Also, without forward declarations provided by this patch the
compiler could not reach step 4 since json_encode for array has been
declared after json_encode for pointers. This suggests possible problems
with user and other STL containers. See
https://stackoverflow.com/questions/28187170/c-enforce-second-pass-name-lookup-in-template-function
https://akrzemi1.wordpress.com/2015/11/19/overload-resolution/
for the discussion.
One possible solution is to declare
template <typename T> void json_encode(std::ostream&, const T&);
and use template specializations instead of overloaded functions, but it
needs special casing to exclude arithmetic types implemented by a
different template --- and possibly it may clash in a similar way with
other user defined types.
* Implement json_encode_object
---------
Co-authored-by: Evgenii Zhemchugov <[email protected]>1 parent e277289 commit 5bb7112
2 files changed
+84
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
14 | 18 | | |
15 | 19 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
13 | 22 | | |
14 | 23 | | |
15 | 24 | | |
16 | 25 | | |
17 | 26 | | |
18 | 27 | | |
19 | 28 | | |
| 29 | + | |
20 | 30 | | |
21 | 31 | | |
22 | 32 | | |
23 | | - | |
| 33 | + | |
24 | 34 | | |
25 | 35 | | |
26 | | - | |
| 36 | + | |
27 | 37 | | |
28 | 38 | | |
29 | | - | |
30 | | - | |
31 | | - | |
| 39 | + | |
| 40 | + | |
32 | 41 | | |
33 | 42 | | |
34 | 43 | | |
35 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
36 | 55 | | |
37 | 56 | | |
38 | 57 | | |
| |||
43 | 62 | | |
44 | 63 | | |
45 | 64 | | |
46 | | - | |
| 65 | + | |
47 | 66 | | |
48 | | - | |
| 67 | + | |
49 | 68 | | |
50 | 69 | | |
51 | 70 | | |
| |||
59 | 78 | | |
60 | 79 | | |
61 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
62 | 127 | | |
63 | 128 | | |
| 129 | + | |
| 130 | + | |
64 | 131 | | |
0 commit comments