@@ -435,22 +435,27 @@ public static explicit operator Double (JsonData data)
435435 return data . inst_double ;
436436 }
437437
438- public static explicit operator Int32 ( JsonData data )
438+ public static explicit operator Int32 ( JsonData data )
439439 {
440- if ( data . type != JsonType . Int )
441- throw new InvalidCastException (
440+ if ( data . type != JsonType . Int && data . type != JsonType . Long )
441+ {
442+ throw new InvalidCastException (
442443 "Instance of JsonData doesn't hold an int" ) ;
444+ }
443445
444- return data . inst_int ;
446+ // cast may truncate data... but that's up to the user to consider
447+ return data . type == JsonType . Int ? data . inst_int : ( int ) data . inst_long ;
445448 }
446449
447- public static explicit operator Int64 ( JsonData data )
450+ public static explicit operator Int64 ( JsonData data )
448451 {
449452 if ( data . type != JsonType . Long && data . type != JsonType . Int )
450- throw new InvalidCastException (
451- "Instance of JsonData doesn't hold an int" ) ;
453+ {
454+ throw new InvalidCastException (
455+ "Instance of JsonData doesn't hold a long" ) ;
456+ }
452457
453- return ( data . type == JsonType . Long ) ? data . inst_long : data . inst_int ;
458+ return data . type == JsonType . Long ? data . inst_long : data . inst_int ;
454459 }
455460
456461 public static explicit operator String ( JsonData data )
@@ -833,7 +838,14 @@ public bool Equals (JsonData x)
833838 return false ;
834839
835840 if ( x . type != this . type )
836- return false ;
841+ {
842+ // further check to see if this is a long to int comparison
843+ if ( ( x . type != JsonType . Int && x . type != JsonType . Long )
844+ || ( this . type != JsonType . Int && this . type != JsonType . Long ) )
845+ {
846+ return false ;
847+ }
848+ }
837849
838850 switch ( this . type ) {
839851 case JsonType . None :
@@ -849,10 +861,26 @@ public bool Equals (JsonData x)
849861 return this . inst_string . Equals ( x . inst_string ) ;
850862
851863 case JsonType . Int :
852- return this . inst_int . Equals ( x . inst_int ) ;
864+ {
865+ if ( x . IsLong )
866+ {
867+ if ( x . inst_long < Int32 . MinValue || x . inst_long > Int32 . MaxValue )
868+ return false ;
869+ return this . inst_int . Equals ( ( int ) x . inst_long ) ;
870+ }
871+ return this . inst_int . Equals ( x . inst_int ) ;
872+ }
853873
854874 case JsonType . Long :
855- return this . inst_long . Equals ( x . inst_long ) ;
875+ {
876+ if ( x . IsInt )
877+ {
878+ if ( this . inst_long < Int32 . MinValue || this . inst_long > Int32 . MaxValue )
879+ return false ;
880+ return x . inst_int . Equals ( ( int ) this . inst_long ) ;
881+ }
882+ return this . inst_long . Equals ( x . inst_long ) ;
883+ }
856884
857885 case JsonType . Double :
858886 return this . inst_double . Equals ( x . inst_double ) ;
0 commit comments