diff --git a/src/LitJson/IJsonWrapper.cs b/src/LitJson/IJsonWrapper.cs index 9b7e2d1..8455e1b 100644 --- a/src/LitJson/IJsonWrapper.cs +++ b/src/LitJson/IJsonWrapper.cs @@ -27,7 +27,8 @@ public enum JsonType Int, Long, Double, - Boolean + Boolean, + Float } public interface IJsonWrapper : IList, IOrderedDictionary @@ -39,6 +40,8 @@ public interface IJsonWrapper : IList, IOrderedDictionary bool IsLong { get; } bool IsObject { get; } bool IsString { get; } + bool IsFloat { get; } + bool GetBoolean (); double GetDouble (); @@ -46,6 +49,7 @@ public interface IJsonWrapper : IList, IOrderedDictionary JsonType GetJsonType (); long GetLong (); string GetString (); + float GetFloat(); void SetBoolean (bool val); void SetDouble (double val); @@ -53,6 +57,7 @@ public interface IJsonWrapper : IList, IOrderedDictionary void SetJsonType (JsonType type); void SetLong (long val); void SetString (string val); + void SetFloat (float val); string ToJson (); void ToJson (JsonWriter writer); diff --git a/src/LitJson/JsonData.cs b/src/LitJson/JsonData.cs index 975308d..2f7c133 100644 --- a/src/LitJson/JsonData.cs +++ b/src/LitJson/JsonData.cs @@ -31,6 +31,7 @@ public class JsonData : IJsonWrapper, IEquatable private string inst_string; private string json; private JsonType type; + private float inst_float; // Used to implement the IOrderedDictionary interface private IList> object_list; @@ -69,6 +70,10 @@ public bool IsObject { public bool IsString { get { return type == JsonType.String; } } + public bool IsFloat + { + get { return type == JsonType.Float; } + } public ICollection Keys { get { EnsureDictionary (); return inst_object.Keys; } @@ -169,6 +174,10 @@ bool IJsonWrapper.IsObject { bool IJsonWrapper.IsString { get { return IsString; } } + bool IJsonWrapper.IsFloat + { + get { return IsFloat; } + } #endregion @@ -333,6 +342,11 @@ public JsonData (long number) type = JsonType.Long; inst_long = number; } + public JsonData(float number) + { + type = JsonType.Float; + inst_float = number; + } public JsonData (object obj) { @@ -348,6 +362,13 @@ public JsonData (object obj) return; } + if (obj is float) + { + type = JsonType.Float; + inst_float = (float)obj; + return; + } + if (obj is Int32) { type = JsonType.Int; inst_int = (int) obj; @@ -403,6 +424,10 @@ public static implicit operator JsonData (String data) { return new JsonData (data); } + public static implicit operator JsonData(float data) + { + return new JsonData (data); + } #endregion @@ -451,6 +476,13 @@ public static explicit operator String (JsonData data) return data.inst_string; } + public static explicit operator float(JsonData data) + { + if(data.type!=JsonType.Float) + throw new InvalidCastException( + "Instance of JsonData doesn't hold a float"); + return data.inst_float; + } #endregion @@ -563,6 +595,14 @@ string IJsonWrapper.GetString () return inst_string; } + float IJsonWrapper.GetFloat() + { + if(type!=JsonType.Float) + throw new InvalidOperationException( + "JsonData instance doesn't hold a float"); + return inst_float; + } + void IJsonWrapper.SetBoolean (bool val) { type = JsonType.Boolean; @@ -598,6 +638,13 @@ void IJsonWrapper.SetString (string val) json = null; } + void IJsonWrapper.SetFloat(float val) + { + type = JsonType.Float; + inst_float = val; + json = null; + } + string IJsonWrapper.ToJson () { return ToJson (); @@ -761,6 +808,12 @@ private static void WriteJson (IJsonWrapper obj, JsonWriter writer) return; } + if (obj.IsFloat) + { + writer.Write(obj.GetFloat()); + return; + } + if (obj.IsInt) { writer.Write (obj.GetInt ()); return; @@ -849,6 +902,8 @@ public bool Equals (JsonData x) case JsonType.Boolean: return this.inst_boolean.Equals (x.inst_boolean); + case JsonType.Float: + return this.inst_float.Equals(x.inst_float); } return false; @@ -896,6 +951,9 @@ public void SetJsonType (JsonType type) case JsonType.Boolean: inst_boolean = default (Boolean); break; + case JsonType.Float: + inst_float = default(float); + break; } this.type = type; @@ -950,6 +1008,8 @@ public override string ToString () case JsonType.String: return inst_string; + case JsonType.Float: + return inst_float.ToString(); } return "Uninitialized JsonData"; diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index 38d68a7..35b247c 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -327,10 +327,22 @@ private static object ReadValue (Type inst_type, JsonReader reader) reader.Token == JsonToken.Int || reader.Token == JsonToken.Long || reader.Token == JsonToken.String || - reader.Token == JsonToken.Boolean) { + reader.Token == JsonToken.Boolean|| + reader.Token==JsonToken.Float) { Type json_type = reader.Value.GetType (); + //int32 cast to int64 add by Melon + if (json_type.Name.Equals("Int32") && inst_type.Name.Equals("Int64")) + { + return reader.Value; + } + //int cast to float or double add by Melon + //if ((json_type.Name.Equals("Double") || json_type.Name.Equals("Float")) && inst_type.Name.Equals("Single")) + //{ + // return reader.Value; + //} + if (value_type.IsAssignableFrom (json_type)) return reader.Value; @@ -438,11 +450,16 @@ private static object ReadValue (Type inst_type, JsonReader reader) PropertyInfo p_info = (PropertyInfo) prop_data.Info; - if (p_info.CanWrite) - p_info.SetValue ( - instance, - ReadValue (prop_data.Type, reader), - null); + if (p_info.CanWrite){ + //p_info.SetValue( + // instance, + // ReadValue(prop_data.Type, reader), + // null); + //use the getSetMethod.invoke instead of SetValue is to suport more platform + // such as mono run in ios,jit is forbidden,so the p_info.setValue cannot be used + p_info.GetSetMethod().Invoke(instance, new object[] { ReadValue(prop_data.Type, reader) }); + } + else ReadValue (prop_data.Type, reader); } @@ -493,6 +510,11 @@ private static IJsonWrapper ReadValue (WrapperFactory factory, instance.SetDouble ((double) reader.Value); return instance; } + if (reader.Token == JsonToken.Float) + { + instance.SetFloat((float)reader.Value); + return instance; + } if (reader.Token == JsonToken.Int) { instance.SetInt ((int) reader.Value); @@ -593,8 +615,18 @@ private static void RegisterBaseExporters () delegate (object obj, JsonWriter writer) { writer.Write ((ulong) obj); }; + base_exporters_table[typeof(float)] = + delegate(object obj, JsonWriter writer) + { + writer.Write((float)obj); + }; + base_exporters_table[typeof(Int64)] = + delegate(object obj, JsonWriter writer) + { + writer.Write((Int64)obj); + }; } - + private static void RegisterBaseImporters () { ImporterFunc importer; @@ -653,6 +685,12 @@ private static void RegisterBaseImporters () RegisterImporter (base_importers_table, typeof (double), typeof (decimal), importer); + importer = delegate(object input) + { + return Convert.ToSingle((float)input); + }; + RegisterImporter(base_importers_table,typeof(float), + typeof(decimal), importer); importer = delegate (object input) { return Convert.ToUInt32 ((long) input); @@ -717,6 +755,12 @@ private static void WriteValue (object obj, JsonWriter writer, return; } + if (obj is float) + { + writer.Write((float)obj); + return; + } + if (obj is Int32) { writer.Write ((int) obj); return; diff --git a/src/LitJson/JsonMockWrapper.cs b/src/LitJson/JsonMockWrapper.cs index dfe7adb..2947957 100644 --- a/src/LitJson/JsonMockWrapper.cs +++ b/src/LitJson/JsonMockWrapper.cs @@ -26,6 +26,7 @@ public class JsonMockWrapper : IJsonWrapper public bool IsLong { get { return false; } } public bool IsObject { get { return false; } } public bool IsString { get { return false; } } + public bool IsFloat { get { return false; } } public bool GetBoolean () { return false; } public double GetDouble () { return 0.0; } @@ -33,6 +34,7 @@ public class JsonMockWrapper : IJsonWrapper public JsonType GetJsonType () { return JsonType.None; } public long GetLong () { return 0L; } public string GetString () { return ""; } + public float GetFloat() { return 0.0f; } public void SetBoolean (bool val) {} public void SetDouble (double val) {} @@ -40,6 +42,7 @@ public void SetInt (int val) {} public void SetJsonType (JsonType type) {} public void SetLong (long val) {} public void SetString (string val) {} + public void SetFloat (float val) {} public string ToJson () { return ""; } public void ToJson (JsonWriter writer) {} diff --git a/src/LitJson/JsonReader.cs b/src/LitJson/JsonReader.cs index d0cd061..0cd4934 100644 --- a/src/LitJson/JsonReader.cs +++ b/src/LitJson/JsonReader.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.IO; using System.Text; +using System.Globalization; namespace LitJson @@ -35,7 +36,8 @@ public enum JsonToken String, Boolean, - Null + Null, + Float } @@ -266,10 +268,18 @@ private void ProcessNumber (string number) number.IndexOf ('E') != -1) { double n_double; + float n_float; if (Double.TryParse (number, out n_double)) { - token = JsonToken.Double; - token_value = n_double; - + if (float.TryParse(number, NumberStyles.Float, null, out n_float)) + { + token = JsonToken.Float; + token_value = n_float; + } + else + { + token = JsonToken.Double; + token_value = n_double; + } return; } } diff --git a/src/LitJson/JsonWriter.cs b/src/LitJson/JsonWriter.cs index 1b266d8..e78f519 100644 --- a/src/LitJson/JsonWriter.cs +++ b/src/LitJson/JsonWriter.cs @@ -332,6 +332,21 @@ public void Write (double number) context.ExpectingValue = false; } + public void Write(float number) + { + DoValidation(Condition.Value); + PutNewline(); + + string str = Convert.ToString(number, number_format); + Put(str); + + if (str.IndexOf('.') == -1 && + str.IndexOf('E') == -1) + writer.Write(".0"); + + context.ExpectingValue = false; + } + public void Write (int number) { DoValidation (Condition.Value);