It is a library to read and write native structs from and to byte arrays by .NET programs in a way that is natural for .NET.
It's main purpose is to be used by the NISAM library.
The library is a port of a similar Java library, so an occasional Java'ism may be found.
[Struct] public class Data { [StructField(N=0,Type=FieldType.INT4)] private int iv; [StructField(N=1,Type=FieldType.FP8)] private double xv; [StructField(N=2,Type=FieldType.FIXSTR,Length=8,Encoding="ISO-8859-1")] private String sv; public int Iv { get { return iv; } set { iv = value; } } public double Xv { get { return xv; } set { xv = value; } } public string Sv { get { return sv; } set { sv = value; } } }
The class got a [Struct] attribute. Each field in the class got a [StructField] attribute with an element N that determines the order of the fields and an element type that describes the datatype in the native struct plus some optional elements that are needed for some field types.
// read StructReader sr = new StructReader(somebytearray); Data obj = sr.Read<Data>(typeof(Data)); // write Data obj = new Data(); ... StructWriter sw = new StructWriter(); sw.Write(obj); byte[] ba = sw.GetBytes();
To specify a field as an array put an @Array annotation on the field and make the .NET type an array.
To specify a field as a sub struct just specify Type=FieldType.STRUCT in the [StructField] attribute.
The Record library supports polymorphism in records.
You must use the [Selector] attribute on the last field in the super class.
[Struct] public class SuperData { [StructField(N=0,Type=FieldType.INT4)] private int id; [StructField(N=1,Type=FieldType.INT4)] [Selector] [SubType(Value=1,Type=typeof(DataX))] [SubType(Value=2,Type=typeof(DataY))] private int typ; public int Id { get { return id; } set { id = value; } } public int Typ { get { return typ; } set { typ = value; } } }
Usage is as simple as:
Data obj = sr.Read<Data>(typeof(Data));
This will actually read DataX or DataY instances depending on the value of the typ field.
Note that the numbering of the struct fields continue in the sub class - it does not reset to zero.
All the C# code is licensed under the Apache License, Version 2.0.
This library is written by Arne Vajhoej (arne@vajhoej.dk).
Bug reports, bug fixes and comments in general are welcome.
Since this is a hobby project, then I can not give any guarantees about timeline for new releases.
ArrayFieldAttribute | Annotation for arrays. |
BCDUtil | Class TimeUtil converts between bytes with Binary Coded Decimals ant BigDecimal objects. |
FieldInfo | Class FieldInfo contains information about a native struct field needed for conversions. |
RecordException | Class RecordException encapsulates exceptions related to record processing. |
SelectorAttribute | Annotation for selection of sub types. |
StructAttribute | Annotation for structs. |
StructFieldAttribute | Annotation for fields. |
StructInfo | Class StructInfo contains information about a native struct needed for reading and/or writing. |
StructInfoCache | Class StructInfoCache caches StructInfo objects in a singleton cache. |
StructReader | Class StructReader reads a .NET object from a byte array containing a native struct. |
StructReaderStream | Class StructReaderStream reads a .NET object from an input stream containing a native struct. |
StructWriter | Class StructWriter writes a .NET object to a byte array as a native struct. |
SubClassAndPad | Class SubClassAndPad contains information about class and padding for select field. |
SubTypeAttribute | Annotation for sub types. |
TimeUtil | Class TimeUtil converts between integers in various time formats and DateTime objects. |
Util | Utility class to process lists and to work with files instead of byte arrays. |
Util2 | Utility class to process lists and to work with files instead of byte arrays trying to work even with variable length structs. |
VAXFloatUtil | Class VAXFloatUtil converts between VAX floating point and IEEE floating point. |
ConvertSelector | Converts a selector of any type to a usable integer selector. |
ElementsProvider | Get number of elements in array. Note: can only be used with struct fields that are arrays. |
LengthProvider | Get length of field. Note: can only be used with struct fields of field types FIXSTR, FIXSTRNULTERM, PACKEDBCD and ZONEDPBCD. |
MaxLengthProvider | Get max length of struct. Note: can only be used with struct fields of field types FIXSTR, FIXSTRNULTERM, PACKEDBCD and ZONEDBCD. |
UtilObjectHandlerProcessT | Process object. |
UtilTransformerConvertT1, T2 | Convert object. |
Util2ObjectHandlerProcessT | Process object. |
Util2TransformerConvertT1, T2 | Convert object. |
Alignment | Enum Alignment specifies alignment within native struct. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Endian | Enum Endian specifies endianess within native struct. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ExtEndian | Enum ExtEndian specifies endianess in field within native struct. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
FieldType |
Enum FieldType specifies native struct types.
Semantics:
|