/// <summary> /// 将json字符串反序列化成对象,json中须包含__type信息 /// </summary> /// <param name="input"></param> /// <returns></returns> public static object DeserializeObject(string input) { JavaScriptSerializer serializer = new JavaScriptSerializer(new SimpleTypeResolver()); serializer.MaxJsonLength = JSONSerializerFactory.GetMaxJsonLength(); JSONSerializerFactory.RegisterConverters(serializer); return(serializer.DeserializeObject(input)); }
/// <summary> /// 序列化对象 /// </summary> /// <param name="input">要序列化的对象</param> /// <param name="type">要序列化的类型,可以是input的基类或input实现的接口</param> /// <param name="resolverTypeLevel">要输出类型信息的级别深度</param> /// <returns>序列化结果</returns> /// <remarks>替代系统提供的序列化调用</remarks> public static string Serialize(object input, Type type, int resolverTypeLevel) { JavaScriptSerializer serializer = resolverTypeLevel > 0 ? JSONSerializerFactory.GetJavaScriptSerializer(input.GetType()) : JSONSerializerFactory.GetJavaScriptSerializer(); serializer.MaxJsonLength = JSONSerializerFactory.GetMaxJsonLength(); input = PreSerializeObject(input, type, resolverTypeLevel - 1); return(serializer.Serialize(input)); }
/// <summary> /// 序列化,结果中包含__type信息 /// </summary> /// <param name="input"></param> /// <param name="addPrimitiveConverters">是否增加原始类型的转换器</param> /// <returns></returns> public static string SerializeWithType(object input, bool addPrimitiveConverters = false) { JavaScriptSerializer serializer = new JavaScriptSerializer(new SimpleTypeResolver()); serializer.MaxJsonLength = JSONSerializerFactory.GetMaxJsonLength(); JSONSerializerFactory.RegisterConverters(serializer); if (addPrimitiveConverters) { serializer.RegisterConverters(InternalDateTimeConverter.Instances); } return(serializer.Serialize(input)); }