using System; using System.Collections.Generic; using System.Text; // // // To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: // // using QuickType; // // var welcome = Welcome.FromJson(jsonString); namespace TyphoonApi { using System; using System.Collections.Generic; using System.Globalization; using Newtonsoft.Json; using Newtonsoft.Json.Converters; public partial class Welcome { [JsonProperty("?xml")] public Xml Xml { get; set; } [JsonProperty("alert")] public Alert Alert { get; set; } } public partial class Alert { [JsonProperty("@xmlns")] public string Xmlns { get; set; } [JsonProperty("identifier")] public string Identifier { get; set; } [JsonProperty("sender")] public string Sender { get; set; } [JsonProperty("sent")] public DateTimeOffset Sent { get; set; } [JsonProperty("status")] public string Status { get; set; } [JsonProperty("msgType")] public string MsgType { get; set; } [JsonProperty("scope")] public string Scope { get; set; } [JsonProperty("references")] public string References { get; set; } [JsonProperty("info")] public Info Info { get; set; } } public partial class Info { [JsonProperty("language")] public string Language { get; set; } [JsonProperty("category")] public string Category { get; set; } [JsonProperty("event")] public string Event { get; set; } [JsonProperty("urgency")] public string Urgency { get; set; } [JsonProperty("severity")] public string Severity { get; set; } [JsonProperty("certainty")] public string Certainty { get; set; } [JsonProperty("eventCode")] public EventCode EventCode { get; set; } [JsonProperty("effective")] public DateTimeOffset Effective { get; set; } [JsonProperty("onset")] public DateTimeOffset Onset { get; set; } [JsonProperty("expires")] public DateTimeOffset Expires { get; set; } [JsonProperty("senderName")] public string SenderName { get; set; } [JsonProperty("headline")] public string Headline { get; set; } [JsonProperty("description")] public string Description { get; set; } [JsonProperty("instruction")] public string Instruction { get; set; } [JsonProperty("web")] public Uri Web { get; set; } [JsonProperty("parameter")] public EventCode Parameter { get; set; } [JsonProperty("area")] public List Area { get; set; } } public partial class Area { [JsonProperty("areaDesc")] public string AreaDesc { get; set; } [JsonProperty("geocode")] public EventCode Geocode { get; set; } } public partial class EventCode { [JsonProperty("valueName")] public string ValueName { get; set; } [JsonProperty("value")] public string Value { get; set; } } public partial class Xml { [JsonProperty("@version")] public string Version { get; set; } [JsonProperty("@encoding")] public string Encoding { get; set; } } public enum ValueName { AlertTitle, ProfileCapTwpEvent10, TaiwanGeocode103 }; public partial class Welcome { public static Welcome FromJson(string json) => JsonConvert.DeserializeObject(json, QuickType.Converter.Settings); } public static class Serialize { public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); } internal static class Converter { public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore, DateParseHandling = DateParseHandling.None, Converters = { ValueNameConverter.Singleton, new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } }, }; } internal class ValueNameConverter : JsonConverter { public override bool CanConvert(Type t) => t == typeof(ValueName) || t == typeof(ValueName?); public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) return null; var value = serializer.Deserialize(reader); switch (value) { case "Taiwan_Geocode_103": return ValueName.TaiwanGeocode103; case "alert_title": return ValueName.AlertTitle; case "profile:CAP-TWP:Event:1.0": return ValueName.ProfileCapTwpEvent10; } throw new Exception("Cannot unmarshal type ValueName"); } public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) { if (untypedValue == null) { serializer.Serialize(writer, null); return; } var value = (ValueName)untypedValue; switch (value) { case ValueName.TaiwanGeocode103: serializer.Serialize(writer, "Taiwan_Geocode_103"); return; case ValueName.AlertTitle: serializer.Serialize(writer, "alert_title"); return; case ValueName.ProfileCapTwpEvent10: serializer.Serialize(writer, "profile:CAP-TWP:Event:1.0"); return; } throw new Exception("Cannot marshal type ValueName"); } public static readonly ValueNameConverter Singleton = new ValueNameConverter(); } }