Test case
16 generated files · +1,160 −16test/inputs/json/misc/31189.json
Acsharp-recordsdefault / QuickType.cs+206 −0
| @@ -0,0 +1,206 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | +#pragma warning disable CS8604 | |
| 14 | +#pragma warning disable CS8625 | |
| 15 | +#pragma warning disable CS8765 | |
| 16 | + | |
| 17 | +namespace QuickType | |
| 18 | +{ | |
| 19 | + using System; | |
| 20 | + using System.Collections.Generic; | |
| 21 | + | |
| 22 | + using System.Globalization; | |
| 23 | + using Newtonsoft.Json; | |
| 24 | + using Newtonsoft.Json.Converters; | |
| 25 | + | |
| 26 | + public partial record TopLevel | |
| 27 | + { | |
| 28 | + [JsonProperty("details", Required = Required.Always)] | |
| 29 | + public Uri Details { get; set; } | |
| 30 | + | |
| 31 | + [JsonProperty("rates", Required = Required.Always)] | |
| 32 | + public Rate[] Rates { get; set; } | |
| 33 | + | |
| 34 | + [JsonProperty("version", Required = Required.AllowNull)] | |
| 35 | + public object Version { get; set; } | |
| 36 | + } | |
| 37 | + | |
| 38 | + public partial record Rate | |
| 39 | + { | |
| 40 | + [JsonProperty("code", Required = Required.Always)] | |
| 41 | + public string Code { get; set; } | |
| 42 | + | |
| 43 | + [JsonProperty("country_code", Required = Required.Always)] | |
| 44 | + public string CountryCode { get; set; } | |
| 45 | + | |
| 46 | + [JsonProperty("name", Required = Required.Always)] | |
| 47 | + public string Name { get; set; } | |
| 48 | + | |
| 49 | + [JsonProperty("periods", Required = Required.Always)] | |
| 50 | + public Period[] Periods { get; set; } | |
| 51 | + } | |
| 52 | + | |
| 53 | + public partial record Period | |
| 54 | + { | |
| 55 | + [JsonProperty("effective_from", Required = Required.Always)] | |
| 56 | + public EffectiveFromUnion EffectiveFrom { get; set; } | |
| 57 | + | |
| 58 | + [JsonProperty("rates", Required = Required.Always)] | |
| 59 | + public Rates Rates { get; set; } | |
| 60 | + } | |
| 61 | + | |
| 62 | + public partial record Rates | |
| 63 | + { | |
| 64 | + [JsonProperty("parking", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 65 | + public double? Parking { get; set; } | |
| 66 | + | |
| 67 | + [JsonProperty("reduced", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 68 | + public double? Reduced { get; set; } | |
| 69 | + | |
| 70 | + [JsonProperty("reduced1", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 71 | + public double? Reduced1 { get; set; } | |
| 72 | + | |
| 73 | + [JsonProperty("reduced2", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 74 | + public double? Reduced2 { get; set; } | |
| 75 | + | |
| 76 | + [JsonProperty("standard", Required = Required.Always)] | |
| 77 | + public double Standard { get; set; } | |
| 78 | + | |
| 79 | + [JsonProperty("super_reduced", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 80 | + public double? SuperReduced { get; set; } | |
| 81 | + } | |
| 82 | + | |
| 83 | + public enum EffectiveFromEnum { The00000101 }; | |
| 84 | + | |
| 85 | + public partial struct EffectiveFromUnion | |
| 86 | + { | |
| 87 | + public DateTimeOffset? DateTime; | |
| 88 | + public EffectiveFromEnum? Enum; | |
| 89 | + | |
| 90 | + public static implicit operator EffectiveFromUnion(DateTimeOffset DateTime) => new EffectiveFromUnion { DateTime = DateTime }; | |
| 91 | + public static implicit operator EffectiveFromUnion(EffectiveFromEnum Enum) => new EffectiveFromUnion { Enum = Enum }; | |
| 92 | + } | |
| 93 | + | |
| 94 | + public partial record TopLevel | |
| 95 | + { | |
| 96 | + public static TopLevel FromJson(string json) => JsonConvert.DeserializeObject<TopLevel>(json, QuickType.Converter.Settings); | |
| 97 | + } | |
| 98 | + | |
| 99 | + public static partial class Serialize | |
| 100 | + { | |
| 101 | + public static string ToJson(this TopLevel self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 102 | + } | |
| 103 | + | |
| 104 | + internal static partial class Converter | |
| 105 | + { | |
| 106 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 107 | + { | |
| 108 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 109 | + DateParseHandling = DateParseHandling.None, | |
| 110 | + Converters = | |
| 111 | + { | |
| 112 | + EffectiveFromUnionConverter.Singleton, | |
| 113 | + EffectiveFromEnumConverter.Singleton, | |
| 114 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 115 | + }, | |
| 116 | + }; | |
| 117 | + } | |
| 118 | + | |
| 119 | + internal class EffectiveFromUnionConverter : JsonConverter | |
| 120 | + { | |
| 121 | + public override bool CanConvert(Type t) => t == typeof(EffectiveFromUnion) || t == typeof(EffectiveFromUnion?); | |
| 122 | + | |
| 123 | + public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) | |
| 124 | + { | |
| 125 | + switch (reader.TokenType) | |
| 126 | + { | |
| 127 | + case JsonToken.String: | |
| 128 | + case JsonToken.Date: | |
| 129 | + var stringValue = serializer.Deserialize<string>(reader); | |
| 130 | + DateTimeOffset dt; | |
| 131 | + if (DateTimeOffset.TryParse(stringValue, out dt)) | |
| 132 | + { | |
| 133 | + return new EffectiveFromUnion { DateTime = dt }; | |
| 134 | + } | |
| 135 | + if (stringValue == "0000-01-01") | |
| 136 | + { | |
| 137 | + return new EffectiveFromUnion { Enum = EffectiveFromEnum.The00000101 }; | |
| 138 | + } | |
| 139 | + break; | |
| 140 | + } | |
| 141 | + throw new Exception("Cannot unmarshal type EffectiveFromUnion"); | |
| 142 | + } | |
| 143 | + | |
| 144 | + public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) | |
| 145 | + { | |
| 146 | + var value = (EffectiveFromUnion)untypedValue; | |
| 147 | + if (value.DateTime != null) | |
| 148 | + { | |
| 149 | + serializer.Serialize(writer, value.DateTime.Value.ToString("o", System.Globalization.CultureInfo.InvariantCulture)); | |
| 150 | + return; | |
| 151 | + } | |
| 152 | + if (value.Enum != null) | |
| 153 | + { | |
| 154 | + if (value.Enum == EffectiveFromEnum.The00000101) | |
| 155 | + { | |
| 156 | + serializer.Serialize(writer, "0000-01-01"); | |
| 157 | + return; | |
| 158 | + } | |
| 159 | + } | |
| 160 | + throw new Exception("Cannot marshal type EffectiveFromUnion"); | |
| 161 | + } | |
| 162 | + | |
| 163 | + public static readonly EffectiveFromUnionConverter Singleton = new EffectiveFromUnionConverter(); | |
| 164 | + } | |
| 165 | + | |
| 166 | + internal class EffectiveFromEnumConverter : JsonConverter | |
| 167 | + { | |
| 168 | + public override bool CanConvert(Type t) => t == typeof(EffectiveFromEnum) || t == typeof(EffectiveFromEnum?); | |
| 169 | + | |
| 170 | + public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) | |
| 171 | + { | |
| 172 | + if (reader.TokenType == JsonToken.Null) return null; | |
| 173 | + var value = serializer.Deserialize<string>(reader); | |
| 174 | + if (value == "0000-01-01") | |
| 175 | + { | |
| 176 | + return EffectiveFromEnum.The00000101; | |
| 177 | + } | |
| 178 | + throw new Exception("Cannot unmarshal type EffectiveFromEnum"); | |
| 179 | + } | |
| 180 | + | |
| 181 | + public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) | |
| 182 | + { | |
| 183 | + if (untypedValue == null) | |
| 184 | + { | |
| 185 | + serializer.Serialize(writer, null); | |
| 186 | + return; | |
| 187 | + } | |
| 188 | + var value = (EffectiveFromEnum)untypedValue; | |
| 189 | + if (value == EffectiveFromEnum.The00000101) | |
| 190 | + { | |
| 191 | + serializer.Serialize(writer, "0000-01-01"); | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + throw new Exception("Cannot marshal type EffectiveFromEnum"); | |
| 195 | + } | |
| 196 | + | |
| 197 | + public static readonly EffectiveFromEnumConverter Singleton = new EffectiveFromEnumConverter(); | |
| 198 | + } | |
| 199 | +} | |
| 200 | +#pragma warning restore CS8618 | |
| 201 | +#pragma warning restore CS8601 | |
| 202 | +#pragma warning restore CS8602 | |
| 203 | +#pragma warning restore CS8603 | |
| 204 | +#pragma warning restore CS8604 | |
| 205 | +#pragma warning restore CS8625 | |
| 206 | +#pragma warning restore CS8765 |
Acsharp-SystemTextJsondefault / QuickType.cs+316 −0
| @@ -0,0 +1,316 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'System.Text.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | + | |
| 14 | +namespace QuickType | |
| 15 | +{ | |
| 16 | + using System; | |
| 17 | + using System.Collections.Generic; | |
| 18 | + | |
| 19 | + using System.Text.Json; | |
| 20 | + using System.Text.Json.Serialization; | |
| 21 | + using System.Globalization; | |
| 22 | + | |
| 23 | + public partial class TopLevel | |
| 24 | + { | |
| 25 | + [JsonRequired] | |
| 26 | + [JsonPropertyName("details")] | |
| 27 | + public Uri Details { get; set; } | |
| 28 | + | |
| 29 | + [JsonRequired] | |
| 30 | + [JsonPropertyName("rates")] | |
| 31 | + public Rate[] Rates { get; set; } | |
| 32 | + | |
| 33 | + [JsonRequired] | |
| 34 | + [JsonPropertyName("version")] | |
| 35 | + public object Version { get; set; } | |
| 36 | + } | |
| 37 | + | |
| 38 | + public partial class Rate | |
| 39 | + { | |
| 40 | + [JsonRequired] | |
| 41 | + [JsonPropertyName("code")] | |
| 42 | + public string Code { get; set; } | |
| 43 | + | |
| 44 | + [JsonRequired] | |
| 45 | + [JsonPropertyName("country_code")] | |
| 46 | + public string CountryCode { get; set; } | |
| 47 | + | |
| 48 | + [JsonRequired] | |
| 49 | + [JsonPropertyName("name")] | |
| 50 | + public string Name { get; set; } | |
| 51 | + | |
| 52 | + [JsonRequired] | |
| 53 | + [JsonPropertyName("periods")] | |
| 54 | + public Period[] Periods { get; set; } | |
| 55 | + } | |
| 56 | + | |
| 57 | + public partial class Period | |
| 58 | + { | |
| 59 | + [JsonRequired] | |
| 60 | + [JsonPropertyName("effective_from")] | |
| 61 | + public EffectiveFromUnion EffectiveFrom { get; set; } | |
| 62 | + | |
| 63 | + [JsonRequired] | |
| 64 | + [JsonPropertyName("rates")] | |
| 65 | + public Rates Rates { get; set; } | |
| 66 | + } | |
| 67 | + | |
| 68 | + public partial class Rates | |
| 69 | + { | |
| 70 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 71 | + [JsonPropertyName("parking")] | |
| 72 | + public double? Parking { get; set; } | |
| 73 | + | |
| 74 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 75 | + [JsonPropertyName("reduced")] | |
| 76 | + public double? Reduced { get; set; } | |
| 77 | + | |
| 78 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 79 | + [JsonPropertyName("reduced1")] | |
| 80 | + public double? Reduced1 { get; set; } | |
| 81 | + | |
| 82 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 83 | + [JsonPropertyName("reduced2")] | |
| 84 | + public double? Reduced2 { get; set; } | |
| 85 | + | |
| 86 | + [JsonRequired] | |
| 87 | + [JsonPropertyName("standard")] | |
| 88 | + public double Standard { get; set; } | |
| 89 | + | |
| 90 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 91 | + [JsonPropertyName("super_reduced")] | |
| 92 | + public double? SuperReduced { get; set; } | |
| 93 | + } | |
| 94 | + | |
| 95 | + public enum EffectiveFromEnum { The00000101 }; | |
| 96 | + | |
| 97 | + public partial struct EffectiveFromUnion | |
| 98 | + { | |
| 99 | + public DateTimeOffset? DateTime; | |
| 100 | + public EffectiveFromEnum? Enum; | |
| 101 | + | |
| 102 | + public static implicit operator EffectiveFromUnion(DateTimeOffset DateTime) => new EffectiveFromUnion { DateTime = DateTime }; | |
| 103 | + public static implicit operator EffectiveFromUnion(EffectiveFromEnum Enum) => new EffectiveFromUnion { Enum = Enum }; | |
| 104 | + } | |
| 105 | + | |
| 106 | + public partial class TopLevel | |
| 107 | + { | |
| 108 | + public static TopLevel FromJson(string json) => global::System.Text.Json.JsonSerializer.Deserialize<TopLevel>(json, QuickType.Converter.Settings); | |
| 109 | + } | |
| 110 | + | |
| 111 | + public static partial class Serialize | |
| 112 | + { | |
| 113 | + public static string ToJson(this TopLevel self) => JsonSerializer.Serialize(self, QuickType.Converter.Settings); | |
| 114 | + } | |
| 115 | + | |
| 116 | + internal static partial class Converter | |
| 117 | + { | |
| 118 | + public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General) | |
| 119 | + { | |
| 120 | + Converters = | |
| 121 | + { | |
| 122 | + EffectiveFromUnionConverter.Singleton, | |
| 123 | + EffectiveFromEnumConverter.Singleton, | |
| 124 | + new DateOnlyConverter(), | |
| 125 | + new TimeOnlyConverter(), | |
| 126 | + IsoDateTimeOffsetConverter.Singleton | |
| 127 | + }, | |
| 128 | + }; | |
| 129 | + } | |
| 130 | + | |
| 131 | + internal class EffectiveFromUnionConverter : JsonConverter<EffectiveFromUnion> | |
| 132 | + { | |
| 133 | + public override bool CanConvert(Type t) => t == typeof(EffectiveFromUnion); | |
| 134 | + | |
| 135 | + public override EffectiveFromUnion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 136 | + { | |
| 137 | + switch (reader.TokenType) | |
| 138 | + { | |
| 139 | + case JsonTokenType.String: | |
| 140 | + var stringValue = reader.GetString(); | |
| 141 | + DateTimeOffset dt; | |
| 142 | + if (DateTimeOffset.TryParse(stringValue, out dt)) | |
| 143 | + { | |
| 144 | + return new EffectiveFromUnion { DateTime = dt }; | |
| 145 | + } | |
| 146 | + if (stringValue == "0000-01-01") | |
| 147 | + { | |
| 148 | + return new EffectiveFromUnion { Enum = EffectiveFromEnum.The00000101 }; | |
| 149 | + } | |
| 150 | + break; | |
| 151 | + } | |
| 152 | + throw new JsonException("Cannot unmarshal type EffectiveFromUnion"); | |
| 153 | + } | |
| 154 | + | |
| 155 | + public override void Write(Utf8JsonWriter writer, EffectiveFromUnion value, JsonSerializerOptions options) | |
| 156 | + { | |
| 157 | + if (value.DateTime != null) | |
| 158 | + { | |
| 159 | + JsonSerializer.Serialize(writer, value.DateTime.Value.ToString("o", System.Globalization.CultureInfo.InvariantCulture), options); | |
| 160 | + return; | |
| 161 | + } | |
| 162 | + if (value.Enum != null) | |
| 163 | + { | |
| 164 | + if (value.Enum == EffectiveFromEnum.The00000101) | |
| 165 | + { | |
| 166 | + JsonSerializer.Serialize(writer, "0000-01-01", options); | |
| 167 | + return; | |
| 168 | + } | |
| 169 | + } | |
| 170 | + throw new NotSupportedException("Cannot marshal type EffectiveFromUnion"); | |
| 171 | + } | |
| 172 | + | |
| 173 | + public static readonly EffectiveFromUnionConverter Singleton = new EffectiveFromUnionConverter(); | |
| 174 | + } | |
| 175 | + | |
| 176 | + internal class EffectiveFromEnumConverter : JsonConverter<EffectiveFromEnum> | |
| 177 | + { | |
| 178 | + public override bool CanConvert(Type t) => t == typeof(EffectiveFromEnum); | |
| 179 | + | |
| 180 | + public override EffectiveFromEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 181 | + { | |
| 182 | + var value = reader.GetString(); | |
| 183 | + if (value == "0000-01-01") | |
| 184 | + { | |
| 185 | + return EffectiveFromEnum.The00000101; | |
| 186 | + } | |
| 187 | + throw new JsonException("Cannot unmarshal type EffectiveFromEnum"); | |
| 188 | + } | |
| 189 | + | |
| 190 | + public override void Write(Utf8JsonWriter writer, EffectiveFromEnum value, JsonSerializerOptions options) | |
| 191 | + { | |
| 192 | + if (value == EffectiveFromEnum.The00000101) | |
| 193 | + { | |
| 194 | + JsonSerializer.Serialize(writer, "0000-01-01", options); | |
| 195 | + return; | |
| 196 | + } | |
| 197 | + throw new NotSupportedException("Cannot marshal type EffectiveFromEnum"); | |
| 198 | + } | |
| 199 | + | |
| 200 | + public static readonly EffectiveFromEnumConverter Singleton = new EffectiveFromEnumConverter(); | |
| 201 | + } | |
| 202 | + | |
| 203 | + public class DateOnlyConverter : JsonConverter<DateOnly> | |
| 204 | + { | |
| 205 | + private readonly string serializationFormat; | |
| 206 | + public DateOnlyConverter() : this(null) { } | |
| 207 | + | |
| 208 | + public DateOnlyConverter(string? serializationFormat) | |
| 209 | + { | |
| 210 | + this.serializationFormat = serializationFormat ?? "yyyy-MM-dd"; | |
| 211 | + } | |
| 212 | + | |
| 213 | + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 214 | + { | |
| 215 | + var value = reader.GetString(); | |
| 216 | + return DateOnly.Parse(value!); | |
| 217 | + } | |
| 218 | + | |
| 219 | + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) | |
| 220 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 221 | + } | |
| 222 | + | |
| 223 | + public class TimeOnlyConverter : JsonConverter<TimeOnly> | |
| 224 | + { | |
| 225 | + private readonly string serializationFormat; | |
| 226 | + | |
| 227 | + public TimeOnlyConverter() : this(null) { } | |
| 228 | + | |
| 229 | + public TimeOnlyConverter(string? serializationFormat) | |
| 230 | + { | |
| 231 | + this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff"; | |
| 232 | + } | |
| 233 | + | |
| 234 | + public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 235 | + { | |
| 236 | + var value = reader.GetString(); | |
| 237 | + return TimeOnly.Parse(value!); | |
| 238 | + } | |
| 239 | + | |
| 240 | + public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) | |
| 241 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 242 | + } | |
| 243 | + | |
| 244 | + internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset> | |
| 245 | + { | |
| 246 | + public override bool CanConvert(Type t) => t == typeof(DateTimeOffset); | |
| 247 | + | |
| 248 | + private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"; | |
| 249 | + | |
| 250 | + private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind; | |
| 251 | + private string? _dateTimeFormat; | |
| 252 | + private CultureInfo? _culture; | |
| 253 | + | |
| 254 | + public DateTimeStyles DateTimeStyles | |
| 255 | + { | |
| 256 | + get => _dateTimeStyles; | |
| 257 | + set => _dateTimeStyles = value; | |
| 258 | + } | |
| 259 | + | |
| 260 | + public string? DateTimeFormat | |
| 261 | + { | |
| 262 | + get => _dateTimeFormat ?? string.Empty; | |
| 263 | + set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value; | |
| 264 | + } | |
| 265 | + | |
| 266 | + public CultureInfo Culture | |
| 267 | + { | |
| 268 | + get => _culture ?? CultureInfo.CurrentCulture; | |
| 269 | + set => _culture = value; | |
| 270 | + } | |
| 271 | + | |
| 272 | + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) | |
| 273 | + { | |
| 274 | + string text; | |
| 275 | + | |
| 276 | + | |
| 277 | + if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal | |
| 278 | + || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal) | |
| 279 | + { | |
| 280 | + value = value.ToUniversalTime(); | |
| 281 | + } | |
| 282 | + | |
| 283 | + text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture); | |
| 284 | + | |
| 285 | + writer.WriteStringValue(text); | |
| 286 | + } | |
| 287 | + | |
| 288 | + public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 289 | + { | |
| 290 | + string? dateText = reader.GetString(); | |
| 291 | + | |
| 292 | + if (string.IsNullOrEmpty(dateText) == false) | |
| 293 | + { | |
| 294 | + if (!string.IsNullOrEmpty(_dateTimeFormat)) | |
| 295 | + { | |
| 296 | + return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles); | |
| 297 | + } | |
| 298 | + else | |
| 299 | + { | |
| 300 | + return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles); | |
| 301 | + } | |
| 302 | + } | |
| 303 | + else | |
| 304 | + { | |
| 305 | + return default(DateTimeOffset); | |
| 306 | + } | |
| 307 | + } | |
| 308 | + | |
| 309 | + | |
| 310 | + public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter(); | |
| 311 | + } | |
| 312 | +} | |
| 313 | +#pragma warning restore CS8618 | |
| 314 | +#pragma warning restore CS8601 | |
| 315 | +#pragma warning restore CS8602 | |
| 316 | +#pragma warning restore CS8603 |
Acsharpdefault / QuickType.cs+206 −0
| @@ -0,0 +1,206 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | +#pragma warning disable CS8604 | |
| 14 | +#pragma warning disable CS8625 | |
| 15 | +#pragma warning disable CS8765 | |
| 16 | + | |
| 17 | +namespace QuickType | |
| 18 | +{ | |
| 19 | + using System; | |
| 20 | + using System.Collections.Generic; | |
| 21 | + | |
| 22 | + using System.Globalization; | |
| 23 | + using Newtonsoft.Json; | |
| 24 | + using Newtonsoft.Json.Converters; | |
| 25 | + | |
| 26 | + public partial class TopLevel | |
| 27 | + { | |
| 28 | + [JsonProperty("details", Required = Required.Always)] | |
| 29 | + public Uri Details { get; set; } | |
| 30 | + | |
| 31 | + [JsonProperty("rates", Required = Required.Always)] | |
| 32 | + public Rate[] Rates { get; set; } | |
| 33 | + | |
| 34 | + [JsonProperty("version", Required = Required.AllowNull)] | |
| 35 | + public object Version { get; set; } | |
| 36 | + } | |
| 37 | + | |
| 38 | + public partial class Rate | |
| 39 | + { | |
| 40 | + [JsonProperty("code", Required = Required.Always)] | |
| 41 | + public string Code { get; set; } | |
| 42 | + | |
| 43 | + [JsonProperty("country_code", Required = Required.Always)] | |
| 44 | + public string CountryCode { get; set; } | |
| 45 | + | |
| 46 | + [JsonProperty("name", Required = Required.Always)] | |
| 47 | + public string Name { get; set; } | |
| 48 | + | |
| 49 | + [JsonProperty("periods", Required = Required.Always)] | |
| 50 | + public Period[] Periods { get; set; } | |
| 51 | + } | |
| 52 | + | |
| 53 | + public partial class Period | |
| 54 | + { | |
| 55 | + [JsonProperty("effective_from", Required = Required.Always)] | |
| 56 | + public EffectiveFromUnion EffectiveFrom { get; set; } | |
| 57 | + | |
| 58 | + [JsonProperty("rates", Required = Required.Always)] | |
| 59 | + public Rates Rates { get; set; } | |
| 60 | + } | |
| 61 | + | |
| 62 | + public partial class Rates | |
| 63 | + { | |
| 64 | + [JsonProperty("parking", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 65 | + public double? Parking { get; set; } | |
| 66 | + | |
| 67 | + [JsonProperty("reduced", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 68 | + public double? Reduced { get; set; } | |
| 69 | + | |
| 70 | + [JsonProperty("reduced1", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 71 | + public double? Reduced1 { get; set; } | |
| 72 | + | |
| 73 | + [JsonProperty("reduced2", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 74 | + public double? Reduced2 { get; set; } | |
| 75 | + | |
| 76 | + [JsonProperty("standard", Required = Required.Always)] | |
| 77 | + public double Standard { get; set; } | |
| 78 | + | |
| 79 | + [JsonProperty("super_reduced", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 80 | + public double? SuperReduced { get; set; } | |
| 81 | + } | |
| 82 | + | |
| 83 | + public enum EffectiveFromEnum { The00000101 }; | |
| 84 | + | |
| 85 | + public partial struct EffectiveFromUnion | |
| 86 | + { | |
| 87 | + public DateTimeOffset? DateTime; | |
| 88 | + public EffectiveFromEnum? Enum; | |
| 89 | + | |
| 90 | + public static implicit operator EffectiveFromUnion(DateTimeOffset DateTime) => new EffectiveFromUnion { DateTime = DateTime }; | |
| 91 | + public static implicit operator EffectiveFromUnion(EffectiveFromEnum Enum) => new EffectiveFromUnion { Enum = Enum }; | |
| 92 | + } | |
| 93 | + | |
| 94 | + public partial class TopLevel | |
| 95 | + { | |
| 96 | + public static TopLevel FromJson(string json) => JsonConvert.DeserializeObject<TopLevel>(json, QuickType.Converter.Settings); | |
| 97 | + } | |
| 98 | + | |
| 99 | + public static partial class Serialize | |
| 100 | + { | |
| 101 | + public static string ToJson(this TopLevel self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 102 | + } | |
| 103 | + | |
| 104 | + internal static partial class Converter | |
| 105 | + { | |
| 106 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 107 | + { | |
| 108 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 109 | + DateParseHandling = DateParseHandling.None, | |
| 110 | + Converters = | |
| 111 | + { | |
| 112 | + EffectiveFromUnionConverter.Singleton, | |
| 113 | + EffectiveFromEnumConverter.Singleton, | |
| 114 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 115 | + }, | |
| 116 | + }; | |
| 117 | + } | |
| 118 | + | |
| 119 | + internal class EffectiveFromUnionConverter : JsonConverter | |
| 120 | + { | |
| 121 | + public override bool CanConvert(Type t) => t == typeof(EffectiveFromUnion) || t == typeof(EffectiveFromUnion?); | |
| 122 | + | |
| 123 | + public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) | |
| 124 | + { | |
| 125 | + switch (reader.TokenType) | |
| 126 | + { | |
| 127 | + case JsonToken.String: | |
| 128 | + case JsonToken.Date: | |
| 129 | + var stringValue = serializer.Deserialize<string>(reader); | |
| 130 | + DateTimeOffset dt; | |
| 131 | + if (DateTimeOffset.TryParse(stringValue, out dt)) | |
| 132 | + { | |
| 133 | + return new EffectiveFromUnion { DateTime = dt }; | |
| 134 | + } | |
| 135 | + if (stringValue == "0000-01-01") | |
| 136 | + { | |
| 137 | + return new EffectiveFromUnion { Enum = EffectiveFromEnum.The00000101 }; | |
| 138 | + } | |
| 139 | + break; | |
| 140 | + } | |
| 141 | + throw new Exception("Cannot unmarshal type EffectiveFromUnion"); | |
| 142 | + } | |
| 143 | + | |
| 144 | + public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) | |
| 145 | + { | |
| 146 | + var value = (EffectiveFromUnion)untypedValue; | |
| 147 | + if (value.DateTime != null) | |
| 148 | + { | |
| 149 | + serializer.Serialize(writer, value.DateTime.Value.ToString("o", System.Globalization.CultureInfo.InvariantCulture)); | |
| 150 | + return; | |
| 151 | + } | |
| 152 | + if (value.Enum != null) | |
| 153 | + { | |
| 154 | + if (value.Enum == EffectiveFromEnum.The00000101) | |
| 155 | + { | |
| 156 | + serializer.Serialize(writer, "0000-01-01"); | |
| 157 | + return; | |
| 158 | + } | |
| 159 | + } | |
| 160 | + throw new Exception("Cannot marshal type EffectiveFromUnion"); | |
| 161 | + } | |
| 162 | + | |
| 163 | + public static readonly EffectiveFromUnionConverter Singleton = new EffectiveFromUnionConverter(); | |
| 164 | + } | |
| 165 | + | |
| 166 | + internal class EffectiveFromEnumConverter : JsonConverter | |
| 167 | + { | |
| 168 | + public override bool CanConvert(Type t) => t == typeof(EffectiveFromEnum) || t == typeof(EffectiveFromEnum?); | |
| 169 | + | |
| 170 | + public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) | |
| 171 | + { | |
| 172 | + if (reader.TokenType == JsonToken.Null) return null; | |
| 173 | + var value = serializer.Deserialize<string>(reader); | |
| 174 | + if (value == "0000-01-01") | |
| 175 | + { | |
| 176 | + return EffectiveFromEnum.The00000101; | |
| 177 | + } | |
| 178 | + throw new Exception("Cannot unmarshal type EffectiveFromEnum"); | |
| 179 | + } | |
| 180 | + | |
| 181 | + public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) | |
| 182 | + { | |
| 183 | + if (untypedValue == null) | |
| 184 | + { | |
| 185 | + serializer.Serialize(writer, null); | |
| 186 | + return; | |
| 187 | + } | |
| 188 | + var value = (EffectiveFromEnum)untypedValue; | |
| 189 | + if (value == EffectiveFromEnum.The00000101) | |
| 190 | + { | |
| 191 | + serializer.Serialize(writer, "0000-01-01"); | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + throw new Exception("Cannot marshal type EffectiveFromEnum"); | |
| 195 | + } | |
| 196 | + | |
| 197 | + public static readonly EffectiveFromEnumConverter Singleton = new EffectiveFromEnumConverter(); | |
| 198 | + } | |
| 199 | +} | |
| 200 | +#pragma warning restore CS8618 | |
| 201 | +#pragma warning restore CS8601 | |
| 202 | +#pragma warning restore CS8602 | |
| 203 | +#pragma warning restore CS8603 | |
| 204 | +#pragma warning restore CS8604 | |
| 205 | +#pragma warning restore CS8625 | |
| 206 | +#pragma warning restore CS8765 |
Mflowdefault / TopLevel.js+10 −2
| @@ -23,10 +23,15 @@ export type Rate = { | ||
| 23 | 23 | }; |
| 24 | 24 | |
| 25 | 25 | export type Period = { |
| 26 | - effective_from: Date; | |
| 26 | + effective_from: EffectiveFromUnion; | |
| 27 | 27 | rates: Rates; |
| 28 | 28 | }; |
| 29 | 29 | |
| 30 | +export type EffectiveFromUnion = Date | EffectiveFromEnum; | |
| 31 | + | |
| 32 | +export type EffectiveFromEnum = | |
| 33 | + "0000-01-01"; | |
| 34 | + | |
| 30 | 35 | export type Rates = { |
| 31 | 36 | parking?: number; |
| 32 | 37 | reduced?: number; |
| @@ -212,7 +217,7 @@ const typeMap: any = { | ||
| 212 | 217 | { json: "periods", js: "periods", typ: a(r("Period")) }, |
| 213 | 218 | ], false), |
| 214 | 219 | "Period": o([ |
| 215 | - { json: "effective_from", js: "effective_from", typ: Date }, | |
| 220 | + { json: "effective_from", js: "effective_from", typ: u(Date, r("EffectiveFromEnum")) }, | |
| 216 | 221 | { json: "rates", js: "rates", typ: r("Rates") }, |
| 217 | 222 | ], false), |
| 218 | 223 | "Rates": o([ |
| @@ -223,6 +228,9 @@ const typeMap: any = { | ||
| 223 | 228 | { json: "standard", js: "standard", typ: 3.14 }, |
| 224 | 229 | { json: "super_reduced", js: "super_reduced", typ: u(undefined, 3.14) }, |
| 225 | 230 | ], false), |
| 231 | + "EffectiveFromEnum": [ | |
| 232 | + "0000-01-01", | |
| 233 | + ], | |
| 226 | 234 | }; |
| 227 | 235 | |
| 228 | 236 | module.exports = { |
Ajava-lombokdefault / src / main / java / io / quicktype / EffectiveFromEnum.java+22 −0
| @@ -0,0 +1,22 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import com.fasterxml.jackson.annotation.*; | |
| 5 | + | |
| 6 | +public enum EffectiveFromEnum { | |
| 7 | + THE_00000101; | |
| 8 | + | |
| 9 | + @JsonValue | |
| 10 | + public String toValue() { | |
| 11 | + switch (this) { | |
| 12 | + case THE_00000101: return "0000-01-01"; | |
| 13 | + } | |
| 14 | + return null; | |
| 15 | + } | |
| 16 | + | |
| 17 | + @JsonCreator | |
| 18 | + public static EffectiveFromEnum forValue(String value) throws IOException { | |
| 19 | + if (value.equals("0000-01-01")) return THE_00000101; | |
| 20 | + throw new IOException("Cannot deserialize EffectiveFromEnum"); | |
| 21 | + } | |
| 22 | +} |
Ajava-lombokdefault / src / main / java / io / quicktype / EffectiveFromUnion.java+55 −0
| @@ -0,0 +1,55 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.io.IOException; | |
| 5 | +import com.fasterxml.jackson.core.*; | |
| 6 | +import com.fasterxml.jackson.databind.*; | |
| 7 | +import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | +import com.fasterxml.jackson.core.type.*; | |
| 9 | +import java.time.LocalDate; | |
| 10 | + | |
| 11 | +@JsonDeserialize(using = EffectiveFromUnion.Deserializer.class) | |
| 12 | +@JsonSerialize(using = EffectiveFromUnion.Serializer.class) | |
| 13 | +public class EffectiveFromUnion { | |
| 14 | + public LocalDate dateValue; | |
| 15 | + public EffectiveFromEnum enumValue; | |
| 16 | + | |
| 17 | + static class Deserializer extends JsonDeserializer<EffectiveFromUnion> { | |
| 18 | + @Override | |
| 19 | + public EffectiveFromUnion deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 20 | + EffectiveFromUnion value = new EffectiveFromUnion(); | |
| 21 | + switch (jsonParser.currentToken()) { | |
| 22 | + case VALUE_STRING: | |
| 23 | + String string = jsonParser.readValueAs(String.class); | |
| 24 | + try { | |
| 25 | + value.dateValue = LocalDate.parse(string); | |
| 26 | + } catch (Exception ex) { | |
| 27 | + // Ignored | |
| 28 | + } | |
| 29 | + try { | |
| 30 | + value.enumValue = EffectiveFromEnum.forValue(string); | |
| 31 | + } catch (Exception ex) { | |
| 32 | + // Ignored | |
| 33 | + } | |
| 34 | + break; | |
| 35 | + default: throw new IOException("Cannot deserialize EffectiveFromUnion"); | |
| 36 | + } | |
| 37 | + return value; | |
| 38 | + } | |
| 39 | + } | |
| 40 | + | |
| 41 | + static class Serializer extends JsonSerializer<EffectiveFromUnion> { | |
| 42 | + @Override | |
| 43 | + public void serialize(EffectiveFromUnion obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 44 | + if (obj.dateValue != null) { | |
| 45 | + jsonGenerator.writeObject(obj.dateValue.format(java.time.format.DateTimeFormatter.ISO_DATE)); | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + if (obj.enumValue != null) { | |
| 49 | + jsonGenerator.writeObject(obj.enumValue); | |
| 50 | + return; | |
| 51 | + } | |
| 52 | + throw new IOException("EffectiveFromUnion must not be null"); | |
| 53 | + } | |
| 54 | + } | |
| 55 | +} |
Mjava-lombokdefault / src / main / java / io / quicktype / Period.java+3 −3
| @@ -4,13 +4,13 @@ import com.fasterxml.jackson.annotation.*; | ||
| 4 | 4 | import java.time.LocalDate; |
| 5 | 5 | |
| 6 | 6 | public class Period { |
| 7 | - private LocalDate effectiveFrom; | |
| 7 | + private EffectiveFromUnion effectiveFrom; | |
| 8 | 8 | private Rates rates; |
| 9 | 9 | |
| 10 | 10 | @JsonProperty("effective_from") |
| 11 | - public LocalDate getEffectiveFrom() { return effectiveFrom; } | |
| 11 | + public EffectiveFromUnion getEffectiveFrom() { return effectiveFrom; } | |
| 12 | 12 | @JsonProperty("effective_from") |
| 13 | - public void setEffectiveFrom(LocalDate value) { this.effectiveFrom = value; } | |
| 13 | + public void setEffectiveFrom(EffectiveFromUnion value) { this.effectiveFrom = value; } | |
| 14 | 14 | |
| 15 | 15 | @JsonProperty("rates") |
| 16 | 16 | public Rates getRates() { return rates; } |
Ajavadefault / src / main / java / io / quicktype / EffectiveFromEnum.java+22 −0
| @@ -0,0 +1,22 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import com.fasterxml.jackson.annotation.*; | |
| 5 | + | |
| 6 | +public enum EffectiveFromEnum { | |
| 7 | + THE_00000101; | |
| 8 | + | |
| 9 | + @JsonValue | |
| 10 | + public String toValue() { | |
| 11 | + switch (this) { | |
| 12 | + case THE_00000101: return "0000-01-01"; | |
| 13 | + } | |
| 14 | + return null; | |
| 15 | + } | |
| 16 | + | |
| 17 | + @JsonCreator | |
| 18 | + public static EffectiveFromEnum forValue(String value) throws IOException { | |
| 19 | + if (value.equals("0000-01-01")) return THE_00000101; | |
| 20 | + throw new IOException("Cannot deserialize EffectiveFromEnum"); | |
| 21 | + } | |
| 22 | +} |
Ajavadefault / src / main / java / io / quicktype / EffectiveFromUnion.java+55 −0
| @@ -0,0 +1,55 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.io.IOException; | |
| 5 | +import com.fasterxml.jackson.core.*; | |
| 6 | +import com.fasterxml.jackson.databind.*; | |
| 7 | +import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | +import com.fasterxml.jackson.core.type.*; | |
| 9 | +import java.time.LocalDate; | |
| 10 | + | |
| 11 | +@JsonDeserialize(using = EffectiveFromUnion.Deserializer.class) | |
| 12 | +@JsonSerialize(using = EffectiveFromUnion.Serializer.class) | |
| 13 | +public class EffectiveFromUnion { | |
| 14 | + public LocalDate dateValue; | |
| 15 | + public EffectiveFromEnum enumValue; | |
| 16 | + | |
| 17 | + static class Deserializer extends JsonDeserializer<EffectiveFromUnion> { | |
| 18 | + @Override | |
| 19 | + public EffectiveFromUnion deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 20 | + EffectiveFromUnion value = new EffectiveFromUnion(); | |
| 21 | + switch (jsonParser.currentToken()) { | |
| 22 | + case VALUE_STRING: | |
| 23 | + String string = jsonParser.readValueAs(String.class); | |
| 24 | + try { | |
| 25 | + value.dateValue = LocalDate.parse(string); | |
| 26 | + } catch (Exception ex) { | |
| 27 | + // Ignored | |
| 28 | + } | |
| 29 | + try { | |
| 30 | + value.enumValue = EffectiveFromEnum.forValue(string); | |
| 31 | + } catch (Exception ex) { | |
| 32 | + // Ignored | |
| 33 | + } | |
| 34 | + break; | |
| 35 | + default: throw new IOException("Cannot deserialize EffectiveFromUnion"); | |
| 36 | + } | |
| 37 | + return value; | |
| 38 | + } | |
| 39 | + } | |
| 40 | + | |
| 41 | + static class Serializer extends JsonSerializer<EffectiveFromUnion> { | |
| 42 | + @Override | |
| 43 | + public void serialize(EffectiveFromUnion obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 44 | + if (obj.dateValue != null) { | |
| 45 | + jsonGenerator.writeObject(obj.dateValue.format(java.time.format.DateTimeFormatter.ISO_DATE)); | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + if (obj.enumValue != null) { | |
| 49 | + jsonGenerator.writeObject(obj.enumValue); | |
| 50 | + return; | |
| 51 | + } | |
| 52 | + throw new IOException("EffectiveFromUnion must not be null"); | |
| 53 | + } | |
| 54 | + } | |
| 55 | +} |
Mjavadefault / src / main / java / io / quicktype / Period.java+3 −3
| @@ -4,13 +4,13 @@ import com.fasterxml.jackson.annotation.*; | ||
| 4 | 4 | import java.time.LocalDate; |
| 5 | 5 | |
| 6 | 6 | public class Period { |
| 7 | - private LocalDate effectiveFrom; | |
| 7 | + private EffectiveFromUnion effectiveFrom; | |
| 8 | 8 | private Rates rates; |
| 9 | 9 | |
| 10 | 10 | @JsonProperty("effective_from") |
| 11 | - public LocalDate getEffectiveFrom() { return effectiveFrom; } | |
| 11 | + public EffectiveFromUnion getEffectiveFrom() { return effectiveFrom; } | |
| 12 | 12 | @JsonProperty("effective_from") |
| 13 | - public void setEffectiveFrom(LocalDate value) { this.effectiveFrom = value; } | |
| 13 | + public void setEffectiveFrom(EffectiveFromUnion value) { this.effectiveFrom = value; } | |
| 14 | 14 | |
| 15 | 15 | @JsonProperty("rates") |
| 16 | 16 | public Rates getRates() { return rates; } |
Mjavascriptdefault / TopLevel.js+4 −1
| @@ -183,7 +183,7 @@ const typeMap = { | ||
| 183 | 183 | { json: "periods", js: "periods", typ: a(r("Period")) }, |
| 184 | 184 | ], false), |
| 185 | 185 | "Period": o([ |
| 186 | - { json: "effective_from", js: "effective_from", typ: Date }, | |
| 186 | + { json: "effective_from", js: "effective_from", typ: u(Date, r("EffectiveFromEnum")) }, | |
| 187 | 187 | { json: "rates", js: "rates", typ: r("Rates") }, |
| 188 | 188 | ], false), |
| 189 | 189 | "Rates": o([ |
| @@ -194,6 +194,9 @@ const typeMap = { | ||
| 194 | 194 | { json: "standard", js: "standard", typ: 3.14 }, |
| 195 | 195 | { json: "super_reduced", js: "super_reduced", typ: u(undefined, 3.14) }, |
| 196 | 196 | ], false), |
| 197 | + "EffectiveFromEnum": [ | |
| 198 | + "0000-01-01", | |
| 199 | + ], | |
| 197 | 200 | }; |
| 198 | 201 | |
| 199 | 202 | module.exports = { |
Mkotlin-jacksondefault / TopLevel.kt+32 −2
| @@ -29,7 +29,9 @@ private fun <T> ObjectMapper.convert(k: kotlin.reflect.KClass<*>, fromJson: (Jso | ||
| 29 | 29 | val mapper = jacksonObjectMapper().apply { |
| 30 | 30 | propertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE |
| 31 | 31 | setSerializationInclusion(JsonInclude.Include.NON_NULL) |
| 32 | - convert(LocalDate::class, { LocalDate.parse(it.asText()) }, { "\"${java.time.format.DateTimeFormatter.ISO_LOCAL_DATE.format(it)}\"" }) | |
| 32 | + convert(LocalDate::class, { LocalDate.parse(it.asText()) }, { "\"${java.time.format.DateTimeFormatter.ISO_LOCAL_DATE.format(it)}\"" }) | |
| 33 | + convert(EffectiveFromEnum::class, { EffectiveFromEnum.fromValue(it.asText()) }, { "\"${it.value}\"" }) | |
| 34 | + convert(EffectiveFromUnion::class, { EffectiveFromUnion.fromJson(it) }, { it.toJson() }, true) | |
| 33 | 35 | } |
| 34 | 36 | |
| 35 | 37 | data class TopLevel ( |
| @@ -64,12 +66,40 @@ data class Rate ( | ||
| 64 | 66 | |
| 65 | 67 | data class Period ( |
| 66 | 68 | @get:JsonProperty("effective_from", required=true)@field:JsonProperty("effective_from", required=true) |
| 67 | - val effectiveFrom: LocalDate, | |
| 69 | + val effectiveFrom: EffectiveFromUnion, | |
| 68 | 70 | |
| 69 | 71 | @get:JsonProperty(required=true)@field:JsonProperty(required=true) |
| 70 | 72 | val rates: Rates |
| 71 | 73 | ) |
| 72 | 74 | |
| 75 | +sealed class EffectiveFromUnion { | |
| 76 | + class DateValue(val value: LocalDate) : EffectiveFromUnion() | |
| 77 | + class EnumValue(val value: EffectiveFromEnum) : EffectiveFromUnion() | |
| 78 | + | |
| 79 | + fun toJson(): String = mapper.writeValueAsString(when (this) { | |
| 80 | + is DateValue -> this.value | |
| 81 | + is EnumValue -> this.value | |
| 82 | + }) | |
| 83 | + | |
| 84 | + companion object { | |
| 85 | + fun fromJson(jn: JsonNode): EffectiveFromUnion = when (jn) { | |
| 86 | + is TextNode -> try { DateValue(mapper.treeToValue(jn)) } catch (e: Exception) { EnumValue(mapper.treeToValue(jn)) } | |
| 87 | + else -> throw IllegalArgumentException() | |
| 88 | + } | |
| 89 | + } | |
| 90 | +} | |
| 91 | + | |
| 92 | +enum class EffectiveFromEnum(val value: String) { | |
| 93 | + The00000101("0000-01-01"); | |
| 94 | + | |
| 95 | + companion object { | |
| 96 | + fun fromValue(value: String): EffectiveFromEnum = when (value) { | |
| 97 | + "0000-01-01" -> The00000101 | |
| 98 | + else -> throw IllegalArgumentException() | |
| 99 | + } | |
| 100 | + } | |
| 101 | +} | |
| 102 | + | |
| 73 | 103 | data class Rates ( |
| 74 | 104 | val parking: Double? = null, |
| 75 | 105 | val reduced: Double? = null, |
Mkotlindefault / TopLevel.kt+32 −2
| @@ -17,7 +17,9 @@ private fun <T> Klaxon.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonValue | ||
| 17 | 17 | }) |
| 18 | 18 | |
| 19 | 19 | private val klaxon = Klaxon() |
| 20 | - .convert(LocalDate::class, { LocalDate.parse(it.string!!) }, { "\"${java.time.format.DateTimeFormatter.ISO_LOCAL_DATE.format(it)}\"" }) | |
| 20 | + .convert(LocalDate::class, { LocalDate.parse(it.string!!) }, { "\"${java.time.format.DateTimeFormatter.ISO_LOCAL_DATE.format(it)}\"" }) | |
| 21 | + .convert(EffectiveFromEnum::class, { EffectiveFromEnum.fromValue(it.string!!) }, { "\"${it.value}\"" }) | |
| 22 | + .convert(EffectiveFromUnion::class, { EffectiveFromUnion.fromJson(it) }, { it.toJson() }, true) | |
| 21 | 23 | |
| 22 | 24 | data class TopLevel ( |
| 23 | 25 | val details: String, |
| @@ -43,11 +45,39 @@ data class Rate ( | ||
| 43 | 45 | |
| 44 | 46 | data class Period ( |
| 45 | 47 | @Json(name = "effective_from") |
| 46 | - val effectiveFrom: LocalDate, | |
| 48 | + val effectiveFrom: EffectiveFromUnion, | |
| 47 | 49 | |
| 48 | 50 | val rates: Rates |
| 49 | 51 | ) |
| 50 | 52 | |
| 53 | +sealed class EffectiveFromUnion { | |
| 54 | + class DateValue(val value: LocalDate) : EffectiveFromUnion() | |
| 55 | + class EnumValue(val value: EffectiveFromEnum) : EffectiveFromUnion() | |
| 56 | + | |
| 57 | + public fun toJson(): String = klaxon.toJsonString(when (this) { | |
| 58 | + is DateValue -> this.value | |
| 59 | + is EnumValue -> this.value | |
| 60 | + }) | |
| 61 | + | |
| 62 | + companion object { | |
| 63 | + public fun fromJson(jv: JsonValue): EffectiveFromUnion = when (jv.inside) { | |
| 64 | + is String -> try { DateValue(jv.string?.let { LocalDate.parse(it) }!!) } catch (e: Exception) { EnumValue(jv.string?.let { EffectiveFromEnum.fromValue(it) }!!) } | |
| 65 | + else -> throw IllegalArgumentException() | |
| 66 | + } | |
| 67 | + } | |
| 68 | +} | |
| 69 | + | |
| 70 | +enum class EffectiveFromEnum(val value: String) { | |
| 71 | + The00000101("0000-01-01"); | |
| 72 | + | |
| 73 | + companion object { | |
| 74 | + public fun fromValue(value: String): EffectiveFromEnum = when (value) { | |
| 75 | + "0000-01-01" -> The00000101 | |
| 76 | + else -> throw IllegalArgumentException() | |
| 77 | + } | |
| 78 | + } | |
| 79 | +} | |
| 80 | + | |
| 51 | 81 | data class Rates ( |
| 52 | 82 | val parking: Double? = null, |
| 53 | 83 | val reduced: Double? = null, |
Mkotlinxdefault / TopLevel.kt+12 −1
| @@ -35,11 +35,22 @@ data class Rate ( | ||
| 35 | 35 | @Serializable |
| 36 | 36 | data class Period ( |
| 37 | 37 | @SerialName("effective_from") |
| 38 | - val effectiveFrom: LocalDate, | |
| 38 | + val effectiveFrom: EffectiveFromUnion, | |
| 39 | 39 | |
| 40 | 40 | val rates: Rates |
| 41 | 41 | ) |
| 42 | 42 | |
| 43 | +@Serializable | |
| 44 | +sealed class EffectiveFromUnion { | |
| 45 | + class DateValue(val value: LocalDate) : EffectiveFromUnion() | |
| 46 | + class EnumValue(val value: EffectiveFromEnum) : EffectiveFromUnion() | |
| 47 | +} | |
| 48 | + | |
| 49 | +@Serializable | |
| 50 | +enum class EffectiveFromEnum(val value: String) { | |
| 51 | + @SerialName("0000-01-01") The00000101("0000-01-01"); | |
| 52 | +} | |
| 53 | + | |
| 43 | 54 | @Serializable |
| 44 | 55 | data class Rates ( |
| 45 | 56 | val parking: Double? = null, |
Apythondefault / quicktype.py+173 −0
| @@ -0,0 +1,173 @@ | ||
| 1 | +import datetime | |
| 2 | +import re | |
| 3 | +from enum import Enum | |
| 4 | +from dataclasses import dataclass | |
| 5 | +from typing import Any, TypeVar, Type, cast, Callable | |
| 6 | +import dateutil.parser | |
| 7 | + | |
| 8 | + | |
| 9 | +T = TypeVar("T") | |
| 10 | +EnumT = TypeVar("EnumT", bound=Enum) | |
| 11 | + | |
| 12 | + | |
| 13 | +def from_float(x: Any) -> float: | |
| 14 | + assert isinstance(x, (float, int)) and not isinstance(x, bool) | |
| 15 | + return float(x) | |
| 16 | + | |
| 17 | + | |
| 18 | +def from_none(x: Any) -> Any: | |
| 19 | + assert x is None | |
| 20 | + return x | |
| 21 | + | |
| 22 | + | |
| 23 | +def from_union(fs, x): | |
| 24 | + for f in fs: | |
| 25 | + try: | |
| 26 | + return f(x) | |
| 27 | + except: | |
| 28 | + pass | |
| 29 | + assert False | |
| 30 | + | |
| 31 | + | |
| 32 | +def to_float(x: Any) -> float: | |
| 33 | + assert isinstance(x, (int, float)) | |
| 34 | + return x | |
| 35 | + | |
| 36 | + | |
| 37 | +def from_date(x: Any) -> datetime.date: | |
| 38 | + assert isinstance(x, str) and re.match(r"^\d{4}-\d{2}-\d{2}$", x) | |
| 39 | + return dateutil.parser.parse(x).date() | |
| 40 | + | |
| 41 | + | |
| 42 | +def to_enum(c: Type[EnumT], x: Any) -> EnumT: | |
| 43 | + assert isinstance(x, c) | |
| 44 | + return x.value | |
| 45 | + | |
| 46 | + | |
| 47 | +def to_class(c: Type[T], x: Any) -> dict: | |
| 48 | + assert isinstance(x, c) | |
| 49 | + return cast(Any, x).to_dict() | |
| 50 | + | |
| 51 | + | |
| 52 | +def from_str(x: Any) -> str: | |
| 53 | + assert isinstance(x, str) | |
| 54 | + return x | |
| 55 | + | |
| 56 | + | |
| 57 | +def from_list(f: Callable[[Any], T], x: Any) -> list[T]: | |
| 58 | + assert isinstance(x, list) | |
| 59 | + return [f(y) for y in x] | |
| 60 | + | |
| 61 | + | |
| 62 | +class EffectiveFromEnum(Enum): | |
| 63 | + THE_00000101 = "0000-01-01" | |
| 64 | + | |
| 65 | + | |
| 66 | +@dataclass | |
| 67 | +class Rates: | |
| 68 | + standard: float | |
| 69 | + parking: float | None | |
| 70 | + reduced: float | None | |
| 71 | + reduced1: float | None | |
| 72 | + reduced2: float | None | |
| 73 | + super_reduced: float | None | |
| 74 | + | |
| 75 | + @staticmethod | |
| 76 | + def from_dict(obj: Any) -> 'Rates': | |
| 77 | + assert isinstance(obj, dict) | |
| 78 | + standard = from_float(obj.get("standard")) | |
| 79 | + parking = from_union([from_float, from_none], obj.get("parking")) | |
| 80 | + reduced = from_union([from_float, from_none], obj.get("reduced")) | |
| 81 | + reduced1 = from_union([from_float, from_none], obj.get("reduced1")) | |
| 82 | + reduced2 = from_union([from_float, from_none], obj.get("reduced2")) | |
| 83 | + super_reduced = from_union([from_float, from_none], obj.get("super_reduced")) | |
| 84 | + return Rates(standard, parking, reduced, reduced1, reduced2, super_reduced) | |
| 85 | + | |
| 86 | + def to_dict(self) -> dict: | |
| 87 | + result: dict = {} | |
| 88 | + result["standard"] = to_float(self.standard) | |
| 89 | + if self.parking is not None: | |
| 90 | + result["parking"] = from_union([to_float, from_none], self.parking) | |
| 91 | + if self.reduced is not None: | |
| 92 | + result["reduced"] = from_union([to_float, from_none], self.reduced) | |
| 93 | + if self.reduced1 is not None: | |
| 94 | + result["reduced1"] = from_union([to_float, from_none], self.reduced1) | |
| 95 | + if self.reduced2 is not None: | |
| 96 | + result["reduced2"] = from_union([to_float, from_none], self.reduced2) | |
| 97 | + if self.super_reduced is not None: | |
| 98 | + result["super_reduced"] = from_union([to_float, from_none], self.super_reduced) | |
| 99 | + return result | |
| 100 | + | |
| 101 | + | |
| 102 | +@dataclass | |
| 103 | +class Period: | |
| 104 | + effective_from: datetime.date | EffectiveFromEnum | |
| 105 | + rates: Rates | |
| 106 | + | |
| 107 | + @staticmethod | |
| 108 | + def from_dict(obj: Any) -> 'Period': | |
| 109 | + assert isinstance(obj, dict) | |
| 110 | + effective_from = from_union([from_date, EffectiveFromEnum], obj.get("effective_from")) | |
| 111 | + rates = Rates.from_dict(obj.get("rates")) | |
| 112 | + return Period(effective_from, rates) | |
| 113 | + | |
| 114 | + def to_dict(self) -> dict: | |
| 115 | + result: dict = {} | |
| 116 | + result["effective_from"] = from_union([lambda x: x.isoformat(), lambda x: to_enum(EffectiveFromEnum, x)], self.effective_from) | |
| 117 | + result["rates"] = to_class(Rates, self.rates) | |
| 118 | + return result | |
| 119 | + | |
| 120 | + | |
| 121 | +@dataclass | |
| 122 | +class Rate: | |
| 123 | + code: str | |
| 124 | + country_code: str | |
| 125 | + name: str | |
| 126 | + periods: list[Period] | |
| 127 | + | |
| 128 | + @staticmethod | |
| 129 | + def from_dict(obj: Any) -> 'Rate': | |
| 130 | + assert isinstance(obj, dict) | |
| 131 | + code = from_str(obj.get("code")) | |
| 132 | + country_code = from_str(obj.get("country_code")) | |
| 133 | + name = from_str(obj.get("name")) | |
| 134 | + periods = from_list(Period.from_dict, obj.get("periods")) | |
| 135 | + return Rate(code, country_code, name, periods) | |
| 136 | + | |
| 137 | + def to_dict(self) -> dict: | |
| 138 | + result: dict = {} | |
| 139 | + result["code"] = from_str(self.code) | |
| 140 | + result["country_code"] = from_str(self.country_code) | |
| 141 | + result["name"] = from_str(self.name) | |
| 142 | + result["periods"] = from_list(lambda x: to_class(Period, x), self.periods) | |
| 143 | + return result | |
| 144 | + | |
| 145 | + | |
| 146 | +@dataclass | |
| 147 | +class TopLevel: | |
| 148 | + details: str | |
| 149 | + rates: list[Rate] | |
| 150 | + version: None | |
| 151 | + | |
| 152 | + @staticmethod | |
| 153 | + def from_dict(obj: Any) -> 'TopLevel': | |
| 154 | + assert isinstance(obj, dict) | |
| 155 | + details = from_str(obj.get("details")) | |
| 156 | + rates = from_list(Rate.from_dict, obj.get("rates")) | |
| 157 | + version = from_none(obj.get("version")) | |
| 158 | + return TopLevel(details, rates, version) | |
| 159 | + | |
| 160 | + def to_dict(self) -> dict: | |
| 161 | + result: dict = {} | |
| 162 | + result["details"] = from_str(self.details) | |
| 163 | + result["rates"] = from_list(lambda x: to_class(Rate, x), self.rates) | |
| 164 | + result["version"] = from_none(self.version) | |
| 165 | + return result | |
| 166 | + | |
| 167 | + | |
| 168 | +def top_level_from_dict(s: Any) -> TopLevel: | |
| 169 | + return TopLevel.from_dict(s) | |
| 170 | + | |
| 171 | + | |
| 172 | +def top_level_to_dict(x: TopLevel) -> Any: | |
| 173 | + return to_class(TopLevel, x) |
Mtypescriptdefault / TopLevel.ts+9 −2
| @@ -21,10 +21,14 @@ export interface Rate { | ||
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | export interface Period { |
| 24 | - effective_from: Date; | |
| 24 | + effective_from: EffectiveFromUnion; | |
| 25 | 25 | rates: Rates; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | +export type EffectiveFromUnion = Date | EffectiveFromEnum; | |
| 29 | + | |
| 30 | +export type EffectiveFromEnum = "0000-01-01"; | |
| 31 | + | |
| 28 | 32 | export interface Rates { |
| 29 | 33 | parking?: number; |
| 30 | 34 | reduced?: number; |
| @@ -212,7 +216,7 @@ const typeMap: any = { | ||
| 212 | 216 | { json: "periods", js: "periods", typ: a(r("Period")) }, |
| 213 | 217 | ], false), |
| 214 | 218 | "Period": o([ |
| 215 | - { json: "effective_from", js: "effective_from", typ: Date }, | |
| 219 | + { json: "effective_from", js: "effective_from", typ: u(Date, r("EffectiveFromEnum")) }, | |
| 216 | 220 | { json: "rates", js: "rates", typ: r("Rates") }, |
| 217 | 221 | ], false), |
| 218 | 222 | "Rates": o([ |
| @@ -223,4 +227,7 @@ const typeMap: any = { | ||
| 223 | 227 | { json: "standard", js: "standard", typ: 3.14 }, |
| 224 | 228 | { json: "super_reduced", js: "super_reduced", typ: u(undefined, 3.14) }, |
| 225 | 229 | ], false), |
| 230 | + "EffectiveFromEnum": [ | |
| 231 | + "0000-01-01", | |
| 232 | + ], | |
| 226 | 233 | }; |
No generated files match these filters.