### Install Jason Dependency Source: https://jason.hexdocs.pm/ Add the Jason library to your project's dependencies in mix.exs. ```elixir def deps do [{:jason, "~> 1.4"}] end ``` -------------------------------- ### Create New Ordered Object and Encode to JSON Source: https://jason.hexdocs.pm/Jason.OrderedObject.md Demonstrates creating a new Jason.OrderedObject from a sorted list of key-value pairs and then encoding it to a JSON string, preserving the order. ```elixir iex> %{a: 1, c: 3, b: 2} ...> |> Enum.sort() ...> |> Jason.OrderedObject.new() ...> |> Jason.encode!() "{\"a\":1,\"b\":2,\"c\":3}" ``` -------------------------------- ### Configure Absinthe.Plug with Jason Source: https://jason.hexdocs.pm/ Specify Jason as the JSON codec for Absinthe.Plug when used directly or within a Phoenix router. ```elixir # When called directly: plug Absinthe.Plug, schema: MyApp.Schema, json_codec: Jason ``` ```elixir # When used in phoenix router: forward "/api", to: Absinthe.Plug, init_opts: [schema: MyApp.Schema, json_codec: Jason] ``` -------------------------------- ### `new` Source: https://jason.hexdocs.pm/Jason.EncodeError.md Function to create a new `Jason.EncodeError`. Consult the api-reference.md for a complete listing of available functions. ```APIDOC ## `new` ### Description Function to create a new `Jason.EncodeError`. Consult the api-reference.md for a complete listing of available functions. ### Usage ```elixir Jason.EncodeError.new(message) ``` ### Parameters #### Path Parameters - **message** (String.t()) - Required - The error message describing the encoding failure. ``` -------------------------------- ### Configure Absinthe.Plug with Jason Source: https://jason.hexdocs.pm/readme.md When using Absinthe.Plug, specify Jason as the :json_codec option to enable its use for JSON handling. This applies both when called directly and when used within a Phoenix router. ```elixir plug Absinthe.Plug, schema: MyApp.Schema, json_codec: Jason ``` ```elixir forward "/api", to: Absinthe.Plug, init_opts: [schema: MyApp.Schema, json_codec: Jason] ``` -------------------------------- ### Jason.Formatter.minimize_to_iodata/2 Source: https://jason.hexdocs.pm/Jason.Formatter.md Minimizes JSON-encoded input and returns the result as iodata. This is preferred for IO operations or network transmission to optimize memory usage and performance. ```APIDOC ## Jason.Formatter.minimize_to_iodata/2 ### Description Minimizes JSON-encoded `input` and returns iodata. This function should be preferred to `minimize/2`, if the minimized JSON will be handed over to one of the IO functions or sent over the socket. The Erlang runtime is able to leverage vectorised writes and avoid allocating a continuous buffer for the whole resulting string, lowering memory use and increasing performance. ### Function Signature ```elixir @spec minimize_to_iodata(iodata(), opts()) :: iodata() ``` ``` -------------------------------- ### Derive Jason.Encoder for External Structs Source: https://jason.hexdocs.pm/readme.md If you need to encode a struct you do not own, use Protocol.derive/3 outside of any module to implement Jason.Encoder. You can specify fields to encode using the 'only' option or encode all fields by omitting it. ```elixir Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...]) Protocol.derive(Jason.Encoder, NameOfTheStruct) ``` -------------------------------- ### Jason.Formatter.pretty_print_to_iodata/2 Source: https://jason.hexdocs.pm/Jason.Formatter.md Pretty-prints JSON-encoded input and returns the result as iodata. This is preferred for IO operations or network transmission to optimize memory usage and performance. ```APIDOC ## Jason.Formatter.pretty_print_to_iodata/2 ### Description Pretty-prints JSON-encoded `input` and returns iodata. This function should be preferred to `pretty_print/2`, if the pretty-printed JSON will be handed over to one of the IO functions or sent over the socket. The Erlang runtime is able to leverage vectorised writes and avoid allocating a continuous buffer for the whole resulting string, lowering memory use and increasing performance. ### Function Signature ```elixir @spec pretty_print_to_iodata(iodata(), opts()) :: iodata() ``` ``` -------------------------------- ### Compile-time JSON Map Encoding with json_map_take Source: https://jason.hexdocs.pm/Jason.Helpers.md Use `json_map_take/2` to encode a map with a compile-time list of keys into a `Jason.Fragment`. This macro is equivalent to calling `Map.take/2` before encoding and works similarly to `json_map/1`. ```elixir map = %{a: 1, b: 2, c: 3} fragment = json_map_take(map, [:c, :b]) Jason.encode!(fragment) ``` -------------------------------- ### Implement Jason.Encoder for Collection Types Source: https://jason.hexdocs.pm/ Provide custom encoders for unsupported collection types like MapSet, Range, and Stream by converting them to lists. ```elixir defimpl Jason.Encoder, for: [MapSet, Range, Stream] do def encode(struct, opts) do Jason.Encode.list(Enum.to_list(struct), opts) end end ``` -------------------------------- ### Derive Jason.Encoder for Structs (All Fields) Source: https://jason.hexdocs.pm/ Use the @derive macro to automatically generate a Jason.Encoder implementation for a struct, encoding all its fields. Use with caution. ```elixir @derive Jason.Encoder defstruct # ... ``` -------------------------------- ### `Jason.Encoder` Derivation with `:only` option Source: https://jason.hexdocs.pm/Jason.Encoder.md This implementation is generated when `@derive {Jason.Encoder, only: [:foo]}` is used. It encodes only the specified fields. ```elixir defimpl Jason.Encoder, for: Test do def encode(value, opts) do Jason.Encode.map(Map.take(value, [:foo]), opts) end end ``` -------------------------------- ### Compile-time JSON decoding with ~j and modifiers Source: https://jason.hexdocs.pm/Jason.Sigil.md Use `~j` for JSON strings to decode them at compile-time using `Jason.decode!/2`. Modifiers like `a`, `A`, `r`, and `c` can be appended to alter decoding behavior. ```elixir iex> ~j"0" 0 ``` ```elixir iex> ~j"[1, 2, 3]" [1, 2, 3] ``` ```elixir iex> ~j'"string"'r "string" ``` ```elixir iex> ~j"{}" %{} ``` ```elixir iex> ~j'{"atom": "value"}'a %{atom: "value"} ``` ```elixir iex> ~j'{"#{:j}": #{~c'"j"'}}'A %{j: "j"} ``` -------------------------------- ### Compile-time JSON Map Encoding with json_map Source: https://jason.hexdocs.pm/Jason.Helpers.md Use `json_map/1` to encode a keyword list into a `Jason.Fragment` at compile time. The resulting fragment can then be encoded into a JSON string using `Jason.encode!/2`. This macro only allows keys with ASCII characters that do not require escaping. ```elixir fragment = json_map(foo: 1, bar: 2) Jason.encode!(fragment) ``` -------------------------------- ### Jason Decode Empty JSON Source: https://jason.hexdocs.pm/Jason.md Demonstrates decoding an empty JSON object, which results in a successful parse returning an empty map. ```elixir iex> Jason.decode("{}") {:ok, %{}} ``` -------------------------------- ### `Jason.Encoder` Derivation with `:except` option Source: https://jason.hexdocs.pm/Jason.Encoder.md This implementation is generated when `@derive {Jason.Encoder, except: [:foo]}` is used. It encodes all fields except the specified ones. ```elixir defimpl Jason.Encoder, for: Test do def encode(value, opts) do Jason.Encode.map(Map.take(value, [:bar, :baz]), opts) end end ``` -------------------------------- ### Derive Jason.Encoder for Structs (Specific Fields) Source: https://jason.hexdocs.pm/ Use the @derive macro to automatically generate a Jason.Encoder implementation for a struct, specifying which fields to include. ```elixir @derive {Jason.Encoder, only: [....]} defstruct # ... ``` -------------------------------- ### Jason Decode Invalid JSON Source: https://jason.hexdocs.pm/Jason.md Illustrates the error handling for invalid JSON input, showing a `Jason.DecodeError` with details about the position and token where the error occurred. ```elixir iex> Jason.decode("invalid") {:error, %Jason.DecodeError{data: "invalid", position: 0, token: nil}} ``` -------------------------------- ### Derive Jason.Encoder for External Structs (All Fields) Source: https://jason.hexdocs.pm/ Use Protocol.derive to add a Jason.Encoder implementation to a struct you do not own, encoding all its fields. Use with caution. ```elixir Protocol.derive(Jason.Encoder, NameOfTheStruct) ``` -------------------------------- ### Jason.Formatter.minimize/2 Source: https://jason.hexdocs.pm/Jason.Formatter.md Minimizes JSON-encoded input. The output will contain one JSON object per line, with no trailing newline. It accepts options to control the record separator. ```APIDOC ## Jason.Formatter.minimize/2 ### Description Minimizes JSON-encoded `input`. `input` may contain multiple JSON objects or arrays, optionally separated by whitespace. Minimized output will contain one object per line. No trailing newline is emitted. ### Function Signature ```elixir @spec minimize(iodata(), opts()) :: binary() ``` ### Options * `:record_separator` - controls the string used as newline (default: `"\n"`). ### Example ```elixir inex> Jason.Formatter.minimize(~s|{ "a" : "b" , "c": \n\n 2}|) ~s|{"a":"b","c":2}| ``` -------------------------------- ### json_map_take Source: https://jason.hexdocs.pm/Jason.Helpers.md Encodes a JSON map from a variable containing a map and a compile-time list of keys. It's equivalent to calling `Map.take/2` before encoding and works similarly to `json_map/2`. ```APIDOC ## `json_map_take` ### Description Encodes a JSON map from a variable containing a map and a compile-time list of keys, similar to `json_map/2` but with pre-selection of keys. ### Macro Signature `json_map_take(map_variable, keys_list)` ### Parameters #### Map Variable - **map_variable** (map) - A variable containing the map to encode. #### Keys List - **keys_list** (list) - A compile-time list of keys to include from the map. ### Behavior Equivalent to `Map.take/2` before encoding. Preserves the order of the keys as specified in `keys_list`. ### Example ```elixir map = %{a: 1, b: 2, c: 3} fragment = json_map_take(map, [:c, :b]) Jason.encode!(fragment) # Output: "{\"c\":3,\"b\":2}" ``` ``` -------------------------------- ### Minimize JSON to Iodata Source: https://jason.hexdocs.pm/Jason.Formatter.md Minimizes JSON-encoded input and returns iodata. This function is preferred over `minimize/2` when the output will be used with IO functions or sent over a socket, as it can improve performance and reduce memory usage by leveraging vectorized writes. ```elixir @spec minimize_to_iodata(iodata(), opts()) :: iodata() Minimizes JSON-encoded `input` and returns iodata. This function should be preferred to `minimize/2`, if the minimized JSON will be handed over to one of the IO functions or sent over the socket. The Erlang runtime is able to leverage vectorised writes and avoid allocating a continuous buffer for the whole resulting string, lowering memory use and increasing performance. ``` -------------------------------- ### decode! Source: https://jason.hexdocs.pm/Jason.md Parses a JSON value from iodata. It raises an error if decoding fails. ```APIDOC ## `decode!` ### Description Parses a JSON value from `input` iodata. Similar to `decode/2` except it will unwrap the error tuple and raise in case of errors. ### Signature ```elixir @spec decode!(iodata(), [decode_opt()]) :: term() | no_return() ``` ### Examples ```elixir Jason.decode!("{}") # %{} Jason.decode!("invalid") # ** (Jason.DecodeError) unexpected byte at position 0: 0x69 ("i") ``` ``` -------------------------------- ### Compile-time raw JSON decoding with ~J Source: https://jason.hexdocs.pm/Jason.Sigil.md Use `~J` for raw JSON strings when you need to ignore Elixir interpolations and escape characters during compile-time decoding. ```elixir iex> ~J'"#{string}"' "#{string}" ``` ```elixir iex> ~J'"\u0078\\y"' "x\\y" ``` ```elixir iex> ~J'{"#{key}": "#{}"}'a %{"\#{key}": "\#{}"} ``` -------------------------------- ### Jason.Formatter.pretty_print/2 Source: https://jason.hexdocs.pm/Jason.Formatter.md Pretty-prints JSON-encoded input. Objects in the output will be separated by newlines. It supports options for indentation, line separators, record separators, and spacing after colons. ```APIDOC ## Jason.Formatter.pretty_print/2 ### Description Pretty-prints JSON-encoded `input`. `input` may contain multiple JSON objects or arrays, optionally separated by whitespace. Objects in output will be separated by newlines. No trailing newline is emitted. ### Function Signature ```elixir @spec pretty_print(iodata(), opts()) :: binary() ``` ### Options * `:indent` - used for nested objects and arrays (default: two spaces - `" "`); * `:line_separator` - used in nested objects (default: `"\n"`); * `:record_separator` - separates root-level objects and arrays (default is the value for `:line_separator` option); * `:after_colon` - printed after a colon inside objects (default: one space - `" "`). ### Example ```elixir inex> Jason.Formatter.pretty_print(~s|{"a":{"b": [1, 2]}}|) ~s|{ "a": { "b": [ 1, 2 ] } }| ``` -------------------------------- ### Function Specification for `encode` Source: https://jason.hexdocs.pm/Jason.Encoder.md Specifies the signature for the `encode` function, which takes a term and encoding options, returning iodata. ```elixir @spec encode(t(), opts()) :: iodata() ``` -------------------------------- ### keyword Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir keyword list to its JSON object representation. ```APIDOC ## `keyword` ### Description Encodes an Elixir keyword list to its JSON object representation. ### Signature ```elixir @spec keyword(keyword(), opts()) :: iodata() ``` ``` -------------------------------- ### Derive Jason.Encoder for External Structs (Specific Fields) Source: https://jason.hexdocs.pm/ Use Protocol.derive to add a Jason.Encoder implementation to a struct you do not own, specifying which fields to include. ```elixir Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...]) ``` -------------------------------- ### json_map Source: https://jason.hexdocs.pm/Jason.Helpers.md Encodes a JSON map from a compile-time keyword. It encodes keys at compile time and strives to create a flat iodata structure for maximum efficiency. It returns a %Jason.Fragment{} struct that needs to be passed to a main encoding function like `Jason.encode/2`. ```APIDOC ## `json_map` ### Description Encodes a JSON map from a compile-time keyword, optimizing for efficiency and returning a %Jason.Fragment{} struct. ### Macro Signature `json_map(keyword_list)` ### Parameters #### Keyword List - **keyword_list** (keyword) - A compile-time keyword list representing the JSON map. ### Restrictions Only allows keys that do not require escaping, meaning only ASCII characters from 0x1F..0x7F excluding '\', '/' and '"'. Control characters are also excluded. ### Behavior Preserves the order of the keys. ### Example ```elixir fragment = json_map(foo: 1, bar: 2) Jason.encode!(fragment) # Output: "{\"foo\":1,\"bar\":2}" ``` ``` -------------------------------- ### Type Specification for `opts` Source: https://jason.hexdocs.pm/Jason.Encoder.md Defines the type for encoding options, which can be passed to various `Jason.Encode` functions. ```elixir @type opts() :: Jason.Encode.opts() ``` -------------------------------- ### Encode term to iodata, returning {:ok, iodata} or {:error, ...} Source: https://jason.hexdocs.pm/Jason.md Use `encode_to_iodata/1` to generate JSON as iodata, which is more efficient for IO operations. It returns `{:ok, iodata}` on success or `{:error, ...}` on failure. ```elixir @spec encode_to_iodata(term(), [encode_opt()]) :: {:ok, iodata()} | {:error, Jason.EncodeError.t() | Exception.t()} ``` ```elixir iex> {:ok, iodata} = Jason.encode_to_iodata(%{a: 1}) iex> IO.iodata_to_binary(iodata) ~S|{"a":1}| ``` ```elixir iex> Jason.encode_to_iodata("\xFF") {:error, %Jason.EncodeError{message: "invalid byte 0xFF in <<255>>"}} ``` -------------------------------- ### Encode JSON Fragment with Jason Source: https://jason.hexdocs.pm/ Use Jason.Fragment to embed already encoded JSON within a structure to avoid unnecessary decoding/encoding cycles. ```elixir already_encoded_json = Jason.encode!(%{hello: "world"}) Jason.encode!(%{foo: Jason.Fragment.new(already_encoded_json)}) ``` -------------------------------- ### Jason Decode Function Signature Source: https://jason.hexdocs.pm/Jason.md Provides the type specification for the `Jason.decode/2` function, indicating it accepts iodata and decode options, returning either a successful parse or a decode error. ```elixir @spec decode(iodata(), [decode_opt()]) :: {:ok, term()} | {:error, Jason.DecodeError.t()} ``` -------------------------------- ### message/1 Source: https://jason.hexdocs.pm/Jason.DecodeError.md Provides a human-readable error message for a given Jason.DecodeError. ```APIDOC ## `message` ### Function Signature ```elixir message(error :: t()) :: String.t() ``` ### Description The `message/1` function takes a `Jason.DecodeError` struct and returns a formatted string that describes the decoding error. This is useful for displaying user-friendly error messages when JSON parsing fails. ``` -------------------------------- ### Encode term to iodata, raising on error Source: https://jason.hexdocs.pm/Jason.md Use `encode_to_iodata!/1` to generate JSON as iodata and raise an error if encoding fails. This is similar to `encode_to_iodata/1` but unwraps the error tuple. ```elixir @spec encode_to_iodata!(term(), [encode_opt()]) :: iodata() | no_return() ``` ```elixir iex> iodata = Jason.encode_to_iodata!(%{a: 1}) iex> IO.iodata_to_binary(iodata) ~S|{"a":1}| ``` ```elixir iex> Jason.encode_to_iodata!("\xFF") ** (Jason.EncodeError) invalid byte 0xFF in <<255>> ``` -------------------------------- ### Default `Jason.Encoder` Derivation Source: https://jason.hexdocs.pm/Jason.Encoder.md This implementation is generated when `@derive Jason.Encoder` is used. It encodes all struct fields except `:__struct__`. ```elixir defimpl Jason.Encoder, for: Test do def encode(value, opts) do Jason.Encode.map(Map.take(value, [:foo, :bar, :baz]), opts) end end ``` -------------------------------- ### encode_to_iodata! Source: https://jason.hexdocs.pm/Jason.md Generates JSON as iodata from an Elixir term. It raises an error if encoding fails. ```APIDOC ## `encode_to_iodata!` ### Description Generates JSON corresponding to `input` and returns iodata. Similar to `encode_to_iodata/1` except it will unwrap the error tuple and raise in case of errors. ### Function Signature ```elixir @spec encode_to_iodata!(term(), [encode_opt()]) :: iodata() | no_return() ``` ### Examples ```elixir iex> iodata = Jason.encode_to_iodata!(%{a: 1}) iex> IO.iodata_to_binary(iodata) ~S|{"a":1}"| iex> Jason.encode_to_iodata!("\xFF") ** (Jason.EncodeError) invalid byte 0xFF in <<255>> ``` ``` -------------------------------- ### Jason Decode Options Type Source: https://jason.hexdocs.pm/Jason.md Defines the available options for the `Jason.decode/2` function, specifying how JSON data should be interpreted. ```elixir @type decode_opt() :: {:keys, keys()} | {:strings, strings()} | {:floats, floats()} | {:objects, objects()} ``` -------------------------------- ### Jason.Encoder.encode/2 Source: https://jason.hexdocs.pm/Jason.Encoder.md Encodes a value to JSON using the Jason.Encoder protocol. The `opts` argument is opaque and can be passed to various functions in `Jason.Encode`. ```APIDOC ## `encode` ```elixir @spec encode(t(), opts()) :: iodata() ``` Encodes `value` to JSON. The argument `opts` is opaque - it can be passed to various functions in `Jason.Encode` (or to the protocol function itself) for encoding values to JSON. ``` -------------------------------- ### Jason.Encode.opts Type Source: https://jason.hexdocs.pm/Jason.Encode.md Defines the opaque type for encoding options. ```elixir @opaque opts() ``` -------------------------------- ### list Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir list to its JSON array representation. ```APIDOC ## `list` ### Description Encodes an Elixir list to its JSON array representation. ### Signature ```elixir @spec list(list(), opts()) :: iodata() ``` ``` -------------------------------- ### Jason.decode Source: https://jason.hexdocs.pm/Jason.md Parses a JSON value from iodata. Supports various decoding options for keys, strings, floats, and objects. ```APIDOC ## Jason.decode ### Description Parses a JSON value from `input` iodata. ### Method `decode(iodata(), [decode_opt()])` ### Parameters #### Options - `:keys` (keys()) - Controls how keys in objects are decoded. Possible values are: `:strings` (default), `:atoms`, `:atoms!`, or a custom decoder function. - `:strings` (strings()) - Controls how strings are decoded. Possible values are: `:reference` (default) or `:copy`. - `:floats` (floats()) - Controls how floats are decoded. Possible values are: `:native` (default) or `:decimals`. - `:objects` (objects()) - Controls how objects are decoded. Possible values are: `:maps` (default) or `:ordered_objects`. ### Examples ```elixir iex> Jason.decode("{}") {:ok, %{}} iex> Jason.decode("invalid") {:error, %Jason.DecodeError{data: "invalid", position: 0, token: nil}} ``` ### Response #### Success Response - `{:ok, term()}` - The decoded Elixir term. #### Error Response - `{:error, Jason.DecodeError.t()}` - An error tuple containing details about the decoding failure. ``` -------------------------------- ### Pretty Print JSON to Iodata Source: https://jason.hexdocs.pm/Jason.Formatter.md Pretty-prints JSON-encoded input and returns iodata. This function is preferred over `pretty_print/2` for IO operations or network transmission, as it allows the Erlang runtime to optimize data handling, potentially reducing memory allocation and increasing speed. ```elixir @spec pretty_print_to_iodata(iodata(), opts()) :: iodata() Pretty-prints JSON-encoded `input` and returns iodata. This function should be preferred to `pretty_print/2`, if the pretty-printed JSON will be handed over to one of the IO functions or sent over the socket. The Erlang runtime is able to leverage vectorised writes and avoid allocating a continuous buffer for the whole resulting string, lowering memory use and increasing performance. ``` -------------------------------- ### encode! Source: https://jason.hexdocs.pm/Jason.md Generates a JSON string from an Elixir term. It raises an error if encoding fails. ```APIDOC ## `encode!` ### Description Generates JSON corresponding to `input`. Similar to `encode/1` except it will unwrap the error tuple and raise in case of errors. ### Function Signature ```elixir @spec encode!(term(), [encode_opt()]) :: String.t() | no_return() ``` ### Examples ```elixir iex> Jason.encode!(%{a: 1}) ~S|{"a":1}"| iex> Jason.encode!("\xFF") ** (Jason.EncodeError) invalid byte 0xFF in <<255>> ``` ``` -------------------------------- ### Jason Encode Options Type Source: https://jason.hexdocs.pm/Jason.md Defines the available options for encoding JSON data with Jason, controlling aspects like escaping, map representation, and pretty-printing. ```elixir @type encode_opt() :: {:escape, escape()} | {:maps, maps()} | {:pretty, boolean() | Jason.Formatter.opts()} ``` -------------------------------- ### encode_to_iodata Source: https://jason.hexdocs.pm/Jason.md Generates JSON as iodata from an Elixir term, returning an {:ok, iodata} tuple or an error tuple. ```APIDOC ## `encode_to_iodata` ### Description Generates JSON corresponding to `input` and returns iodata. This function should be preferred to `encode/2`, if the generated JSON will be handed over to one of the IO functions or sent over the socket. The Erlang runtime is able to leverage vectorised writes and avoid allocating a continuous buffer for the whole resulting string, lowering memory use and increasing performance. ### Function Signature ```elixir @spec encode_to_iodata(term(), [encode_opt()]) :: {:ok, iodata()} | {:error, Jason.EncodeError.t() | Exception.t()} ``` ### Examples ```elixir iex> {:ok, iodata} = Jason.encode_to_iodata(%{a: 1}) iex> IO.iodata_to_binary(iodata) ~S|{"a":1}"| iex> Jason.encode_to_iodata("\xFF") {:error, %Jason.EncodeError{message: "invalid byte 0xFF in <<255>>"}} ``` ``` -------------------------------- ### string Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir string to its JSON string representation. ```APIDOC ## `string` ### Description Encodes an Elixir string to its JSON string representation. ### Signature ```elixir @spec string(String.t(), opts()) :: iodata() ``` ``` -------------------------------- ### Jason Strings Type Source: https://jason.hexdocs.pm/Jason.md Specifies how strings are handled during decoding, either by creating references to the original data or by making copies. ```elixir @type strings() :: :reference | :copy ``` -------------------------------- ### encode Source: https://jason.hexdocs.pm/Jason.md Generates JSON corresponding to the input term. The generation is controlled by the `Jason.Encoder` protocol. ```APIDOC ## `encode` ### Description Generates JSON corresponding to `input`. The generation is controlled by the `Jason.Encoder` protocol, please refer to the module to read more on how to define the protocol for custom data types. ### Signature ```elixir @spec encode(term(), [encode_opt()]) :: {:ok, String.t()} | {:error, Jason.EncodeError.t() | Exception.t()} ``` ### Options * `:escape` - controls how strings are encoded. Possible values are: * `:json` (default) - the regular JSON escaping as defined by RFC 7159. * `:javascript_safe` - additionally escapes the LINE SEPARATOR (U+2028) and PARAGRAPH SEPARATOR (U+2029) characters to make the produced JSON valid JavaScript. * `:html_safe` - similar to `:javascript_safe`, but also escapes the `/` character to prevent XSS. * `:unicode_safe` - escapes all non-ascii characters. * `:maps` - controls how maps are encoded. Possible values are: * `:strict` - checks the encoded map for duplicate keys and raises if they appear. For example `%{:foo => 1, "foo" => 2}` would be rejected, since both keys would be encoded to the string `"foo"`. * `:naive` (default) - does not perform the check. * `:pretty` - controls pretty printing of the output. Possible values are: * `true` to pretty print with default configuration * a keyword of options as specified by `Jason.Formatter.pretty_print/2`. ### Examples ```elixir Jason.encode(%{a: 1}) # {:ok, ~S|{"a":1}|} Jason.encode("\xFF") # {:error, %Jason.EncodeError{message: "invalid byte 0xFF in <<255>>"}} ``` ``` -------------------------------- ### Jason.Encode.keyword Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir keyword list to JSON. Accepts encoding options. ```elixir @spec keyword( keyword(), opts() ) :: iodata() ``` -------------------------------- ### Jason.DecodeError Type Definition Source: https://jason.hexdocs.pm/Jason.DecodeError.md Defines the structure of the %Jason.DecodeError{} struct, including its fields for error messages, position, and the problematic token. ```elixir @type t() :: %Jason.DecodeError{ __exception__: true, data: String.t(), position: integer(), token: term() } ``` -------------------------------- ### value Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes any Elixir term to its JSON representation. This is equivalent to calling the `Jason.Encoder.encode/2` protocol function and is slightly more efficient for built-in types due to internal dispatching. ```APIDOC ## `value` ### Description Encodes any Elixir term to its JSON representation. This is equivalent to calling the `Jason.Encoder.encode/2` protocol function and is slightly more efficient for built-in types due to internal dispatching. ### Signature ```elixir @spec value(term(), opts()) :: iodata() ``` ``` -------------------------------- ### Pretty Print JSON Data Source: https://jason.hexdocs.pm/Jason.Formatter.md Pretty-prints JSON-encoded input. Objects in the output are separated by newlines. Options control indentation, line separators, record separators, and spacing after colons. This function is suitable for human-readable output. ```elixir @spec pretty_print(iodata(), opts()) :: binary() Pretty-prints JSON-encoded `input`. `input` may contain multiple JSON objects or arrays, optionally separated by whitespace (e.g., one object per line). Objects in output will be separated by newlines. No trailing newline is emitted. ## Options * `:indent` - used for nested objects and arrays (default: two spaces - `" "`); * `:line_separator` - used in nested objects (default: `"\n"`); * `:record_separator` - separates root-level objects and arrays (default is the value for `:line_separator` option); * `:after_colon` - printed after a colon inside objects (default: one space - `" "`). ## Examples iex> Jason.Formatter.pretty_print(~s|{"a":{"b": [1, 2]}}|) ~s|{ "a": { "b": [ 1, 2 ] } }| ``` -------------------------------- ### Jason.Encode.list Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir list to JSON. Accepts encoding options. ```elixir @spec list(list(), opts()) :: iodata() ``` -------------------------------- ### Jason.Encode.string Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir string to JSON. Accepts encoding options. ```elixir @spec string(String.t(), opts()) :: iodata() ``` -------------------------------- ### Encode Elixir Map to JSON Source: https://jason.hexdocs.pm/ Use Jason.encode! to convert an Elixir map into a JSON string. ```elixir iex(1)> Jason.encode!(%{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"}) "{\"age\":44,\"name\":\"Steve Irwin\",\"nationality\":\"Australian\"}" ``` -------------------------------- ### `t` Source: https://jason.hexdocs.pm/Jason.EncodeError.md Defines the type specification for `Jason.EncodeError`. It is a struct with a `__exception__` field set to true and a `message` field of type String. ```APIDOC ## `t` ### Description Defines the type specification for `Jason.EncodeError`. It is a struct with a `__exception__` field set to true and a `message` field of type String. ### Type Specification ```elixir @type t() :: %Jason.EncodeError{__exception__: true, message: String.t()} ``` ``` -------------------------------- ### struct Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir struct to its JSON object representation. ```APIDOC ## `struct` ### Description Encodes an Elixir struct to its JSON object representation. ### Signature ```elixir @spec struct(struct(), opts()) :: iodata() ``` ``` -------------------------------- ### Jason.Encode.map Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir map to JSON. Accepts encoding options. ```elixir @spec map(map(), opts()) :: iodata() ``` -------------------------------- ### Jason Keys Type Source: https://jason.hexdocs.pm/Jason.md Specifies the strategy for decoding JSON object keys, allowing them to be atoms, strings, or custom-decoded terms. ```elixir @type keys() :: :atoms | :atoms! | :strings | :copy | (String.t() -> term()) ``` -------------------------------- ### Minimize JSON Data Source: https://jason.hexdocs.pm/Jason.Formatter.md Minimizes JSON-encoded input. Handles multiple JSON objects or arrays separated by whitespace. The minimized output contains one object per line without a trailing newline. The `:record_separator` option controls the string used as a newline. ```elixir @spec minimize(iodata(), opts()) :: binary() Minimizes JSON-encoded `input`. `input` may contain multiple JSON objects or arrays, optionally separated by whitespace (e.g., one object per line). Minimized output will contain one object per line. No trailing newline is emitted. ## Options * `:record_separator` - controls the string used as newline (default: `"\n"`). ## Examples iex> Jason.Formatter.minimize(~s|{ "a" : "b" , "c": 2}|) ~s|{"a":"b","c":2}| ``` -------------------------------- ### Decode JSON String to Elixir Map Source: https://jason.hexdocs.pm/ Use Jason.decode! to convert a JSON string into an Elixir map. ```elixir iex(2)> Jason.decode!(~s({"age":44,"name":"Steve Irwin","nationality":"Australian"})) %{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"} ``` -------------------------------- ### OrderedObject Type Definition Source: https://jason.hexdocs.pm/Jason.OrderedObject.md Defines the type for Jason.OrderedObject, which holds a list of key-value pairs. ```elixir @type t() :: %Jason.OrderedObject{values: [{String.Chars.t(), term()}]} ``` -------------------------------- ### Jason.Encode.struct Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir struct to JSON. Accepts encoding options. ```elixir @spec struct( struct(), opts() ) :: iodata() ``` -------------------------------- ### Jason.Encode.integer Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir integer to JSON. ```elixir @spec integer(integer()) :: iodata() ``` -------------------------------- ### Decode JSON with Error Raising Source: https://jason.hexdocs.pm/Jason.md Parses a JSON value from iodata. Raises an error if decoding fails. ```elixir @spec decode!(iodata(), [decode_opt()]) :: term() | no_return() ``` ```elixir iex> Jason.decode!("{}") %{} ``` ```elixir iex> Jason.decode!("invalid") ** (Jason.DecodeError) unexpected byte at position 0: 0x69 ("i") ``` -------------------------------- ### Jason.Encode.value Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes any Elixir term to JSON. Equivalent to calling Jason.Encoder.encode/2. This function is slightly more efficient for built-in types due to internal dispatching. ```elixir @spec value(term(), opts()) :: iodata() ``` -------------------------------- ### Jason Objects Type Source: https://jason.hexdocs.pm/Jason.md Controls how JSON objects are decoded, either as standard Elixir maps or as ordered objects preserving insertion order. ```elixir @type objects() :: :maps | :ordered_objects ``` -------------------------------- ### map Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir map to its JSON object representation. ```APIDOC ## `map` ### Description Encodes an Elixir map to its JSON object representation. ### Signature ```elixir @spec map(map(), opts()) :: iodata() ``` ``` -------------------------------- ### Jason.Encode.float Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir float to JSON. ```elixir @spec float(float()) :: iodata() ``` -------------------------------- ### Define Jason.EncodeError Type Source: https://jason.hexdocs.pm/Jason.EncodeError.md Defines the structure of the %Jason.EncodeError{} struct, which is used to represent encoding errors in Jason. It includes the `__exception__` field set to true and a `message` field for the error details. ```elixir @type t() :: %Jason.EncodeError{__exception__: true, message: String.t()} ``` -------------------------------- ### Encode term to JSON string, raising on error Source: https://jason.hexdocs.pm/Jason.md Use `encode!/1` to convert an Elixir term to a JSON string. It will raise a `Jason.EncodeError` if the term cannot be encoded. ```elixir @spec encode!(term(), [encode_opt()]) :: String.t() | no_return() ``` ```elixir iex> Jason.encode!(%{a: 1}) ~S|{"a":1}| ``` ```elixir iex> Jason.encode!("\xFF") ** (Jason.EncodeError) invalid byte 0xFF in <<255>> ``` -------------------------------- ### integer Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir integer to its JSON number representation. ```APIDOC ## `integer` ### Description Encodes an Elixir integer to its JSON number representation. ### Signature ```elixir @spec integer(integer()) :: iodata() ``` ``` -------------------------------- ### float Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir float to its JSON number representation. ```APIDOC ## `float` ### Description Encodes an Elixir float to its JSON number representation. ### Signature ```elixir @spec float(float()) :: iodata() ``` ``` -------------------------------- ### Encode Elixir Term to JSON Source: https://jason.hexdocs.pm/Jason.md Generates JSON corresponding to an Elixir term. The generation is controlled by the Jason.Encoder protocol. ```elixir @spec encode(term(), [encode_opt()]) :: {:ok, String.t()} | {:error, Jason.EncodeError.t() | Exception.t()} ``` ```elixir iex> Jason.encode(%{a: 1}) {:ok, ~S|{"a":1}|} ``` ```elixir iex> Jason.encode("\xFF") {:error, %Jason.EncodeError{message: "invalid byte 0xFF in <<255>>"}} ``` -------------------------------- ### Jason Floats Type Source: https://jason.hexdocs.pm/Jason.md Defines how floating-point numbers are decoded, offering native Elixir float conversion or arbitrary precision using the Decimal library. ```elixir @type floats() :: :native | :decimals ``` -------------------------------- ### atom Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir atom to its JSON string representation. ```APIDOC ## `atom` ### Description Encodes an Elixir atom to its JSON string representation. ### Signature ```elixir @spec atom(atom(), opts()) :: iodata() ``` ``` -------------------------------- ### Type Specification for `t` Source: https://jason.hexdocs.pm/Jason.Encoder.md Defines the general type `t` for any term that can be encoded. ```elixir @type t() :: term() ``` -------------------------------- ### Jason.Encode.atom Function Source: https://jason.hexdocs.pm/Jason.Encode.md Encodes an Elixir atom to JSON. Accepts encoding options. ```elixir @spec atom(atom(), opts()) :: iodata() ``` -------------------------------- ### Jason.DecodeError Type Source: https://jason.hexdocs.pm/Jason.DecodeError.md Defines the structure of the DecodeError type, including the error message, the position of the error in the input, and the token that caused the error. ```APIDOC ## `t` ### Type Definition ```elixir @type t() :: %Jason.DecodeError{ __exception__: true, data: String.t(), position: integer(), token: term() } ``` ### Description The `t()` type represents a decoding error encountered by the Jason library. It provides details about the error, such as the error message (`data`), the location in the input string where the error occurred (`position`), and the specific token that led to the failure (`token`). The `__exception__` field is set to `true` to indicate that this struct represents an exception. ``` -------------------------------- ### Jason Maps Type Source: https://jason.hexdocs.pm/Jason.md Defines the representation for JSON objects during encoding, supporting naive or strict map conversions. ```elixir @type maps() :: :naive | :strict ``` -------------------------------- ### Jason Escape Type Source: https://jason.hexdocs.pm/Jason.md Specifies the types of escaping that can be applied during JSON encoding, including standard JSON, Unicode-safe, HTML-safe, and JavaScript-safe. ```elixir @type escape() :: :json | :unicode_safe | :html_safe | :javascript_safe ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.