rejectedsoftware/vibe.d — pull request #306 by japplegame (2013-09-07 13:22:08+00:00)
Title: fix serialize to Json/Bson for const struct classes

Function serializeToJson/Bson ignores custom toJson/Bson members for constant structs/classes.
```d
struct A {
int value;
static A fromJson(Json val) { return A(val.get!int); }
Json toJson() const { return Json(value); }
}
shared static this() {
assert(serializeToJson(A(123)) == Json(123)); // okay, works as expected
assert(serializeToJson(const A(123)) == Json(123)); // fails, serializes as object {"value": 123} instead simple integer.
}
```
Also I believe that functions toJson/Bson/String should returns Json, Bson and string values accordingly, but not values just implicitly convertible to those types.

Tests for this pull request:
```d
import vibe.d;

// struct with Json conversions
struct A {
int value;

static A fromJson(Json val) { return A(val.get!int); }
Json toJson() const { return Json(value); }
}

// struct with Bson conversions
struct B {
int value;

static B fromBson(Bson val) { return B(val.get!int); }
Bson toBson() const { return Bson(value); }
}

// struct with string conversions
struct C {
int value;

static C fromString(string val) { return C(val.to!int); }
string toString() const { return value.to!string; }
}

// struct without any conversions
struct D {
int value;
}

shared static this() {
assert(serializeToJson(const A(123)) == Json(123));
assert(serializeToJson(A(123)) == Json(123));
assert(serializeToBson(const A(123)) == Bson(123));
assert(serializeToBson(A(123)) == Bson(123));

assert(serializeToJson(const B(123)) == serializeToJson(["value": 123]));
assert(serializeToJson(B(123)) == serializeToJson(["value": 123]));
assert(serializeToBson(const B(123)) == Bson(123));
assert(serializeToBson(B(123)) == Bson(123));

assert(serializeToJson(const C(123)) == Json("123"));
assert(serializeToJson(C(123)) == Json("123"));
assert(serializeToBson(const C(123)) == Bson("123"));
assert(serializeToBson(C(123)) == Bson("123"));

assert(serializeToJson(const D(123)) == serializeToJson(["value": 123]));
assert(serializeToJson(D(123)) == serializeToJson(["value": 123]));
assert(serializeToBson(const D(123)) == serializeToBson(["value": 123]));
assert(serializeToBson(D(123)) == serializeToBson(["value": 123]));
}
```