From 04bcbde6483faa6a24fff771185904e97e6ea506 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Wed, 24 Jan 2018 13:20:34 +0100 Subject: [PATCH 1/4] Updated SourceLink to 2.7.6 --- src/LitJson/LitJSON.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LitJson/LitJSON.csproj b/src/LitJson/LitJSON.csproj index a2c967a..eae09ba 100644 --- a/src/LitJson/LitJSON.csproj +++ b/src/LitJson/LitJSON.csproj @@ -11,9 +11,9 @@ - - - + + + From fb2278aedc7f6a420788af27246007bd4d8757a6 Mon Sep 17 00:00:00 2001 From: Philip Schell Date: Tue, 17 Apr 2018 23:24:04 +0200 Subject: [PATCH 2/4] Create method for checking of key Create a method for checking if a key exist, clone the behavour from Dictionary, so working with a json object is less writing code for checking. --- src/LitJson/JsonData.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/LitJson/JsonData.cs b/src/LitJson/JsonData.cs index 0fdc2d7..7e3ecd9 100644 --- a/src/LitJson/JsonData.cs +++ b/src/LitJson/JsonData.cs @@ -73,6 +73,16 @@ public bool IsString { public ICollection Keys { get { EnsureDictionary (); return inst_object.Keys; } } + + /// + /// Determines whether the json contains an element that has the specified key. + /// + /// The key to locate in the json. + /// true if the json contains an element that has the specified key; otherwise, false. + public Boolean ContainsKey(String key) { + EnsureDictionary(); + return this.inst_object.Keys.Contains(key); + } #endregion From 726c05debc9bb8c4a3a57ff0aabd843d1b26aba0 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Wed, 9 May 2018 09:55:43 +0200 Subject: [PATCH 3/4] Fix long & DateTimeOffset issues, fix sourcelink crlf issues --- .gitattributes | 1 + appveyor.yml | 2 +- src/LitJson/JsonData.cs | 50 +++++++++++++++++++++++++++++--------- src/LitJson/JsonMapper.cs | 12 +++++++++ src/LitJson/LitJSON.csproj | 6 ++--- test/JsonDataTest.cs | 3 ++- 6 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..7cbeb4e --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.cs eol=lf \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml index 9a0220c..9333ae2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,4 +9,4 @@ build_script: test: off init: - - git config --global core.autocrlf true \ No newline at end of file + - git config --global core.autocrlf input \ No newline at end of file diff --git a/src/LitJson/JsonData.cs b/src/LitJson/JsonData.cs index 7e3ecd9..ca4da38 100644 --- a/src/LitJson/JsonData.cs +++ b/src/LitJson/JsonData.cs @@ -435,22 +435,27 @@ public static explicit operator Double (JsonData data) 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) @@ -833,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: @@ -849,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); diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index bf70994..0fc106a 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -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); }; @@ -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 ( diff --git a/src/LitJson/LitJSON.csproj b/src/LitJson/LitJSON.csproj index eae09ba..62d0992 100644 --- a/src/LitJson/LitJSON.csproj +++ b/src/LitJson/LitJSON.csproj @@ -11,9 +11,9 @@ - - - + + + diff --git a/test/JsonDataTest.cs b/test/JsonDataTest.cs index 28e7b9d..e9e3782 100644 --- a/test/JsonDataTest.cs +++ b/test/JsonDataTest.cs @@ -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"); From aee3e16648554cc011e2526ec3c0ca5de094a736 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Wed, 9 May 2018 10:24:35 +0200 Subject: [PATCH 4/4] Fix test line ending issues --- test/JsonMapperTest.cs | 30 ++++++++++++++++++------------ test/JsonWriterTest.cs | 35 +++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/test/JsonMapperTest.cs b/test/JsonMapperTest.cs index 4441398..f4426c5 100644 --- a/test/JsonMapperTest.cs +++ b/test/JsonMapperTest.cs @@ -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; @@ -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"); diff --git a/test/JsonWriterTest.cs b/test/JsonWriterTest.cs index 2e0c685..907b21d 100644 --- a/test/JsonWriterTest.cs +++ b/test/JsonWriterTest.cs @@ -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; @@ -276,7 +279,7 @@ public void PrettyPrintTest () writer.WriteObjectEnd (); writer.WriteArrayEnd (); - Assert.AreEqual (writer.ToString (), json); + Assert.AreEqual (json, writer.ToString ()); } [Test]