From b4ee1b2c022fe9332484d602c7f2627390e2b17b Mon Sep 17 00:00:00 2001 From: MingLin_Outside Date: Thu, 24 Nov 2016 16:54:09 +0800 Subject: [PATCH 1/8] Support Unity3D FIXED BUG: List<> deserialization errors when #define SIMPLE_JSON_NO_LINQ_EXPRESSION --- src/SimpleJson.sln | 18 +++++++++- src/SimpleJson/SimpleJson-Unity.csproj | 49 ++++++++++++++++++++++++++ src/SimpleJson/SimpleJson.cs | 5 +++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/SimpleJson/SimpleJson-Unity.csproj diff --git a/src/SimpleJson.sln b/src/SimpleJson.sln index f7eaf8f..27c883d 100644 --- a/src/SimpleJson.sln +++ b/src/SimpleJson.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 -VisualStudioVersion = 12.0.20827.3 +VisualStudioVersion = 12.0.31101.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{AEDAD4B4-9F2C-4992-8CD6-A43DFDFF7172}" EndProject @@ -42,6 +42,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimleJson.Tests-WindowsStor EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleJson-Portable-WP8WinStoreNet45", "SimpleJson\SimpleJson-Portable-WP8WinStoreNet45.csproj", "{EC52A094-1CDB-4F5D-8A8A-86F501FC601C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleJson-Unity", "SimpleJson\SimpleJson-Unity.csproj", "{B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -288,6 +290,20 @@ Global {EC52A094-1CDB-4F5D-8A8A-86F501FC601C}.Release|Mixed Platforms.Build.0 = Release|Any CPU {EC52A094-1CDB-4F5D-8A8A-86F501FC601C}.Release|x64.ActiveCfg = Release|Any CPU {EC52A094-1CDB-4F5D-8A8A-86F501FC601C}.Release|x86.ActiveCfg = Release|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Debug|ARM.ActiveCfg = Debug|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Debug|x64.ActiveCfg = Debug|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Debug|x86.ActiveCfg = Debug|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Release|Any CPU.Build.0 = Release|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Release|ARM.ActiveCfg = Release|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Release|x64.ActiveCfg = Release|Any CPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/SimpleJson/SimpleJson-Unity.csproj b/src/SimpleJson/SimpleJson-Unity.csproj new file mode 100644 index 0000000..323b575 --- /dev/null +++ b/src/SimpleJson/SimpleJson-Unity.csproj @@ -0,0 +1,49 @@ + + + + + Debug + AnyCPU + {B6DF8C2D-A8F8-4FDB-B0E5-DFEEAA76CFE6} + Library + Properties + SimpleJson + SimpleJson + v3.5 + 512 + + + + true + full + false + ..\..\bin\Unity\Debug\ + TRACE;DEBUG;SIMPLE_JSON_NO_LINQ_EXPRESSION;SIMPLE_JSON_DATACONTRACT; + prompt + 4 + + + pdbonly + true + ..\..\bin\Unity\Release\ + TRACE;SIMPLE_JSON_NO_LINQ_EXPRESSION;SIMPLE_JSON_DATACONTRACT; + prompt + 4 + + + + + + + + + + + + \ No newline at end of file diff --git a/src/SimpleJson/SimpleJson.cs b/src/SimpleJson/SimpleJson.cs index 2ab9742..c85a3cc 100644 --- a/src/SimpleJson/SimpleJson.cs +++ b/src/SimpleJson/SimpleJson.cs @@ -1450,7 +1450,12 @@ public virtual object DeserializeObject(object value, Type type) else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || ReflectionUtils.IsAssignableFrom(typeof(IList), type)) { Type innerType = ReflectionUtils.GetGenericListElementType(type); + //Modity By Zhang Minglin +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + list = (IList)(ConstructorCache[type] ?? ConstructorCache[typeof(List<>).MakeGenericType(innerType)])(); +#else list = (IList)(ConstructorCache[type] ?? ConstructorCache[typeof(List<>).MakeGenericType(innerType)])(jsonObject.Count); +#endif foreach (object o in jsonObject) list.Add(DeserializeObject(o, innerType)); } From f0a0a4eca553a206679865848a5b979bd1bf1879 Mon Sep 17 00:00:00 2001 From: MingLinZh Date: Thu, 24 Nov 2016 16:57:46 +0800 Subject: [PATCH 2/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f3c5ffc..73ebe6f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Install-Package SimpleJson * Windows Phone 7.1 (Mango) * Windows Phone 8 * Portable Class Libraries (PCL) +* Unity **Note:** By default SimpleJson expects `System.Linq`. If you are targeting older version of .NET framework (.net < 3.0 or WP7.0) you will need to add `#define SIMPLE_JSON_NO_LINQ_EXPRESSION`. From a0956210a2363b26e76f180c9c86779cb120f0a9 Mon Sep 17 00:00:00 2001 From: MingLin_Outside Date: Tue, 29 Nov 2016 10:20:59 +0800 Subject: [PATCH 3/8] PocoJsonSerializerStrategy.DeserializeObject() - Support Dictionary Formats([{"Key":?,"Value":?},]) --- src/SimpleJson/SimpleJson.cs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/SimpleJson/SimpleJson.cs b/src/SimpleJson/SimpleJson.cs index c85a3cc..b023818 100644 --- a/src/SimpleJson/SimpleJson.cs +++ b/src/SimpleJson/SimpleJson.cs @@ -1450,7 +1450,7 @@ public virtual object DeserializeObject(object value, Type type) else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || ReflectionUtils.IsAssignableFrom(typeof(IList), type)) { Type innerType = ReflectionUtils.GetGenericListElementType(type); - //Modity By Zhang Minglin + //Modify by xtqqksszml@163.com #if SIMPLE_JSON_NO_LINQ_EXPRESSION list = (IList)(ConstructorCache[type] ?? ConstructorCache[typeof(List<>).MakeGenericType(innerType)])(); #else @@ -1460,6 +1460,30 @@ public virtual object DeserializeObject(object value, Type type) list.Add(DeserializeObject(o, innerType)); } obj = list; + + //Modify by xtqqksszml@163.com + //Support Dictionary Formats([{"Key":?,"Value":?},]) + if (ReflectionUtils.IsTypeDictionary(type)) + { + Type[] types = ReflectionUtils.GetGenericTypeArguments(type); + Type keyType = types[0]; + Type valueType = types[1]; + + Type genericType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); + + IDictionary dict = (IDictionary)ConstructorCache[genericType](); + + foreach (object o in jsonObject) + { + IDictionary element = o as IDictionary; + if (element != null) + { + dict.Add(DeserializeObject(element["Key"], keyType) + , DeserializeObject(element["Value"], valueType)); + } + } + obj = dict; + } } } return obj; From 02a5c677a9cfe59497ff3eb4f0f22416c44e388b Mon Sep 17 00:00:00 2001 From: MingLin_Outside Date: Wed, 30 Nov 2016 11:13:59 +0800 Subject: [PATCH 4/8] Support uint, ulong --- src/SimpleJson/SimpleJson.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/SimpleJson/SimpleJson.cs b/src/SimpleJson/SimpleJson.cs index b023818..ba6013b 100644 --- a/src/SimpleJson/SimpleJson.cs +++ b/src/SimpleJson/SimpleJson.cs @@ -907,9 +907,18 @@ static object ParseNumber(char[] json, ref int index, ref bool success) } else { + //Modify by xtqqksszml@163.com + //Support uint, ulong long number; success = long.TryParse(new string(json, index, charLength), NumberStyles.Any, CultureInfo.InvariantCulture, out number); - returnNumber = number; + if (success) + returnNumber = number; + else + { + ulong unsign_number; + success = ulong.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out unsign_number); + returnNumber = unsign_number; + } } index = lastIndex + 1; return returnNumber; @@ -1379,14 +1388,17 @@ public virtual object DeserializeObject(object value, Type type) } else if (value is bool) return value; - + + //Modify by xtqqksszml@163.com + //Support uint, ulong bool valueIsLong = value is long; + bool valueIsULong = value is ulong; bool valueIsDouble = value is double; - if ((valueIsLong && type == typeof(long)) || (valueIsDouble && type == typeof(double))) + if ((valueIsLong && type == typeof(long)) || (valueIsDouble && type == typeof(double)) || (valueIsULong && type == typeof(ulong))) return value; - if ((valueIsDouble && type != typeof(double)) || (valueIsLong && type != typeof(long))) + if ((valueIsDouble && type != typeof(double)) || (valueIsLong && type != typeof(long)) || (valueIsULong && type != typeof(ulong))) { - obj = type == typeof(int) || type == typeof(long) || type == typeof(double) || type == typeof(float) || type == typeof(bool) || type == typeof(decimal) || type == typeof(byte) || type == typeof(short) + obj = type == typeof(int) || type == typeof(long) || type == typeof(uint) || type == typeof(ulong) || type == typeof(double) || type == typeof(float) || type == typeof(bool) || type == typeof(decimal) || type == typeof(byte) || type == typeof(short) ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) : value; } From b9cb6b49a824c061f9fe920d2c50e90357d9a4d2 Mon Sep 17 00:00:00 2001 From: Zhang Minglin Date: Sun, 16 Apr 2017 13:24:31 +0800 Subject: [PATCH 5/8] Format({ "":{...} "":{...} "":{...} ... }) deserialize support Dictionary key for except string type of it --- src/SimpleJson/SimpleJson.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/SimpleJson/SimpleJson.cs b/src/SimpleJson/SimpleJson.cs index ba6013b..1e30efe 100644 --- a/src/SimpleJson/SimpleJson.cs +++ b/src/SimpleJson/SimpleJson.cs @@ -1420,8 +1420,10 @@ public virtual object DeserializeObject(object value, Type type) IDictionary dict = (IDictionary)ConstructorCache[genericType](); + // Modify by xtqqksszml@163.com + // Key support the other types foreach (KeyValuePair kvp in jsonObject) - dict.Add(kvp.Key, DeserializeObject(kvp.Value, valueType)); + dict.Add(DeserializeObject(kvp.Key, keyType), DeserializeObject(kvp.Value, valueType)); obj = dict; } From abec2e5451cbc538729565db447696a6b49ba73b Mon Sep 17 00:00:00 2001 From: ZhangMinglin Date: Wed, 8 Nov 2017 16:35:33 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=8F=90=E4=BE=9B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=96=B0=E7=9A=84SerializeObject,=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BC=A0=E5=85=A5StringBuider=EF=BC=8C=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E5=A0=86=E5=86=85=E5=AD=98=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SimpleJson/SimpleJson.cs | 265 ++++++++++++++++++++++++++++++++++- 1 file changed, 260 insertions(+), 5 deletions(-) diff --git a/src/SimpleJson/SimpleJson.cs b/src/SimpleJson/SimpleJson.cs index 1e30efe..fd53688 100644 --- a/src/SimpleJson/SimpleJson.cs +++ b/src/SimpleJson/SimpleJson.cs @@ -579,7 +579,25 @@ public static object DeserializeObject(string json, Type type, IJsonSerializerSt ? jsonObject : (jsonSerializerStrategy ?? CurrentJsonSerializerStrategy).DeserializeObject(jsonObject, type); } - +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + public static object DeserializeObject(ref object target,string json, Type type, IJsonSerializerStrategy jsonSerializerStrategy) + { + object jsonObject = DeserializeObject(json); + return type == null || jsonObject != null && ReflectionUtils.IsAssignableFrom(jsonObject.GetType(), type) + ? jsonObject + : (jsonSerializerStrategy ?? CurrentJsonSerializerStrategy).DeserializeObject(ref target, jsonObject, + type); + } +#else + public static object DeserializeObject(object target, string json, Type type, IJsonSerializerStrategy jsonSerializerStrategy) + { + object jsonObject = DeserializeObject(json); + return type == null || jsonObject != null && ReflectionUtils.IsAssignableFrom(jsonObject.GetType(), type) + ? jsonObject + : (jsonSerializerStrategy ?? CurrentJsonSerializerStrategy).DeserializeObject(target, jsonObject, + type); + } +#endif public static object DeserializeObject(string json, Type type) { return DeserializeObject(json, type, null); @@ -594,7 +612,14 @@ public static T DeserializeObject(string json) { return (T)DeserializeObject(json, typeof(T), null); } - + public static T DeserializeObject(object target,string json) + { +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + return (T)DeserializeObject(ref target, json, typeof(T), null); +#else + return (T)DeserializeObject(target, json, typeof(T), null); +#endif + } /// /// Converts a IDictionary<string,object> / IList<object> object into a JSON string /// @@ -613,6 +638,20 @@ public static string SerializeObject(object json) return SerializeObject(json, CurrentJsonSerializerStrategy); } + /// + /// ±ÜÃâ¶ÑÄÚ´æ·ÖÅäµÄ½âÎö·½Ê½ + /// Modify By ZhangMinglin + /// + public static StringBuilder SerializeObject(object json, StringBuilder builder) + { + if (builder == null) + { + return null; + } + bool success = SerializeValue(CurrentJsonSerializerStrategy, json, builder); + return (success ? builder : null); + } + public static string EscapeToJavascriptString(string jsonString) { if (string.IsNullOrEmpty(jsonString)) @@ -1243,6 +1282,12 @@ interface IJsonSerializerStrategy [SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate", Justification="Need to support .NET 2")] bool TrySerializeNonPrimitiveObject(object input, out object output); object DeserializeObject(object value, Type type); +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + object DeserializeObject(ref object target, object value, Type type); +#else + object DeserializeObject(object target,object value, Type type); +#endif + } [GeneratedCode("simple-json", "1.0.0")] @@ -1433,14 +1478,21 @@ public virtual object DeserializeObject(object value, Type type) obj = value; else { - obj = ConstructorCache[type](); + if(type.IsClass) + obj = ConstructorCache[type](); + else + obj = Activator.CreateInstance(type); foreach (KeyValuePair> setter in SetCache[type]) { object jsonValue; if (jsonObject.TryGetValue(setter.Key, out jsonValue)) { jsonValue = DeserializeObject(jsonValue, setter.Value.Key); +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + setter.Value.Value(ref obj, jsonValue); +#else setter.Value.Value(obj, jsonValue); +#endif } } } @@ -1506,7 +1558,197 @@ public virtual object DeserializeObject(object value, Type type) return ReflectionUtils.ToNullableType(obj, type); return obj; } + [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + public virtual object DeserializeObject(ref object target, object value, Type type) +#else + public virtual object DeserializeObject(object target, object value, Type type) +#endif + { + if (type == null) throw new ArgumentNullException("type"); + string str = value as string; + + if (type == typeof(Guid) && string.IsNullOrEmpty(str)) + return default(Guid); + if (value == null) + return null; + + object obj = null; + + if (str != null) + { + if (str.Length != 0) // We know it can't be null now. + { + if (type == typeof(DateTime) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTime))) + return DateTime.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); + if (type == typeof(DateTimeOffset) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset))) + return DateTimeOffset.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); + if (type == typeof(Guid) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid))) + return new Guid(str); + if (type == typeof(Uri)) + { + bool isValid = Uri.IsWellFormedUriString(str, UriKind.RelativeOrAbsolute); + + Uri result; + if (isValid && Uri.TryCreate(str, UriKind.RelativeOrAbsolute, out result)) + return result; + + return null; + } + + if (type == typeof(string)) + return str; + + return Convert.ChangeType(str, type, CultureInfo.InvariantCulture); + } + else + { + if (type == typeof(Guid)) + obj = default(Guid); + else if (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)) + obj = null; + else + obj = str; + } + // Empty string case + if (!ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)) + return str; + } + else if (value is bool) + return value; + + //Modify by xtqqksszml@163.com + //Support uint, ulong + bool valueIsLong = value is long; + bool valueIsULong = value is ulong; + bool valueIsDouble = value is double; + if ((valueIsLong && type == typeof(long)) || (valueIsDouble && type == typeof(double)) || (valueIsULong && type == typeof(ulong))) + return value; + if ((valueIsDouble && type != typeof(double)) || (valueIsLong && type != typeof(long)) || (valueIsULong && type != typeof(ulong))) + { + obj = type == typeof(int) || type == typeof(long) || type == typeof(uint) || type == typeof(ulong) || type == typeof(double) || type == typeof(float) || type == typeof(bool) || type == typeof(decimal) || type == typeof(byte) || type == typeof(short) + ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) + : value; + } + else + { + IDictionary objects = value as IDictionary; + if (objects != null) + { + IDictionary jsonObject = objects; + + if (ReflectionUtils.IsTypeDictionary(type)) + { + // if dictionary then + Type[] types = ReflectionUtils.GetGenericTypeArguments(type); + Type keyType = types[0]; + Type valueType = types[1]; + + Type genericType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); + + IDictionary dict = (IDictionary)ConstructorCache[genericType](); + + // Modify by xtqqksszml@163.com + // Key support the other types + foreach (KeyValuePair kvp in jsonObject) + dict.Add(DeserializeObject(kvp.Key, keyType), DeserializeObject(kvp.Value, valueType)); + + obj = dict; + } + else + { + if (type == typeof(object)) + obj = value; + else + { + + if (target.GetType() == type && target != null) + obj = target; + else + { + if (type.IsClass) + obj = ConstructorCache[type](); + else + obj = Activator.CreateInstance(type); + } + + foreach (KeyValuePair> setter in SetCache[type]) + { + object jsonValue; + if (jsonObject.TryGetValue(setter.Key, out jsonValue)) + { + jsonValue = DeserializeObject(jsonValue, setter.Value.Key); +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + setter.Value.Value(ref obj, jsonValue); +#else + setter.Value.Value(obj, jsonValue); +#endif + } + } + } + } + } + else + { + IList valueAsList = value as IList; + if (valueAsList != null) + { + IList jsonObject = valueAsList; + IList list = null; + + if (type.IsArray) + { + list = (IList)ConstructorCache[type](jsonObject.Count); + int i = 0; + foreach (object o in jsonObject) + list[i++] = DeserializeObject(o, type.GetElementType()); + } + else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || ReflectionUtils.IsAssignableFrom(typeof(IList), type)) + { + Type innerType = ReflectionUtils.GetGenericListElementType(type); + //Modify by xtqqksszml@163.com +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + list = (IList)(ConstructorCache[type] ?? ConstructorCache[typeof(List<>).MakeGenericType(innerType)])(); +#else + list = (IList)(ConstructorCache[type] ?? ConstructorCache[typeof(List<>).MakeGenericType(innerType)])(jsonObject.Count); +#endif + foreach (object o in jsonObject) + list.Add(DeserializeObject(o, innerType)); + } + obj = list; + + //Modify by xtqqksszml@163.com + //Support Dictionary Formats([{"Key":?,"Value":?},]) + if (ReflectionUtils.IsTypeDictionary(type)) + { + Type[] types = ReflectionUtils.GetGenericTypeArguments(type); + Type keyType = types[0]; + Type valueType = types[1]; + + Type genericType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); + + IDictionary dict = (IDictionary)ConstructorCache[genericType](); + + foreach (object o in jsonObject) + { + IDictionary element = o as IDictionary; + if (element != null) + { + dict.Add(DeserializeObject(element["Key"], keyType) + , DeserializeObject(element["Value"], valueType)); + } + } + obj = dict; + } + } + } + return obj; + } + if (ReflectionUtils.IsNullableType(type)) + return ReflectionUtils.ToNullableType(obj, type); + return obj; + } protected virtual object SerializeEnum(Enum p) { return Convert.ToDouble(p, CultureInfo.InvariantCulture); @@ -1651,7 +1893,11 @@ class ReflectionUtils private static readonly object[] EmptyObjects = new object[] { }; public delegate object GetDelegate(object source); +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + public delegate void SetDelegate(ref object source, object value); +#else public delegate void SetDelegate(object source, object value); +#endif public delegate object ConstructorDelegate(params object[] args); public delegate TValue ThreadSafeDictionaryValueFactory(TKey key); @@ -1984,12 +2230,21 @@ public static SetDelegate GetSetMethod(FieldInfo fieldInfo) public static SetDelegate GetSetMethodByReflection(PropertyInfo propertyInfo) { MethodInfo methodInfo = GetSetterMethodInfo(propertyInfo); - return delegate(object source, object value) { methodInfo.Invoke(source, new object[] { value }); }; +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + return delegate (ref object source, object value) { methodInfo.Invoke(source, new object[] { value }); }; +#else + return delegate (object source, object value) { methodInfo.Invoke(source, new object[] { value }); }; +#endif } public static SetDelegate GetSetMethodByReflection(FieldInfo fieldInfo) { - return delegate(object source, object value) { fieldInfo.SetValue(source, value); }; +#if SIMPLE_JSON_NO_LINQ_EXPRESSION + return delegate (ref object source, object value) { fieldInfo.SetValue(source, value); }; +#else + return delegate (object source, object value) { fieldInfo.SetValue(source, value); }; +#endif + } #if !SIMPLE_JSON_NO_LINQ_EXPRESSION From 1885e4773688b53706750f27dcf1355ad66204f1 Mon Sep 17 00:00:00 2001 From: ZhangMinglin Date: Wed, 8 Nov 2017 17:07:45 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E9=81=BF=E5=85=8DDeserializeObject?= =?UTF-8?q?=E5=BC=95=E8=B5=B7=E5=A0=86=E5=86=85=E5=AD=98=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SimpleJson/SimpleJson.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/SimpleJson/SimpleJson.cs b/src/SimpleJson/SimpleJson.cs index fd53688..6f7ce21 100644 --- a/src/SimpleJson/SimpleJson.cs +++ b/src/SimpleJson/SimpleJson.cs @@ -825,9 +825,19 @@ static object ParseValue(char[] json, ref int index, ref bool success) return null; } + [ThreadStatic] + static StringBuilder tempStringBuilder = new StringBuilder(BUILDER_CAPACITY); static string ParseString(char[] json, ref int index, ref bool success) { - StringBuilder s = new StringBuilder(BUILDER_CAPACITY); + ///ÐÞ¸ÄΪʹÓÃÒ»¸ö¾²Ì¬»º´æStringBuilder,¼õÉÙ¶ÑÄÚ´æ·ÖÅ䣬²»¹ý´Ë´¦»áÒýÆðÒ»¸ö³¤×¤µÄ¶ÑÄÚ´æ + ///Modify by ZhangMinglin + /// + /// Old: + ///StringBuilder s = new StringBuilder(BUILDER_CAPACITY); + /// Now: + tempStringBuilder.Length = 0; + StringBuilder s = tempStringBuilder; + char c; EatWhitespace(json, ref index); From 607707830f1b7d613df53a1a8f55b4d18d706788 Mon Sep 17 00:00:00 2001 From: ZhangMinglin Date: Wed, 8 Nov 2017 18:36:31 +0800 Subject: [PATCH 8/8] =?UTF-8?q?ThreadStatic=E4=BF=AE=E9=A5=B0=E7=9A=84?= =?UTF-8?q?=E9=9D=99=E6=80=81=E5=AD=97=E6=AE=B5=E6=88=90=E5=91=98=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=8F=AA=E4=BC=9A=E5=8F=91=E7=94=9F=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=EF=BC=8C=E6=89=80=E4=BB=A5=E6=8A=8A=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=93=8D=E4=BD=9C=E8=BD=AC=E7=A7=BB=E8=87=B3=E5=AE=9E?= =?UTF-8?q?=E9=99=85=E4=BD=BF=E7=94=A8=E6=97=B6=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SimpleJson/SimpleJson.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/SimpleJson/SimpleJson.cs b/src/SimpleJson/SimpleJson.cs index 6f7ce21..e15e653 100644 --- a/src/SimpleJson/SimpleJson.cs +++ b/src/SimpleJson/SimpleJson.cs @@ -826,7 +826,7 @@ static object ParseValue(char[] json, ref int index, ref bool success) } [ThreadStatic] - static StringBuilder tempStringBuilder = new StringBuilder(BUILDER_CAPACITY); + static StringBuilder tempStringBuilder; static string ParseString(char[] json, ref int index, ref bool success) { ///ÐÞ¸ÄΪʹÓÃÒ»¸ö¾²Ì¬»º´æStringBuilder,¼õÉÙ¶ÑÄÚ´æ·ÖÅ䣬²»¹ý´Ë´¦»áÒýÆðÒ»¸ö³¤×¤µÄ¶ÑÄÚ´æ @@ -835,7 +835,14 @@ static string ParseString(char[] json, ref int index, ref bool success) /// Old: ///StringBuilder s = new StringBuilder(BUILDER_CAPACITY); /// Now: - tempStringBuilder.Length = 0; + if(tempStringBuilder == null) + { + tempStringBuilder = new StringBuilder(BUILDER_CAPACITY); + } + else + { + tempStringBuilder.Length = 0; + } StringBuilder s = tempStringBuilder; char c;