我下面有我的RestSharpJsonNetSerializer类。 我正在序列化一个具有枚举类型的几个属性的对象。
我正在使用以下属性。但是生成的JSON仍然显示枚举的int值,而不是它们的字符串值。 我想我可能使用了错误的属性或者需要在我的序列化程序类中添加更改。这个比较新,所以需要一些帮助。
public class PTypeData
{
[JsonProperty (ItemConverterType = typeof(StringEnumConverter))]
public Enums.Type Type {get;set;}
[JsonProperty (ItemConverterType = typeof(StringEnumConverter))]
public Enums.Status Status {get;set;}
}
RestSharpJsonNetSerializer类:
public class RestSharpJsonNetSerializer : RestSharp.Serializers.ISerializer
{
private readonly Newtonsoft.Json.JsonSerializer _serializer;
/// <summary>
/// Default serializer
/// </summary>
public RestSharpJsonNetSerializer () {
ContentType = "application/json";
_serializer = new Newtonsoft.Json.JsonSerializer {
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include
};
}
/// <summary>
/// Default serializer with overload for allowing custom Json.NET settings
/// </summary>
public RestSharpJsonNetSerializer (Newtonsoft.Json.JsonSerializer serializer){
ContentType = "application/json";
_serializer = serializer;
}
/// <summary>
/// Serialize the object as JSON
/// </summary>
/// <param name="obj">Object to serialize</param>
/// <returns>JSON as String</returns>
public string Serialize(object obj) {
using (var stringWriter = new StringWriter()) {
using (var jsonTextWriter = new JsonTextWriter(stringWriter)) {
jsonTextWriter.Formatting = Formatting.Indented;
jsonTextWriter.QuoteChar = '"';
_serializer.Serialize(jsonTextWriter, obj);
var result = stringWriter.ToString();
return result;
}
}
}
/// <summary>
/// Unused for JSON Serialization
/// </summary>
public string DateFormat { get; set; }
/// <summary>
/// Unused for JSON Serialization
/// </summary>
public string RootElement { get; set; }
/// <summary>
/// Unused for JSON Serialization
/// </summary>
public string Namespace { get; set; }
/// <summary>
/// Content type for serialized content
/// </summary>
public string ContentType { get; set; }
}