Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for enums (or other string convertable types) as keys to m... #333

Merged
merged 3 commits into from
Oct 3, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions source/vibe/data/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,16 @@ Json serializeToJson(T)(T value)
return Json(ret);
} else static if( isAssociativeArray!TU ){
Json[string] ret;
foreach( string key, value; value )
ret[key] = serializeToJson(value);
alias KeyType!T TK;
foreach( key, value; value ) {
static if(is(TK == string)) {
ret[key] = serializeToJson(value);
} else static if(is(TK == enum)) {
ret[to!string(key)] = serializeToJson(value);
} else static if(isStringSerializable!(TK)) {
ret[key.toString()] = serializeToJson(value);
}
}
return Json(ret);
} else static if(isJsonSerializable!TU) {
return value.toJson();
Expand Down Expand Up @@ -963,9 +971,18 @@ T deserializeJson(T)(Json src)
return dst;
} else static if( isAssociativeArray!T ){
alias typeof(T.init.values[0]) TV;
Unqual!TV[string] dst;
foreach( string key, value; src )
dst[key] = deserializeJson!(Unqual!TV)(value);
alias KeyType!T TK;
Unqual!TV[TK] dst;
foreach( string key, value; src ) {
static if(is(TK == string)) {
dst[key] = deserializeJson!(Unqual!TV)(value);
} else static if(is(TK == enum)) {
dst[to!(TK)(key)] = deserializeJson!(Unqual!TV)(value);
} else static if(isStringSerializable!TK) {
auto dsk = TK.fromString(key);
dst[dsk] = deserializeJson!(Unqual!TV)(value);
}
}
return dst;
} else static if (isJsonSerializable!T) {
return T.fromJson(src);
Expand Down Expand Up @@ -1057,6 +1074,40 @@ unittest {
assert(c.b == d.b);
}

unittest {
static struct C { int value; static C fromString(string val) { return C(val.to!int); } string toString() const { return value.to!string; } }
enum Color { Red, Green, Blue }
{
static class T {
string[Color] enumIndexedMap;
string[C] stringableIndexedMap;
this() {
enumIndexedMap = [ Color.Red : "magenta", Color.Blue : "deep blue" ];
stringableIndexedMap = [ C(42) : "forty-two" ];
}
}

T original = new T;
original.enumIndexedMap[Color.Green] = "olive";
T other;
deserializeJson(other, serializeToJson(original));
assert(serializeToJson(other) == serializeToJson(original));
}
{
static struct S {
string[Color] enumIndexedMap;
string[C] stringableIndexedMap;
}

S *original = new S;
original.enumIndexedMap = [ Color.Red : "magenta", Color.Blue : "deep blue" ];
original.enumIndexedMap[Color.Green] = "olive";
original.stringableIndexedMap = [ C(42) : "forty-two" ];
S other;
deserializeJson(other, serializeToJson(original));
assert(serializeToJson(other) == serializeToJson(original));
}
}

/**
Writes the given JSON object as a JSON string into the destination range.
Expand Down