Skip to content

Commit 198fc86

Browse files
axelsonlukaszsamson
authored andcommitted
vendor Jason
This prevents module conflicts with the user's version of Jason since both are loaded into the same beam instance (at least until #253) is addressed.
1 parent 926d2ac commit 198fc86

24 files changed

+454
-300
lines changed

README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Jason
1+
# JasonVendored
22

33
A blazing fast JSON parser and generator in pure Elixir.
44

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

1010
Both parser and generator fully conform to
1111
[RFC 8259](https://tools.ietf.org/html/rfc8259) and
@@ -26,10 +26,10 @@ end
2626
## Basic Usage
2727

2828
``` elixir
29-
iex(1)> Jason.encode!(%{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"})
29+
iex(1)> JasonVendored.encode!(%{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"})
3030
"{\"age\":44,\"name\":\"Steve Irwin\",\"nationality\":\"Australian\"}"
3131

32-
iex(2)> Jason.decode!(~s({"age":44,"name":"Steve Irwin","nationality":"Australian"}))
32+
iex(2)> JasonVendored.decode!(~s({"age":44,"name":"Steve Irwin","nationality":"Australian"}))
3333
%{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"}
3434
```
3535

@@ -39,17 +39,17 @@ Full documentation can be found at [https://hexdocs.pm/jason](https://hexdocs.pm
3939

4040
### Postgrex
4141

42-
Versions starting at 0.14.0 use `Jason` by default. For earlier versions, please refer to
42+
Versions starting at 0.14.0 use `JasonVendored` by default. For earlier versions, please refer to
4343
[previous versions of this document](https://github.com/michalmuskala/jason/tree/v1.1.2#postgrex).
4444

4545
### Ecto
4646

47-
Versions starting at 3.0.0 use `Jason` by default. For earlier versions, please refer to
47+
Versions starting at 3.0.0 use `JasonVendored` by default. For earlier versions, please refer to
4848
[previous versions of this document](https://github.com/michalmuskala/jason/tree/v1.1.2#ecto).
4949

5050
### Plug (and Phoenix)
5151

52-
Phoenix starting at 1.4.0 uses `Jason` by default. For earlier versions, please refer to
52+
Phoenix starting at 1.4.0 uses `JasonVendored` by default. For earlier versions, please refer to
5353
[previous versions of this document](https://github.com/michalmuskala/jason/tree/v1.1.2#plug-and-phoenix).
5454

5555
### Absinthe
@@ -60,12 +60,12 @@ You need to pass the `:json_codec` option to `Absinthe.Plug`
6060
# When called directly:
6161
plug Absinthe.Plug,
6262
schema: MyApp.Schema,
63-
json_codec: Jason
63+
json_codec: JasonVendored
6464

6565
# When used in phoenix router:
6666
forward "/api",
6767
to: Absinthe.Plug,
68-
init_opts: [schema: MyApp.Schema, json_codec: Jason]
68+
init_opts: [schema: MyApp.Schema, json_codec: JasonVendored]
6969
```
7070

7171
## Benchmarks
@@ -85,15 +85,15 @@ A HTML report of the benchmarks (after their execution) can be found in
8585

8686
## Differences to Poison
8787

88-
Jason has a couple feature differences compared to Poison.
88+
JasonVendored has a couple feature differences compared to Poison.
8989

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

9999
### Encoders
@@ -102,9 +102,9 @@ If you require encoders for any of the unsupported collection types, I suggest
102102
adding the needed implementations directly to your project:
103103

104104
```elixir
105-
defimpl Jason.Encoder, for: [MapSet, Range, Stream] do
105+
defimpl JasonVendored.Encoder, for: [MapSet, Range, Stream] do
106106
def encode(struct, opts) do
107-
Jason.Encode.list(Enum.to_list(struct), opts)
107+
JasonVendored.Encode.list(Enum.to_list(struct), opts)
108108
end
109109
end
110110
```
@@ -114,7 +114,7 @@ if you own the struct, you can derive the implementation specifying
114114
which fields should be encoded to JSON:
115115

116116
```elixir
117-
@derive {Jason.Encoder, only: [....]}
117+
@derive {JasonVendored.Encoder, only: [....]}
118118
defstruct # ...
119119
```
120120

@@ -123,16 +123,16 @@ used carefully to avoid accidentally leaking private information
123123
when new fields are added:
124124

125125
```elixir
126-
@derive Jason.Encoder
126+
@derive JasonVendored.Encoder
127127
defstruct # ...
128128
```
129129

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

133133
```elixir
134-
Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...])
135-
Protocol.derive(Jason.Encoder, NameOfTheStruct)
134+
Protocol.derive(JasonVendored.Encoder, NameOfTheStruct, only: [...])
135+
Protocol.derive(JasonVendored.Encoder, NameOfTheStruct)
136136
```
137137

138138
## Injecting an already encoded JSON inside a to-be-encoded structure
@@ -151,7 +151,7 @@ or if it is already provided by another system (e.g. `jsonb_agg` with Postgres).
151151

152152
## License
153153

154-
Jason is released under the Apache License 2.0 - see the [LICENSE](LICENSE) file.
154+
JasonVendored is released under the Apache License 2.0 - see the [LICENSE](LICENSE) file.
155155

156156
Some elements of tests and benchmarks have their origins in the
157157
[Poison library](https://github.com/devinus/poison) and were initially licensed under [CC0-1.0](https://creativecommons.org/publicdomain/zero/1.0/).

bench/decode.exs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
decode_jobs = %{
2-
"Jason" => fn {json, _} -> Jason.decode!(json) end,
2+
"JasonVendored" => fn {json, _} -> JasonVendored.decode!(json) end,
33
"Poison" => fn {json, _} -> Poison.decode!(json) end,
4-
"JSX" => fn {json, _} -> JSX.decode!(json, [:strict]) end,
5-
"Tiny" => fn {json, _} -> Tiny.decode!(json) end,
6-
"jsone" => fn {json, _} -> :jsone.decode(json) end,
7-
"jiffy" => fn {json, _} -> :jiffy.decode(json, [:return_maps, :use_nil]) end,
8-
"JSON" => fn {json, _} -> JSON.decode!(json) end,
4+
"JSX" => fn {json, _} -> JSX.decode!(json, [:strict]) end,
5+
"Tiny" => fn {json, _} -> Tiny.decode!(json) end,
6+
"jsone" => fn {json, _} -> :jsone.decode(json) end,
7+
"jiffy" => fn {json, _} -> :jiffy.decode(json, [:return_maps, :use_nil]) end,
8+
"JSON" => fn {json, _} -> JSON.decode!(json) end
99
# "binary_to_term/1" => fn {_, etf} -> :erlang.binary_to_term(etf) end,
1010
}
1111

@@ -19,18 +19,18 @@ decode_inputs = [
1919
"JSON Generator (Pretty)",
2020
"UTF-8 escaped",
2121
"UTF-8 unescaped",
22-
"Issue 90",
22+
"Issue 90"
2323
]
2424

25-
read_data = fn (name) ->
25+
read_data = fn name ->
2626
file =
2727
name
28-
|> String.downcase
28+
|> String.downcase()
2929
|> String.replace(~r/([^\w]|-|_)+/, "-")
3030
|> String.trim("-")
3131

3232
json = File.read!(Path.expand("data/#{file}.json", __DIR__))
33-
etf = :erlang.term_to_binary(Jason.decode!(json))
33+
etf = :erlang.term_to_binary(JasonVendored.decode!(json))
3434

3535
{json, etf}
3636
end
@@ -45,7 +45,7 @@ end
4545
IO.puts("\n")
4646

4747
Benchee.run(decode_jobs,
48-
# parallel: 4,
48+
# parallel: 4,
4949
warmup: 5,
5050
time: 30,
5151
memory_time: 1,
@@ -54,6 +54,6 @@ Benchee.run(decode_jobs,
5454
load: "output/runs/*.benchee",
5555
formatters: [
5656
{Benchee.Formatters.HTML, file: Path.expand("output/decode.html", __DIR__)},
57-
Benchee.Formatters.Console,
57+
Benchee.Formatters.Console
5858
]
5959
)

bench/encode.exs

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
encode_jobs = %{
2-
"Jason" => &Jason.encode_to_iodata!/1,
3-
"Jason strict" => &Jason.encode_to_iodata!(&1, maps: :strict),
4-
"Poison" => &Poison.encode_to_iodata!/1,
5-
"JSX" => &JSX.encode!/1,
6-
"Tiny" => &Tiny.encode!/1,
7-
"jsone" => &:jsone.encode/1,
8-
"jiffy" => &:jiffy.encode/1,
9-
"JSON" => &JSON.encode!/1,
2+
"JasonVendored" => &JasonVendored.encode_to_iodata!/1,
3+
"JasonVendored strict" => &JasonVendored.encode_to_iodata!(&1, maps: :strict),
4+
"Poison" => &Poison.encode_to_iodata!/1,
5+
"JSX" => &JSX.encode!/1,
6+
"Tiny" => &Tiny.encode!/1,
7+
"jsone" => &:jsone.encode/1,
8+
"jiffy" => &:jiffy.encode/1,
9+
"JSON" => &JSON.encode!/1
1010
# "term_to_binary" => &:erlang.term_to_binary/1,
1111
}
1212

@@ -22,28 +22,28 @@ encode_inputs = [
2222
"Canada",
2323
]
2424

25-
read_data = fn (name) ->
25+
read_data = fn name ->
2626
name
27-
|> String.downcase
27+
|> String.downcase()
2828
|> String.replace(~r/([^\w]|-|_)+/, "-")
2929
|> String.trim("-")
3030
|> (&"data/#{&1}.json").()
3131
|> Path.expand(__DIR__)
32-
|> File.read!
32+
|> File.read!()
3333
end
3434

35-
3635
Benchee.run(encode_jobs,
37-
# parallel: 4,
36+
# parallel: 4,
3837
warmup: 5,
3938
time: 30,
4039
memory_time: 1,
41-
inputs: for name <- encode_inputs, into: %{} do
42-
name
43-
|> read_data.()
44-
|> Jason.decode!()
45-
|> (&{name, &1}).()
46-
end,
40+
inputs:
41+
for name <- encode_inputs, into: %{} do
42+
name
43+
|> read_data.()
44+
|> JasonVendored.decode!()
45+
|> (&{name, &1}).()
46+
end,
4747
formatters: [
4848
{Benchee.Formatters.HTML, file: Path.expand("output/encode.html", __DIR__)},
4949
Benchee.Formatters.Console

bench/mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule JasonBench.MixProject do
1+
defmodule JasonVendoredBench.MixProject do
22
use Mix.Project
33

44
def project do

lib/codegen.ex

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
defmodule Jason.Codegen do
1+
defmodule JasonVendored.Codegen do
22
@moduledoc false
33

4-
alias Jason.{Encode, EncodeError}
4+
alias JasonVendored.{Encode, EncodeError}
55

66
def jump_table(ranges, default) do
77
ranges
@@ -94,12 +94,12 @@ defmodule Jason.Codegen do
9494
defp ranges_to_orddict(ranges) do
9595
ranges
9696
|> Enum.flat_map(fn
97-
{int, value} when is_integer(int) ->
98-
[{int, value}]
97+
{int, value} when is_integer(int) ->
98+
[{int, value}]
9999

100-
{enum, value} ->
101-
Enum.map(enum, &{&1, value})
102-
end)
100+
{enum, value} ->
101+
Enum.map(enum, &{&1, value})
102+
end)
103103
|> :orddict.from_list()
104104
end
105105

0 commit comments

Comments
 (0)