ValueCodec


Object Hierarchy:

Object hierarchy for ValueCodec

Description:


public abstract class ValueCodec : Object

A rule for converting one value between a Vala type and JSON, YAML or TOML.

A codec tells the serializer which type to expect and how to read and write it. Use the same codec in both directions. For ordinary code, choose a ready-made Codec through Deser.ObjectCodec, Deser.create_codec_for_array or Deser.create_codec_for_map; you do not need to subclass either codec base class. For example, declare a model and use the remaining statements inside a method that throws Deser.Error:

class Item : Deser.DataObject {
public string name { get; set; }
}

var item_codec = new Deser.ObjectCodec<Item> ();
var item = new Item () { name = "a" };
string json = Deser.JsonWorker.serialize (item, item_codec);
Item copy = new Deser.JsonWorker (json).deserialize_with_codec (item_codec);

Use the array factory for standard element types; compose codecs explicitly for custom rules or nested collections:

var items_codec = Deser.create_codec_for_array<Item> ();
var items = new GLib.GenericArray<Item> ();
items.add (copy);
string yaml = Deser.YamlWorker.serialize (items, items_codec);
var restored = new Deser.YamlWorker (yaml).deserialize_with_codec (items_codec);

For example, an array of maps whose values are arrays of integer arrays ([{"numbers":1,2],[3}]) needs one codec per level:

var row_codec = Deser.create_codec_for_array<int> ();
var grid_codec = new Deser.GenericArrayCodec<GLib.GenericArray<int>> (row_codec);
var map_codec = new Deser.HashTableCodec<
GLib.GenericArray<GLib.GenericArray<int>>> (grid_codec);
Deser.Codec<GLib.GenericArray<
GLib.HashTable<string, GLib.GenericArray<GLib.GenericArray<int>>>>> root_codec =
new Deser.GenericArrayCodec<
GLib.HashTable<string, GLib.GenericArray<GLib.GenericArray<int>>>> (map_codec);

string input = "[{\"numbers\":[[1,2],[3]]}]";
var result = new Deser.JsonWorker (input).deserialize_with_codec (root_codec);
int number = result[0].lookup ("numbers")[0][1]; // 2
string output = Deser.JsonWorker.serialize_with_codec (result, root_codec);

A concrete object property such as `RepoBody body` gets an object codec automatically. Only properties with both get and set are mapped by default; use DataObject.get_ignored_properties to omit more. For a collection property of a DataObject, override CodecProvider.get_property_codec to provide its codec. For irregular scalar input, return `new CodecAuto (property.value_type)` for that property or pass `Deser.create_auto_codec<int> ()` to Deser.create_codec_for_array; this explicitly permits safe conversions such as text `"42"` to `int`. Defaults remain strict, and failed or lossy conversions raise an error. See CodecAuto.

For polymorphic objects, see Deser.ObjectCodec and ConcreteTypeResolver. Codec is the typed API used by workers; ValueCodec is the lower-level API used by object properties and codecs that operate on NullableValue directly. A typed codec bridges both APIs for non-null values: its Codec.decode_value calls Codec.decode followed by Codec.pack, while Codec.encode_value calls Codec.unpack followed by Codec.encode. Accepted nulls are handled by the base methods. Neither pack nor unpack reads or writes a document.

A codec does not store document state. The worker supplies a ValueView, while DecodeContext and EncodeContext carry settings and the current error path for each operation.


Namespace: Deser
Package: libdeser-0.1

Content:

Properties:

Creation methods:

Methods:

Inherited Members: