我使用Newtonsoft。Json序列化器,将c#类转换为Json。对于某些类,我不需要序列化器到单个属性的实例,而是只需在对象上调用ToString,即
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString() { return string.Format("{0} {1}", FirstName, LastName ); }
}
我应该怎么做才能将Person对象序列化为其ToString()方法的结果?我可能有很多这样的类,所以我不想以一个特定于Person类的序列化器结束,我想要一个可以适用于任何类的序列化器(我猜是通过属性)。
您可以使用自定义JsonConverter
:
public class ToStringJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
要使用转换器,用[JsonConverter]
属性修饰任何需要序列化为字符串的类,如下所示:
[JsonConverter(typeof(ToStringJsonConverter))]
public class Person
{
...
}
下面是演示转换器的示例:
class Program
{
static void Main(string[] args)
{
Company company = new Company
{
CompanyName = "Initrode",
Boss = new Person { FirstName = "Head", LastName = "Honcho" },
Employees = new List<Person>
{
new Person { FirstName = "Joe", LastName = "Schmoe" },
new Person { FirstName = "John", LastName = "Doe" }
}
};
string json = JsonConvert.SerializeObject(company, Formatting.Indented);
Console.WriteLine(json);
}
}
public class Company
{
public string CompanyName { get; set; }
public Person Boss { get; set; }
public List<Person> Employees { get; set; }
}
[JsonConverter(typeof(ToStringJsonConverter))]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
输出:{
"CompanyName": "Initrode",
"Boss": "Head Honcho",
"Employees": [
"Joe Schmoe",
"John Doe"
]
}
如果您还需要能够从字符串转换回对象,您可以在转换器上实现ReadJson
方法,以便它查找public static Parse(string)
方法并调用它。注意:一定要将转换器的CanRead
方法更改为返回true
(或者干脆删除CanRead
重载),否则ReadJson
将永远不会被调用。
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
MethodInfo parse = objectType.GetMethod("Parse", new Type[] { typeof(string) });
if (parse != null && parse.IsStatic && parse.ReturnType == objectType)
{
return parse.Invoke(null, new object[] { (string)reader.Value });
}
throw new JsonException(string.Format(
"The {0} type does not have a public static Parse(string) method that returns a {0}.",
objectType.Name));
}
当然,要使上述工作,您还需要确保在您正在转换的每个类上实现一个合适的Parse
方法,如果它还不存在的话。对于上面显示的示例Person
类,该方法可能看起来像这样:
public static Person Parse(string s)
{
if (string.IsNullOrWhiteSpace(s))
throw new ArgumentException("s cannot be null or empty", "s");
string[] parts = s.Split(new char[] { ' ' }, 2);
Person p = new Person { FirstName = parts[0] };
if (parts.Length > 1)
p.LastName = parts[1];
return p;
}
往返演示:https://dotnetfiddle.net/fd4EG4
如果不打算大规模使用,有一种更快的方法可以做到这一点,在下面的例子中,它是为RecordType属性
完成的[JsonIgnore]
public RecordType RecType { get; set; }
[JsonProperty(PropertyName = "RecordType")]
private string RecordTypeString => RecType.ToString();
您可以简单地尝试Newtonsoft的JSON构建器库并使用以下代码序列化Person类型的对象:
Dictionary<string, object> collection = new Dictionary<string, object>()
{
{"First", new Person(<add FirstName as constructor>)},
{"Second", new Person(<add LastName as constructor>)},
};
string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
});
我没有时间测试我的解决方案,但它应该工作。假设所有你正在使用的类都是你自己的,为什么你不做一个ToString覆盖所有的类和那些需要使用Newtonsoft的类。Json序列化器只能在ToString方法中序列化并返回。这样,当您想要获得对象的序列化字符串时,总是可以调用ToString。