views:

232

answers:

4

I'm looking for a D template library to take an arbitrary variable and marshal it into a transportable bundle. The variable might be a basic value type (int, char[], real) or might be a struct or class and even might contain or be a reference type. A system that can do this without any per type help would be nice but I suspect that it's to much to ask so I'd be happy with something that uses light weight annotations.

If nothing like that exists suggestions on how to structure it would be nice. I can think of a few ways to do the sterilization but I'm not sure how to specify the annotations.

Background: After trying to use ASMX and WCF web services and not likening them I'm felling like I want to try my hand at the RPC problem.

edit: BTW I don't care to much what the format in the middle is (XML, JASON, YAML, binary) as long as it's portable.

+1  A: 

I recommend you write your own, as it's a useful exercise in templating and helps you adapt the serialization format to your specific requirements.

You might want to take a look at tools.serialize (http://dsource.org/projects/scrapple/browser/trunk/tools/tools/serialize.d) as a starting point.

[edit] SORRY! Didn't realize it was you! :D

FeepingCreature
It's funny when you get pointed at your own site in answerer to a question you ask. (OTOH, at least I didn't write that as that would be just annoying ;)
BCS
+3  A: 

Have a look at Google Protocol Buffers. Maybe you can use the C++ or C bindings directly, or write D bindings yourself.

JesperE
I guess I only put it in the tags but I'm looking for a meta-programming solution, e.i. template functions. OTOH it might be cool to add D support to that.
BCS
As the author of one of the C# ports (so I know what is/isn't involved), I was going to suggest exactly that (i.e. write a D version). +1
Marc Gravell
@Marc, do you have a link to your c# version?
BCS
+1  A: 

I am afraid that you have to implement your own. I am looking for such thing as well.

To FeepingCreature: the author is one of the contributors of scrapple project. ^^)

stanleyxu2005
Oops. Didn't see his name.
FeepingCreature
This is not a forum. You should post this as a comment, not an answer.
Zifre
My reputation was not enough to post a comment, now I can ^^)
stanleyxu2005
+1  A: 

Here's a basic one I wrote for D 1.x. It was written quite some time ago, so it might be possible to improve it, but it does work. The actual format is basically network byte-order binary, so it should be safe to store and transfer the bytes.

http://gist.github.com/100885

It doesn't support classes or arbitrary pointers. To do that properly, you'd want something which memorised what references it had already serialised. If you restrict yourself to value types, arrays and AAs, it'll do the job.

If you do want to extend it to support classes, my advice would be to require toStream and fromStream methods to be defined.

DK