Polymarket.JsonUtil (Polymarket v0.1.1)

Copy Markdown View Source

Helpers for massaging raw JSON (as decoded from an external API) into the shapes our schemas expect.

Summary

Functions

Fetches key from map, accepting either the atom key or its string form.

Recursively rewrites a map's camelCase keys (atom or string) into snake_case string keys, descending into nested maps and lists.

Functions

lenient_fetch(map, key)

@spec lenient_fetch(map(), atom()) :: {:ok, term()} | :error

Fetches key from map, accepting either the atom key or its string form.

The HTTP layer decodes JSON to atom keys, but a body we re-decoded locally has string keys — so try the atom first, then fall back to the string.

snake_case_keys(attrs)

@spec snake_case_keys(map()) :: map()

Recursively rewrites a map's camelCase keys (atom or string) into snake_case string keys, descending into nested maps and lists.

Bridges the Gamma API's camelCase payloads to our snake_case schema fields.

iex> Polymarket.JsonUtil.snake_case_keys(%{conditionId: "0x1", feeSchedule: %{rebateRate: 0.2}})
%{"condition_id" => "0x1", "fee_schedule" => %{"rebate_rate" => 0.2}}