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
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-06-30 18:33:53 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class JsonPathElement {
|
|
|
|
public:
|
|
|
|
enum class Kind {
|
|
|
|
Key,
|
|
|
|
Index,
|
|
|
|
AnyIndex,
|
|
|
|
AnyKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonPathElement(size_t index)
|
|
|
|
: m_kind(Kind::Index)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
JsonPathElement(StringView key)
|
2020-06-30 18:33:53 +00:00
|
|
|
: m_kind(Kind::Key)
|
|
|
|
, m_key(key)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Kind kind() const { return m_kind; }
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString const& key() const
|
2020-06-30 18:33:53 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_kind == Kind::Key);
|
2020-06-30 18:33:53 +00:00
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t index() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_kind == Kind::Index);
|
2020-06-30 18:33:53 +00:00
|
|
|
return m_index;
|
|
|
|
}
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString to_deprecated_string() const
|
2020-06-30 18:33:53 +00:00
|
|
|
{
|
|
|
|
switch (m_kind) {
|
|
|
|
case Kind::Key:
|
|
|
|
return key();
|
|
|
|
case Kind::Index:
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::number(index());
|
2020-06-30 18:33:53 +00:00
|
|
|
default:
|
|
|
|
return "*";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonPathElement any_array_element;
|
|
|
|
static JsonPathElement any_object_element;
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(JsonPathElement const& other) const
|
2020-06-30 18:33:53 +00:00
|
|
|
{
|
|
|
|
switch (other.kind()) {
|
|
|
|
case Kind::Key:
|
|
|
|
return (m_kind == Kind::Key && other.key() == key()) || m_kind == Kind::AnyKey;
|
|
|
|
case Kind::Index:
|
|
|
|
return (m_kind == Kind::Index && other.index() == index()) || m_kind == Kind::AnyIndex;
|
|
|
|
case Kind::AnyKey:
|
|
|
|
return m_kind == Kind::Key;
|
|
|
|
case Kind::AnyIndex:
|
|
|
|
return m_kind == Kind::Index;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Kind m_kind;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_key;
|
2020-06-30 18:33:53 +00:00
|
|
|
size_t m_index { 0 };
|
|
|
|
|
|
|
|
JsonPathElement(Kind kind)
|
|
|
|
: m_kind(kind)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class JsonPath : public Vector<JsonPathElement> {
|
|
|
|
public:
|
2022-04-01 17:58:27 +00:00
|
|
|
JsonValue resolve(JsonValue const&) const;
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString to_deprecated_string() const;
|
2020-06-30 18:33:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-06-30 18:33:53 +00:00
|
|
|
using AK::JsonPath;
|
|
|
|
using AK::JsonPathElement;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|