Binary data-structure serialization
Robert Jacques
sandford at jhu.edu
Fri Jun 4 08:27:49 PDT 2010
On Fri, 04 Jun 2010 08:43:11 -0400, Robert M. Münch
<robert.muench at robertmuench.de> wrote:
> On 2010-06-04 02:27:56 +0200, Robert Jacques said:
>
>> I've written a JSON serializer/de-serializer which uses compile time
>> type reflection. Doesn't handle cycles or polymorphism yet, but I'd
>> gladly share it if you (or others) are interested.
>
> Thanks for the offer. AFAIK JSON is focused for wire-transfer, what I'm
> looking for is a way to efficiently write such structures into a file in
> binary form and re-create the run-time structure fast.
>
> What I need is directed to a database / transactional system where
> millions of in/out operations happen.
>
Pretty much all serialization methods have some amount of verification
overhead, which it sounds like you neither want nor need. I'd recommend
directly using .tupleof for structs/classes. Here's some simplified code
snippets from my JSON library:
// From toJson(T)(T value)
static if(is(T == struct) || is(T == class)) {
foreach(i,v;value.tupleof) {
result[getVariableName!(T,i)] = toJson(v);
}
}
// From fromJson(T)(ref T dst)
static if(is(T == struct) || is(T == class)) {
static if(__traits(compiles, dst = new T() ))
if(dst is null) dst = new T();
static if(is(T == class)) enforce(dst !is null);
foreach(i,v;dst.tupleof) { // Warning V is read-only because
ref v doesn't compile
auto pJson = getVariableName!(T,i) in obj;
if(!pJson) continue;
static if( is(typeof(v)==ulong) ) {
dst.tupleof[i] = roundTo!(long)(pJson.number);
// Hack around bug 3418
} else {
pJson.fromJson( dst.tupleof[i] );
}
}
}
More information about the Digitalmars-d
mailing list