Permalink
Browse files

+ updated SimpleJson to v0.10

  • Loading branch information...
1 parent 81bf2e5 commit d6976e60e9a33bef45820811f99283b89d1bcd59 @prabirshrestha committed Dec 15, 2011
Showing with 142 additions and 30 deletions.
  1. +141 −29 src/Nancy.Facebook/SimpleJson.cs
  2. +1 −1 src/Nancy.Facebook/packages.config
@@ -15,8 +15,12 @@
//#define SIMPLE_JSON_DATACONTRACT
// NOTE: uncomment the following line to use Reflection.Emit (better performance) instead of method.invoke().
+// don't enable ReflectionEmit for WinRT, Silverlight and WP7.
//#define SIMPLE_JSON_REFLECTIONEMIT
+// NOTE: uncomment the following line if you are compiling under Window Metro style application/library.
+//#define SIMPLE_JSON_WINRT;
+
// original json parsing code from http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
using System;
@@ -41,9 +45,9 @@ namespace SimpleJson
{
#region JsonArray
- /// <summary>
- /// Represents the json array.
- /// </summary>
+ /// <summary>
+ /// Represents the json array.
+ /// </summary>
#if SIMPLE_JSON_OBJARRAYINTERNAL
internal
#else
@@ -76,9 +80,9 @@ public override string ToString()
#region JsonObject
- /// <summary>
- /// Represents the json object.
- /// </summary>
+ /// <summary>
+ /// Represents the json object.
+ /// </summary>
#if SIMPLE_JSON_OBJARRAYINTERNAL
internal
#else
@@ -317,7 +321,12 @@ public override bool TryConvert(ConvertBinder binder, out object result)
if ((targetType == typeof(IEnumerable)) ||
(targetType == typeof(IEnumerable<KeyValuePair<string, object>>)) ||
(targetType == typeof(IDictionary<string, object>)) ||
- (targetType == typeof(IDictionary)))
+#if SIMPLE_JSON_WINRT
+ (targetType == typeof(IDictionary<,>))
+#else
+ (targetType == typeof(IDictionary))
+#endif
+)
{
result = this;
return true;
@@ -514,8 +523,13 @@ public static object DeserializeObject(string json, Type type, IJsonSerializerSt
{
object jsonObject = DeserializeObject(json);
- return type == null || jsonObject != null && jsonObject.GetType().IsAssignableFrom(type)
- ? jsonObject
+ return type == null || jsonObject != null &&
+#if SIMPLE_JSON_WINRT
+ jsonObject.GetType().GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())
+#else
+ jsonObject.GetType().IsAssignableFrom(type)
+#endif
+ ? jsonObject
: (jsonSerializerStrategy ?? CurrentJsonSerializerStrategy).DeserializeObject(jsonObject, type);
}
@@ -576,6 +590,11 @@ public static string EscapeToJavascriptString(string jsonString)
sb.Append('\\');
++i;
}
+ else if (lookahead == '"')
+ {
+ sb.Append("\"");
+ ++i;
+ }
else if (lookahead == 't')
{
sb.Append('\t');
@@ -1207,10 +1226,23 @@ public PocoJsonSerializerStrategy()
protected virtual void BuildMap(Type type, SafeDictionary<string, CacheResolver.MemberMap> memberMaps)
{
- foreach (PropertyInfo info in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
+#if SIMPLE_JSON_WINRT
+ foreach (PropertyInfo info in type.GetTypeInfo().DeclaredProperties) {
+ var getMethod = info.GetMethod;
+ if(getMethod==null || !getMethod.IsPublic || getMethod.IsStatic) continue;
+#else
+ foreach (PropertyInfo info in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) {
+#endif
memberMaps.Add(info.Name, new CacheResolver.MemberMap(info));
- foreach (FieldInfo info in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
+ }
+#if SIMPLE_JSON_WINRT
+ foreach (FieldInfo info in type.GetTypeInfo().DeclaredFields) {
+ if(!info.IsPublic || info.IsStatic) continue;
+#else
+ foreach (FieldInfo info in type.GetFields(BindingFlags.Public | BindingFlags.Instance)) {
+#endif
memberMaps.Add(info.Name, new CacheResolver.MemberMap(info));
+ }
}
public virtual bool SerializeNonPrimitiveObject(object input, out object output)
@@ -1226,9 +1258,15 @@ public virtual object DeserializeObject(object value, Type type)
return null;
else if ((value is long && type == typeof(long)) || (value is double && type == typeof(double)))
return value;
- else if ((value is double && type != typeof(double)) || (value is long && type != typeof(long)))
- return typeof(IConvertible).IsAssignableFrom(type) ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) : value;
-
+ else if ((value is double && type != typeof(double)) || (value is long && type != typeof(long))) {
+ return
+ #if SIMPLE_JSON_WINRT
+ type == typeof(int) || type == typeof(long) || type == typeof(double) ||type == typeof(float) || type == typeof(bool) || type == typeof(decimal) ||type == typeof(byte) || type == typeof(short)
+ #else
+ typeof(IConvertible).IsAssignableFrom(type)
+ #endif
+ ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) : value;
+ }
object obj = null;
if (value is IDictionary<string, object>)
@@ -1238,11 +1276,21 @@ public virtual object DeserializeObject(object value, Type type)
if (ReflectionUtils.IsTypeDictionary(type))
{
// if dictionary then
+#if SIMPLE_JSON_WINRT
+ Type keyType = type.GetTypeInfo().GenericTypeArguments[0];
+ Type valueType = type.GetTypeInfo().GenericTypeArguments[1];
+#else
Type keyType = type.GetGenericArguments()[0];
Type valueType = type.GetGenericArguments()[1];
+#endif
+
Type genericType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType);
- IDictionary dict = (IDictionary)CacheResolver.GetNewInstance(genericType);
+#if SIMPLE_JSON_WINRT
+ dynamic dict = CacheResolver.GetNewInstance(genericType);
+#else
+ IDictionary dict = (IDictionary)CacheResolver.GetNewInstance(genericType);
+#endif
foreach (KeyValuePair<string, object> kvp in jsonObject)
{
dict.Add(kvp.Key, DeserializeObject(kvp.Value, valueType));
@@ -1255,17 +1303,24 @@ public virtual object DeserializeObject(object value, Type type)
obj = CacheResolver.GetNewInstance(type);
SafeDictionary<string, CacheResolver.MemberMap> maps = CacheResolver.LoadMaps(type);
- foreach (KeyValuePair<string, CacheResolver.MemberMap> keyValuePair in maps)
+ if (maps == null)
{
- CacheResolver.MemberMap v = keyValuePair.Value;
- if (v.Setter == null)
- continue;
-
- string jsonKey = keyValuePair.Key;
- if (jsonObject.ContainsKey(jsonKey))
+ obj = value;
+ }
+ else
+ {
+ foreach (KeyValuePair<string, CacheResolver.MemberMap> keyValuePair in maps)
{
- object jsonValue = DeserializeObject(jsonObject[jsonKey], v.Type);
- v.Setter(obj, jsonValue);
+ CacheResolver.MemberMap v = keyValuePair.Value;
+ if (v.Setter == null)
+ continue;
+
+ string jsonKey = keyValuePair.Key;
+ if (jsonObject.ContainsKey(jsonKey))
+ {
+ object jsonValue = DeserializeObject(jsonObject[jsonKey], v.Type);
+ v.Setter(obj, jsonValue);
+ }
}
}
}
@@ -1282,9 +1337,19 @@ public virtual object DeserializeObject(object value, Type type)
foreach (object o in jsonObject)
list[i++] = DeserializeObject(o, type.GetElementType());
}
- else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || typeof(IList).IsAssignableFrom(type))
+ else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) ||
+#if SIMPLE_JSON_WINRT
+ typeof(IList).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())
+#else
+ typeof(IList).IsAssignableFrom(type)
+#endif
+)
{
+#if SIMPLE_JSON_WINRT
+ Type innerType = type.GetTypeInfo().GenericTypeArguments[0];
+#else
Type innerType = type.GetGenericArguments()[0];
+#endif
Type genericType = typeof(List<>).MakeGenericType(innerType);
list = (IList)CacheResolver.GetNewInstance(genericType);
foreach (object o in jsonObject)
@@ -1370,14 +1435,21 @@ protected override void BuildMap(Type type, SafeDictionary<string, CacheResolver
}
string jsonKey;
-
+#if SIMPLE_JSON_WINRT
+ foreach (PropertyInfo info in type.GetTypeInfo().DeclaredProperties)
+#else
foreach (PropertyInfo info in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
+#endif
{
if (CanAdd(info, out jsonKey))
map.Add(jsonKey, new CacheResolver.MemberMap(info));
}
+#if SIMPLE_JSON_WINRT
+ foreach (FieldInfo info in type.GetTypeInfo().DeclaredFields)
+#else
foreach (FieldInfo info in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
+#endif
{
if (CanAdd(info, out jsonKey))
map.Add(jsonKey, new CacheResolver.MemberMap(info));
@@ -1419,18 +1491,29 @@ class ReflectionUtils
{
public static Attribute GetAttribute(MemberInfo info, Type type)
{
+#if SIMPLE_JSON_WINRT
+ if (info == null || type == null || !info.IsDefined(type))
+ return null;
+ return info.GetCustomAttribute(type);
+#else
if (info == null || type == null || !Attribute.IsDefined(info, type))
return null;
-
return Attribute.GetCustomAttribute(info, type);
+#endif
}
public static Attribute GetAttribute(Type objectType, Type attributeType)
{
+
+#if SIMPLE_JSON_WINRT
+ if (objectType == null || attributeType == null || !objectType.GetTypeInfo().IsDefined(attributeType))
+ return null;
+ return objectType.GetTypeInfo().GetCustomAttribute(attributeType);
+#else
if (objectType == null || attributeType == null || !Attribute.IsDefined(objectType, attributeType))
return null;
-
return Attribute.GetCustomAttribute(objectType, attributeType);
+#endif
}
public static bool IsTypeGenericeCollectionInterface(Type type)
@@ -1445,8 +1528,13 @@ public static bool IsTypeGenericeCollectionInterface(Type type)
public static bool IsTypeDictionary(Type type)
{
+#if SIMPLE_JSON_WINRT
+ if (typeof(IDictionary<,>).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
+ return true;
+#else
if (typeof(IDictionary).IsAssignableFrom(type))
return true;
+#endif
if (!type.IsGenericType)
return false;
@@ -1456,7 +1544,6 @@ public static bool IsTypeDictionary(Type type)
}
}
- /*********************/
#if SIMPLE_JSON_INTERNAL
internal
#else
@@ -1523,7 +1610,20 @@ public static object GetNewInstance(Type type)
ConstructorCache.Add(type, c);
return c();
#else
+#if SIMPLE_JSON_WINRT
+ IEnumerable<ConstructorInfo> constructorInfos = type.GetTypeInfo().DeclaredConstructors;
+ ConstructorInfo constructorInfo = null;
+ foreach (ConstructorInfo item in constructorInfos) // FirstOrDefault()
+ {
+ if (item.GetParameters().Length == 0) // Default ctor - make sure it doesn't contain any parameters
+ {
+ constructorInfo = item;
+ break;
+ }
+ }
+#else
ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
+#endif
c = delegate { return constructorInfo.Invoke(null); };
ConstructorCache.Add(type, c);
return c();
@@ -1597,7 +1697,11 @@ static SetHandler CreateSetHandler(FieldInfo fieldInfo)
static GetHandler CreateGetHandler(PropertyInfo propertyInfo)
{
+#if SIMPLE_JSON_WINRT
+ MethodInfo getMethodInfo = propertyInfo.GetMethod;
+#else
MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true);
+#endif
if (getMethodInfo == null)
return null;
#if SIMPLE_JSON_REFLECTIONEMIT
@@ -1613,13 +1717,21 @@ static GetHandler CreateGetHandler(PropertyInfo propertyInfo)
return (GetHandler)dynamicGet.CreateDelegate(typeof(GetHandler));
#else
+#if SIMPLE_JSON_WINRT
+ return delegate(object instance) { return getMethodInfo.Invoke(instance, new Type[] { }); };
+#else
return delegate(object instance) { return getMethodInfo.Invoke(instance, Type.EmptyTypes); };
#endif
+#endif
}
static SetHandler CreateSetHandler(PropertyInfo propertyInfo)
{
+#if SIMPLE_JSON_WINRT
+ MethodInfo setMethodInfo = propertyInfo.SetMethod;
+#else
MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true);
+#endif
if (setMethodInfo == null)
return null;
#if SIMPLE_JSON_REFLECTIONEMIT
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Nancy" version="0.9.0" />
- <package id="SimpleJson" version="0.8.0.0" />
+ <package id="SimpleJson" version="0.10.0.0" />
</packages>

0 comments on commit d6976e6

Please sign in to comment.