Skip to content

Commit

Permalink
Fix serialization of unsigned integer types in JsonSerializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Feb 24, 2014
1 parent fc34e61 commit 020d28e
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions source/vibe/data/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,17 @@ struct Json {
/// ditto
this(bool v) { m_type = Type.bool_; m_bool = v; }
/// ditto
this(int v) { m_type = Type.int_; m_int = v; }
this(byte v) { this(cast(long)v); }
/// ditto
this(ubyte v) { this(cast(long)v); }
/// ditto
this(short v) { this(cast(long)v); }
/// ditto
this(ushort v) { this(cast(long)v); }
/// ditto
this(int v) { this(cast(long)v); }
/// ditto
this(uint v) { this(cast(long)v); }
/// ditto
this(long v) { m_type = Type.int_; m_int = v; }
/// ditto
Expand Down Expand Up @@ -347,14 +357,15 @@ struct Json {
@property inout(T) get(T)()
inout {
checkType!T();
static if( is(T == bool) ) return m_bool;
else static if( is(T == double) ) return m_float;
else static if( is(T == float) ) return cast(T)m_float;
else static if( is(T == long) ) return m_int;
else static if( is(T : long) ){ enforce(m_int <= T.max && m_int >= T.min); return cast(T)m_int; }
else static if( is(T == string) ) return m_string;
else static if( is(T == Json[]) ) return m_array;
else static if( is(T == Json[string]) ) return m_object;
static if (is(T == bool)) return m_bool;
else static if (is(T == double)) return m_float;
else static if (is(T == float)) return cast(T)m_float;
else static if (is(T == long)) return m_int;
else static if (is(T == ulong)) return cast(ulong)m_int;
else static if (is(T : long)){ enforce(m_int <= T.max && m_int >= T.min); return cast(T)m_int; }
else static if (is(T == string)) return m_string;
else static if (is(T == Json[])) return m_array;
else static if (is(T == Json[string])) return m_object;
else static assert("JSON can only be casted to (bool, long, double, string, Json[] or Json[string]. Not "~T.stringof~".");
}
/// ditto
Expand Down Expand Up @@ -1136,6 +1147,12 @@ unittest {
assert(t.l == u.l);
}

unittest
{
assert(uint.max == serializeToJson(uint.max).deserializeJson!uint);
assert(ulong.max == serializeToJson(ulong.max).deserializeJson!ulong);
}

unittest {
static struct A { int value; static A fromJson(Json val) { return A(val.get!int); } Json toJson() const { return Json(value); } }
static struct C { int value; static C fromString(string val) { return C(val.to!int); } string toString() const { return value.to!string; } }
Expand Down

0 comments on commit 020d28e

Please sign in to comment.