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