1

I have my RestSharpJsonNetSerializer class below. I am serializing an object that has several properties of enum types.

I am using properties below. But the resulting JSON still displays int values of enums, not their string values. I guess I could be using wrong properties or need to add changes to my serializer class. Relatively new with this, so need some help please.

public class PTypeData
{
    [JsonProperty (ItemConverterType = typeof(StringEnumConverter))]
    public Enums.Type Type {get;set;}
    [JsonProperty (ItemConverterType = typeof(StringEnumConverter))]
    public Enums.Status Status {get;set;} 
 }

RestSharpJsonNetSerializer class:

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; }
    }
Razkar
  • 539
  • 5
  • 26
  • Looks like a duplicate of [Why serializing Version with JsonPropertyAttribute doesn't work?](https://stackoverflow.com/q/38319305/3744182). As explained there use `[JsonConverter(typeof(StringEnumConverter))]` not `ItemConverterType = typeof(StringEnumConverter)`. See also [parsing an enumeration in JSON.net](https://stackoverflow.com/q/7799769/3744182) and maybe [How to tell Json.Net globally to apply the StringEnumConverter to all enums](https://stackoverflow.com/q/7427909/3744182). – dbc Jan 04 '18 at 18:53
  • 1
    [JsonConverter(typeof(StringEnumConverter))] did it. Thank you! – Razkar Jan 04 '18 at 19:03

0 Answers0