Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] [Elixir] Use built-in JSON encoder and decoder #21038

Open
efcasado opened this issue Apr 5, 2025 · 0 comments
Open

[REQ] [Elixir] Use built-in JSON encoder and decoder #21038

efcasado opened this issue Apr 5, 2025 · 0 comments

Comments

@efcasado
Copy link

efcasado commented Apr 5, 2025

Description

Elixir 1.18 added native built-in support for JSON encoding and decoding.

Tesla's JSON middleware supports the build-in module too.

Using the built-in JSON module would spare us the need to pull additional libraries like Jason. Also, the JSON module seems to implement a wider spectrum of the JSON specification. For example, Jason doesn't like atoms like :"TLS 1.3 / AES_128_GCM" and automatically generated modules like

defmodule Cloudflare.Model.UrlscannerGetScanV2200ResponseStatsTlsStatsInnerProtocols do
  @moduledoc """
  
  """

  @derive Jason.Encoder
  defstruct [
    :"TLS 1.3 / AES_128_GCM"
  ]

  @type t :: %__MODULE__{
    :"TLS 1.3 / AES_128_GCM" => float()
  }

  def decode(value) do
    value
  end
end

wouldn't compile and would result in the following error

== Compilation error in file lib/model/urlscanner_get_scan_v2_200_response_stats_tls_stats_inner_protocols.ex ==
** (Jason.EncodeError) invalid byte 0x2F in literal key: "TLS 1.3 / AES_128_GCM"
    (jason 1.4.4) lib/codegen.ex:120: Jason.Codegen."-check_safe_key!/1-lc$^0/1-0-"/2
    (jason 1.4.4) lib/codegen.ex:118: Jason.Codegen.check_safe_key!/1
    (jason 1.4.4) lib/codegen.ex:113: Jason.Codegen.escape_key/3
    (jason 1.4.4) lib/codegen.ex:107: Jason.Codegen.encode_pair/2
    (elixir 1.18.3) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
    (jason 1.4.4) lib/codegen.ex:48: Jason.Codegen.build_kv_iodata/2
    (jason 1.4.4) expanding macro: Jason.Encoder.Any.__deriving__/3
    lib/model/urlscanner_get_scan_v2_200_response_stats_tls_stats_inner_protocols.ex:10: Cloudflare.Model.UrlscannerGetScanV2200ResponseStatsTlsStatsInnerProtocols (module)

The built-in JSON module handles these scenarios well.

In order to be able to use the built-in JSON module, we would need to bump the minimum required version of Elixir from 1.10 to 1.18.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant