internal object Render(Type type, bool ignoreTypeSafe) { if (!type.HasDefaultConstructor()) { ExceptionHelper.ThrowNoDefaultConstructor(type); } IExtendConverter cvt = ExtendConverter.Instance(); if (cvt.CanConvert(type)) { return(cvt.FromJson(type, this, ignoreTypeSafe)); } object instance = Activator.CreateInstance(type, true); IReflector r = Reflector.Bind(instance, ReflectorPolicy.InstancePublicIgnoreCase); foreach (KeyValuePair <string, object> kvp in _dict) { string propertyName = kvp.Key; Type targetType = r.GetPropertyType(propertyName); var data = JsonHelper.Render(kvp.Value, targetType, ignoreTypeSafe); r.SetPropertyValue(propertyName, data); } return(instance); }
internal static object ConvertToSimpleType(string jsonText, Type targetType, bool ignoreTypeSafe = false) { IExtendConverter cvt = ExtendConverter.Instance(); object result = null; if (cvt.CanConvert(targetType)) { try { result = cvt.FromJson(targetType, jsonText, ignoreTypeSafe); } catch (Exception ex) { throw ex.CreateWrapException <JsonExtendConverterException>(); } } else { if (ignoreTypeSafe) { //Note: If we don't care type safe, always remove quoter if exist, then cast if (jsonText.StartsWith(JsonConstant.Quot) && jsonText.EndsWith(JsonConstant.Quot) && jsonText.Length > 1) { jsonText = jsonText.UnBracketing(StringPair.DoubleQuote); } result = TypeCast.ChangeToTypeOrNullableType(jsonText, targetType); } else { //Note: If we care type, strict follow JSON standard if (targetType == typeof(string)) { if ((!jsonText.StartsWith(JsonConstant.Quot)) || (!jsonText.EndsWith(JsonConstant.Quot))) { ExceptionHelper.ThrowSyntaxNoQuotError(); } result = jsonText.UnBracketing(StringPair.DoubleQuote); } else { result = TypeCast.ChangeToTypeOrNullableType(jsonText, targetType); } } } return(result); }
internal object Render(Type type, bool ignoreTypeSafe) { IExtendConverter cvt = ExtendConverter.Instance(); if (cvt.CanConvert(type)) { return(cvt.FromJson(type, this, ignoreTypeSafe)); } Type elementType = type.GetElementType(); Array array = (Array)Activator.CreateInstance(elementType.MakeArrayType(), this.Length); for (int i = 0; i < _array.Count; i++) { object jsonElement = _array[i]; var data = JsonHelper.Render(jsonElement, elementType, ignoreTypeSafe); array.SetValue(data, i); } return(array); }
//Note:Json standard only support 6 data types (Null, Boolean, Number, String, Object and Array) public static string ToJsonString(this object instance) { if (instance == null) { return(JsonConstant.Null); } //Note:As the instance!=null, we don't need check Nullable<T> Type t = instance.GetType(); //Note:before go to normal, check if the customized extend converter can handle it. IExtendConverter cvt = ExtendConverter.Instance(); if (cvt.CanConvert(t)) { try { return(cvt.ToJsonString(instance)); } catch (Exception ex) { throw ex.CreateWrapException <JsonExtendConverterException>(); } } TypeCode code = Type.GetTypeCode(t); switch (code) { case TypeCode.Boolean: return(instance.ToString().ToLower()); case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: case TypeCode.Byte: case TypeCode.SByte: return(instance.ToString()); case TypeCode.String: case TypeCode.Char: return(((string)instance).Bracketing(StringPair.DoubleQuote)); } StringBuilder str = new StringBuilder(); if (!t.IsArray)//Note:Object { IReflector r = Reflector.Bind(instance, ReflectorPolicy.InstancePublicIgnoreCase); foreach (PropertyInfo propertyInfo in r.FindAllProperties()) { if (propertyInfo.Name == "Item")//Extend:Item is special name for indexer, not support currently. { continue; } string name = char.ToLower(propertyInfo.Name[0]) + propertyInfo.Name.Substring(1); str.Append(name.Bracketing(StringPair.DoubleQuote)); str.Append(JsonConstant.Colon); str.Append(r.GetPropertyValue(propertyInfo.Name).ToJsonString()); str.Append(JsonConstant.Comma); } str.RemoveEnd(JsonConstant.Comma); str.Bracketing(StringPair.CurlyBracket); return(str.ToString()); } else//Note:Array { Array array = instance as Array; if (array.Length > 0) { foreach (object element in array) { str.Append(element.ToJsonString()); str.Append(JsonConstant.Comma); } str.RemoveEnd(JsonConstant.Comma); } str.Bracketing(StringPair.SquareBracket); return(str.ToString()); } }