Skip to content

Add JSON support #789

@paule96

Description

@paule96

1. Description

It would be nice to directly access JsonElements / JsonObjects properties and use them in queries. (for simplification I will only use JsonElements from System.Text.Json in the following issue. But Newtonssoft.json is important too)

So that you can easily do something like:

var jsonDocument = JsonDocument.Parse(@"[
    {
        ""first"": 1,
        ""City"": ""Paris"",
        ""third"": ""012-04-23T18:25:43.511Z""
    }]");
jsonDocument.RootElement.Where("City ==\"Paris\"");

Currently, it will tell you that the JsonElement doesn't contain the property City. What is true but also not.
The correct C# linq Query would look like:

jsonDocument.RootElement.Where(e => e.GetProperty("City").GetRawText() == "Paris");

It would be nice to have a default translator for JsonElement to "normal" dynamic linq queries.
It would be especially useful when the Expression and the JSON is not defined.

I'm aware of the issue #415. He showed a way how you could first convert the JsonElement to a dynamic type. But that would mean to create always a new dynamic type if you do not know the type. And technically I know the type, but the type is not nice to query with Linq. (JsonElement) This solution makes it also impossible to cache the compiled query, because I don't know the JSON structure and that means all the time I need to change the input parameter of my expression, which forces a recompile.

So for me the best solution would be if dynamicLinq could detect that the starting parameter of the expression is a JsonElement and then restructur internally the Expressions to the correct format.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions