Mercurial > code
comparison cpp/json_util/json_util.cpp @ 630:04dffcdb8e1a
json_util: initial import, closes #776 @1h
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 20 Mar 2018 13:18:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
629:3a4107730b24 | 630:04dffcdb8e1a |
---|---|
1 /* | |
2 * json_util.cpp -- utilities for JSON | |
3 * | |
4 * Copyright (c) 2018 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include "json_util.hpp" | |
20 | |
21 namespace json_util { | |
22 | |
23 boost::optional<nlohmann::json> get(const nlohmann::json& json, | |
24 const nlohmann::json::json_pointer& key) noexcept | |
25 { | |
26 // Unfortunately, there is no find using pointer yet. | |
27 try { | |
28 return json.at(key); | |
29 } catch (...) { | |
30 return boost::none; | |
31 } | |
32 } | |
33 | |
34 boost::optional<bool> get_bool(const nlohmann::json& json, | |
35 const nlohmann::json::json_pointer& key) noexcept | |
36 { | |
37 const auto v = get(json, key); | |
38 | |
39 if (!v || !v->is_boolean()) | |
40 return boost::none; | |
41 | |
42 return v->get<bool>(); | |
43 } | |
44 | |
45 boost::optional<std::uint64_t> get_int(const nlohmann::json& json, | |
46 const nlohmann::json::json_pointer& key) noexcept | |
47 { | |
48 const auto v = get(json, key); | |
49 | |
50 if (!v || !v->is_number_integer()) | |
51 return boost::none; | |
52 | |
53 return v->get<std::uint64_t>(); | |
54 } | |
55 | |
56 boost::optional<std::uint64_t> get_uint(const nlohmann::json& json, | |
57 const nlohmann::json::json_pointer& key) noexcept | |
58 { | |
59 const auto v = get(json, key); | |
60 | |
61 if (!v || !v->is_number_unsigned()) | |
62 return boost::none; | |
63 | |
64 return v->get<std::uint64_t>(); | |
65 } | |
66 | |
67 boost::optional<std::string> get_string(const nlohmann::json& json, | |
68 const nlohmann::json::json_pointer& key) noexcept | |
69 { | |
70 const auto v = get(json, key); | |
71 | |
72 if (!v || !v->is_string()) | |
73 return boost::none; | |
74 | |
75 return v->get<std::string>(); | |
76 } | |
77 | |
78 boost::optional<bool> optional_bool(const nlohmann::json& json, | |
79 const nlohmann::json::json_pointer& key, | |
80 bool def) noexcept | |
81 { | |
82 const auto v = get(json, key); | |
83 | |
84 if (!v) | |
85 return def; | |
86 if (!v->is_boolean()) | |
87 return boost::none; | |
88 | |
89 return v->get<bool>(); | |
90 } | |
91 | |
92 boost::optional<std::int64_t> optional_int(const nlohmann::json& json, | |
93 const nlohmann::json::json_pointer& key, | |
94 std::int64_t def) noexcept | |
95 { | |
96 const auto v = get(json, key); | |
97 | |
98 if (!v) | |
99 return def; | |
100 if (!v->is_number_integer()) | |
101 return boost::none; | |
102 | |
103 return v->get<std::int64_t>(); | |
104 } | |
105 | |
106 boost::optional<std::uint64_t> optional_uint(const nlohmann::json& json, | |
107 const nlohmann::json::json_pointer& key, | |
108 std::uint64_t def) noexcept | |
109 { | |
110 const auto v = get(json, key); | |
111 | |
112 if (!v) | |
113 return def; | |
114 if (!v->is_number_unsigned()) | |
115 return boost::none; | |
116 | |
117 return v->get<std::uint64_t>(); | |
118 } | |
119 | |
120 boost::optional<std::string> optional_string(const nlohmann::json& json, | |
121 const nlohmann::json::json_pointer& key, | |
122 const std::string& def) noexcept | |
123 { | |
124 const auto v = get(json, key); | |
125 | |
126 if (!v) | |
127 return def; | |
128 if (!v->is_string()) | |
129 return boost::none; | |
130 | |
131 return v->get<std::string>(); | |
132 } | |
133 | |
134 std::string pretty(const nlohmann::json& value) | |
135 { | |
136 switch (value.type()) { | |
137 case nlohmann::json::value_t::boolean: | |
138 return value.get<bool>() ? "true" : "false"; | |
139 case nlohmann::json::value_t::string: | |
140 return value.get<std::string>(); | |
141 default: | |
142 return value.dump(); | |
143 } | |
144 } | |
145 | |
146 bool contains(const nlohmann::json& array, const nlohmann::json& value) noexcept | |
147 { | |
148 for (const auto &v : array) | |
149 if (v == value) | |
150 return true; | |
151 | |
152 return false; | |
153 } | |
154 | |
155 } // !json_util |