elixir-sqlite/ecto_sqlite3

Support for the JSON module added in Elixir 1.18

Closed this issue ยท 11 comments

It is currently not possible to use EctoSQLite3 configured to use the JSON module which was added in Elixir 1.18 because it does not define an encode function, only encode! / encode_to_iodata!.

As of right now, I'm not sure if this is an issue that should be fixed in Elixir itself or in EctoSQLite3.

Need to make it configurable for sure.

@Th3-M4jor you should be able to use the JSON module provided by Elixir 1.18

https://github.com/elixir-lang/elixir/blame/v1.18.1/lib/elixir/lib/json.ex

They implemented the same encode/decode calls.

config :my_app, :ecto_sqlite3, json_library: JSON

When I tried that on my app, tests fail because of

** (UndefinedFunctionError) function JSON.encode/1 is undefined or private. Did you mean:

           * decode/1
           * decode/3
           * decode!/1
           * encode!/1
           * encode!/2
     
     stacktrace:
       (elixir 1.18.1) JSON.encode(%{})
       (ecto 3.12.5) lib/ecto/type.ex:1045: Ecto.Type.process_dumpers/3
       (ecto 3.12.5) lib/ecto/repo/schema.ex:1110: Ecto.Repo.Schema.dump_field!/6
       (ecto 3.12.5) lib/ecto/repo/schema.ex:213: Ecto.Repo.Schema.extract_value/6
       (elixir 1.18.1) lib/enum.ex:1846: anonymous fn/3 in Enum.map_reduce/3
       (stdlib 6.2) maps.erl:860: :maps.fold_1/4
       (elixir 1.18.1) lib/enum.ex:2558: Enum.map_reduce/3
       (ecto 3.12.5) lib/ecto/repo/schema.ex:87: anonymous fn/5 in Ecto.Repo.Schema.extract_header_and_fields/8
       (elixir 1.18.1) lib/enum.ex:1840: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (ecto 3.12.5) lib/ecto/repo/schema.ex:86: Ecto.Repo.Schema.extract_header_and_fields/8
       (ecto 3.12.5) lib/ecto/repo/schema.ex:48: Ecto.Repo.Schema.do_insert_all/7
       (elixir 1.18.1) lib/enum.ex:992: anonymous fn/3 in Enum.each/2
       (elixir 1.18.1) lib/enum.ex:4511: anonymous fn/3 in Enum.each/2
       (elixir 1.18.1) lib/stream.ex:1761: anonymous fn/3 in Enumerable.Stream.reduce/3
       (elixir 1.18.1) lib/stream.ex:302: Stream.after_chunk_while/2
       (elixir 1.18.1) lib/stream.ex:1790: Enumerable.Stream.do_done/2
       (elixir 1.18.1) lib/enum.ex:4511: Enum.each/2
       (ecto_sql 3.12.1) lib/ecto/adapters/sql.ex:1400: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
       (db_connection 2.7.0) lib/db_connection.ex:1756: DBConnection.run_transaction/4
       ......

@josevalim do you have a suggestion on how best to support Elixir 1.17 with Jason and Elixir 1.18 with the stdlib JSON? I can flip the call to use encode! and decode!, but I am unsure if that is a desirable alteration for ecto.

It should be opt-in, as you mentioned, in order to keep compatibility. But we may need to refine our API usage in Ecto. Please open up an issue.

Sorry, use encode! but decode (without bang!).

Yea fair point. I'll use that on decode

@Th3-M4jor I pushed an update for this in 0.18.1

Just pulled it in and all my unit tests pass with it. Thanks!

Good to hear. Happy bug feature making ๐Ÿ˜„

I faced this issue, checked the source code and the configuration should be under :ecto_sqlite3 and not under :my_app

config :ecto_sqlite3, json_library: JSON