boxed_can_serialize


Description:


[ Version ( since = "0.10" ) ]
public bool boxed_can_serialize (Type gboxed_type, out NodeType node_type)

Checks whether it is possible to serialize a `GBoxed` of type `gboxed_type` into a [struct@Json.

Node].

The type of the node is placed inside `node_type` if the function returns `TRUE`, and it's undefined otherwise.

Example: Boxed serialization:

public struct MyStruct {
public int64 a;
public int64 b;

public static Json.Node serialize_func (void* _boxed) {
assert (_boxed != null);

MyStruct* boxed = (MyStruct*) _boxed;

Json.Node node = new Json.Node (Json.NodeType.OBJECT);
Json.Object obj = new Json.Object ();
obj.set_int_member ("a", boxed.a);
obj.set_int_member ("b", boxed.b);
node.set_object (obj);
return node;
}
}

public static int main (string[] args) {

// Check whether the struct is serializeable:
// Output: ``serializeable(1): false``
bool tmp = Json.boxed_can_serialize (typeof (MyStruct), null);
print ("serializeable(1): %s\n", tmp.to_string ());

// Register our BoxedDeserializeFunc:
Json.boxed_register_serialize_func (typeof (MyStruct), Json.NodeType.OBJECT, MyStruct.serialize_func);

// Check again:
// Output: ``serializeable(2): true``
tmp = Json.boxed_can_serialize (typeof (MyStruct), null);
print ("serializeable(2): %s\n", tmp.to_string ());


// Serialize the struct:
MyStruct stru = {10, 20};
Json.Node root = Json.boxed_serialize (typeof (MyStruct), &stru);

// Node to string:
Json.Generator generator = new Json.Generator ();
generator.set_root (root);
string data = generator.to_data (null);

// Output: ``{"a":10,"b":20}``
print (data);
print ("\n");

return 0;
}

valac --pkg json-glib-1.0 serialization-boxed.vala

Parameters:

gboxed_type

a boxed type

node_type

the node type to which the boxed type can be serialized into

Returns:

`TRUE` if the type can be serialized, and `FALSE` otherwise


Namespace: Json
Package: json-glib-1.0



2022 vala-language.org