Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
benhaney committed Oct 28, 2019
0 parents commit a92d9c3
Show file tree
Hide file tree
Showing 13 changed files with 541 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
jsonrs-*.tar

16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Jsonrs

A JSON encoding/decoding library for Elixir powered by Rust's Serde though a NIF

## Why?

"Pure Elixir" JSON libraries use a ton of memory when encoding very large JSON strings. Serde and its zero-copy properties allows for much more performant encoding than native Elixir can, and Rust's safety guarentees along with Rustler allow us to ensure that the NIF we produce won't take down the BEAM.

## Examples
```elixir
iex> Jsonrs.encode(%{"x" => [1,2], "y" => 0.5})
{:ok, "{\"x\":[1,2],\"y\":0.5}"}

iex> Jsonrs.decode!("{\"x\":[1,2],\"y\":0.5}")
%{"x" => [1, 2], "y" => 0.5}
```
86 changes: 86 additions & 0 deletions lib/jsonrs.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
defmodule Jsonrs do
@moduledoc """
A JSON library powered by Rust's Serde through a NIF
"""
use Rustler, otp_app: :jsonrs, crate: "jsonrs"

@doc """
Generates JSON corresponding to `input`.
## Examples
iex> Jsonrs.encode(%{"x" => [1,2]})
{:ok, "{\\"x\\":[1,2]}"}
iex> Jsonrs.encode("\\xFF")
{:error, :encode_error}
"""
@spec encode(term) :: {:ok, String.t()} | {:error, :encode_error}
def encode(input) do
{:ok, encode!(input)}
rescue
e in ErlangError -> {:error, e.original}
end

@doc """
Parses a JSON value from `input` string.
## Examples
iex> Jsonrs.decode("{\\"x\\":[1,2]}")
{:ok, %{"x" => [1, 2]}}
iex> Jsonrs.decode("invalid")
{:error, "expected value at line 1 column 1"}
"""
@spec decode(String.t()) :: {:ok, term} | {:error, String.t()}
def decode(input) do
{:ok, decode!(input)}
rescue
e in ErlangError -> {:error, e.original}
end

@doc """
Generates JSON corresponding to `input`.
Similar to `encode/1` except it will raise in case of errors.
## Examples
iex> Jsonrs.encode!(%{"x" => [1,2]})
"{\\"x\\":[1,2]}"
iex> Jsonrs.encode!("\\xFF")
** (ErlangError) Erlang error: :encode_error
"""
@spec encode!(term) :: String.t()
def encode!(_input), do: :erlang.nif_error(:nif_not_loaded)

@doc """
Parses a JSON value from `input` string.
Similar to `decode/1` except it will raise in case of errors.
## Examples
iex> Jsonrs.decode!("{\\"x\\":[1,2]}")
%{"x" => [1, 2]}
iex> Jsonrs.decode!("invalid")
** (ErlangError) Erlang error: "expected value at line 1 column 1"
"""
@spec decode!(String.t()) :: term
def decode!(_input), do: :erlang.nif_error(:nif_not_loaded)

@doc """
Identical to `encode\1`. Exists to implement Phoenix interface and encodes to a single normal string.
"""
@spec encode_to_iodata(term) :: {:ok, String.t()} | {:error, :encode_error}
def encode_to_iodata(input), do: encode(input)

@doc """
Identical to `encode!\1`. Exists to implement Phoenix interface and encodes to a single normal string.
"""
@spec encode_to_iodata!(term) :: String.t()
def encode_to_iodata!(input), do: encode!(input)
end
35 changes: 35 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Jsoner.MixProject do
use Mix.Project

def project do
[
app: :jsonrs,
version: "0.1.0",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
compilers: [:rustler] ++ Mix.compilers(),
rustler_crates: rustler_crates(),
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:rustler, "~> 0.21.0"}
]
end

defp rustler_crates do
[
jsonrs: [path: "native/jsonrs", mode: if(Mix.env() == :prod, do: :release, else: :debug)]
]
end
end
4 changes: 4 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%{
"rustler": {:hex, :rustler, "0.21.0", "68cc4fc015d0b9541865ea78e78e9ef2dd91ee4be80bf543fd15791102a45aca", [:mix], [{:toml, "~> 0.5.2", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm"},
"toml": {:hex, :toml, "0.5.2", "e471388a8726d1ce51a6b32f864b8228a1eb8edc907a0edf2bb50eab9321b526", [:mix], [], "hexpm"},
}
5 changes: 5 additions & 0 deletions native/jsonrs/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
Loading

0 comments on commit a92d9c3

Please sign in to comment.