Skip to content

Commit 726c05d

Browse files
committed
Fix long & DateTimeOffset issues, fix sourcelink crlf issues
1 parent a149833 commit 726c05d

File tree

6 files changed

+58
-16
lines changed

6 files changed

+58
-16
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.cs eol=lf

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ build_script:
99
test: off
1010

1111
init:
12-
- git config --global core.autocrlf true
12+
- git config --global core.autocrlf input

src/LitJson/JsonData.cs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/LitJson/JsonMapper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ private static void RegisterBaseImporters ()
621621
RegisterImporter (base_importers_table, typeof (int),
622622
typeof (ulong), importer);
623623

624+
importer = delegate (object input) {
625+
return Convert.ToInt64((int)input);
626+
};
627+
RegisterImporter(base_importers_table, typeof(int),
628+
typeof(long), importer);
629+
624630
importer = delegate (object input) {
625631
return Convert.ToSByte ((int) input);
626632
};
@@ -681,6 +687,12 @@ private static void RegisterBaseImporters ()
681687
};
682688
RegisterImporter (base_importers_table, typeof (string),
683689
typeof (DateTime), importer);
690+
691+
importer = delegate (object input) {
692+
return DateTimeOffset.Parse((string)input, datetime_format);
693+
};
694+
RegisterImporter(base_importers_table, typeof(string),
695+
typeof(DateTimeOffset), importer);
684696
}
685697

686698
private static void RegisterImporter (

src/LitJson/LitJSON.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
14-
<PackageReference Include="SourceLink.Create.GitHub" Version="2.7.6" PrivateAssets="All" />
15-
<DotNetCliToolReference Include="dotnet-sourcelink-git" Version="2.7.6" />
16-
<DotNetCliToolReference Include="dotnet-sourcelink" Version="2.7.6" />
14+
<PackageReference Include="SourceLink.Create.GitHub" Version="2.8.1" PrivateAssets="All" />
15+
<DotNetCliToolReference Include="dotnet-sourcelink-git" Version="2.8.1" />
16+
<DotNetCliToolReference Include="dotnet-sourcelink" Version="2.8.1" />
1717
</ItemGroup>
1818

1919
<PropertyGroup>

test/JsonDataTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ public void EqualsTest ()
147147
b = 10L;
148148
Assert.IsTrue (a.Equals (b), "A4");
149149

150+
// Int now comparable to long
150151
b = 10;
151-
Assert.IsFalse (a.Equals (b), "A5");
152+
Assert.IsTrue (a.Equals (b), "A5");
152153
b = 11L;
153154
Assert.IsFalse (a.Equals (b), "A6");
154155

0 commit comments

Comments
 (0)