RustyJson.OrderedObject (rustyjson v0.3.9)

Copy Markdown View Source

An ordered JSON object that preserves key insertion order.

A wrapper around a keyword list (that supports non-atom keys) allowing for proper protocol implementations.

This struct is used when decoding JSON with objects: :ordered_objects option. It preserves the original order of keys as they appeared in the JSON input.

Implements the Access behaviour and Enumerable protocol with complexity similar to keywords/lists.

Decoding

iex> RustyJson.decode!(~s({"b":2,"a":1}), objects: :ordered_objects)
%RustyJson.OrderedObject{values: [{"b", 2}, {"a", 1}]}

Encoding

When encoded back to JSON, the original key order is preserved:

iex> obj = %RustyJson.OrderedObject{values: [{"b", 2}, {"a", 1}]}
iex> RustyJson.encode!(obj)
~s({"b":2,"a":1})

Access

iex> obj = RustyJson.OrderedObject.new([{"x", 1}, {"y", 2}])
iex> obj["x"]
1

Summary

Functions

Creates a new OrderedObject from a list of key-value tuples.

Types

t()

@type t() :: %RustyJson.OrderedObject{values: [{String.Chars.t(), term()}]}

Functions

new(values)

@spec new([{String.Chars.t(), term()}]) :: t()

Creates a new OrderedObject from a list of key-value tuples.