### Add Jason Dependency to Mix Project Source: https://github.com/michalmuskala/jason/blob/master/README.md Add `jason` to your dependencies in `mix.exs` to install the package. ```elixir def deps do [{:jason, "~> 1.4"}] end ``` -------------------------------- ### Configure Absinthe.Plug with Jason Codec Source: https://github.com/michalmuskala/jason/blob/master/README.md Specify `Jason` as the `:json_codec` option when using `Absinthe.Plug` to ensure proper JSON handling in your GraphQL API. ```elixir plug Absinthe.Plug, schema: MyApp.Schema, json_codec: Jason ``` ```elixir forward "/api", to: Absinthe.Plug, init_opts: [schema: MyApp.Schema, json_codec: Jason] ``` -------------------------------- ### Derive Jason.Encoder for External Structs (All Fields) Source: https://github.com/michalmuskala/jason/blob/master/README.md Use `Protocol.derive/3` outside of a module to generate a `Jason.Encoder` for a struct you do not own, encoding all its fields. Use with caution. ```elixir Protocol.derive(Jason.Encoder, NameOfTheStruct) ``` -------------------------------- ### Implement Jason.Encoder for Unsupported Collections Source: https://github.com/michalmuskala/jason/blob/master/README.md Provide custom `Jason.Encoder` implementations for collection types like `MapSet`, `Range`, and `Stream` by converting them to lists before encoding. ```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 External Structs (Specific Fields) Source: https://github.com/michalmuskala/jason/blob/master/README.md Use `Protocol.derive/3` outside of a module to generate a `Jason.Encoder` for a struct you do not own, specifying the fields to encode. ```elixir Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...]) ``` -------------------------------- ### Derive Jason.Encoder for Structs (All Fields) Source: https://github.com/michalmuskala/jason/blob/master/README.md Use the `@derive Jason.Encoder` attribute to automatically generate a `Jason.Encoder` implementation for a struct, including all its fields. Use with caution to avoid leaking private data. ```elixir @derive Jason.Encoder defstruct # ... ``` -------------------------------- ### Derive Jason.Encoder for Structs (Specific Fields) Source: https://github.com/michalmuskala/jason/blob/master/README.md Use the `@derive {Jason.Encoder, only: [...]}` attribute to automatically generate a `Jason.Encoder` implementation for a struct, specifying which fields to include. ```elixir @derive {Jason.Encoder, only: [....]} defstruct # ... ``` -------------------------------- ### Injecting Pre-encoded JSON Fragments Source: https://github.com/michalmuskala/jason/blob/master/README.md Use `Jason.Fragment` to embed already JSON-encoded strings within a structure that is about to be encoded. This prevents a decoding/encoding roundtrip, which is useful for caching or integrating with systems that provide pre-encoded JSON. ```elixir already_encoded_json = Jason.encode!(%{hello: "world"}) Jason.encode!(%{foo: Jason.Fragment.new(already_encoded_json)}) ``` -------------------------------- ### Encode Elixir Data to JSON Source: https://github.com/michalmuskala/jason/blob/master/README.md Use `Jason.encode!` to convert Elixir data structures into JSON strings. This function will raise an error if encoding fails. ```elixir iex(1)> Jason.encode!(%{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"}) "{\"age\":44,\"name\":\"Steve Irwin\",\"nationality\":\"Australian\"}" ``` -------------------------------- ### Decode JSON String to Elixir Data Source: https://github.com/michalmuskala/jason/blob/master/README.md Use `Jason.decode!` to parse a JSON string into Elixir data structures. This function will raise an error if decoding fails. ```elixir iex(2)> Jason.decode!(~s({"age":44,"name":"Steve Irwin","nationality":"Australian"})) %{"age" => 44, "name" => "Steve Irwin", "nationality" => "Australian"} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.