Recent

Author Topic: Variant type extensions for serialization  (Read 802 times)

serbod

  • Full Member
  • ***
  • Posts: 142
Variant type extensions for serialization
« on: October 01, 2020, 03:13:25 pm »
Two new managed type, making data objects prototyping and serialization/deserialization to modern formats simple and easy.

Variant is managed type (like string or array), so it not need to be manually allocated or destroyed.

Also, Variant allow using properties and methods even if they not defined for this type. And allow in runtime detect type of data contained in variant.


https://github.com/serbod/NovaLib/blob/master/VarDicts.pas

Variant Dictionary - contain name-value pairs, behave like object in JavaScript. Can store any Variant as value, even another Dictionary.
Auto-sorted by names, binary search used to get value by name

Usage:

Code: Pascal  [Select][+][-]
  1. var MyDict: Variant;
  2. MyDict := VarDictCreate();
  3. MyDict.Field1 := 1;
  4. MyDict.Field2 := 'test';
  5. WriteLn(MyDict); // {Field1:1,Field2:"test"}

Methods:
  GetCount() - get stored items count
  GetValue(Index) - get item value for given Index (0..GetCount()-1)
  GetName(Index) - get item name for given Index (0..GetCount()-1)
  GetNameIndex(Name) - get index of item Name, or <0 if Name not found
  SetValue(Name, Value) - set value for Name


https://github.com/serbod/NovaLib/blob/master/VarLists.pas

Variant List - dynamic array of any Variant types

Usage:
Code: Pascal  [Select][+][-]
  1. var MyList: Variant;
  2. MyList := VarListCreate;
  3. MyList.AddValue(1);
  4. MyList.AddValue('test');
  5. WriteLn(MyList.GetValue(0), MyList.GetValue(1));
  6.  

Methods:
  SetCapacity(MaxCount) - allocate memory for list values
  AddValue(Value) - add value to end of list. Capacity increased if needed
  GetCount() - get stored items count
  GetValue(Index) - get item value for given index (0..GetCount()-1)
  SetValue(Index, Value) - set item value for given index (0..GetCount()-1)


Variant <-> JSON serialization
https://github.com/serbod/NovaLib/blob/master/JsonUnit.pas

Variant <-> Bencode serialization
https://github.com/serbod/NovaLib/blob/master/BencodeUnit.pas

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Variant type extensions for serialization
« Reply #1 on: October 01, 2020, 07:00:14 pm »
It seems useful to me. Bookmarked and will be back to use it later.

Thank you for sharing it.

 

TinyPortal © 2005-2018