95 lines
2.8 KiB
C#
95 lines
2.8 KiB
C#
using DevDisciples.Parsing;
|
|
|
|
namespace DevDisciples.Json.Tools;
|
|
|
|
// See https://docs.hevodata.com/sources/engg-analytics/streaming/rest-api/writing-jsonpath-expressions/
|
|
|
|
public static partial class JsonPath
|
|
{
|
|
public interface IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
}
|
|
|
|
public readonly struct WildCardSyntax : IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
|
|
public WildCardSyntax(Lexer<JsonPathToken>.Token token)
|
|
{
|
|
Token = token;
|
|
}
|
|
}
|
|
|
|
public readonly struct ArrayIndexSyntax : IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
public Lexer<JsonPathToken>.Token Index { get; }
|
|
public int IndexAsInt => int.Parse(Token.Lexeme);
|
|
|
|
public ArrayIndexSyntax(Lexer<JsonPathToken>.Token token, Lexer<JsonPathToken>.Token index)
|
|
{
|
|
Token = token;
|
|
Index = index;
|
|
}
|
|
}
|
|
|
|
public readonly struct ArrayIndexListSyntax : IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
public Lexer<JsonPathToken>.Token[] Indices { get; }
|
|
public int ValueAt(int index) => int.Parse(Indices[index].Lexeme);
|
|
|
|
public ArrayIndexListSyntax(Lexer<JsonPathToken>.Token token, Lexer<JsonPathToken>.Token[] indices)
|
|
{
|
|
Token = token;
|
|
Indices = indices;
|
|
}
|
|
}
|
|
|
|
public readonly struct ObjectIndexSyntax : IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
public Lexer<JsonPathToken>.Token Index { get; }
|
|
|
|
public ObjectIndexSyntax(Lexer<JsonPathToken>.Token token, Lexer<JsonPathToken>.Token index)
|
|
{
|
|
Token = token;
|
|
Index = index;
|
|
}
|
|
}
|
|
|
|
public readonly struct ObjectIndexListSyntax : IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
public Lexer<JsonPathToken>.Token[] Indexes { get; }
|
|
|
|
public ObjectIndexListSyntax(Lexer<JsonPathToken>.Token token, Lexer<JsonPathToken>.Token[] indexes)
|
|
{
|
|
Token = token;
|
|
Indexes = indexes;
|
|
}
|
|
}
|
|
|
|
public readonly struct PropertyAccessorSyntax : IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
public IJsonPathSyntax Getter { get; }
|
|
|
|
public PropertyAccessorSyntax(Lexer<JsonPathToken>.Token token, IJsonPathSyntax getter)
|
|
{
|
|
Token = token;
|
|
Getter = getter;
|
|
}
|
|
}
|
|
|
|
public readonly struct PropertySyntax : IJsonPathSyntax
|
|
{
|
|
public Lexer<JsonPathToken>.Token Token { get; }
|
|
|
|
public PropertySyntax(Lexer<JsonPathToken>.Token token)
|
|
{
|
|
Token = token;
|
|
}
|
|
}
|
|
} |