diff --git a/JSONArray.cs b/JSONArray.cs
index 6298b80..9358b14 100644
--- a/JSONArray.cs
+++ b/JSONArray.cs
@@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Collections.Generic;
+using ln.json.mapping;
namespace ln.json
{
@@ -11,6 +12,11 @@ namespace ln.json
public int Count => values.Count;
+ public override object ToNative()
+ {
+ return JSONMapper.DefaultMapper.FromJson(this, typeof(object[]));
+ }
+
JSONValue[] Values
{
get => values.ToArray();
diff --git a/JSONNumber.cs b/JSONNumber.cs
index cddfabf..81f139e 100644
--- a/JSONNumber.cs
+++ b/JSONNumber.cs
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
+using ln.types;
namespace ln.json
{
public class JSONNumber : JSONValue
@@ -7,6 +8,10 @@ namespace ln.json
public Decimal Decimal => decValue;
readonly decimal decValue;
+ public double AsDouble => (double)decValue;
+ public long AsLong => (long)decValue;
+ public int AsInt => (int)decValue;
+
public override object ToNative()
{
if (Decimal.Ceiling(decValue).Equals(Decimal.Floor(decValue)))
@@ -35,7 +40,7 @@ namespace ln.json
public JSONNumber(double doubleValue)
: base(JSONValueType.NUMBER)
{
- decValue = new Decimal(doubleValue);
+ decValue = doubleValue.ToDecimal();
}
public JSONNumber(decimal decValue)
: base(JSONValueType.NUMBER)
diff --git a/attributes/JSONMappingAttribute.cs b/attributes/JSONMappingAttribute.cs
new file mode 100644
index 0000000..c121d32
--- /dev/null
+++ b/attributes/JSONMappingAttribute.cs
@@ -0,0 +1,12 @@
+using System;
+namespace ln.json.attributes
+{
+ public class JSONMappingAttribute : Attribute
+ {
+ public bool Private { get; set; } = false;
+
+ public JSONMappingAttribute()
+ {
+ }
+ }
+}
diff --git a/ln.json.csproj b/ln.json.csproj
index a837576..07b689f 100644
--- a/ln.json.csproj
+++ b/ln.json.csproj
@@ -51,9 +51,12 @@
+
+
+
diff --git a/mapping/JSONDateTimeMapping.cs b/mapping/JSONDateTimeMapping.cs
index 81531e2..a23806a 100644
--- a/mapping/JSONDateTimeMapping.cs
+++ b/mapping/JSONDateTimeMapping.cs
@@ -9,6 +9,7 @@
// **/
using System;
using ln.json;
+using ln.types;
namespace ln.json.mapping
{
@@ -19,7 +20,7 @@ namespace ln.json.mapping
{
}
- public override object FromJson(JSONMapper mapper, JSONValue json) => DateTimeOffset.FromUnixTimeSeconds((long)((JSONNumber)json).Decimal).DateTime;
- public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(new DateTimeOffset((DateTime)value).ToUnixTimeSeconds());
+ public override object FromJson(JSONMapper mapper, JSONValue json) => DateTimeExtensions.FromUnixTimeSeconds(((JSONNumber)json).AsDouble);
+ public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(((DateTime)value).ToUnixTimeSeconds());
}
}
diff --git a/mapping/JSONEnumMapping.cs b/mapping/JSONEnumMapping.cs
new file mode 100644
index 0000000..762d826
--- /dev/null
+++ b/mapping/JSONEnumMapping.cs
@@ -0,0 +1,21 @@
+using System;
+namespace ln.json.mapping
+{
+ public class JSONEnumMapping : JSONMapping
+ {
+ public JSONEnumMapping(Type targetType)
+ :base(targetType)
+ {
+ }
+
+ public override JSONValue ToJson(JSONMapper mapper, object value)
+ {
+ return new JSONString(value.ToString());
+ }
+ public override object FromJson(JSONMapper mapper, JSONValue json)
+ {
+ return Enum.Parse(TargetType, json.ToNative().ToString());
+ }
+
+ }
+}
diff --git a/mapping/JSONMapper.cs b/mapping/JSONMapper.cs
index c463481..8c01a4d 100644
--- a/mapping/JSONMapper.cs
+++ b/mapping/JSONMapper.cs
@@ -122,11 +122,14 @@ namespace ln.json.mapping
if (this != DefaultMapper)
return DefaultMapper.FindMapping(targetType);
- if (!targetType.IsPrimitive && !targetType.IsValueType)
+ if (!targetType.IsPrimitive)
{
if (targetType.IsArray)
{
Add(new JSONArrayMapping(targetType));
+ } else if (targetType.IsEnum)
+ {
+ Add(new JSONEnumMapping(targetType));
} else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(IEnumerable<>)))
{
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
diff --git a/mapping/JSONObjectMapping.cs b/mapping/JSONObjectMapping.cs
index f02ac46..19e0409 100644
--- a/mapping/JSONObjectMapping.cs
+++ b/mapping/JSONObjectMapping.cs
@@ -10,6 +10,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
+using ln.json.attributes;
namespace ln.json.mapping
{
public interface IJSONApply
@@ -24,30 +25,45 @@ namespace ln.json.mapping
Dictionary types = new Dictionary();
public JSONObjectMapping(Type type)
- :base(type)
+ : base(type)
{
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
{
- setters.Add(fieldInfo.Name, (object arg1, object arg2) => fieldInfo.SetValue(arg1, arg2));
- getters.Add(fieldInfo.Name, (object arg) => fieldInfo.GetValue(arg));
- types.Add(fieldInfo.Name, fieldInfo.FieldType);
+ JSONMappingAttribute mappingAttribute = fieldInfo.GetCustomAttribute();
+ if ((mappingAttribute == null) || !mappingAttribute.Private)
+ {
+ setters.Add(fieldInfo.Name, (object arg1, object arg2) => fieldInfo.SetValue(arg1, arg2));
+ getters.Add(fieldInfo.Name, (object arg) => fieldInfo.GetValue(arg));
+ types.Add(fieldInfo.Name, fieldInfo.FieldType);
+ }
}
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
- setters.Add(propertyInfo.Name, (object arg1, object arg2) => propertyInfo.SetValue(arg1, arg2));
- getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
- types.Add(propertyInfo.Name, propertyInfo.PropertyType);
+ JSONMappingAttribute mappingAttribute = propertyInfo.GetCustomAttribute();
+ if ((mappingAttribute == null) || !mappingAttribute.Private)
+ {
+ setters.Add(propertyInfo.Name, (object arg1, object arg2) => propertyInfo.SetValue(arg1, arg2));
+ getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
+ types.Add(propertyInfo.Name, propertyInfo.PropertyType);
+ }
}
}
public override object FromJson(JSONMapper mapper, JSONValue json)
{
- JSONObject jObject = (JSONObject)json;
- object o = Activator.CreateInstance(TargetType);
+ if (json is JSONObject)
+ {
+ JSONObject jObject = (JSONObject)json;
+ object o = Activator.CreateInstance(TargetType);
- Apply(mapper, jObject, o);
+ Apply(mapper, jObject, o);
- return o;
+ return o;
+ }
+ else
+ {
+ return json.ToNative();
+ }
}
public override JSONValue ToJson(JSONMapper mapper, object value)