From a5cd8de44303d51e5c0bb62659654516d6d6c1d7 Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Wed, 14 Aug 2024 00:01:27 +1000 Subject: [PATCH] Fix Cldr.Json with charlist argument --- CHANGELOG.md | 8 ++++++++ lib/cldr/utils/json.ex | 17 +++++++++++++++-- mix.exs | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8000db2..6884664 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ **Cldr Utils from version 2.27.0 requires Elixir 1.12 or later** +## Cldr Utils version 2.28.1 + +This is the changelog for Cldr Utils v2.28.1 released on August 14th, 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` when calling with a charlist instead of a binary. + ## 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) diff --git a/lib/cldr/utils/json.ex b/lib/cldr/utils/json.ex index 5e5402c..cb8ea02 100644 --- a/lib/cldr/utils/json.ex +++ b/lib/cldr/utils/json.ex @@ -30,12 +30,18 @@ if Code.ensure_loaded?(:json) do %{foo: 1} """ - def decode!(string) do + def decode!(string) when is_binary(string) do {json, :ok, ""} = :json.decode(string, :ok, %{null: nil}) json end - def decode!(string, [keys: :atoms]) do + def decode!(charlist) when is_list(charlist) do + charlist + |> List.to_string() + |> decode!() + end + + def decode!(string, [keys: :atoms]) when is_binary(string) do push = fn key, value, acc -> [{String.to_atom(key), value} | acc] end @@ -48,5 +54,12 @@ if Code.ensure_loaded?(:json) do {json, :ok, ""} = :json.decode(string, :ok, decoders) json end + + def decode!(charlist, options) when is_list(charlist) do + charlist + |> List.to_string() + |> decode!(options) + end + end end \ No newline at end of file diff --git a/mix.exs b/mix.exs index fe3c110..97c7ef2 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Cldr.Utils.MixProject do use Mix.Project - @version "2.28.0" + @version "2.28.1" @source_url "https://github.com/elixir-cldr/cldr_utils" def project do