Skip to content

Latest commit

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

ZType Binary Serialization/Deserialization

Overview

The ZType library provides a way to serialize and deserialize various data types into a compact binary format. It supports the following types:

  • Boolean (bool)
  • Integer (int)
  • Floating-point (float)
  • Arrays ([]ZType)
  • Maps (std.StringHashMap(ZType))

This is WIP and might not be the most optimal way to do this.

Usage

Initialization

To initialize a ZType instance, use the init function. The function takes an allocator and a value of any supported type.

conststd=@import("std");
constZType=@import("path/to/ztype.zig");
pubfnmain() !void {
vargpa=std.heap.GeneralPurposeAllocator(.{}){};
constallocator=gpa.allocator();
constzbool=tryZType.init(allocator, true);
constzint=tryZType.init(allocator, 42);
constzfloat=tryZType.init(allocator, 3.14);
// TODO: needs to be fixedconstlist_of_elems: [3]ZType= .{zbool, zint, zfloat}
constzarray=tryZType.init(allocator, list_of_elems);
// Deinitialize when donezbool.deinit(allocator);
zint.deinit(allocator);
zfloat.deinit(allocator);
zarray.deinit(allocator);
}

Deinitialization

To free the memory allocated for a ZType instance, use the deinit function.

ztype.deinit(allocator);

Accessing Values

To access the original values stored in a ZType instance, use the appropriate getter functions:

  • getBool()
  • getInt()
  • getFloat()
  • getArray()
  • getMap()

Each getter function returns an error if the type does not match.

const value = try ztype.getInt();

Example

Here is a complete example demonstrating the usage of the ZType library:

conststd=@import("std");
constZType=@import("path/to/ztype.zig");
pubfnmain() !void {
vargpa=std.heap.GeneralPurposeAllocator(.{}){};
constallocator=gpa.allocator();
constzbool=tryZType.init(allocator, true);
deferzbool.deinit(allocator);
constzint=tryZType.init(allocator, 42);
deferzint.deinit(allocator);
constzfloat=tryZType.init(allocator, 3.14);
deferzfloat.deinit(allocator);
constlist_of_elems: [3]ZType= .{zbool, zint, zfloat}
constzarray=tryZType.init(allocator, list_of_elems);
deferzarray.deinit(allocator);
constbool_value=tryzbool.getBool();
constint_value=tryzint.getInt();
constfloat_value=tryzfloat.getFloat();
constarray_value=tryzarray.getArray(allocator);
std.debug.print("Bool: {}\n", .{bool_value});
std.debug.print("Int: {}\n", .{int_value});
std.debug.print("Float: {}\n", .{float_value});
std.debug.print("Array: {}\n", .{array_value});
}

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages