-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace OpenApiGenerator.JsonConverters; | ||
|
||
public enum TestEnum | ||
Check warning on line 3 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnum.cs GitHub Actions / Build, test and publish / Build, test and publish
Check warning on line 3 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnum.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
{ | ||
PullingManifest, | ||
Success, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace OpenApiGenerator.JsonConverters; | ||
|
||
public sealed partial class TestEnumClass | ||
{ | ||
[global::System.Text.Json.Serialization.JsonPropertyName("status")] | ||
[global::System.Text.Json.Serialization.JsonConverter(typeof(TestEnumJsonStringEnumConverter))] | ||
public TestEnum? Status { get; set; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
|
||
namespace OpenApiGenerator.JsonConverters; | ||
|
||
public static class TestEnumExtensions | ||
{ | ||
public static string ToValueString(this TestEnum value) | ||
{ | ||
return value switch | ||
{ | ||
TestEnum.PullingManifest => "pulling manifest", | ||
TestEnum.Success => "success", | ||
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null), | ||
}; | ||
} | ||
|
||
public static TestEnum ToEnum(string value) | ||
{ | ||
return value switch | ||
{ | ||
"pulling manifest" => TestEnum.PullingManifest, | ||
"success" => TestEnum.Success, | ||
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null), | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace OpenApiGenerator.JsonConverters; | ||
|
||
public sealed class TestEnumJsonStringEnumConverter : global::System.Text.Json.Serialization.JsonConverter<TestEnum> | ||
{ | ||
public override TestEnum Read( | ||
ref global::System.Text.Json.Utf8JsonReader reader, | ||
global::System.Type typeToConvert, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case global::System.Text.Json.JsonTokenType.String: | ||
{ | ||
var stringValue = reader.GetString(); | ||
if (stringValue != null) | ||
{ | ||
return TestEnumExtensions.ToEnum(stringValue); | ||
} | ||
|
||
break; | ||
} | ||
case global::System.Text.Json.JsonTokenType.Number: | ||
{ | ||
return (TestEnum)reader.GetInt32(); | ||
} | ||
default: | ||
throw new global::System.ArgumentOutOfRangeException(); | ||
Check warning on line 27 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
Check warning on line 27 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
} | ||
|
||
return default; | ||
} | ||
|
||
public override void Write( | ||
global::System.Text.Json.Utf8JsonWriter writer, | ||
TestEnum value, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToValueString()); | ||
Check warning on line 38 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
Check warning on line 38 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace OpenApiGenerator.JsonConverters; | ||
|
||
public sealed class TestEnumNewtonsoftJsonStringEnumConverter : Newtonsoft.Json.JsonConverter<TestEnum> | ||
{ | ||
public override void WriteJson(JsonWriter writer, TestEnum value, Newtonsoft.Json.JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value.ToValueString()); | ||
Check warning on line 10 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
Check warning on line 10 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
} | ||
|
||
public override TestEnum ReadJson(JsonReader reader, Type objectType, TestEnum existingValue, bool hasExistingValue, | ||
Newtonsoft.Json.JsonSerializer serializer) | ||
{ | ||
if (hasExistingValue) | ||
{ | ||
return existingValue; | ||
} | ||
|
||
var type = reader.TokenType; | ||
Check warning on line 21 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
Check warning on line 21 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
|
||
switch (type) | ||
{ | ||
case JsonToken.String: | ||
{ | ||
var stringValue = reader.ReadAsString(); | ||
if (stringValue != null) | ||
{ | ||
return TestEnumExtensions.ToEnum(stringValue); | ||
} | ||
break; | ||
} | ||
case JsonToken.Integer: | ||
{ | ||
var numValue = reader.ReadAsInt32(); | ||
if (numValue != null) | ||
{ | ||
return (TestEnum)numValue.Value; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
} |