1 module hunt.serialization.BinarySerializer;
2 
3 import std.array : Appender;
4 import std.traits;
5 
6 import hunt.serialization.Common;
7 import hunt.serialization.Specify;
8 
9 
10 /**
11  * 
12  */
13 struct BinarySerializer {
14     private {
15         Appender!(ubyte[]) _buffer;
16     }
17 
18     ubyte[] oArchive(SerializationOptions options, T)(T val) if (!isArray!T && !isAssociativeArray!T) {
19         Unqual!T copy = val;
20         specify!(options)(this, copy);
21         return _buffer.data();
22     }
23 
24     ubyte[] oArchive(SerializationOptions options, T)(const ref T val)
25             if (!isDynamicArray!T && !isAssociativeArray!T && !isAggregateType!T) {
26         T copy = val;
27         specify!(options)(this, copy);
28         return _buffer.data();
29     }
30 
31     ubyte[] oArchive(SerializationOptions options, T)(const(T)[] val) {
32         auto copy = (cast(T[]) val).dup;
33         specify!(options)(this, copy);
34         return _buffer.data();
35     }
36 
37     ubyte[] oArchive(SerializationOptions options, K, V)(const(V[K]) val) {
38         auto copy = cast(V[K]) val.dup;
39         specify!(options)(this, copy);
40         return _buffer.data();
41     }
42 
43     void putUbyte(ref ubyte val) {
44         _buffer.put(val);
45     }
46 
47     void putClass(SerializationOptions options, T)(T val) if (is(T == class)) {
48         specifyClass!(options)(this, val);
49     }
50 
51     void putRaw(ubyte[] val) {
52         _buffer.put(val);
53     }
54 
55     bool isNullObj() {
56         return false;
57     }
58 
59 }