Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON encoding #46

Closed
travisjeffery opened this issue Feb 19, 2016 · 13 comments
Closed

JSON encoding #46

travisjeffery opened this issue Feb 19, 2016 · 13 comments

Comments

@travisjeffery
Copy link

Would be useful to encode to JSON, e.g. map of strings:

BSON.encode_json(doc)
%{"_id" => "559c3c676b92c9bf5c0898fd", "name" => "Travis"}

I'm pretty new to Elixir and this is above my level still, but when I can do it I'll make a PR if there isn't already.

@ericmj
Copy link
Collaborator

ericmj commented Feb 19, 2016

You can use a JSON library for that, for example https://github.com/devinus/poison.

@ericmj ericmj closed this as completed Feb 19, 2016
@travisjeffery
Copy link
Author

Right but even then you have to the hardwork of re-encoding them yourself: devinus/poison#66 (comment).

@ericmj
Copy link
Collaborator

ericmj commented Feb 19, 2016

If you are using master of this library you can implement the protocol for ids:

defimpl Poison.Encoder, for: BSON.ObjectId do
  def encode(id), do: BSON.ObjectId.encode(id)
end

@travisjeffery
Copy link
Author

Ah nice!

Here's the implementation in case anyone else hits this:

defimpl Poison.Encoder, for: BSON.ObjectId do
  def encode(id, options) do
    BSON.ObjectId.encode!(id) |> Poison.Encoder.encode(options)
  end
end

Thanks!

@beno
Copy link

beno commented Sep 19, 2016

Where do I add this code? When I try it in various places I always get:

warning: the Poison.Encoder protocol has already been consolidated, an implementation for BSON.ObjectId has no effect

I don't reference Poison.Encoder anywhere else.

@ericmj
Copy link
Collaborator

ericmj commented Sep 19, 2016

@beno Anywhere in your Elixir files under the lib/ directory.

@beno
Copy link

beno commented Sep 19, 2016

But then I run into the above warning and the code indeed has no effect.

@ericmj
Copy link
Collaborator

ericmj commented Sep 19, 2016

If you get that warning you have some other issue with your project since you should not get it. If you have some way to share your project I can take a look at it.

@beno
Copy link

beno commented Sep 19, 2016

When I create a new mix app, with 2 dependencies (poison and mongoldb) and just the code above in lib I run into this issue. But interestingly only when I type recompile at the iex prompt, not when I do a full quit/start cycle.

I got confused here, because the initial compile also gives a warning:

warning: function BSON.ObjectId.encode!/1 is undefined or private

So the code above is not valid out of the box. Can you please shed some light on this?

@ericmj
Copy link
Collaborator

ericmj commented Sep 19, 2016

The recompile issue was a bug in Elixir. Update and it should be fixed.

BSON.ObjectId.encode!/1 is only available on the master branch so far.

@beno
Copy link

beno commented Sep 19, 2016

I was on Elixir 1.3.1, have just now updated to Elixir 1.3.2 but the consolidated protocol warning remains.

@ericmj here is a repo which produces the error https://github.com/beno/consolidate_error

@ericmj
Copy link
Collaborator

ericmj commented Sep 19, 2016

@beno Please open an issue on the Elixir project if the warning is still happening.

@beno
Copy link

beno commented Sep 19, 2016

Allright, done elixir-lang/elixir#5228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants