Coder Social home page Coder Social logo

jazz's Introduction

jazz - JSON handling library for Elixir

Jazz is a JSON handling library written in Elixir, for Elixir.

Examples

use Jazz

JSON.encode!(%{name: "David", surname: "Davidson"})
  |> IO.puts # => {"name":"David","surname":"Davidson"}

JSON.decode!(~S/{"name":"David","surname":"Davidson"}/)
  |> IO.inspect # => %{"name" => "David", "surname" => "Davidson"}

JSON.decode!(~S/{"name":"David","surname":"Davidson"}/, keys: :atoms)
  |> IO.inspect # => %{name: "David", surname: "Davidson"}

# would raise if the keys weren't already existing atoms
JSON.decode!(~S/{"name":"David","surname":"Davidson"}/, keys: :atoms!)
  |> IO.inspect # => %{name: "David", surname: "Davidson"}

defmodule Person do
  defstruct name: nil, surname: nil
end

JSON.encode!(%Person{name: "David", surname: "Davidson"})
  |> IO.puts # => {"name":"David","surname":"Davidson"}

JSON.decode!(~S/{"name":"David","surname":"Davidson"}/, as: Person)
  |> IO.inspect # => %Person{name: "David", surname: "Davidson"}

defimpl JSON.Encoder, for: HashDict do
  def encode(self, options) do
    HashDict.to_list(self) |> Enum.into(%{})
  end
end

defimpl JSON.Decoder, for: HashDict do
  def decode(_new, parsed, _options) do
    parsed |> Enum.into(HashDict.new)
  end
end

JSON.encode!(HashDict.new([name: "David", surname: "Davidson"]))
  |> IO.puts # => {"name":"David","surname":"Davidson"}

JSON.decode!(~S/{"name":"David","surname":"Davidson"}/, as: HashDict)
  |> IO.inspect # => #HashDict<[{"name", "David" }, { "surname", "Davidson" }]>

jazz's People

Contributors

ericmj avatar meh avatar sasa1977 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jazz's Issues

Emoji in string causes Jazz to choke

iex> json = %S/{"str": "✔︎"}/
"{\"str\": \"✔︎\"}"
iex> JSON.decode!(json)
# hangs forever and immediately takes 500mb of memory then slowly falls to 100mb

Running on elixir 0.12.3 and 0.12.4-dev

Exlir 0.13.3-dev issues

JSON.encode!([name: "David", surname: "Davidson"])

produces:

** (FunctionClauseError) no function clause matching in Jazz.Encoder.Any.to_json/2
(jazz) lib/jazz/encoder.ex:115: Jazz.Encoder.Any.to_json({:name, "David"}, [])
(jazz) lib/jazz/encoder.ex:12: Jazz.Encode.it/2
(jazz) lib/jazz.ex:18: Jazz.encode!/2
(jazz) lib/jazz/encoder.ex:73: anonymous fn/2 in Jazz.Encoder.List.encode/3
(elixir) lib/enum.ex:962: Enum."-map/2-lc$^0/1-0-"/2
(jazz) lib/jazz/encoder.ex:72: Jazz.Encoder.List.encode/3
(jazz) lib/jazz/encoder.ex:60: Jazz.Encoder.List.to_json/2
(jazz) lib/jazz/encoder.ex:12: Jazz.Encode.it/2

Why are are only the nil, true, false atoms matched in Encoder?

Hi just a question, because I stumbled over it while coding on a JSON layer in my phoenix app.

Why is the Jazz.Encoder protocol implementation for Atom limited to true, false, nil?

I certainly see the need to handle nil separately, but wouldn't a more general implementation like

  def to_json(f, _) do
    Atom.to_string(f)
  end

be a bit user friendlier?

I'm asking because I wanted to serialize a map that contains atoms as values (for instance like %{id: :not_found} and was surprised that this is currently not possible ....

Cheerio
Björn

Jazz could not compile with elixir v1.5.1

warning: the dependency :jazz requires Elixir "~> 1.0.0-rc1" but you are running on v1.5.1
== Compilation error in file lib/jazz/encoder.ex == ** (CompileError) lib/jazz/encoder.ex:55: inlined function indentation/1 undefined (stdlib) lists.erl:1338: :lists.foreach/2 lib/jazz/encoder.ex:55: (file)

could not compile dependency :jazz, "mix compile" failed. You can recompile this dependency with "mix deps.compile jazz", update it with "mix deps.update jazz" or clean it with "mix deps.clean jazz"

dependency warning for elixir 1.1.1

I'm getting this warning when installing deps:

warning: the dependency :jazz requires Elixir "~> 1.0.0-rc1" but you are running on v1.1.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.