• Stars
    star
    1,619
  • Rank 28,903 (Top 0.6 %)
  • Language
    Elixir
  • License
    Other
  • Created over 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A blazing fast JSON parser and generator in pure Elixir.

Jason

A blazing fast JSON parser and generator in pure Elixir.

The parser and generator are at least twice as fast as other Elixir/Erlang libraries (most notably Poison). The performance is comparable to jiffy, which is implemented in C as a NIF. Jason is usually only twice as slow.

Both parser and generator fully conform to RFC 8259 and ECMA 404 standards. The parser is tested using JSONTestSuite.

Installation

The package can be installed by adding jason to your list of dependencies in mix.exs:

def deps do
  [{:jason, "~> 1.4"}]
end

Basic Usage

iex(1)> Jason.encode!(%{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"})
"{\"age\":44,\"name\":\"Steve Irwin\",\"nationality\":\"Australian\"}"

iex(2)> Jason.decode!(~s({"age":44,"name":"Steve Irwin","nationality":"Australian"}))
%{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"}

Full documentation can be found at https://hexdocs.pm/jason.

Use with other libraries

Postgrex

Versions starting at 0.14.0 use Jason by default. For earlier versions, please refer to previous versions of this document.

Ecto

Versions starting at 3.0.0 use Jason by default. For earlier versions, please refer to previous versions of this document.

Plug (and Phoenix)

Phoenix starting at 1.4.0 uses Jason by default. For earlier versions, please refer to previous versions of this document.

Absinthe

You need to pass the :json_codec option to Absinthe.Plug

# When called directly:
plug Absinthe.Plug,
  schema: MyApp.Schema,
  json_codec: Jason

# When used in phoenix router:
forward "/api",
  to: Absinthe.Plug,
  init_opts: [schema: MyApp.Schema, json_codec: Jason]

Benchmarks

Detailed benchmarks (including memory measurements): https://gist.github.com/michalmuskala/4d64a5a7696ca84ac7c169a0206640d5

HTML reports for the benchmark (only performance measurements): http://michal.muskala.eu/jason/decode.html and http://michal.muskala.eu/jason/encode.html

Running

Benchmarks against most popular Elixir & Erlang json libraries can be executed after going into the bench/ folder and then executing mix bench.encode and mix bench.decode. A HTML report of the benchmarks (after their execution) can be found in bench/output/encode.html and bench/output/decode.html respectively.

Differences to Poison

Jason has a couple feature differences compared to Poison.

  • Jason follows the JSON spec more strictly, for example it does not allow unescaped newline characters in JSON strings - e.g. "\"\n\"" will produce a decoding error.
  • no support for decoding into data structures (the as: option).
  • no built-in encoders for MapSet, Range and Stream.
  • no support for encoding arbitrary structs - explicit implementation of the Jason.Encoder protocol is always required.
  • different pretty-printing customisation options (default pretty: true works the same)

Encoders

If you require encoders for any of the unsupported collection types, I suggest adding the needed implementations directly to your project:

defimpl Jason.Encoder, for: [MapSet, Range, Stream] do
  def encode(struct, opts) do
    Jason.Encode.list(Enum.to_list(struct), opts)
  end
end

If you need to encode some struct that does not implement the protocol, if you own the struct, you can derive the implementation specifying which fields should be encoded to JSON:

@derive {Jason.Encoder, only: [....]}
defstruct # ...

It is also possible to encode all fields, although this should be used carefully to avoid accidentally leaking private information when new fields are added:

@derive Jason.Encoder
defstruct # ...

Finally, if you don't own the struct you want to encode to JSON, you may use Protocol.derive/3 placed outside of any module:

Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...])
Protocol.derive(Jason.Encoder, NameOfTheStruct)

Injecting an already encoded JSON inside a to-be-encoded structure

If parts of the to-be-encoded structure are already JSON-encoded, you can use Jason.Fragment to mark the parts as already encoded, and avoid a decoding/encoding roundtrip.

already_encoded_json = Jason.encode!(%{hello: "world"})
Jason.encode!(%{foo: Jason.Fragment.new(already_encoded_json)})

This feature is especially useful if you need to cache a part of the JSON, or if it is already provided by another system (e.g. jsonb_agg with Postgres).

License

Jason is released under the Apache License 2.0 - see the LICENSE file.

Some elements of tests and benchmarks have their origins in the Poison library and were initially licensed under CC0-1.0.

More Repositories

1

plug_attack

A plug building toolkit for blocking and throttling abusive requests
Elixir
419
star
2

decompile

Elixir
156
star
3

persistent_ets

Elixir
131
star
4

phoenix_etag

ETag support for phoenix
Elixir
43
star
5

env

Env is an improved application configuration reader for Elixir.
Elixir
30
star
6

plug_webhook

Simple tool for building plugs that handle wehbooks and verify signature.
Elixir
24
star
7

debounce

A process-based debouncer for Elixir
Elixir
23
star
8

format

A simple string formatter for Elixir
Elixir
17
star
9

xref.rs

Rust reimplementation of subset of Erlang's xref
Rust
15
star
10

horde

Persistent, distributed processes for Elixir
Elixir
8
star
11

fast_beam

Rust library for reading .beam files
Rust
8
star
12

hex_view_old

Elixir
7
star
13

time_format

Fast date/time formatting for elixir
Elixir
7
star
14

better_assert

Erlang
6
star
15

hex_view

Source explorer for hex packages
Elixir
5
star
16

simple_format

Rail's simple_format helper for Phoenix.HTML
Elixir
5
star
17

persist

GenStage-based persistence layer guaranteeing at-least-once delivery of the events.
Elixir
4
star
18

tic-tac-toe-workshop

Elixir
4
star
19

aoc-2020

Advent of code for 2020
Rust
3
star
20

exrb

Ruby-Elixir interoperability
Ruby
3
star
21

manager_supervisor

Elixir
3
star
22

superconsole

Enhancements to rails console based on pry
Ruby
2
star
23

llscm

Scheme to llvm compilter
Haskell
2
star
24

trace

Elixir
1
star
25

infiniq

Elixir
1
star
26

matasano

My solutions to the Matasano challenge in Elixir.
Elixir
1
star
27

file_count_optim

Benchmark for testing implementations of Enumerable.count/1 for File.Stream.
Elixir
1
star
28

dotfiles

Emacs Lisp
1
star
29

janusze

Ruby
1
star
30

jason_native

C++
1
star