Skip to content

Commit

Permalink
Fix Cldr.Json.decode!
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 9, 2024
1 parent 7639cad commit 59708fd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

**Cldr Utils from version 2.27.0 requires Elixir 1.12 or later**

## Cldr Utils version 2.28.0

This is the changelog for Cldr Utils v2.28.0 released on July 10th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_utils/tags)

### Bug Fixes

* Fix `Cldr.Json.decode!/1` to return only the decoded JSON.

### Enhancements

* Add `Cldr.Json.decode!/2` that implements the `keys: :atoms` option from `Jason`.

## Cldr Utils version 2.27.0

This is the changelog for Cldr Utils v2.27.0 released on June 23rd, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_utils/tags)
Expand Down
31 changes: 30 additions & 1 deletion lib/cldr/utils/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,38 @@ if Code.ensure_loaded?(:json) do
```
"""
@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) do
:json.decode(string)
{json, :ok, ""} = :json.decode(string, :ok, %{null: nil})
json
end

def decode!(string, [keys: :atoms]) 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

end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.Utils.MixProject do
use Mix.Project

@version "2.27.0"
@version "2.28.0"
@source_url "https://github.com/elixir-cldr/cldr_utils"

def project do
Expand Down
2 changes: 2 additions & 0 deletions test/cldr_utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule CldrUtilsTest do
doctest Cldr.String

if Code.ensure_loaded?(:json) do
doctest Cldr.Json

Check failure on line 13 in test/cldr_utils_test.exs

View workflow job for this annotation

GitHub Actions / Test on OTP 25.3.2 / Elixir 1.14.4

** (CompileError) test/cldr_utils_test.exs:13: module Cldr.Json is not loaded and could not be found

test "Cldr.Json proxy" do
assert Cldr.Json.decode!("{}") == %{}
end
Expand Down

0 comments on commit 59708fd

Please sign in to comment.