Exjpet - The easy way to ejpet in Elixir | search in JSON document

Hi

Exjpet is a thin wrapper around ejpet, a erlang library to check, search (and capture) in JSON documents.

By default, its uses Poison to handle JSON, but ejpet has backend for jsx, jiffy, jsone, mochiweb.

iex(1)> import Exjpet.Expression
iex(2)> epm = Exjpet.compile(list([:some, capture(object([with_key: "foo"]), as: "caught"), :some])) 
...
iex(3)> json = Exjpet.decode(~s([1, 2, {"foo": 42}, 3, "foo", {"bar": "foo"}]))
[1, 2, %{"foo" => 42}, 3, "foo", %{"bar" => "foo"}]
iex(4)> Exjpet.run(json, epm)                                                                       
{true, %{"caught" => [%{"foo" => 42}]}}

Cheers,

Nicolas -

6 Likes

And Jason? The new current JSON processor standard used in Phoenix and elsewhere that is used like poison but has near the speed of jiffy without the NIF? :slight_smile:

3 Likes

Done !

Thank for your comment :slight_smile:

2 Likes

Hi !

Exjpet now provides its own backend to ejpet leveraging compile-time matching function generation.

e.g. (from the doc)

defmodule MyMatcher do
  use Exjpet.Matcher

  match ~s("nomatch"), _state do
    :error
  end

  match ~s[{#"foo": (?<value>_)}], state do
    state
    |> Map.put(:node, jnode)
    |> Map.put(:captures, captures)
    |> Map.put(:value, value)
  end
end

state = %{fooNode: nil}
Poison.decode!(~s({"foobar": 42})) |> MyMatcher.match(state)
%{fooNode: nil, captures: %{"value" => '*'}, node: %{"foobar" => 42}, value: '*'}

Thanks to Elixir macros, the module MyMatcher is fully defined at compile-time, including all sub-expression matching functions.

Feel the power of custom json matching code, without paying the price :slight_smile:

I would be pleased to have your thoughts !