toon_codec/encode/internal
Internal utilities for array structure detection.
This module provides functions to analyze arrays and determine whether they should be encoded as inline, tabular, or expanded form. Check if a JSON value is a primitive (not array or object). Check if a JSON value is an object. Check if a JSON value is an array. Check if all values in a list are primitives.
Returns True if the list is empty or all elements are primitives. Check if all values in a list are arrays. Check if all values in a list are objects. Check if all arrays in a list contain only primitives.
Used to detect arrays of primitive arrays. Detect if an array of objects can be encoded in tabular form.
Returns Some(fields) if tabular encoding is possible, where fields is the ordered list of field names. Returns None otherwise.
Tabular encoding requires:
- All elements are objects
- All objects have the same set of keys
- All values in all objects are primitives (no nested structures)
Examples
let objects = [
Object([#("id", Number(1.0)), #("name", JsonString("Alice"))]),
Object([#("id", Number(2.0)), #("name", JsonString("Bob"))]),
]
detect_tabular(objects)
// -> Some(["id", "name"])
Check if all values in an object are primitives. Check if all objects in a list have the same keys and all primitive values. Extract field values from an object in a specified order.
Returns the values corresponding to the given field names, in the same order as the field names list.
Examples
let obj = Object([#("name", JsonString("Alice")), #("id", Number(1.0))])
extract_field_values(obj, ["id", "name"])
// -> [Number(1.0), JsonString("Alice")]
Values
pub fn all_arrays_of_primitives(
values: List(types.JsonValue),
) -> Bool
pub fn all_values_primitive(
fields: List(#(String, types.JsonValue)),
) -> Bool
pub fn detect_tabular(
values: List(types.JsonValue),
) -> option.Option(List(String))
pub fn extract_field_values(
obj: types.JsonValue,
field_names: List(String),
) -> List(types.JsonValue)
pub fn is_array(value: types.JsonValue) -> Bool
pub fn is_array_of_arrays(values: List(types.JsonValue)) -> Bool
pub fn is_array_of_objects(values: List(types.JsonValue)) -> Bool
pub fn is_array_of_primitives(
values: List(types.JsonValue),
) -> Bool
pub fn is_object(value: types.JsonValue) -> Bool
pub fn is_primitive(value: types.JsonValue) -> Bool