|
| 1 | +/* Copyright (c) 2013 Dropbox, Inc. |
| 2 | + * |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + * of this software and associated documentation files (the "Software"), to deal |
| 5 | + * in the Software without restriction, including without limitation the rights |
| 6 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + * copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in |
| 11 | + * all copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | + * THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "json_helper.hpp" |
| 23 | +#include "json.hpp" |
| 24 | + |
| 25 | +namespace json { |
| 26 | + |
| 27 | +// Constructors |
| 28 | +Json::Json() noexcept : m_json(nullptr) {} |
| 29 | +Json::Json(std::nullptr_t) noexcept : m_json(nullptr) {} |
| 30 | +Json::Json(double value) : m_json(value) {} |
| 31 | +Json::Json(int value) : m_json(value) {} |
| 32 | +Json::Json(bool value) : m_json(value) {} |
| 33 | +Json::Json(const std::string &value) : m_json(value) {} |
| 34 | +Json::Json(std::string &&value) : m_json(std::move(value)) {} |
| 35 | +Json::Json(const char * value) : m_json(value) {} |
| 36 | +Json::Json(const array &values) : m_json(nlohmann::json::array()) { |
| 37 | + for (const auto& v : values) { |
| 38 | + m_json.push_back(v.m_json); |
| 39 | + } |
| 40 | +} |
| 41 | +Json::Json(array &&values) : m_json(nlohmann::json::array()) { |
| 42 | + for (auto& v : values) { |
| 43 | + m_json.push_back(std::move(v.m_json)); |
| 44 | + } |
| 45 | +} |
| 46 | +Json::Json(const object &values) : m_json(nlohmann::json::object()) { |
| 47 | + for (const auto& kv : values) { |
| 48 | + m_json[kv.first] = kv.second.m_json; |
| 49 | + } |
| 50 | +} |
| 51 | +Json::Json(object &&values) : m_json(nlohmann::json::object()) { |
| 52 | + for (auto& kv : values) { |
| 53 | + m_json[kv.first] = std::move(kv.second.m_json); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +Json::Json(const nlohmann::json& j) : m_json(j) {} |
| 58 | + |
| 59 | +// Type accessors |
| 60 | +Json::Type Json::type() const { |
| 61 | + if (m_json.is_null()) return NUL; |
| 62 | + if (m_json.is_number()) return NUMBER; |
| 63 | + if (m_json.is_boolean()) return BOOL; |
| 64 | + if (m_json.is_string()) return STRING; |
| 65 | + if (m_json.is_array()) return ARRAY; |
| 66 | + if (m_json.is_object()) return OBJECT; |
| 67 | + return NUL; |
| 68 | +} |
| 69 | + |
| 70 | +// Accessors |
| 71 | +double Json::number_value() const { return m_json.get<double>(); } |
| 72 | +int Json::int_value() const { return m_json.get<int>(); } |
| 73 | +bool Json::bool_value() const { return m_json.get<bool>(); } |
| 74 | +const std::string& Json::string_value() const { |
| 75 | + static const std::string empty; |
| 76 | + return m_json.is_string() ? m_json.get_ref<const std::string&>() : empty; |
| 77 | +} |
| 78 | + |
| 79 | +const Json::array& Json::array_items() const { |
| 80 | + static const array empty; |
| 81 | + if (!is_array()) return empty; |
| 82 | + static thread_local array items; |
| 83 | + items.clear(); |
| 84 | + for (const auto& item : m_json) { |
| 85 | + items.emplace_back(item); |
| 86 | + } |
| 87 | + return items; |
| 88 | +} |
| 89 | + |
| 90 | +const Json::object& Json::object_items() const { |
| 91 | + static const object empty; |
| 92 | + if (!is_object()) return empty; |
| 93 | + static thread_local object items; |
| 94 | + items.clear(); |
| 95 | + for (auto it = m_json.begin(); it != m_json.end(); ++it) { |
| 96 | + items.emplace(it.key(), Json(it.value())); |
| 97 | + } |
| 98 | + return items; |
| 99 | +} |
| 100 | + |
| 101 | +// Operators |
| 102 | +const Json& Json::operator[](size_t i) const { |
| 103 | + static const Json empty; |
| 104 | + if (!is_array() || i >= m_json.size()) return empty; |
| 105 | + static thread_local Json result; |
| 106 | + result = Json(m_json[i]); |
| 107 | + return result; |
| 108 | +} |
| 109 | + |
| 110 | +const Json& Json::operator[](const std::string& key) const { |
| 111 | + static const Json empty; |
| 112 | + if (!is_object()) return empty; |
| 113 | + auto it = m_json.find(key); |
| 114 | + if (it == m_json.end()) return empty; |
| 115 | + static thread_local Json result; |
| 116 | + result = Json(*it); |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | +bool Json::operator==(const Json& other) const { |
| 121 | + return m_json == other.m_json; |
| 122 | +} |
| 123 | + |
| 124 | +bool Json::operator<(const Json& other) const { |
| 125 | + return m_json < other.m_json; |
| 126 | +} |
| 127 | + |
| 128 | +// Parsing |
| 129 | +Json Json::parse(const std::string& in, std::string& err, JsonParse strategy) { |
| 130 | + try { |
| 131 | + auto result = nlohmann::json::parse(in); |
| 132 | + err.clear(); |
| 133 | + return Json(result); |
| 134 | + } catch (const nlohmann::json::parse_error& e) { |
| 135 | + err = e.what(); |
| 136 | + return Json(); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +std::vector<Json> Json::parse_multi(const std::string& in, std::string::size_type& parser_stop_pos, std::string& err, JsonParse strategy) { |
| 141 | + std::vector<Json> result; |
| 142 | + try { |
| 143 | + auto json_array = nlohmann::json::parse("[" + in + "]"); |
| 144 | + for (const auto& element : json_array) { |
| 145 | + result.emplace_back(element); |
| 146 | + } |
| 147 | + parser_stop_pos = in.size(); |
| 148 | + err.clear(); |
| 149 | + } catch (const nlohmann::json::parse_error& e) { |
| 150 | + err = e.what(); |
| 151 | + } |
| 152 | + return result; |
| 153 | +} |
| 154 | + |
| 155 | +void Json::dump(std::string& out) const { |
| 156 | + out = m_json.dump(); |
| 157 | +} |
| 158 | + |
| 159 | +bool Json::has_shape(const shape& types, std::string& err) const { |
| 160 | + if (!is_object()) { |
| 161 | + err = "expected JSON object, got " + dump(); |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + for (const auto& item : types) { |
| 166 | + const auto it = m_json.find(item.first); |
| 167 | + if (it == m_json.end() || Json(*it).type() != item.second) { |
| 168 | + err = "bad type for " + item.first + " in " + dump(); |
| 169 | + return false; |
| 170 | + } |
| 171 | + } |
| 172 | + return true; |
| 173 | +} |
| 174 | + |
| 175 | +} // namespace json |
0 commit comments