Skip to content

Commit af47eaf

Browse files
committed
Merge branch 'release/0.13.0'
* release/0.13.0: Fix test line ending issues Fix long & DateTimeOffset issues, fix sourcelink crlf issues Create method for checking of key Updated SourceLink to 2.7.6
2 parents 9a8a026 + aee3e16 commit af47eaf

8 files changed

Lines changed: 105 additions & 44 deletions

File tree

.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: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ public bool IsString {
7373
public ICollection<string> Keys {
7474
get { EnsureDictionary (); return inst_object.Keys; }
7575
}
76+
77+
/// <summary>
78+
/// Determines whether the json contains an element that has the specified key.
79+
/// </summary>
80+
/// <param name="key">The key to locate in the json.</param>
81+
/// <returns>true if the json contains an element that has the specified key; otherwise, false.</returns>
82+
public Boolean ContainsKey(String key) {
83+
EnsureDictionary();
84+
return this.inst_object.Keys.Contains(key);
85+
}
7686
#endregion
7787

7888

@@ -425,22 +435,27 @@ public static explicit operator Double (JsonData data)
425435
return data.inst_double;
426436
}
427437

428-
public static explicit operator Int32 (JsonData data)
438+
public static explicit operator Int32(JsonData data)
429439
{
430-
if (data.type != JsonType.Int)
431-
throw new InvalidCastException (
440+
if (data.type != JsonType.Int && data.type != JsonType.Long)
441+
{
442+
throw new InvalidCastException(
432443
"Instance of JsonData doesn't hold an int");
444+
}
433445

434-
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;
435448
}
436449

437-
public static explicit operator Int64 (JsonData data)
450+
public static explicit operator Int64(JsonData data)
438451
{
439452
if (data.type != JsonType.Long && data.type != JsonType.Int)
440-
throw new InvalidCastException (
441-
"Instance of JsonData doesn't hold an int");
453+
{
454+
throw new InvalidCastException(
455+
"Instance of JsonData doesn't hold a long");
456+
}
442457

443-
return (data.type == JsonType.Long) ? data.inst_long : data.inst_int;
458+
return data.type == JsonType.Long ? data.inst_long : data.inst_int;
444459
}
445460

446461
public static explicit operator String (JsonData data)
@@ -823,7 +838,14 @@ public bool Equals (JsonData x)
823838
return false;
824839

825840
if (x.type != this.type)
826-
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+
}
827849

828850
switch (this.type) {
829851
case JsonType.None:
@@ -839,10 +861,26 @@ public bool Equals (JsonData x)
839861
return this.inst_string.Equals (x.inst_string);
840862

841863
case JsonType.Int:
842-
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+
}
843873

844874
case JsonType.Long:
845-
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+
}
846884

847885
case JsonType.Double:
848886
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.1.0" PrivateAssets="All" />
15-
<DotNetCliToolReference Include="dotnet-sourcelink-git" Version="2.1.0" />
16-
<DotNetCliToolReference Include="dotnet-sourcelink" Version="2.1.0" />
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

test/JsonMapperTest.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,15 @@ public void ExportPrettyPrint ()
351351
sample["flaming"] = "pie";
352352
sample["nine"] = 9;
353353

354-
string expected = @"
355-
{
356-
""rolling"" : ""stones"",
357-
""flaming"" : ""pie"",
358-
""nine"" : 9
359-
}";
354+
string expected =string.Join(
355+
Environment.NewLine,
356+
new [] {
357+
"",
358+
"{",
359+
" \"rolling\" : \"stones\",",
360+
" \"flaming\" : \"pie\",",
361+
" \"nine\" : 9",
362+
"}"});
360363

361364
JsonWriter writer = new JsonWriter ();
362365
writer.PrettyPrint = true;
@@ -368,12 +371,15 @@ public void ExportPrettyPrint ()
368371
writer.Reset ();
369372
writer.IndentValue = 8;
370373

371-
expected = @"
372-
{
373-
""rolling"" : ""stones"",
374-
""flaming"" : ""pie"",
375-
""nine"" : 9
376-
}";
374+
expected = string.Join(
375+
Environment.NewLine,
376+
new [] {
377+
"",
378+
"{",
379+
" \"rolling\" : \"stones\",",
380+
" \"flaming\" : \"pie\",",
381+
" \"nine\" : 9",
382+
"}"});
377383
JsonMapper.ToJson (sample, writer);
378384

379385
Assert.AreEqual (expected, writer.ToString (), "A2");

test/JsonWriterTest.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,24 @@ public void PrettyPrintTest ()
232232
{
233233
JsonWriter writer = new JsonWriter ();
234234

235-
string json = @"
236-
[
237-
{
238-
""precision"" : ""zip"",
239-
""Latitude"" : 37.7668,
240-
""Longitude"" : -122.3959,
241-
""City"" : ""SAN FRANCISCO""
242-
},
243-
{
244-
""precision"" : ""zip"",
245-
""Latitude"" : 37.371991,
246-
""Longitude"" : -122.02602,
247-
""City"" : ""SUNNYVALE""
248-
}
249-
]";
235+
string json = string.Join(
236+
Environment.NewLine,
237+
new [] {
238+
"",
239+
"[",
240+
" {",
241+
" \"precision\" : \"zip\",",
242+
" \"Latitude\" : 37.7668,",
243+
" \"Longitude\" : -122.3959,",
244+
" \"City\" : \"SAN FRANCISCO\"",
245+
" },",
246+
" {",
247+
" \"precision\" : \"zip\",",
248+
" \"Latitude\" : 37.371991,",
249+
" \"Longitude\" : -122.02602,",
250+
" \"City\" : \"SUNNYVALE\"",
251+
" }",
252+
"]"});
250253

251254
writer.PrettyPrint = true;
252255

@@ -276,7 +279,7 @@ public void PrettyPrintTest ()
276279
writer.WriteObjectEnd ();
277280
writer.WriteArrayEnd ();
278281

279-
Assert.AreEqual (writer.ToString (), json);
282+
Assert.AreEqual (json, writer.ToString ());
280283
}
281284

282285
[Test]

0 commit comments

Comments
 (0)