0

What setting should I change to get the property value of an enum instead of its string representation when serializing an object? I have the following class.

public class ProductModel
{
    public long ProductId { get; set; }

    public int ContainerType { get; set; }

    public SolidForm SolidForm { get; set; }
}

(for example) NOW ---> my json =

{ "ProductId" : 22222,
  "ContainerType" : 1111,
  "SolidForm" : "Solid"
}

but I need this after serialization. (not enum as string)

{ "ProductId" : 22222,
  "ContainerType" : 1111,
  "SolidForm" : 1
}

I want that all enums in my object converted to int.

this my settings of Json Serialization

JsonSerializerSettings = new JsonSerializerSettings
    {
        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
        Error = delegate (object sender, ErrorEventArgs args)
        {
            args.ErrorContext.Handled = true;
        }
    }
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Igor Strekha
  • 176
  • 3
  • 17

1 Answers1

1

The default in Newtonsoft.Json is to serialize enums as int. Assuming you mean Newtonsoft.Json.

Is your enum decorated with the attribute [JsonConverter(typeof(StringEnumConverter))]?

Kricke242
  • 59
  • 4