Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float support like double #42

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/LitJson/IJsonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public enum JsonType
Int,
Long,
Double,
Boolean
Boolean,
Float
}

public interface IJsonWrapper : IList, IOrderedDictionary
Expand All @@ -39,20 +40,24 @@ public interface IJsonWrapper : IList, IOrderedDictionary
bool IsLong { get; }
bool IsObject { get; }
bool IsString { get; }
bool IsFloat { get; }


bool GetBoolean ();
double GetDouble ();
int GetInt ();
JsonType GetJsonType ();
long GetLong ();
string GetString ();
float GetFloat();

void SetBoolean (bool val);
void SetDouble (double val);
void SetInt (int val);
void SetJsonType (JsonType type);
void SetLong (long val);
void SetString (string val);
void SetFloat (float val);

string ToJson ();
void ToJson (JsonWriter writer);
Expand Down
60 changes: 60 additions & 0 deletions src/LitJson/JsonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class JsonData : IJsonWrapper, IEquatable<JsonData>
private string inst_string;
private string json;
private JsonType type;
private float inst_float;

// Used to implement the IOrderedDictionary interface
private IList<KeyValuePair<string, JsonData>> object_list;
Expand Down Expand Up @@ -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<string> Keys {
get { EnsureDictionary (); return inst_object.Keys; }
Expand Down Expand Up @@ -169,6 +174,10 @@ bool IJsonWrapper.IsObject {
bool IJsonWrapper.IsString {
get { return IsString; }
}
bool IJsonWrapper.IsFloat
{
get { return IsFloat; }
}
#endregion


Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -950,6 +1008,8 @@ public override string ToString ()

case JsonType.String:
return inst_string;
case JsonType.Float:
return inst_float.ToString();
}

return "Uninitialized JsonData";
Expand Down
58 changes: 51 additions & 7 deletions src/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/LitJson/JsonMockWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ 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; }
public int GetInt () { return 0; }
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) {}
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) {}
Expand Down
18 changes: 14 additions & 4 deletions src/LitJson/JsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Globalization;


namespace LitJson
Expand All @@ -35,7 +36,8 @@ public enum JsonToken
String,

Boolean,
Null
Null,
Float
}


Expand Down Expand Up @@ -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;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/LitJson/JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down