1

We want to serialize an object to JSON with Newton.JSON, but this Object use an enum and is then converted in the underlying int (see 'Datatype' in JSON below). How can ve proceed to get the "enum string" representation?

{
    "Timestamp": 1538568112852,
    "Metrics": [{
        "Name": "bdSeq",
        "Timestamp": 1538568112852,
        "Datatype": 4,
        "IsNull": false,
        "LongValue": 0
    }],
    "Seq": 18446744073709551615
}
1

1 Answer 1

0

You can substitute standard JsonSerializer with your own.

In startup.cs:

services.AddScoped(typeof(JsonSerializer), typeof(EnumAsStringSerializer));

And the class itself:

 public class EnumAsStringSerializer : JsonSerializer
    {
        public EnumAsStringSerializer()
        {
            this.ContractResolver = new CamelCasePropertyNamesContractResolver();
            this.Converters.Add(new StringEnumConverter
            {
                CamelCaseText = true,
            });
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.