如何序列化用 [ScriptIgnore] 属性修饰的 属性?
How to serialize a property which is decorated with the [ScriptIgnore] attribute?
我正在尝试序列化一个对象,该对象具有一些带有 [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));
}
您可以通过编码和提供 JavaScriptConverter 对象来控制整个序列化过程。
为了测试,让我们使用这个简单的 class 和一个用 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>();
}
我们只是将忽略的 属性 添加到输出中。就是这样!
当你想序列化所有东西时,你会提供转换器;如果没有转换器,默认情况下,注释属性将被忽略。
用法:
serializer.RegisterConverters(new List<JavaScriptConverter> { new TestObjectConverter() });
输出:
{"TestString":"test"}
完整代码转储:
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>();
}
}
我正在尝试序列化一个对象,该对象具有一些带有 [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));
}
您可以通过编码和提供 JavaScriptConverter 对象来控制整个序列化过程。
为了测试,让我们使用这个简单的 class 和一个用 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>();
}
我们只是将忽略的 属性 添加到输出中。就是这样!
当你想序列化所有东西时,你会提供转换器;如果没有转换器,默认情况下,注释属性将被忽略。
用法:
serializer.RegisterConverters(new List<JavaScriptConverter> { new TestObjectConverter() });
输出:
{"TestString":"test"}
完整代码转储:
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>();
}
}