Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/JsonObject.h>
8#include <AK/JsonPath.h>
9#include <AK/JsonValue.h>
10
11namespace AK {
12
13JsonPathElement JsonPathElement::any_array_element { Kind::AnyIndex };
14JsonPathElement JsonPathElement::any_object_element { Kind::AnyKey };
15
16JsonValue JsonPath::resolve(JsonValue const& top_root) const
17{
18 auto root = top_root;
19 for (auto const& element : *this) {
20 switch (element.kind()) {
21 case JsonPathElement::Kind::Key:
22 root = JsonValue { root.as_object().get(element.key()).value() };
23 break;
24 case JsonPathElement::Kind::Index:
25 root = JsonValue { root.as_array().at(element.index()) };
26 break;
27 default:
28 VERIFY_NOT_REACHED();
29 }
30 }
31 return root;
32}
33
34DeprecatedString JsonPath::to_deprecated_string() const
35{
36 StringBuilder builder;
37 builder.append("{ ."sv);
38 for (auto const& el : *this) {
39 builder.append("sv > "sv);
40 builder.append(el.to_deprecated_string());
41 }
42 builder.append("sv }"sv);
43 return builder.to_deprecated_string();
44}
45
46}