TypeSafeAPI.JSON.Encoded (TypeSafe AI v0.1.0-alpha.3)

Copy Markdown View Source

A value that has already been serialized to JSON iodata, and encodes as those bytes wherever it appears.

TypeSafeAPI.evaluate_many/4 sends one question set with many states. Keeping the questions as a TypeSafeAPI.JSON.OrderedObject would mean walking and serializing the whole set once per request; wrapping it here serializes it once, and every request body splices the same iodata in.

encoded = TypeSafeAPI.JSON.Encoded.new(TypeSafeAPI.JSON.OrderedObject.new([{"a", 1}]))
JSON.encode!(%{"questions" => encoded})
#=> ~s({"questions":{"a":1}})

The original term is kept in object so callers can still read the structure. The two are captured together at construction and never diverge, because nothing modifies the struct afterwards.

Summary

Functions

Serializes object once and wraps it with its bytes.

The term that was encoded.

The cached JSON bytes, as a binary.

Types

t()

@type t() :: %TypeSafeAPI.JSON.Encoded{iodata: iodata(), object: term()}

Functions

new(object)

@spec new(term()) :: t()

Serializes object once and wraps it with its bytes.

object(encoded)

@spec object(t()) :: term()

The term that was encoded.

to_string(encoded)

@spec to_string(t()) :: String.t()

The cached JSON bytes, as a binary.