WeaviateEx.Types.Deserialize (WeaviateEx v0.7.4)

View Source

Deserializes Weaviate response values to Elixir types.

This module provides functions to convert Weaviate API response data back into rich Elixir types like DateTime, GeoCoordinate, etc.

Usage

You can deserialize individual values with a type hint:

# Date strings
{:ok, dt} = Deserialize.deserialize("2024-01-01T00:00:00Z", :date)
# => {:ok, ~U[2024-01-01 00:00:00Z]}

# GeoCoordinates
{:ok, geo} = Deserialize.deserialize(%{"latitude" => 52.37, "longitude" => 4.90}, :geo_coordinates)
# => {:ok, %GeoCoordinate{latitude: 52.37, longitude: 4.90}}

Or deserialize entire property maps with schema hints:

schema = %{"created_at" => :date, "location" => :geo_coordinates}
{:ok, props} = Deserialize.deserialize_properties(raw_props, schema)

Type Hints

  • :date - Parse ISO8601 string to DateTime
  • :geo_coordinates - Parse coordinate map to GeoCoordinate struct
  • :phone_number - Parse phone map to PhoneNumber.Output struct
  • :blob - Parse base64 string to Blob struct
  • :auto - Attempt automatic detection based on value structure

Summary

Functions

Attempts to auto-deserialize all properties in a map.

Deserializes a value based on the provided type hint.

Deserializes a value, raising on error.

Deserializes a map of properties using a schema that maps property names to types.

Deserializes properties, raising on error.

Types

schema()

@type schema() :: %{optional(String.t()) => type_hint()}

type_hint()

@type type_hint() :: :date | :geo_coordinates | :phone_number | :blob | :auto | nil

Functions

auto_deserialize(properties)

@spec auto_deserialize(map()) :: {:ok, map()} | {:error, String.t()}

Attempts to auto-deserialize all properties in a map.

Uses heuristics to detect types automatically.

deserialize(value, type \\ nil)

@spec deserialize(any(), type_hint()) :: {:ok, any()} | {:error, String.t()}

Deserializes a value based on the provided type hint.

Returns {:ok, value} on success or {:error, reason} on failure.

Examples

iex> Deserialize.deserialize("2024-01-01T00:00:00Z", :date)
{:ok, ~U[2024-01-01 00:00:00Z]}

iex> Deserialize.deserialize(%{"latitude" => 52.37, "longitude" => 4.90}, :geo_coordinates)
{:ok, %GeoCoordinate{latitude: 52.37, longitude: 4.90}}

deserialize!(value, type \\ nil)

@spec deserialize!(any(), type_hint()) :: any()

Deserializes a value, raising on error.

Examples

iex> Deserialize.deserialize!("2024-01-01T00:00:00Z", :date)
~U[2024-01-01 00:00:00Z]

deserialize_properties(properties, schema)

@spec deserialize_properties(map(), schema()) :: {:ok, map()} | {:error, String.t()}

Deserializes a map of properties using a schema that maps property names to types.

Examples

schema = %{
  "created_at" => :date,
  "location" => :geo_coordinates,
  "phone" => :phone_number
}

{:ok, props} = Deserialize.deserialize_properties(raw_props, schema)

deserialize_properties!(properties, schema)

@spec deserialize_properties!(map(), schema()) :: map()

Deserializes properties, raising on error.