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 0fdc2d7..ca4da38 100644 --- a/src/LitJson/JsonData.cs +++ b/src/LitJson/JsonData.cs @@ -73,6 +73,16 @@ public class JsonData : IJsonWrapper, IEquatable 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 @@ -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) @@ -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: @@ -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); 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 a2c967a..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"); 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]