Newtonsoft.Json using StringEnumConverter to make it compatiable with enum type

forgive my poor english for example: A java enum like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public enum SomeTypeEnum {
    A(0),B(1),C(2);

    private int value;
    SomeTypeEnum(int value){
      this.value = value;
    }
    public int getValue(){
      return value;
    }
}
java

may be map to

1
{"someType":"A"}
json

then it can only be deserialized to string Type

however when we do this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public enum SomeType
{
   A=0,B=1,C=2
}
public class SomeTypeTest
{
    [JsonConverter(typeof(StringEnumConverter))]
    public SomeType someType {get;set;}
}
c#

it’s OK whatever do deserializing or serializing, nothing else should be care, just do it!!

记录平时瞎折腾遇到的各种问题, 方便查找
使用 Hugo 构建
主题 Stack 3.29.0Jimmy 设计