Jason - where to place custom types and how to connect with Ecto, Postgrex and Phoenix?

phoenix

#1

Hi, I need help with setting Jason with Phoenix app.

I am reading the docs, but I am not sure where should place custom types and how to connect it with Ecto, Postgrex and Phoenix. Docs aren’t very clear.


#2

For phoenix

In config/confix.exs

config :phoenix, :format_encoders, json: Jason

and in lib/web/endpoint.ex (or wherever you endpoint.ex is) modify the parser plug to include json_decoder: Jason

  plug(
    Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Jason
  )

For ecto and postgrex

Add postgrex_types.ex (doesn’t actually matter what you name it) file somewhere in your app directory with

Postgrex.Types.define(
  MyAppName.PostgrexTypes, # note MyAppName, replace it
  [] ++ Ecto.Adapters.Postgres.extensions(),
  json: Jason
)

don’t forget to replace MyAppName with your ecto app name.

Then in config/config.exs add

config :ecto, json_library: Jason

config :my_app, MyAppName.Repo, # note MyAppName again
  types: MyAppName.PostgrexTypes # here as well

#3

Hi, Thanks for response.

Now I am getting this error:

10:19:05.350 [error] Task #PID<0.178.0> started from #PID<0.73.0> terminating
** (File.Error) could not write to file "/Users/nikolalukic/Desktop/nil_crud/_build/dev/lib/nil_crud/consolidated/Elixir.Poison.Decoder.beam": no such file or directory
    (elixir) lib/file.ex:831: File.write!/3
    (mix) lib/mix/tasks/compile.protocols.ex:132: Mix.Tasks.Compile.Protocols.consolidate/4
    (elixir) lib/task/supervised.ex:85: Task.Supervised.do_apply/2
    (elixir) lib/task/supervised.ex:36: Task.Supervised.reply/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Function: #Function<9.128108451/0 in Mix.Tasks.Compile.Protocols.consolidate/6>
    Args: []

#4

It seems like you are still using poison somewhere.

Can you try deleting _build and building your project again?


#5

Still no luck.

When I remove _build and use cmd+shift+f I am finding huge number of files that are mentioning “poison”.

How can I remove poison completely from the project?


#6

Can you create a repo with a reproducible error on github?


#7

I changed lot of files manually currently and deleted several folders from deps.

Basically I am using naked Phoenix project, --no-brunch --no-html.

The only changes I made was that I added several routes.

I will create new project to try again, and push it to github if I get the same errors.


#8

Why is Poison installed to my deps even when I delete deps folder and recreate it.

I don’t have Poison in my mix.exs


#9

It seems that mix deps.get is Installing poison by default.

I don’t have it mentioned anywhere in the project until I use " mix deps.get ".

This is without “deps” and “_build” and “mix.lock”>

And when I run: mix ecto.create


#10

Here is the repo.


#11

I’ve cloned it and compiled just fine

...
Generated nil_crud app

When does your error happen?


#12

what are your steps?


#13

I deleted my local copy, cloned the repo and run:

mix deps.get

mix deps.compile

mix ecto.create

After this step I am getting:

** (Mix) The database for NilCrud.Repo couldn't be created: an exception was raised:
    ** (UndefinedFunctionError) function NilCrud.PostgrexTypes.find/2 is undefined (module NilCrud.PostgrexTypes is not available)
        NilCrud.PostgrexTypes.find(%Postgrex.TypeInfo{array_elem: 0, base_type: 0, comp_elems: [], input: "boolin", oid: 16, output: "boolout", receive: "boolrecv", send: "boolsend", type: "bool"}, :any)
        (postgrex) lib/postgrex/types.ex:138: Postgrex.Types.find/4
        (postgrex) lib/postgrex/types.ex:131: anonymous fn/4 in Postgrex.Types.associate_type_infos/2
        (elixir) lib/enum.ex:1826: Enum."-reduce/3-lists^foldl/2-0-"/3
        (postgrex) lib/postgrex/types.ex:130: Postgrex.Types.associate_type_infos/2
        (postgrex) lib/postgrex/type_server.ex:122: Postgrex.TypeServer.associate/2
        (stdlib) gen_server.erl:636: :gen_server.try_handle_call/4
        (stdlib) gen_server.erl:665: :gen_server.handle_msg/6
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

I have Postgrex types file:


#14
git clone https://github.com/nikolal/nilcrud
cd nilcrud
mix deps.get
iex -S mix

#15

You have a typo in the module name where you define new types.

in config you have PostgrexTypes with an x

config :nil_crud, NilCrud.Repo,
  types: NilCrud.PostgrexTypes

and in postgrex_typex.ex you have PostgresTypes with an s

Postgrex.Types.define(
  NilCrud.PostgresTypes,
  [] ++ Ecto.Adapters.Postgres.extensions(),
  json: Jason
)

#16

Yes, that was it.

You can pass your username to me. :smiley:

Thank you for your time.