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
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
#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-04-01 17:58:27 +00:00
|
|
|
String 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
String to_string() const
|
|
|
|
{
|
|
|
|
switch (m_kind) {
|
|
|
|
case Kind::Key:
|
|
|
|
return key();
|
|
|
|
case Kind::Index:
|
|
|
|
return String::number(index());
|
|
|
|
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;
|
|
|
|
String m_key;
|
|
|
|
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;
|
2020-06-30 18:33:53 +00:00
|
|
|
String to_string() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|