@@ -75,14 +75,51 @@ namespace {{Namespace}}
7575{{!# ClientClass}}
7676 public sealed class {{ClassName}} : I{{ClassName}}
7777 {
78+ {{ifTemplate 'StringifyEnums' 'true'}}
79+ internal interface IStringifiedEnum
80+ {
81+ string Value { get; }
82+ }
83+ {{/ifTemplate}}
84+
7885 private static JsonSerializerSettings jsonSettings = new JsonSerializerSettings()
7986 {
8087 Converters =
8188 {
89+ {{ifTemplate 'StringifyEnums' 'true'}}
90+ new CustomJsonStringifiedEnumConverter(),
91+ {{/ifTemplate}}
8292 new Newtonsoft.Json.Converters.StringEnumConverter(camelCaseText: false),
8393 {{ > jsonConverters }}
8494 }
8595 };
96+
97+ {{ifTemplate 'StringifyEnums' 'true'}}
98+ internal class CustomJsonStringifiedEnumConverter : JsonConverter
99+ {
100+ public override bool CanConvert(Type objectType)
101+ {
102+ return typeof(IStringifiedEnum).IsAssignableFrom(objectType);
103+ }
104+
105+ public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
106+ {
107+ return reader.Value == null ? null : reader.Value.ToString();
108+ }
109+
110+ public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
111+ {
112+ if (value is IStringifiedEnum v)
113+ {
114+ writer.WriteValue(v.Value);
115+ }
116+ else
117+ {
118+ writer.WriteNull();
119+ }
120+ }
121+ }
122+ {{/ifTemplate}}
86123
87124 private string SerializeBody(GraphQLRequest request)
88125 {
@@ -153,7 +190,7 @@ namespace {{Namespace}}
153190 public sealed class {{pascalCase Name}} {{#each~ Interfaces}}{{~#if @first}}: {{else}}, {{/if~}}I{{pascalCase .}}{{~/each}}
154191 {
155192 {{#each Fields}}
156- [JsonProperty("{{Name}}")]
193+ [JsonProperty("{{Name}}")]
157194 public {{> TypeReference Type}} {{pascalCase Name}} { get; set; }
158195 {{/each}}
159196 }
@@ -170,7 +207,7 @@ namespace {{Namespace}}
170207{{!# Enum}}
171208
172209 {{ifTemplate 'StringifyEnums' 'true'}}
173- public class {{pascalCase Name}}
210+ public class {{pascalCase Name}} : IStringifiedEnum
174211 {
175212 static Dictionary<string, {{pascalCase Name}}> lookup = new Dictionary<string, {{pascalCase Name}}>();
176213
0 commit comments