Skip to content

Commit

Permalink
Merge branch 'release/0.13.0'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
devlead committed May 9, 2018
2 parents 9a8a026 + aee3e16 commit af47eaf
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.cs eol=lf
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ build_script:
test: off

init:
- git config --global core.autocrlf true
- git config --global core.autocrlf input
60 changes: 49 additions & 11 deletions src/LitJson/JsonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public class JsonData : IJsonWrapper, IEquatable<JsonData>
public ICollection<string> Keys {
get { EnsureDictionary (); return inst_object.Keys; }
}

/// <summary>
/// Determines whether the json contains an element that has the specified key.
/// </summary>
/// <param name="key">The key to locate in the json.</param>
/// <returns>true if the json contains an element that has the specified key; otherwise, false.</returns>
public Boolean ContainsKey(String key) {
EnsureDictionary();
return this.inst_object.Keys.Contains(key);
}
#endregion


Expand Down Expand Up @@ -425,22 +435,27 @@ public JsonData (string str)
return data.inst_double;
}

public static explicit operator Int32 (JsonData data)
public static explicit operator Int32(JsonData data)
{
if (data.type != JsonType.Int)
throw new InvalidCastException (
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)
public static explicit operator Int64(JsonData data)
{
if (data.type != JsonType.Long && data.type != JsonType.Int)
throw new InvalidCastException (
"Instance of JsonData doesn't hold an int");
{
throw new InvalidCastException(
"Instance of JsonData doesn't hold a long");
}

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

public static explicit operator String (JsonData data)
Expand Down Expand Up @@ -823,7 +838,14 @@ 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 @@ -839,10 +861,26 @@ public bool Equals (JsonData x)
return this.inst_string.Equals (x.inst_string);

case JsonType.Int:
return this.inst_int.Equals (x.inst_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:
return this.inst_long.Equals (x.inst_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
12 changes: 12 additions & 0 deletions src/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ private static void RegisterBaseImporters ()
RegisterImporter (base_importers_table, typeof (int),
typeof (ulong), importer);

importer = delegate (object input) {
return Convert.ToInt64((int)input);
};
RegisterImporter(base_importers_table, typeof(int),
typeof(long), importer);

importer = delegate (object input) {
return Convert.ToSByte ((int) input);
};
Expand Down Expand Up @@ -681,6 +687,12 @@ private static void RegisterBaseImporters ()
};
RegisterImporter (base_importers_table, typeof (string),
typeof (DateTime), importer);

importer = delegate (object input) {
return DateTimeOffset.Parse((string)input, datetime_format);
};
RegisterImporter(base_importers_table, typeof(string),
typeof(DateTimeOffset), importer);
}

private static void RegisterImporter (
Expand Down
6 changes: 3 additions & 3 deletions src/LitJson/LitJSON.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</PropertyGroup>

<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
<PackageReference Include="SourceLink.Create.GitHub" Version="2.1.0" PrivateAssets="All" />
<DotNetCliToolReference Include="dotnet-sourcelink-git" Version="2.1.0" />
<DotNetCliToolReference Include="dotnet-sourcelink" Version="2.1.0" />
<PackageReference Include="SourceLink.Create.GitHub" Version="2.8.1" PrivateAssets="All" />
<DotNetCliToolReference Include="dotnet-sourcelink-git" Version="2.8.1" />
<DotNetCliToolReference Include="dotnet-sourcelink" Version="2.8.1" />
</ItemGroup>

<PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion test/JsonDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ public void EqualsTest ()
b = 10L;
Assert.IsTrue (a.Equals (b), "A4");

// Int now comparable to long
b = 10;
Assert.IsFalse (a.Equals (b), "A5");
Assert.IsTrue (a.Equals (b), "A5");
b = 11L;
Assert.IsFalse (a.Equals (b), "A6");

Expand Down
30 changes: 18 additions & 12 deletions test/JsonMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,15 @@ public void ExportPrettyPrint ()
sample["flaming"] = "pie";
sample["nine"] = 9;

string expected = @"
{
""rolling"" : ""stones"",
""flaming"" : ""pie"",
""nine"" : 9
}";
string expected =string.Join(
Environment.NewLine,
new [] {
"",
"{",
" \"rolling\" : \"stones\",",
" \"flaming\" : \"pie\",",
" \"nine\" : 9",
"}"});

JsonWriter writer = new JsonWriter ();
writer.PrettyPrint = true;
Expand All @@ -368,12 +371,15 @@ public void ExportPrettyPrint ()
writer.Reset ();
writer.IndentValue = 8;

expected = @"
{
""rolling"" : ""stones"",
""flaming"" : ""pie"",
""nine"" : 9
}";
expected = string.Join(
Environment.NewLine,
new [] {
"",
"{",
" \"rolling\" : \"stones\",",
" \"flaming\" : \"pie\",",
" \"nine\" : 9",
"}"});
JsonMapper.ToJson (sample, writer);

Assert.AreEqual (expected, writer.ToString (), "A2");
Expand Down
35 changes: 19 additions & 16 deletions test/JsonWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,24 @@ public void PrettyPrintTest ()
{
JsonWriter writer = new JsonWriter ();

string json = @"
[
{
""precision"" : ""zip"",
""Latitude"" : 37.7668,
""Longitude"" : -122.3959,
""City"" : ""SAN FRANCISCO""
},
{
""precision"" : ""zip"",
""Latitude"" : 37.371991,
""Longitude"" : -122.02602,
""City"" : ""SUNNYVALE""
}
]";
string json = string.Join(
Environment.NewLine,
new [] {
"",
"[",
" {",
" \"precision\" : \"zip\",",
" \"Latitude\" : 37.7668,",
" \"Longitude\" : -122.3959,",
" \"City\" : \"SAN FRANCISCO\"",
" },",
" {",
" \"precision\" : \"zip\",",
" \"Latitude\" : 37.371991,",
" \"Longitude\" : -122.02602,",
" \"City\" : \"SUNNYVALE\"",
" }",
"]"});

writer.PrettyPrint = true;

Expand Down Expand Up @@ -276,7 +279,7 @@ public void PrettyPrintTest ()
writer.WriteObjectEnd ();
writer.WriteArrayEnd ();

Assert.AreEqual (writer.ToString (), json);
Assert.AreEqual (json, writer.ToString ());
}

[Test]
Expand Down

0 comments on commit af47eaf

Please sign in to comment.