2020-06-30 21:33:53 +03:00
|
|
|
/*
|
2021-04-28 23:46:44 +03:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-06-30 21:33:53 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-30 21:33:53 +03: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 };
|
|
|
|
|
|
|
|
JsonValue JsonPath::resolve(const JsonValue& top_root) const
|
|
|
|
{
|
|
|
|
auto root = top_root;
|
2021-12-15 16:49:35 +03:00
|
|
|
for (auto const& element : *this) {
|
2020-06-30 21:33:53 +03:00
|
|
|
switch (element.kind()) {
|
|
|
|
case JsonPathElement::Kind::Key:
|
|
|
|
root = JsonValue { root.as_object().get(element.key()) };
|
|
|
|
break;
|
|
|
|
case JsonPathElement::Kind::Index:
|
|
|
|
root = JsonValue { root.as_array().at(element.index()) };
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-30 21:33:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
String JsonPath::to_string() const
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append("{ .");
|
2021-12-15 16:49:35 +03:00
|
|
|
for (auto const& el : *this) {
|
2020-06-30 21:33:53 +03:00
|
|
|
builder.append(" > ");
|
|
|
|
builder.append(el.to_string());
|
|
|
|
}
|
|
|
|
builder.append(" }");
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|