Skip to content
Open
Show file tree
Hide file tree
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
52 changes: 40 additions & 12 deletions src/LitJson/JsonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,24 @@ public static explicit operator Double (JsonData data)

public static explicit operator Int32 (JsonData data)
{
if (data.type != JsonType.Int)
if (data.type != JsonType.Int && data.type != JsonType.Long)
{
throw new InvalidCastException (
"Instance of JsonData doesn't hold an int");
}

return data.inst_int;
// cast may truncate data... but that's up to the user to consider
return data.type == JsonType.Int ? data.inst_int : (int) data.inst_long;
}

public static explicit operator Int64 (JsonData data)
{
if (data.type != JsonType.Long)
if (data.type != JsonType.Long && data.type != JsonType.Int) {
throw new InvalidCastException (
"Instance of JsonData doesn't hold an int");
"Instance of JsonData doesn't hold a long");
}

return data.inst_long;
return data.type == JsonType.Long ? data.inst_long : data.inst_int;
}

public static explicit operator String (JsonData data)
Expand Down Expand Up @@ -538,20 +542,26 @@ double IJsonWrapper.GetDouble ()

int IJsonWrapper.GetInt ()
{
if (type != JsonType.Int)
if (type != JsonType.Int
&& type != JsonType.Long)
{
throw new InvalidOperationException (
"JsonData instance doesn't hold an int");
}

return inst_int;
return type == JsonType.Int ? inst_int : (int) inst_long;
}

long IJsonWrapper.GetLong ()
{
if (type != JsonType.Long)
if (type != JsonType.Long
&& type != JsonType.Int)
{
throw new InvalidOperationException (
"JsonData instance doesn't hold a long");
}

return inst_long;
return type == JsonType.Long ? inst_long : inst_int;
}

string IJsonWrapper.GetString ()
Expand Down Expand Up @@ -823,7 +833,13 @@ public bool Equals (JsonData x)
return false;

if (x.type != this.type)
return false;
{
// further check to see if this is a long to int comparison
if ((x.type != JsonType.Int && x.type != JsonType.Long)
|| (this.type != JsonType.Int && this.type != JsonType.Long)) {
return false;
}
}

switch (this.type) {
case JsonType.None:
Expand All @@ -838,11 +854,23 @@ public bool Equals (JsonData x)
case JsonType.String:
return this.inst_string.Equals (x.inst_string);

case JsonType.Int:
case JsonType.Int: {
if (x.IsLong) {
if (x.inst_long < Int32.MinValue || x.inst_long > Int32.MaxValue)
return false;
return this.inst_int.Equals((int) x.inst_long);
}
return this.inst_int.Equals (x.inst_int);
}

case JsonType.Long:
case JsonType.Long: {
if (x.IsInt) {
if (this.inst_long < Int32.MinValue || this.inst_long > Int32.MaxValue)
return false;
return x.inst_int.Equals((int) this.inst_long);
}
return this.inst_long.Equals (x.inst_long);
}

case JsonType.Double:
return this.inst_double.Equals (x.inst_double);
Expand Down
4 changes: 3 additions & 1 deletion test/JsonDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public void EqualsTest ()
Assert.IsTrue (a.Equals (b), "A4");

b = 10;
Assert.IsFalse (a.Equals (b), "A5");
//Assert.IsFalse (a.Equals (b), "A5");
// actually we expect ints and longs to compare true if they are within range
Assert.IsTrue (a.Equals (b), "A5");
b = 11L;
Assert.IsFalse (a.Equals (b), "A6");

Expand Down