diff --git a/CHANGELOG.md b/CHANGELOG.md index 31183d6..8000db2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/cldr/utils/json.ex b/lib/cldr/utils/json.ex index 08688bd..a6d8b22 100644 --- a/lib/cldr/utils/json.ex +++ b/lib/cldr/utils/json.ex @@ -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 diff --git a/mix.exs b/mix.exs index 7b6e01a..fe3c110 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/test/cldr_utils_test.exs b/test/cldr_utils_test.exs index 68c3da4..8a42689 100644 --- a/test/cldr_utils_test.exs +++ b/test/cldr_utils_test.exs @@ -10,6 +10,8 @@ defmodule CldrUtilsTest do doctest Cldr.String if Code.ensure_loaded?(:json) do + doctest Cldr.Json + test "Cldr.Json proxy" do assert Cldr.Json.decode!("{}") == %{} end