On Sat, 13 Oct 2012 12:56:59 +0200, Sönke Ludwig wrote:
On Sat, 13 Oct 2012 12:24:27 +0200, Puming wrote:
Sorry the subject would be "How can I tell serializeToJson() to serialize a struct in a customized way?",
but it seems that the forum's validation code says 'The subject length must not be more than 1 character'...
Sorry, the error message should read "more than 64 characters". I fixed it and also increased the limit to 128.
I'm writing a little server that mainly deals with JSON data.
I came from Java land, so would like to model the domain objects in a more Java-ish way. I use structs to define them, and when writing to HttpResponse, use serializeToJson() function to convert it to Json.
But there is a problem with this solution, one of my Domain object actually contains a field name
ref, and that is a key word.
I can't change the the field name since our API protocol is already finished.
In Java, we can use @annotation to tell the serializer what field name to use, but D doesn't support @annotation yet.
So I came to a thought, which is very similar to
toString(), that if the struct contains a
toJson()method, then the serializer would recognize it and knows that the developer wants to serialize it in his own style, and just calls it. In this way we can customize the serialization process.
Does Vibe.d currently support customizing the serialization process? If not, is my suggestion of any use, and is it easy to implement?
I like it.
serializeToJson()currently only supports toString/fromString for customization, but adding
toJsonis trivial. I would implement it in a way that a struct/class must also have a static
fromJson()in addition to
toJson(), so that it remains symmetric.
Thanks :-) this is great .
Another possibility is to do something like stripping the last '' in a name and then calling the field `ref
. This would avoid the need to write a fulltoJson()` method...
And this is even better :-)