我正在尝试序列化一个具有[ScriptIgnore]
属性的属性的对象。但是,我有时希望JavaScriptSerializer
不忽略具有该属性的属性。是否有可能序列化整个对象尽管有[ScriptIgnore]
属性?
以下是一些示例代码:
public static string ConvertToJson(this object objectToConvert)
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(objectToConvert);
}
public static void ConvertFromJson(this object objectToConvert, string jsonString)
{
var serializer = new JavaScriptSerializer();
object dummy = serializer.Deserialize(jsonString, objectToConvert.GetType());
foreach(PropertyInfo property in objectToConvert.GetType().GetProperties())
if(property.CanRead && property.CanWrite && property.GetCustomAttribute<ScriptIgnoreAttribute>() == null)
property.SetValue(objectToConvert, property.GetValue(dummy));
}
答案 0 :(得分:3)
您可以通过编码和提供JavaScriptConverter对象来控制整个序列化过程。
为了进行测试,我们将这个简单的类与一个使用ScriptIgnore
属性修饰的属性一起使用:
public class TestObject
{
[ScriptIgnore]
public string TestString { get; set; }
}
...然后序列化它的一个实例:
var serializer = new JavaScriptSerializer();
Console.WriteLine(serializer.Serialize(new TestObject { TestString = "test" }));
该物业当然被忽略了。输出:
{}
现在我们将定义JavaScriptConverter
。这里的相关部分是我们对Serialize()
:
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var testObject = obj as TestObject;
if (testObject != null)
{
// Create the representation. This is a simplified example.
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("TestString", testObject.TestString);
return result;
}
return new Dictionary<string, object>();
}
我们只需将ignored属性添加到输出中。就是这样!
当您要序列化所有内容时,您将提供转换器;如果没有转换器,默认情况下,将忽略带注释的属性。
用法:
serializer.RegisterConverters(new List<JavaScriptConverter> { new TestObjectConverter() });
输出:
{ “的TestString”: “测试”}
完整代码转储:
void Main()
{
var serializer = new JavaScriptSerializer();
Console.WriteLine(serializer.Serialize(new TestObject { TestString = "test" })); // prints: {}
serializer.RegisterConverters(new List<JavaScriptConverter> { new TestObjectConverter() });
Console.WriteLine(serializer.Serialize(new TestObject { TestString = "test" })); // prints: {"TestString":"test"}
}
public class TestObject
{
[ScriptIgnore]
public string TestString { get; set; }
}
public class TestObjectConverter : JavaScriptConverter
{
private static readonly IEnumerable<Type> supportedTypes = new List<Type> { typeof(TestObject) };
public override IEnumerable<Type> SupportedTypes => supportedTypes;
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var testObject = obj as TestObject;
if (testObject != null)
{
// Create the representation. This is a simplified example. You can use reflection or hard code all properties to be written or do it any other way you like - up to you.
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("TestString", testObject.TestString);
return result;
}
return new Dictionary<string, object>();
}
}