Packages
cldr_utils
2.29.2
2.29.7
2.29.6
2.29.5
2.29.4
2.29.3
2.29.2
2.29.1
2.29.0
2.28.3
2.28.2
2.28.1
2.28.0
2.27.0
2.26.0
2.25.0
2.24.2
2.24.1
2.24.0
2.23.1
2.23.0
2.22.0
2.21.0
2.20.0
2.19.2
2.19.1
2.19.0
2.18.0
2.17.2
2.17.1
2.17.0
2.17.0-rc.0
2.16.0
2.15.1
2.15.0
2.14.1
2.14.0
2.13.3
retired
2.13.2
2.13.1
2.13.0
retired
2.12.0
2.11.0
2.10.0
2.9.1
2.9.0
retired
2.8.0
2.7.0
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.0
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
Map, Calendar, Digits, Decimal, HTTP, Macro, Math, and String helpers for ex_cldr.
Current section
Files
Jump to
Current section
Files
lib/cldr/utils/json.ex
if Code.ensure_loaded?(:json) do
defmodule Cldr.Json do
@moduledoc """
A wrapper for the OTP 27 :json module.
It implements a `decode!/1` function that wraps
`:json.decode/1` with `decode!/1` so that its
compatible with the calling conventions of
Elixir - which is used by `ex_cldr`.
This allows configuration such as:
```elixir
config :ex_cldr,
json_library: Cldr.Json
```
"""
@doc since: "2.27.0"
@doc """
Implements a Jason-compatible decode!/1,2 function suitable
for decoding CLDR json data.
### Example
iex> Cldr.Json.decode!("{\\"foo\\": 1}")
%{"foo" => 1}
iex> Cldr.Json.decode!("{\\"foo\\": 1}", keys: :atoms)
%{foo: 1}
"""
def decode!(string) when is_binary(string) do
{json, :ok, ""} = :json.decode(string, :ok, %{null: nil})
json
end
def decode!(charlist) when is_list(charlist) do
charlist
|> :erlang.iolist_to_binary()
|> decode!()
end
def decode!(string, [keys: :atoms]) when is_binary(string) do
push = fn key, value, acc ->
[{String.to_atom(key), value} | acc]
end
decoders = %{
null: nil,
object_push: push
}
{json, :ok, ""} = :json.decode(string, :ok, decoders)
json
end
def decode!(charlist, options) when is_list(charlist) do
charlist
|> :erlang.iolist_to_binary()
|> decode!(options)
end
end
end