Using OData to query a collection of "random" JsonObject

20 Views Asked by At

I'm trying to create an apicontroller that can use odata syntax to filter a collection of json objects. This collection can be from json columns in databases or any other datastore. The important thing is that there is no EDM, no entity specific models. The JsonObject can have any form and any amount of properties.

I can get basic things like $skip and $top working with a simple function like this:

  [HttpGet]
  public PageResult<JsonNode> GetItems(ODataQueryOptions<JsonNode> options)
  {
      int total = _store.GetData().Count();
      return new PageResult<JsonNode>(options.ApplyTo(_store.GetData().AsQueryable(), AllowedQueryOptions.None).Cast<JsonNode>(), null, total);
     
  }

For purpose of this question _store.GetData() returns JsonArray. The contained JsonObjects kan have any number of properties.

When I try to use $filter he ApplyTo function fails with an exception stating that there is no such property on the JsonNode class. That is offcourse true.

This brings me to the first question: Is it at all possible to use the asp.net odata implementation to query an object with an indexer?

I have also found several references to using a "custom filter binder"

https://devblogs.microsoft.com/odata/customizing-filter-for-spatial-data-in-asp-net-core-odata-8/

They mention implementing IFilterBinder interface or inheriting from the FilterBinder class.

I think from this it should be possible to use this to change expressions fro property names in a filter from 'title == test' to jsonobject['title'] == "test".

Does anyone have an example or good instructions on how to do this?

Better solutions are welcome, but the data is what it is and I still need to query it. I like to avoid writing a complete parser for filter myself from scratch if there is anything standard in the framework i can use.

0

There are 0 best solutions below