Hi:
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'...
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.
<pre><code class="d">
struct Site
{
string name;
string page;
string ref;
}
</code></pre>
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?
Best regards,
Puming