2020-06-30 18:33:53 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-06-30 18:33:53 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-30 18:33:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonPath.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
JsonPathElement JsonPathElement::any_array_element { Kind::AnyIndex };
|
|
|
|
JsonPathElement JsonPathElement::any_object_element { Kind::AnyKey };
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
JsonValue JsonPath::resolve(JsonValue const& top_root) const
|
2020-06-30 18:33:53 +00:00
|
|
|
{
|
|
|
|
auto root = top_root;
|
2021-12-15 13:49:35 +00:00
|
|
|
for (auto const& element : *this) {
|
2020-06-30 18:33:53 +00:00
|
|
|
switch (element.kind()) {
|
|
|
|
case JsonPathElement::Kind::Key:
|
2022-12-21 14:37:12 +00:00
|
|
|
root = JsonValue { root.as_object().get(element.key()).value() };
|
2020-06-30 18:33:53 +00:00
|
|
|
break;
|
|
|
|
case JsonPathElement::Kind::Index:
|
|
|
|
root = JsonValue { root.as_array().at(element.index()) };
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-30 18:33:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString JsonPath::to_deprecated_string() const
|
2020-06-30 18:33:53 +00:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("{ ."sv);
|
2021-12-15 13:49:35 +00:00
|
|
|
for (auto const& el : *this) {
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("sv > "sv);
|
2022-12-06 01:12:49 +00:00
|
|
|
builder.append(el.to_deprecated_string());
|
2020-06-30 18:33:53 +00:00
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("sv }"sv);
|
2022-12-06 01:12:49 +00:00
|
|
|
return builder.to_deprecated_string();
|
2020-06-30 18:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|