Test case
31 generated files · +2,799 −0test/inputs/schema/enum-of-enums-null.schema
Aschema-cplusplusdefault / quicktype.hpp+153 −0
| @@ -0,0 +1,153 @@ | ||
| 1 | +// To parse this JSON data, first install | |
| 2 | +// | |
| 3 | +// json.hpp https://github.com/nlohmann/json | |
| 4 | +// | |
| 5 | +// Then include this file, and then do | |
| 6 | +// | |
| 7 | +// TopLevel data = nlohmann::json::parse(jsonString); | |
| 8 | + | |
| 9 | +#pragma once | |
| 10 | + | |
| 11 | +#include "json.hpp" | |
| 12 | + | |
| 13 | +#include <optional> | |
| 14 | +#include <stdexcept> | |
| 15 | +#include <regex> | |
| 16 | + | |
| 17 | +#ifndef NLOHMANN_OPT_HELPER | |
| 18 | +#define NLOHMANN_OPT_HELPER | |
| 19 | +namespace nlohmann { | |
| 20 | + template <typename T> | |
| 21 | + struct adl_serializer<std::shared_ptr<T>> { | |
| 22 | + static void to_json(json & j, const std::shared_ptr<T> & opt) { | |
| 23 | + if (!opt) j = nullptr; else j = *opt; | |
| 24 | + } | |
| 25 | + | |
| 26 | + static std::shared_ptr<T> from_json(const json & j) { | |
| 27 | + if (j.is_null()) return std::shared_ptr<T>(); else return std::make_shared<T>(j.get<T>()); | |
| 28 | + } | |
| 29 | + }; | |
| 30 | + template <typename T> | |
| 31 | + struct adl_serializer<std::optional<T>> { | |
| 32 | + static void to_json(json & j, const std::optional<T> & opt) { | |
| 33 | + if (!opt) j = nullptr; else j = *opt; | |
| 34 | + } | |
| 35 | + | |
| 36 | + static std::optional<T> from_json(const json & j) { | |
| 37 | + if (j.is_null()) return std::optional<T>(); else return std::make_optional<T>(j.get<T>()); | |
| 38 | + } | |
| 39 | + }; | |
| 40 | +} | |
| 41 | +#endif | |
| 42 | + | |
| 43 | +namespace quicktype { | |
| 44 | + using nlohmann::json; | |
| 45 | + | |
| 46 | + #ifndef NLOHMANN_UNTYPED_quicktype_HELPER | |
| 47 | + #define NLOHMANN_UNTYPED_quicktype_HELPER | |
| 48 | + inline json get_untyped(const json & j, const char * property) { | |
| 49 | + if (j.find(property) != j.end()) { | |
| 50 | + return j.at(property).get<json>(); | |
| 51 | + } | |
| 52 | + return json(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + inline json get_untyped(const json & j, std::string property) { | |
| 56 | + return get_untyped(j, property.data()); | |
| 57 | + } | |
| 58 | + #endif | |
| 59 | + | |
| 60 | + #ifndef NLOHMANN_OPTIONAL_quicktype_HELPER | |
| 61 | + #define NLOHMANN_OPTIONAL_quicktype_HELPER | |
| 62 | + template <typename T> | |
| 63 | + inline std::shared_ptr<T> get_heap_optional(const json & j, const char * property) { | |
| 64 | + auto it = j.find(property); | |
| 65 | + if (it != j.end() && !it->is_null()) { | |
| 66 | + return j.at(property).get<std::shared_ptr<T>>(); | |
| 67 | + } | |
| 68 | + return std::shared_ptr<T>(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + template <typename T> | |
| 72 | + inline std::shared_ptr<T> get_heap_optional(const json & j, std::string property) { | |
| 73 | + return get_heap_optional<T>(j, property.data()); | |
| 74 | + } | |
| 75 | + template <typename T> | |
| 76 | + inline std::optional<T> get_stack_optional(const json & j, const char * property) { | |
| 77 | + auto it = j.find(property); | |
| 78 | + if (it != j.end() && !it->is_null()) { | |
| 79 | + return j.at(property).get<std::optional<T>>(); | |
| 80 | + } | |
| 81 | + return std::optional<T>(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + template <typename T> | |
| 85 | + inline std::optional<T> get_stack_optional(const json & j, std::string property) { | |
| 86 | + return get_stack_optional<T>(j, property.data()); | |
| 87 | + } | |
| 88 | + #endif | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * The format of the data. | |
| 92 | + * | |
| 93 | + * Turtle format | |
| 94 | + * | |
| 95 | + * RDF XML format | |
| 96 | + * | |
| 97 | + * N-Triples format | |
| 98 | + * | |
| 99 | + * N-Quads format | |
| 100 | + */ | |
| 101 | + enum class DataFormat : int { TURTLE, RDF_XML, N_TRIPLES, N_QUADS }; | |
| 102 | + | |
| 103 | + class TopLevel { | |
| 104 | + public: | |
| 105 | + TopLevel() = default; | |
| 106 | + virtual ~TopLevel() = default; | |
| 107 | + | |
| 108 | + private: | |
| 109 | + std::optional<DataFormat> format; | |
| 110 | + | |
| 111 | + public: | |
| 112 | + /** | |
| 113 | + * The format of the data. | |
| 114 | + */ | |
| 115 | + std::optional<DataFormat> get_format() const { return format; } | |
| 116 | + void set_format(std::optional<DataFormat> value) { this->format = value; } | |
| 117 | + }; | |
| 118 | +} | |
| 119 | + | |
| 120 | +namespace quicktype { | |
| 121 | + void from_json(const json & j, TopLevel & x); | |
| 122 | + void to_json(json & j, const TopLevel & x); | |
| 123 | + | |
| 124 | + void from_json(const json & j, DataFormat & x); | |
| 125 | + void to_json(json & j, const DataFormat & x); | |
| 126 | + | |
| 127 | + inline void from_json(const json & j, TopLevel& x) { | |
| 128 | + x.set_format(get_stack_optional<DataFormat>(j, "format")); | |
| 129 | + } | |
| 130 | + | |
| 131 | + inline void to_json(json & j, const TopLevel & x) { | |
| 132 | + j = json::object(); | |
| 133 | + j["format"] = x.get_format(); | |
| 134 | + } | |
| 135 | + | |
| 136 | + inline void from_json(const json & j, DataFormat & x) { | |
| 137 | + if (j == "turtle") x = DataFormat::TURTLE; | |
| 138 | + else if (j == "rdf_xml") x = DataFormat::RDF_XML; | |
| 139 | + else if (j == "n_triples") x = DataFormat::N_TRIPLES; | |
| 140 | + else if (j == "n_quads") x = DataFormat::N_QUADS; | |
| 141 | + else { throw std::runtime_error("Cannot deserialize to enumeration \"DataFormat\""); } | |
| 142 | + } | |
| 143 | + | |
| 144 | + inline void to_json(json & j, const DataFormat & x) { | |
| 145 | + switch (x) { | |
| 146 | + case DataFormat::TURTLE: j = "turtle"; break; | |
| 147 | + case DataFormat::RDF_XML: j = "rdf_xml"; break; | |
| 148 | + case DataFormat::N_TRIPLES: j = "n_triples"; break; | |
| 149 | + case DataFormat::N_QUADS: j = "n_quads"; break; | |
| 150 | + default: throw std::runtime_error("Unexpected value in enumeration \"DataFormat\": " + std::to_string(static_cast<int>(x))); | |
| 151 | + } | |
| 152 | + } | |
| 153 | +} |
Aschema-csharp-recordsdefault / QuickType.cs+129 −0
| @@ -0,0 +1,129 @@ | ||
| 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 | + /// <summary> | |
| 29 | + /// The format of the data. | |
| 30 | + /// </summary> | |
| 31 | + [JsonProperty("format", Required = Required.AllowNull)] | |
| 32 | + public DataFormat? Format { get; set; } | |
| 33 | + } | |
| 34 | + | |
| 35 | + /// <summary> | |
| 36 | + /// The format of the data. | |
| 37 | + /// | |
| 38 | + /// Turtle format | |
| 39 | + /// | |
| 40 | + /// RDF XML format | |
| 41 | + /// | |
| 42 | + /// N-Triples format | |
| 43 | + /// | |
| 44 | + /// N-Quads format | |
| 45 | + /// </summary> | |
| 46 | + public enum DataFormat { Turtle, RdfXml, NTriples, NQuads }; | |
| 47 | + | |
| 48 | + public partial record TopLevel | |
| 49 | + { | |
| 50 | + public static TopLevel FromJson(string json) => JsonConvert.DeserializeObject<TopLevel>(json, QuickType.Converter.Settings); | |
| 51 | + } | |
| 52 | + | |
| 53 | + public static partial class Serialize | |
| 54 | + { | |
| 55 | + public static string ToJson(this TopLevel self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 56 | + } | |
| 57 | + | |
| 58 | + internal static partial class Converter | |
| 59 | + { | |
| 60 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 61 | + { | |
| 62 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 63 | + DateParseHandling = DateParseHandling.None, | |
| 64 | + Converters = | |
| 65 | + { | |
| 66 | + DataFormatConverter.Singleton, | |
| 67 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 68 | + }, | |
| 69 | + }; | |
| 70 | + } | |
| 71 | + | |
| 72 | + internal class DataFormatConverter : JsonConverter | |
| 73 | + { | |
| 74 | + public override bool CanConvert(Type t) => t == typeof(DataFormat) || t == typeof(DataFormat?); | |
| 75 | + | |
| 76 | + public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) | |
| 77 | + { | |
| 78 | + if (reader.TokenType == JsonToken.Null) return null; | |
| 79 | + var value = serializer.Deserialize<string>(reader); | |
| 80 | + switch (value) | |
| 81 | + { | |
| 82 | + case "turtle": | |
| 83 | + return DataFormat.Turtle; | |
| 84 | + case "rdf_xml": | |
| 85 | + return DataFormat.RdfXml; | |
| 86 | + case "n_triples": | |
| 87 | + return DataFormat.NTriples; | |
| 88 | + case "n_quads": | |
| 89 | + return DataFormat.NQuads; | |
| 90 | + } | |
| 91 | + throw new Exception("Cannot unmarshal type DataFormat"); | |
| 92 | + } | |
| 93 | + | |
| 94 | + public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) | |
| 95 | + { | |
| 96 | + if (untypedValue == null) | |
| 97 | + { | |
| 98 | + serializer.Serialize(writer, null); | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + var value = (DataFormat)untypedValue; | |
| 102 | + switch (value) | |
| 103 | + { | |
| 104 | + case DataFormat.Turtle: | |
| 105 | + serializer.Serialize(writer, "turtle"); | |
| 106 | + return; | |
| 107 | + case DataFormat.RdfXml: | |
| 108 | + serializer.Serialize(writer, "rdf_xml"); | |
| 109 | + return; | |
| 110 | + case DataFormat.NTriples: | |
| 111 | + serializer.Serialize(writer, "n_triples"); | |
| 112 | + return; | |
| 113 | + case DataFormat.NQuads: | |
| 114 | + serializer.Serialize(writer, "n_quads"); | |
| 115 | + return; | |
| 116 | + } | |
| 117 | + throw new Exception("Cannot marshal type DataFormat"); | |
| 118 | + } | |
| 119 | + | |
| 120 | + public static readonly DataFormatConverter Singleton = new DataFormatConverter(); | |
| 121 | + } | |
| 122 | +} | |
| 123 | +#pragma warning restore CS8618 | |
| 124 | +#pragma warning restore CS8601 | |
| 125 | +#pragma warning restore CS8602 | |
| 126 | +#pragma warning restore CS8603 | |
| 127 | +#pragma warning restore CS8604 | |
| 128 | +#pragma warning restore CS8625 | |
| 129 | +#pragma warning restore CS8765 |
Aschema-csharp-SystemTextJsondefault / QuickType.cs+227 −0
| @@ -0,0 +1,227 @@ | ||
| 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 | + /// <summary> | |
| 26 | + /// The format of the data. | |
| 27 | + /// </summary> | |
| 28 | + [JsonRequired] | |
| 29 | + [JsonPropertyName("format")] | |
| 30 | + public DataFormat? Format { get; set; } | |
| 31 | + } | |
| 32 | + | |
| 33 | + /// <summary> | |
| 34 | + /// The format of the data. | |
| 35 | + /// | |
| 36 | + /// Turtle format | |
| 37 | + /// | |
| 38 | + /// RDF XML format | |
| 39 | + /// | |
| 40 | + /// N-Triples format | |
| 41 | + /// | |
| 42 | + /// N-Quads format | |
| 43 | + /// </summary> | |
| 44 | + public enum DataFormat { Turtle, RdfXml, NTriples, NQuads }; | |
| 45 | + | |
| 46 | + public partial class TopLevel | |
| 47 | + { | |
| 48 | + public static TopLevel FromJson(string json) => JsonSerializer.Deserialize<TopLevel>(json, QuickType.Converter.Settings); | |
| 49 | + } | |
| 50 | + | |
| 51 | + public static partial class Serialize | |
| 52 | + { | |
| 53 | + public static string ToJson(this TopLevel self) => JsonSerializer.Serialize(self, QuickType.Converter.Settings); | |
| 54 | + } | |
| 55 | + | |
| 56 | + internal static partial class Converter | |
| 57 | + { | |
| 58 | + public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General) | |
| 59 | + { | |
| 60 | + Converters = | |
| 61 | + { | |
| 62 | + DataFormatConverter.Singleton, | |
| 63 | + new DateOnlyConverter(), | |
| 64 | + new TimeOnlyConverter(), | |
| 65 | + IsoDateTimeOffsetConverter.Singleton | |
| 66 | + }, | |
| 67 | + }; | |
| 68 | + } | |
| 69 | + | |
| 70 | + internal class DataFormatConverter : JsonConverter<DataFormat> | |
| 71 | + { | |
| 72 | + public override bool CanConvert(Type t) => t == typeof(DataFormat); | |
| 73 | + | |
| 74 | + public override DataFormat Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 75 | + { | |
| 76 | + var value = reader.GetString(); | |
| 77 | + switch (value) | |
| 78 | + { | |
| 79 | + case "turtle": | |
| 80 | + return DataFormat.Turtle; | |
| 81 | + case "rdf_xml": | |
| 82 | + return DataFormat.RdfXml; | |
| 83 | + case "n_triples": | |
| 84 | + return DataFormat.NTriples; | |
| 85 | + case "n_quads": | |
| 86 | + return DataFormat.NQuads; | |
| 87 | + } | |
| 88 | + throw new JsonException("Cannot unmarshal type DataFormat"); | |
| 89 | + } | |
| 90 | + | |
| 91 | + public override void Write(Utf8JsonWriter writer, DataFormat value, JsonSerializerOptions options) | |
| 92 | + { | |
| 93 | + switch (value) | |
| 94 | + { | |
| 95 | + case DataFormat.Turtle: | |
| 96 | + JsonSerializer.Serialize(writer, "turtle", options); | |
| 97 | + return; | |
| 98 | + case DataFormat.RdfXml: | |
| 99 | + JsonSerializer.Serialize(writer, "rdf_xml", options); | |
| 100 | + return; | |
| 101 | + case DataFormat.NTriples: | |
| 102 | + JsonSerializer.Serialize(writer, "n_triples", options); | |
| 103 | + return; | |
| 104 | + case DataFormat.NQuads: | |
| 105 | + JsonSerializer.Serialize(writer, "n_quads", options); | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + throw new NotSupportedException("Cannot marshal type DataFormat"); | |
| 109 | + } | |
| 110 | + | |
| 111 | + public static readonly DataFormatConverter Singleton = new DataFormatConverter(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + public class DateOnlyConverter : JsonConverter<DateOnly> | |
| 115 | + { | |
| 116 | + private readonly string serializationFormat; | |
| 117 | + public DateOnlyConverter() : this(null) { } | |
| 118 | + | |
| 119 | + public DateOnlyConverter(string? serializationFormat) | |
| 120 | + { | |
| 121 | + this.serializationFormat = serializationFormat ?? "yyyy-MM-dd"; | |
| 122 | + } | |
| 123 | + | |
| 124 | + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 125 | + { | |
| 126 | + var value = reader.GetString(); | |
| 127 | + return DateOnly.Parse(value!); | |
| 128 | + } | |
| 129 | + | |
| 130 | + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) | |
| 131 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 132 | + } | |
| 133 | + | |
| 134 | + public class TimeOnlyConverter : JsonConverter<TimeOnly> | |
| 135 | + { | |
| 136 | + private readonly string serializationFormat; | |
| 137 | + | |
| 138 | + public TimeOnlyConverter() : this(null) { } | |
| 139 | + | |
| 140 | + public TimeOnlyConverter(string? serializationFormat) | |
| 141 | + { | |
| 142 | + this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff"; | |
| 143 | + } | |
| 144 | + | |
| 145 | + public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 146 | + { | |
| 147 | + var value = reader.GetString(); | |
| 148 | + return TimeOnly.Parse(value!); | |
| 149 | + } | |
| 150 | + | |
| 151 | + public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) | |
| 152 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 153 | + } | |
| 154 | + | |
| 155 | + internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset> | |
| 156 | + { | |
| 157 | + public override bool CanConvert(Type t) => t == typeof(DateTimeOffset); | |
| 158 | + | |
| 159 | + private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"; | |
| 160 | + | |
| 161 | + private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind; | |
| 162 | + private string? _dateTimeFormat; | |
| 163 | + private CultureInfo? _culture; | |
| 164 | + | |
| 165 | + public DateTimeStyles DateTimeStyles | |
| 166 | + { | |
| 167 | + get => _dateTimeStyles; | |
| 168 | + set => _dateTimeStyles = value; | |
| 169 | + } | |
| 170 | + | |
| 171 | + public string? DateTimeFormat | |
| 172 | + { | |
| 173 | + get => _dateTimeFormat ?? string.Empty; | |
| 174 | + set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value; | |
| 175 | + } | |
| 176 | + | |
| 177 | + public CultureInfo Culture | |
| 178 | + { | |
| 179 | + get => _culture ?? CultureInfo.CurrentCulture; | |
| 180 | + set => _culture = value; | |
| 181 | + } | |
| 182 | + | |
| 183 | + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) | |
| 184 | + { | |
| 185 | + string text; | |
| 186 | + | |
| 187 | + | |
| 188 | + if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal | |
| 189 | + || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal) | |
| 190 | + { | |
| 191 | + value = value.ToUniversalTime(); | |
| 192 | + } | |
| 193 | + | |
| 194 | + text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture); | |
| 195 | + | |
| 196 | + writer.WriteStringValue(text); | |
| 197 | + } | |
| 198 | + | |
| 199 | + public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 200 | + { | |
| 201 | + string? dateText = reader.GetString(); | |
| 202 | + | |
| 203 | + if (string.IsNullOrEmpty(dateText) == false) | |
| 204 | + { | |
| 205 | + if (!string.IsNullOrEmpty(_dateTimeFormat)) | |
| 206 | + { | |
| 207 | + return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles); | |
| 208 | + } | |
| 209 | + else | |
| 210 | + { | |
| 211 | + return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles); | |
| 212 | + } | |
| 213 | + } | |
| 214 | + else | |
| 215 | + { | |
| 216 | + return default(DateTimeOffset); | |
| 217 | + } | |
| 218 | + } | |
| 219 | + | |
| 220 | + | |
| 221 | + public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter(); | |
| 222 | + } | |
| 223 | +} | |
| 224 | +#pragma warning restore CS8618 | |
| 225 | +#pragma warning restore CS8601 | |
| 226 | +#pragma warning restore CS8602 | |
| 227 | +#pragma warning restore CS8603 |
Aschema-csharpdefault / QuickType.cs+129 −0
| @@ -0,0 +1,129 @@ | ||
| 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 | + /// <summary> | |
| 29 | + /// The format of the data. | |
| 30 | + /// </summary> | |
| 31 | + [JsonProperty("format", Required = Required.AllowNull)] | |
| 32 | + public DataFormat? Format { get; set; } | |
| 33 | + } | |
| 34 | + | |
| 35 | + /// <summary> | |
| 36 | + /// The format of the data. | |
| 37 | + /// | |
| 38 | + /// Turtle format | |
| 39 | + /// | |
| 40 | + /// RDF XML format | |
| 41 | + /// | |
| 42 | + /// N-Triples format | |
| 43 | + /// | |
| 44 | + /// N-Quads format | |
| 45 | + /// </summary> | |
| 46 | + public enum DataFormat { Turtle, RdfXml, NTriples, NQuads }; | |
| 47 | + | |
| 48 | + public partial class TopLevel | |
| 49 | + { | |
| 50 | + public static TopLevel FromJson(string json) => JsonConvert.DeserializeObject<TopLevel>(json, QuickType.Converter.Settings); | |
| 51 | + } | |
| 52 | + | |
| 53 | + public static partial class Serialize | |
| 54 | + { | |
| 55 | + public static string ToJson(this TopLevel self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 56 | + } | |
| 57 | + | |
| 58 | + internal static partial class Converter | |
| 59 | + { | |
| 60 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 61 | + { | |
| 62 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 63 | + DateParseHandling = DateParseHandling.None, | |
| 64 | + Converters = | |
| 65 | + { | |
| 66 | + DataFormatConverter.Singleton, | |
| 67 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 68 | + }, | |
| 69 | + }; | |
| 70 | + } | |
| 71 | + | |
| 72 | + internal class DataFormatConverter : JsonConverter | |
| 73 | + { | |
| 74 | + public override bool CanConvert(Type t) => t == typeof(DataFormat) || t == typeof(DataFormat?); | |
| 75 | + | |
| 76 | + public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) | |
| 77 | + { | |
| 78 | + if (reader.TokenType == JsonToken.Null) return null; | |
| 79 | + var value = serializer.Deserialize<string>(reader); | |
| 80 | + switch (value) | |
| 81 | + { | |
| 82 | + case "turtle": | |
| 83 | + return DataFormat.Turtle; | |
| 84 | + case "rdf_xml": | |
| 85 | + return DataFormat.RdfXml; | |
| 86 | + case "n_triples": | |
| 87 | + return DataFormat.NTriples; | |
| 88 | + case "n_quads": | |
| 89 | + return DataFormat.NQuads; | |
| 90 | + } | |
| 91 | + throw new Exception("Cannot unmarshal type DataFormat"); | |
| 92 | + } | |
| 93 | + | |
| 94 | + public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) | |
| 95 | + { | |
| 96 | + if (untypedValue == null) | |
| 97 | + { | |
| 98 | + serializer.Serialize(writer, null); | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + var value = (DataFormat)untypedValue; | |
| 102 | + switch (value) | |
| 103 | + { | |
| 104 | + case DataFormat.Turtle: | |
| 105 | + serializer.Serialize(writer, "turtle"); | |
| 106 | + return; | |
| 107 | + case DataFormat.RdfXml: | |
| 108 | + serializer.Serialize(writer, "rdf_xml"); | |
| 109 | + return; | |
| 110 | + case DataFormat.NTriples: | |
| 111 | + serializer.Serialize(writer, "n_triples"); | |
| 112 | + return; | |
| 113 | + case DataFormat.NQuads: | |
| 114 | + serializer.Serialize(writer, "n_quads"); | |
| 115 | + return; | |
| 116 | + } | |
| 117 | + throw new Exception("Cannot marshal type DataFormat"); | |
| 118 | + } | |
| 119 | + | |
| 120 | + public static readonly DataFormatConverter Singleton = new DataFormatConverter(); | |
| 121 | + } | |
| 122 | +} | |
| 123 | +#pragma warning restore CS8618 | |
| 124 | +#pragma warning restore CS8601 | |
| 125 | +#pragma warning restore CS8602 | |
| 126 | +#pragma warning restore CS8603 | |
| 127 | +#pragma warning restore CS8604 | |
| 128 | +#pragma warning restore CS8625 | |
| 129 | +#pragma warning restore CS8765 |
Aschema-dartdefault / TopLevel.dart+63 −0
| @@ -0,0 +1,63 @@ | ||
| 1 | +// To parse this JSON data, do | |
| 2 | +// | |
| 3 | +// final topLevel = topLevelFromJson(jsonString); | |
| 4 | + | |
| 5 | +import 'dart:convert'; | |
| 6 | + | |
| 7 | +TopLevel topLevelFromJson(String str) => TopLevel.fromJson(json.decode(str)); | |
| 8 | + | |
| 9 | +String topLevelToJson(TopLevel data) => json.encode(data.toJson()); | |
| 10 | + | |
| 11 | +class TopLevel { | |
| 12 | + | |
| 13 | + ///The format of the data. | |
| 14 | + final DataFormat? format; | |
| 15 | + | |
| 16 | + TopLevel({ | |
| 17 | + required this.format, | |
| 18 | + }); | |
| 19 | + | |
| 20 | + factory TopLevel.fromJson(Map<String, dynamic> json) => TopLevel( | |
| 21 | + format: dataFormatValues.map[json["format"]], | |
| 22 | + ); | |
| 23 | + | |
| 24 | + Map<String, dynamic> toJson() => { | |
| 25 | + "format": dataFormatValues.reverse[format], | |
| 26 | + }; | |
| 27 | +} | |
| 28 | + | |
| 29 | + | |
| 30 | +///The format of the data. | |
| 31 | +/// | |
| 32 | +///Turtle format | |
| 33 | +/// | |
| 34 | +///RDF XML format | |
| 35 | +/// | |
| 36 | +///N-Triples format | |
| 37 | +/// | |
| 38 | +///N-Quads format | |
| 39 | +enum DataFormat { | |
| 40 | + TURTLE, | |
| 41 | + RDF_XML, | |
| 42 | + N_TRIPLES, | |
| 43 | + N_QUADS | |
| 44 | +} | |
| 45 | + | |
| 46 | +final dataFormatValues = EnumValues({ | |
| 47 | + "turtle": DataFormat.TURTLE, | |
| 48 | + "rdf_xml": DataFormat.RDF_XML, | |
| 49 | + "n_triples": DataFormat.N_TRIPLES, | |
| 50 | + "n_quads": DataFormat.N_QUADS | |
| 51 | +}); | |
| 52 | + | |
| 53 | +class EnumValues<T> { | |
| 54 | + Map<String, T> map; | |
| 55 | + late Map<T, String> reverseMap; | |
| 56 | + | |
| 57 | + EnumValues(this.map); | |
| 58 | + | |
| 59 | + Map<T, String> get reverse { | |
| 60 | + reverseMap = map.map((k, v) => MapEntry(v, k)); | |
| 61 | + return reverseMap; | |
| 62 | + } | |
| 63 | +} |
Aschema-elixirdefault / QuickType.ex+102 −0
| @@ -0,0 +1,102 @@ | ||
| 1 | +# This file was autogenerated using quicktype https://github.com/quicktype/quicktype | |
| 2 | +# | |
| 3 | +# Add Jason to your mix.exs | |
| 4 | +# | |
| 5 | +# Decode a JSON string: TopLevel.from_json(data) | |
| 6 | +# Encode into a JSON string: TopLevel.to_json(struct) | |
| 7 | + | |
| 8 | +defmodule DataFormat do | |
| 9 | + @moduledoc """ | |
| 10 | + The format of the data. | |
| 11 | + | |
| 12 | + Turtle format | |
| 13 | + | |
| 14 | + RDF XML format | |
| 15 | + | |
| 16 | + N-Triples format | |
| 17 | + | |
| 18 | + N-Quads format | |
| 19 | + """ | |
| 20 | + @valid_enum_members [ | |
| 21 | + :turtle, | |
| 22 | + :rdf_xml, | |
| 23 | + :n_triples, | |
| 24 | + :n_quads, | |
| 25 | + ] | |
| 26 | + | |
| 27 | + def valid_atom?(value), do: value in @valid_enum_members | |
| 28 | + | |
| 29 | + def valid_atom_string?(value) do | |
| 30 | + try do | |
| 31 | + atom = String.to_existing_atom(value) | |
| 32 | + atom in @valid_enum_members | |
| 33 | + rescue | |
| 34 | + ArgumentError -> false | |
| 35 | + end | |
| 36 | + end | |
| 37 | + | |
| 38 | + def encode(value) do | |
| 39 | + if valid_atom?(value) do | |
| 40 | + Atom.to_string(value) | |
| 41 | + else | |
| 42 | + {:error, "Unexpected value when encoding atom: #{inspect(value)}"} | |
| 43 | + end | |
| 44 | + end | |
| 45 | + | |
| 46 | + def decode(value) do | |
| 47 | + if valid_atom_string?(value) do | |
| 48 | + String.to_existing_atom(value) | |
| 49 | + else | |
| 50 | + {:error, "Unexpected value when decoding atom: #{inspect(value)}"} | |
| 51 | + end | |
| 52 | + end | |
| 53 | + | |
| 54 | + def from_json(json) do | |
| 55 | + json | |
| 56 | + |> Jason.decode!() | |
| 57 | + |> decode() | |
| 58 | + end | |
| 59 | + | |
| 60 | + def to_json(data) do | |
| 61 | + data | |
| 62 | + |> encode() | |
| 63 | + |> Jason.encode!() | |
| 64 | + end | |
| 65 | +end | |
| 66 | + | |
| 67 | +defmodule TopLevel do | |
| 68 | + @moduledoc """ | |
| 69 | + - `:format` - The format of the data. | |
| 70 | + """ | |
| 71 | + | |
| 72 | + @enforce_keys [:format] | |
| 73 | + defstruct [:format] | |
| 74 | + | |
| 75 | + @type t :: %__MODULE__{ | |
| 76 | + format: DataFormat.t() | nil | |
| 77 | + } | |
| 78 | + | |
| 79 | + def from_map(m) do | |
| 80 | + %TopLevel{ | |
| 81 | + format: m["format"] && DataFormat.decode(m["format"]), | |
| 82 | + } | |
| 83 | + end | |
| 84 | + | |
| 85 | + def from_json(json) do | |
| 86 | + json | |
| 87 | + |> Jason.decode!() | |
| 88 | + |> from_map() | |
| 89 | + end | |
| 90 | + | |
| 91 | + def to_map(struct) do | |
| 92 | + %{ | |
| 93 | + "format" => struct.format && DataFormat.encode(struct.format), | |
| 94 | + } | |
| 95 | + end | |
| 96 | + | |
| 97 | + def to_json(struct) do | |
| 98 | + struct | |
| 99 | + |> to_map() | |
| 100 | + |> Jason.encode!() | |
| 101 | + end | |
| 102 | +end |
Aschema-elmdefault / QuickType.elm+90 −0
| @@ -0,0 +1,90 @@ | ||
| 1 | +-- To decode the JSON data, add this file to your project, run | |
| 2 | +-- | |
| 3 | +-- elm install NoRedInk/elm-json-decode-pipeline | |
| 4 | +-- | |
| 5 | +-- add these imports | |
| 6 | +-- | |
| 7 | +-- import Json.Decode exposing (decodeString) | |
| 8 | +-- import QuickType exposing (quickType) | |
| 9 | +-- | |
| 10 | +-- and you're off to the races with | |
| 11 | +-- | |
| 12 | +-- decodeString quickType myJsonString | |
| 13 | + | |
| 14 | +module QuickType exposing | |
| 15 | + ( QuickType | |
| 16 | + , quickTypeToString | |
| 17 | + , quickType | |
| 18 | + , DataFormat(..) | |
| 19 | + ) | |
| 20 | + | |
| 21 | +import Json.Decode as Jdec | |
| 22 | +import Json.Decode.Pipeline as Jpipe | |
| 23 | +import Json.Encode as Jenc | |
| 24 | +import Dict exposing (Dict) | |
| 25 | + | |
| 26 | +{-| format: | |
| 27 | +The format of the data. | |
| 28 | +-} | |
| 29 | +type alias QuickType = | |
| 30 | + { format : Maybe DataFormat | |
| 31 | + } | |
| 32 | + | |
| 33 | +{-| The format of the data. | |
| 34 | + | |
| 35 | +Turtle format | |
| 36 | + | |
| 37 | +RDF XML format | |
| 38 | + | |
| 39 | +N-Triples format | |
| 40 | + | |
| 41 | +N-Quads format | |
| 42 | +-} | |
| 43 | +type DataFormat | |
| 44 | + = Turtle | |
| 45 | + | RDFXML | |
| 46 | + | NTriples | |
| 47 | + | NQuads | |
| 48 | + | |
| 49 | +-- decoders and encoders | |
| 50 | + | |
| 51 | +quickTypeToString : QuickType -> String | |
| 52 | +quickTypeToString r = Jenc.encode 0 (encodeQuickType r) | |
| 53 | + | |
| 54 | +quickType : Jdec.Decoder QuickType | |
| 55 | +quickType = | |
| 56 | + Jdec.succeed QuickType | |
| 57 | + |> Jpipe.optional "format" (Jdec.nullable dataFormat) Nothing | |
| 58 | + | |
| 59 | +encodeQuickType : QuickType -> Jenc.Value | |
| 60 | +encodeQuickType x = | |
| 61 | + Jenc.object | |
| 62 | + [ ("format", makeNullableEncoder encodeDataFormat x.format) | |
| 63 | + ] | |
| 64 | + | |
| 65 | +dataFormat : Jdec.Decoder DataFormat | |
| 66 | +dataFormat = | |
| 67 | + Jdec.string | |
| 68 | + |> Jdec.andThen (\str -> | |
| 69 | + case str of | |
| 70 | + "turtle" -> Jdec.succeed Turtle | |
| 71 | + "rdf_xml" -> Jdec.succeed RDFXML | |
| 72 | + "n_triples" -> Jdec.succeed NTriples | |
| 73 | + "n_quads" -> Jdec.succeed NQuads | |
| 74 | + somethingElse -> Jdec.fail <| "Invalid DataFormat: " ++ somethingElse | |
| 75 | + ) | |
| 76 | + | |
| 77 | +encodeDataFormat : DataFormat -> Jenc.Value | |
| 78 | +encodeDataFormat x = case x of | |
| 79 | + Turtle -> Jenc.string "turtle" | |
| 80 | + RDFXML -> Jenc.string "rdf_xml" | |
| 81 | + NTriples -> Jenc.string "n_triples" | |
| 82 | + NQuads -> Jenc.string "n_quads" | |
| 83 | + | |
| 84 | +--- encoder helpers | |
| 85 | + | |
| 86 | +makeNullableEncoder : (a -> Jenc.Value) -> Maybe a -> Jenc.Value | |
| 87 | +makeNullableEncoder f m = | |
| 88 | + case m of | |
| 89 | + Just x -> f x | |
| 90 | + Nothing -> Jenc.null |
Aschema-flowdefault / TopLevel.js+214 −0
| @@ -0,0 +1,214 @@ | ||
| 1 | +// @flow | |
| 2 | + | |
| 3 | +// To parse this data: | |
| 4 | +// | |
| 5 | +// const Convert = require("./TopLevel"); | |
| 6 | +// | |
| 7 | +// const topLevel = Convert.toTopLevel(json); | |
| 8 | +// | |
| 9 | +// These functions will throw an error if the JSON doesn't | |
| 10 | +// match the expected interface, even if the JSON is valid. | |
| 11 | + | |
| 12 | +export type TopLevel = { | |
| 13 | + /** | |
| 14 | + * The format of the data. | |
| 15 | + */ | |
| 16 | + format: DataFormat | null; | |
| 17 | +}; | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * The format of the data. | |
| 21 | + * | |
| 22 | + * Turtle format | |
| 23 | + * | |
| 24 | + * RDF XML format | |
| 25 | + * | |
| 26 | + * N-Triples format | |
| 27 | + * | |
| 28 | + * N-Quads format | |
| 29 | + */ | |
| 30 | +export type DataFormat = | |
| 31 | + "turtle" | |
| 32 | + | "rdf_xml" | |
| 33 | + | "n_triples" | |
| 34 | + | "n_quads"; | |
| 35 | + | |
| 36 | +// Converts JSON strings to/from your types | |
| 37 | +// and asserts the results of JSON.parse at runtime | |
| 38 | +function toTopLevel(json: string): TopLevel { | |
| 39 | + return cast(JSON.parse(json), r("TopLevel")); | |
| 40 | +} | |
| 41 | + | |
| 42 | +function topLevelToJson(value: TopLevel): string { | |
| 43 | + return JSON.stringify(uncast(value, r("TopLevel")), null, 2); | |
| 44 | +} | |
| 45 | + | |
| 46 | +function invalidValue(typ: any, val: any, key: any, parent: any = '') { | |
| 47 | + const prettyTyp = prettyTypeName(typ); | |
| 48 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 49 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 50 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 51 | +} | |
| 52 | + | |
| 53 | +function prettyTypeName(typ: any): string { | |
| 54 | + if (Array.isArray(typ)) { | |
| 55 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 56 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 57 | + } else { | |
| 58 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 59 | + } | |
| 60 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 61 | + return typ.literal; | |
| 62 | + } else { | |
| 63 | + return typeof typ; | |
| 64 | + } | |
| 65 | +} | |
| 66 | + | |
| 67 | +function jsonToJSProps(typ: any): any { | |
| 68 | + if (typ.jsonToJS === undefined) { | |
| 69 | + const map: any = {}; | |
| 70 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 71 | + typ.jsonToJS = map; | |
| 72 | + } | |
| 73 | + return typ.jsonToJS; | |
| 74 | +} | |
| 75 | + | |
| 76 | +function jsToJSONProps(typ: any): any { | |
| 77 | + if (typ.jsToJSON === undefined) { | |
| 78 | + const map: any = {}; | |
| 79 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 80 | + typ.jsToJSON = map; | |
| 81 | + } | |
| 82 | + return typ.jsToJSON; | |
| 83 | +} | |
| 84 | + | |
| 85 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 86 | + function transformPrimitive(typ: string, val: any): any { | |
| 87 | + if (typeof typ === typeof val) return val; | |
| 88 | + return invalidValue(typ, val, key, parent); | |
| 89 | + } | |
| 90 | + | |
| 91 | + function transformUnion(typs: any[], val: any): any { | |
| 92 | + // val must validate against one typ in typs | |
| 93 | + const l = typs.length; | |
| 94 | + for (let i = 0; i < l; i++) { | |
| 95 | + const typ = typs[i]; | |
| 96 | + try { | |
| 97 | + return transform(val, typ, getProps); | |
| 98 | + } catch (_) {} | |
| 99 | + } | |
| 100 | + return invalidValue(typs, val, key, parent); | |
| 101 | + } | |
| 102 | + | |
| 103 | + function transformEnum(cases: string[], val: any): any { | |
| 104 | + if (cases.indexOf(val) !== -1) return val; | |
| 105 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 106 | + } | |
| 107 | + | |
| 108 | + function transformArray(typ: any, val: any): any { | |
| 109 | + // val must be an array with no invalid elements | |
| 110 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 111 | + return val.map(el => transform(el, typ, getProps)); | |
| 112 | + } | |
| 113 | + | |
| 114 | + function transformDate(val: any): any { | |
| 115 | + if (val === null) { | |
| 116 | + return null; | |
| 117 | + } | |
| 118 | + const d = new Date(val); | |
| 119 | + if (isNaN(d.valueOf())) { | |
| 120 | + return invalidValue(l("Date"), val, key, parent); | |
| 121 | + } | |
| 122 | + return d; | |
| 123 | + } | |
| 124 | + | |
| 125 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 126 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 127 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 128 | + } | |
| 129 | + const result: any = {}; | |
| 130 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 131 | + const prop = props[key]; | |
| 132 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 133 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 134 | + }); | |
| 135 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 136 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 137 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 138 | + } | |
| 139 | + }); | |
| 140 | + return result; | |
| 141 | + } | |
| 142 | + | |
| 143 | + if (typ === "any") return val; | |
| 144 | + if (typ === null) { | |
| 145 | + if (val === null) return val; | |
| 146 | + return invalidValue(typ, val, key, parent); | |
| 147 | + } | |
| 148 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 149 | + let ref: any = undefined; | |
| 150 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 151 | + ref = typ.ref; | |
| 152 | + typ = typeMap[typ.ref]; | |
| 153 | + } | |
| 154 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 155 | + if (typeof typ === "object") { | |
| 156 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 157 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 158 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 159 | + : invalidValue(typ, val, key, parent); | |
| 160 | + } | |
| 161 | + // Numbers can be parsed by Date but shouldn't be. | |
| 162 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 163 | + return transformPrimitive(typ, val); | |
| 164 | +} | |
| 165 | + | |
| 166 | +function cast<T>(val: any, typ: any): T { | |
| 167 | + return transform(val, typ, jsonToJSProps); | |
| 168 | +} | |
| 169 | + | |
| 170 | +function uncast<T>(val: T, typ: any): any { | |
| 171 | + return transform(val, typ, jsToJSONProps); | |
| 172 | +} | |
| 173 | + | |
| 174 | +function l(typ: any) { | |
| 175 | + return { literal: typ }; | |
| 176 | +} | |
| 177 | + | |
| 178 | +function a(typ: any) { | |
| 179 | + return { arrayItems: typ }; | |
| 180 | +} | |
| 181 | + | |
| 182 | +function u(...typs: any[]) { | |
| 183 | + return { unionMembers: typs }; | |
| 184 | +} | |
| 185 | + | |
| 186 | +function o(props: any[], additional: any) { | |
| 187 | + return { props, additional }; | |
| 188 | +} | |
| 189 | + | |
| 190 | +function m(additional: any) { | |
| 191 | + const props: any[] = []; | |
| 192 | + return { props, additional }; | |
| 193 | +} | |
| 194 | + | |
| 195 | +function r(name: string) { | |
| 196 | + return { ref: name }; | |
| 197 | +} | |
| 198 | + | |
| 199 | +const typeMap: any = { | |
| 200 | + "TopLevel": o([ | |
| 201 | + { json: "format", js: "format", typ: u(r("DataFormat"), null) }, | |
| 202 | + ], false), | |
| 203 | + "DataFormat": [ | |
| 204 | + "turtle", | |
| 205 | + "rdf_xml", | |
| 206 | + "n_triples", | |
| 207 | + "n_quads", | |
| 208 | + ], | |
| 209 | +}; | |
| 210 | + | |
| 211 | +module.exports = { | |
| 212 | + "topLevelToJson": topLevelToJson, | |
| 213 | + "toTopLevel": toTopLevel, | |
| 214 | +}; |
Aschema-golangdefault / quicktype.go+42 −0
| @@ -0,0 +1,42 @@ | ||
| 1 | +// Code generated from JSON Schema using quicktype. DO NOT EDIT. | |
| 2 | +// To parse and unparse this JSON data, add this code to your project and do: | |
| 3 | +// | |
| 4 | +// topLevel, err := UnmarshalTopLevel(bytes) | |
| 5 | +// bytes, err = topLevel.Marshal() | |
| 6 | + | |
| 7 | +package main | |
| 8 | + | |
| 9 | +import "encoding/json" | |
| 10 | + | |
| 11 | +func UnmarshalTopLevel(data []byte) (TopLevel, error) { | |
| 12 | + var r TopLevel | |
| 13 | + err := json.Unmarshal(data, &r) | |
| 14 | + return r, err | |
| 15 | +} | |
| 16 | + | |
| 17 | +func (r *TopLevel) Marshal() ([]byte, error) { | |
| 18 | + return json.Marshal(r) | |
| 19 | +} | |
| 20 | + | |
| 21 | +type TopLevel struct { | |
| 22 | + // The format of the data. | |
| 23 | + Format *DataFormat `json:"format"` | |
| 24 | +} | |
| 25 | + | |
| 26 | +// The format of the data. | |
| 27 | +// | |
| 28 | +// Turtle format | |
| 29 | +// | |
| 30 | +// RDF XML format | |
| 31 | +// | |
| 32 | +// N-Triples format | |
| 33 | +// | |
| 34 | +// N-Quads format | |
| 35 | +type DataFormat string | |
| 36 | + | |
| 37 | +const ( | |
| 38 | + Turtle DataFormat = "turtle" | |
| 39 | + RDFXML DataFormat = "rdf_xml" | |
| 40 | + NTriples DataFormat = "n_triples" | |
| 41 | + NQuads DataFormat = "n_quads" | |
| 42 | +) |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / Converter.java+121 −0
| @@ -0,0 +1,121 @@ | ||
| 1 | +// To use this code, add the following Maven dependency to your project: | |
| 2 | +// | |
| 3 | +// | |
| 4 | +// com.fasterxml.jackson.core : jackson-databind : 2.9.0 | |
| 5 | +// | |
| 6 | +// | |
| 7 | +// Import this package: | |
| 8 | +// | |
| 9 | +// import io.quicktype.Converter; | |
| 10 | +// | |
| 11 | +// Then you can deserialize a JSON string with | |
| 12 | +// | |
| 13 | +// TopLevel data = Converter.fromJsonString(jsonString); | |
| 14 | + | |
| 15 | +package io.quicktype; | |
| 16 | + | |
| 17 | +import java.io.IOException; | |
| 18 | +import com.fasterxml.jackson.databind.*; | |
| 19 | +import com.fasterxml.jackson.databind.module.SimpleModule; | |
| 20 | +import com.fasterxml.jackson.core.JsonParser; | |
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
| 22 | +import java.util.*; | |
| 23 | +import java.util.Date; | |
| 24 | +import java.text.SimpleDateFormat; | |
| 25 | + | |
| 26 | +public class Converter { | |
| 27 | + // Date-time helpers | |
| 28 | + | |
| 29 | + private static final String[] DATE_TIME_FORMATS = { | |
| 30 | + "yyyy-MM-dd'T'HH:mm:ss.SX", | |
| 31 | + "yyyy-MM-dd'T'HH:mm:ss.S", | |
| 32 | + "yyyy-MM-dd'T'HH:mm:ssX", | |
| 33 | + "yyyy-MM-dd'T'HH:mm:ss", | |
| 34 | + "yyyy-MM-dd HH:mm:ss.SX", | |
| 35 | + "yyyy-MM-dd HH:mm:ss.S", | |
| 36 | + "yyyy-MM-dd HH:mm:ssX", | |
| 37 | + "yyyy-MM-dd HH:mm:ss", | |
| 38 | + "HH:mm:ss.SZ", | |
| 39 | + "HH:mm:ss.S", | |
| 40 | + "HH:mm:ssZ", | |
| 41 | + "HH:mm:ss", | |
| 42 | + "yyyy-MM-dd", | |
| 43 | + }; | |
| 44 | + | |
| 45 | + public static Date parseAllDateTimeString(String str) { | |
| 46 | + for (String format : DATE_TIME_FORMATS) { | |
| 47 | + try { | |
| 48 | + return new SimpleDateFormat(format).parse(str); | |
| 49 | + } catch (Exception ex) { | |
| 50 | + // Ignored | |
| 51 | + } | |
| 52 | + } | |
| 53 | + return null; | |
| 54 | + } | |
| 55 | + | |
| 56 | + public static String serializeDateTime(Date datetime) { | |
| 57 | + return new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ").format(datetime); | |
| 58 | + } | |
| 59 | + | |
| 60 | + public static String serializeDate(Date datetime) { | |
| 61 | + return new SimpleDateFormat("yyyy-MM-dd").format(datetime); | |
| 62 | + } | |
| 63 | + | |
| 64 | + public static String serializeTime(Date datetime) { | |
| 65 | + return new SimpleDateFormat("hh:mm:ssZ").format(datetime); | |
| 66 | + } | |
| 67 | + // Serialize/deserialize helpers | |
| 68 | + | |
| 69 | + public static TopLevel fromJsonString(String json) throws IOException { | |
| 70 | + return getObjectReader().readValue(json); | |
| 71 | + } | |
| 72 | + | |
| 73 | + public static String toJsonString(TopLevel obj) throws JsonProcessingException { | |
| 74 | + return getObjectWriter().writeValueAsString(obj); | |
| 75 | + } | |
| 76 | + | |
| 77 | + private static ObjectReader reader; | |
| 78 | + private static ObjectWriter writer; | |
| 79 | + | |
| 80 | + private static void instantiateMapper() { | |
| 81 | + ObjectMapper mapper = new ObjectMapper(); | |
| 82 | + mapper.findAndRegisterModules(); | |
| 83 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
| 84 | + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
| 85 | + SimpleModule module = new SimpleModule(); | |
| 86 | + module.addDeserializer(Date.class, new JsonDeserializer<Date>() { | |
| 87 | + @Override | |
| 88 | + public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 89 | + String value = jsonParser.getText(); | |
| 90 | + return Converter.parseAllDateTimeString(value); | |
| 91 | + } | |
| 92 | + }); | |
| 93 | + module.addDeserializer(Date.class, new JsonDeserializer<Date>() { | |
| 94 | + @Override | |
| 95 | + public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 96 | + String value = jsonParser.getText(); | |
| 97 | + return Converter.parseAllDateTimeString(value); | |
| 98 | + } | |
| 99 | + }); | |
| 100 | + module.addDeserializer(Date.class, new JsonDeserializer<Date>() { | |
| 101 | + @Override | |
| 102 | + public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 103 | + String value = jsonParser.getText(); | |
| 104 | + return Converter.parseAllDateTimeString(value); | |
| 105 | + } | |
| 106 | + }); | |
| 107 | + mapper.registerModule(module); | |
| 108 | + reader = mapper.readerFor(TopLevel.class); | |
| 109 | + writer = mapper.writerFor(TopLevel.class); | |
| 110 | + } | |
| 111 | + | |
| 112 | + private static ObjectReader getObjectReader() { | |
| 113 | + if (reader == null) instantiateMapper(); | |
| 114 | + return reader; | |
| 115 | + } | |
| 116 | + | |
| 117 | + private static ObjectWriter getObjectWriter() { | |
| 118 | + if (writer == null) instantiateMapper(); | |
| 119 | + return writer; | |
| 120 | + } | |
| 121 | +} |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / DataFormat.java+39 −0
| @@ -0,0 +1,39 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import com.fasterxml.jackson.annotation.*; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * The format of the data. | |
| 8 | + * | |
| 9 | + * Turtle format | |
| 10 | + * | |
| 11 | + * RDF XML format | |
| 12 | + * | |
| 13 | + * N-Triples format | |
| 14 | + * | |
| 15 | + * N-Quads format | |
| 16 | + */ | |
| 17 | +public enum DataFormat { | |
| 18 | + TURTLE, RDF_XML, N_TRIPLES, N_QUADS; | |
| 19 | + | |
| 20 | + @JsonValue | |
| 21 | + public String toValue() { | |
| 22 | + switch (this) { | |
| 23 | + case TURTLE: return "turtle"; | |
| 24 | + case RDF_XML: return "rdf_xml"; | |
| 25 | + case N_TRIPLES: return "n_triples"; | |
| 26 | + case N_QUADS: return "n_quads"; | |
| 27 | + } | |
| 28 | + return null; | |
| 29 | + } | |
| 30 | + | |
| 31 | + @JsonCreator | |
| 32 | + public static DataFormat forValue(String value) throws IOException { | |
| 33 | + if (value.equals("turtle")) return TURTLE; | |
| 34 | + if (value.equals("rdf_xml")) return RDF_XML; | |
| 35 | + if (value.equals("n_triples")) return N_TRIPLES; | |
| 36 | + if (value.equals("n_quads")) return N_QUADS; | |
| 37 | + throw new IOException("Cannot deserialize DataFormat"); | |
| 38 | + } | |
| 39 | +} |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / TopLevel.java+15 −0
| @@ -0,0 +1,15 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class TopLevel { | |
| 6 | + private DataFormat format; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * The format of the data. | |
| 10 | + */ | |
| 11 | + @JsonProperty("format") | |
| 12 | + public DataFormat getFormat() { return format; } | |
| 13 | + @JsonProperty("format") | |
| 14 | + public void setFormat(DataFormat value) { this.format = value; } | |
| 15 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / Converter.java+101 −0
| @@ -0,0 +1,101 @@ | ||
| 1 | +// To use this code, add the following Maven dependency to your project: | |
| 2 | +// | |
| 3 | +// | |
| 4 | +// com.fasterxml.jackson.core : jackson-databind : 2.9.0 | |
| 5 | +// com.fasterxml.jackson.datatype : jackson-datatype-jsr310 : 2.9.0 | |
| 6 | +// | |
| 7 | +// Import this package: | |
| 8 | +// | |
| 9 | +// import io.quicktype.Converter; | |
| 10 | +// | |
| 11 | +// Then you can deserialize a JSON string with | |
| 12 | +// | |
| 13 | +// TopLevel data = Converter.fromJsonString(jsonString); | |
| 14 | + | |
| 15 | +package io.quicktype; | |
| 16 | + | |
| 17 | +import java.io.IOException; | |
| 18 | +import com.fasterxml.jackson.databind.*; | |
| 19 | +import com.fasterxml.jackson.databind.module.SimpleModule; | |
| 20 | +import com.fasterxml.jackson.core.JsonParser; | |
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
| 22 | +import java.util.*; | |
| 23 | +import java.time.LocalDate; | |
| 24 | +import java.time.OffsetDateTime; | |
| 25 | +import java.time.OffsetTime; | |
| 26 | +import java.time.ZoneOffset; | |
| 27 | +import java.time.ZonedDateTime; | |
| 28 | +import java.time.format.DateTimeFormatter; | |
| 29 | +import java.time.format.DateTimeFormatterBuilder; | |
| 30 | +import java.time.temporal.ChronoField; | |
| 31 | + | |
| 32 | +public class Converter { | |
| 33 | + // Date-time helpers | |
| 34 | + | |
| 35 | + private static final DateTimeFormatter DATE_TIME_FORMATTER = new DateTimeFormatterBuilder() | |
| 36 | + .appendOptional(DateTimeFormatter.ISO_DATE_TIME) | |
| 37 | + .appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME) | |
| 38 | + .appendOptional(DateTimeFormatter.ISO_INSTANT) | |
| 39 | + .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SX")) | |
| 40 | + .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssX")) | |
| 41 | + .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) | |
| 42 | + .toFormatter() | |
| 43 | + .withZone(ZoneOffset.UTC); | |
| 44 | + | |
| 45 | + public static OffsetDateTime parseDateTimeString(String str) { | |
| 46 | + return ZonedDateTime.from(Converter.DATE_TIME_FORMATTER.parse(str)).toOffsetDateTime(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + private static final DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder() | |
| 50 | + .appendOptional(DateTimeFormatter.ISO_TIME) | |
| 51 | + .appendOptional(DateTimeFormatter.ISO_OFFSET_TIME) | |
| 52 | + .parseDefaulting(ChronoField.YEAR, 2020) | |
| 53 | + .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1) | |
| 54 | + .parseDefaulting(ChronoField.DAY_OF_MONTH, 1) | |
| 55 | + .toFormatter() | |
| 56 | + .withZone(ZoneOffset.UTC); | |
| 57 | + | |
| 58 | + public static OffsetTime parseTimeString(String str) { | |
| 59 | + return ZonedDateTime.from(Converter.TIME_FORMATTER.parse(str)).toOffsetDateTime().toOffsetTime(); | |
| 60 | + } | |
| 61 | + // Serialize/deserialize helpers | |
| 62 | + | |
| 63 | + public static TopLevel fromJsonString(String json) throws IOException { | |
| 64 | + return getObjectReader().readValue(json); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public static String toJsonString(TopLevel obj) throws JsonProcessingException { | |
| 68 | + return getObjectWriter().writeValueAsString(obj); | |
| 69 | + } | |
| 70 | + | |
| 71 | + private static ObjectReader reader; | |
| 72 | + private static ObjectWriter writer; | |
| 73 | + | |
| 74 | + private static void instantiateMapper() { | |
| 75 | + ObjectMapper mapper = new ObjectMapper(); | |
| 76 | + mapper.findAndRegisterModules(); | |
| 77 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
| 78 | + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
| 79 | + SimpleModule module = new SimpleModule(); | |
| 80 | + module.addDeserializer(OffsetDateTime.class, new JsonDeserializer<OffsetDateTime>() { | |
| 81 | + @Override | |
| 82 | + public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 83 | + String value = jsonParser.getText(); | |
| 84 | + return Converter.parseDateTimeString(value); | |
| 85 | + } | |
| 86 | + }); | |
| 87 | + mapper.registerModule(module); | |
| 88 | + reader = mapper.readerFor(TopLevel.class); | |
| 89 | + writer = mapper.writerFor(TopLevel.class); | |
| 90 | + } | |
| 91 | + | |
| 92 | + private static ObjectReader getObjectReader() { | |
| 93 | + if (reader == null) instantiateMapper(); | |
| 94 | + return reader; | |
| 95 | + } | |
| 96 | + | |
| 97 | + private static ObjectWriter getObjectWriter() { | |
| 98 | + if (writer == null) instantiateMapper(); | |
| 99 | + return writer; | |
| 100 | + } | |
| 101 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / DataFormat.java+39 −0
| @@ -0,0 +1,39 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import com.fasterxml.jackson.annotation.*; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * The format of the data. | |
| 8 | + * | |
| 9 | + * Turtle format | |
| 10 | + * | |
| 11 | + * RDF XML format | |
| 12 | + * | |
| 13 | + * N-Triples format | |
| 14 | + * | |
| 15 | + * N-Quads format | |
| 16 | + */ | |
| 17 | +public enum DataFormat { | |
| 18 | + TURTLE, RDF_XML, N_TRIPLES, N_QUADS; | |
| 19 | + | |
| 20 | + @JsonValue | |
| 21 | + public String toValue() { | |
| 22 | + switch (this) { | |
| 23 | + case TURTLE: return "turtle"; | |
| 24 | + case RDF_XML: return "rdf_xml"; | |
| 25 | + case N_TRIPLES: return "n_triples"; | |
| 26 | + case N_QUADS: return "n_quads"; | |
| 27 | + } | |
| 28 | + return null; | |
| 29 | + } | |
| 30 | + | |
| 31 | + @JsonCreator | |
| 32 | + public static DataFormat forValue(String value) throws IOException { | |
| 33 | + if (value.equals("turtle")) return TURTLE; | |
| 34 | + if (value.equals("rdf_xml")) return RDF_XML; | |
| 35 | + if (value.equals("n_triples")) return N_TRIPLES; | |
| 36 | + if (value.equals("n_quads")) return N_QUADS; | |
| 37 | + throw new IOException("Cannot deserialize DataFormat"); | |
| 38 | + } | |
| 39 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / TopLevel.java+15 −0
| @@ -0,0 +1,15 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class TopLevel { | |
| 6 | + private DataFormat format; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * The format of the data. | |
| 10 | + */ | |
| 11 | + @JsonProperty("format") | |
| 12 | + public DataFormat getFormat() { return format; } | |
| 13 | + @JsonProperty("format") | |
| 14 | + public void setFormat(DataFormat value) { this.format = value; } | |
| 15 | +} |
Aschema-javadefault / src / main / java / io / quicktype / Converter.java+101 −0
| @@ -0,0 +1,101 @@ | ||
| 1 | +// To use this code, add the following Maven dependency to your project: | |
| 2 | +// | |
| 3 | +// | |
| 4 | +// com.fasterxml.jackson.core : jackson-databind : 2.9.0 | |
| 5 | +// com.fasterxml.jackson.datatype : jackson-datatype-jsr310 : 2.9.0 | |
| 6 | +// | |
| 7 | +// Import this package: | |
| 8 | +// | |
| 9 | +// import io.quicktype.Converter; | |
| 10 | +// | |
| 11 | +// Then you can deserialize a JSON string with | |
| 12 | +// | |
| 13 | +// TopLevel data = Converter.fromJsonString(jsonString); | |
| 14 | + | |
| 15 | +package io.quicktype; | |
| 16 | + | |
| 17 | +import java.io.IOException; | |
| 18 | +import com.fasterxml.jackson.databind.*; | |
| 19 | +import com.fasterxml.jackson.databind.module.SimpleModule; | |
| 20 | +import com.fasterxml.jackson.core.JsonParser; | |
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
| 22 | +import java.util.*; | |
| 23 | +import java.time.LocalDate; | |
| 24 | +import java.time.OffsetDateTime; | |
| 25 | +import java.time.OffsetTime; | |
| 26 | +import java.time.ZoneOffset; | |
| 27 | +import java.time.ZonedDateTime; | |
| 28 | +import java.time.format.DateTimeFormatter; | |
| 29 | +import java.time.format.DateTimeFormatterBuilder; | |
| 30 | +import java.time.temporal.ChronoField; | |
| 31 | + | |
| 32 | +public class Converter { | |
| 33 | + // Date-time helpers | |
| 34 | + | |
| 35 | + private static final DateTimeFormatter DATE_TIME_FORMATTER = new DateTimeFormatterBuilder() | |
| 36 | + .appendOptional(DateTimeFormatter.ISO_DATE_TIME) | |
| 37 | + .appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME) | |
| 38 | + .appendOptional(DateTimeFormatter.ISO_INSTANT) | |
| 39 | + .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SX")) | |
| 40 | + .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssX")) | |
| 41 | + .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) | |
| 42 | + .toFormatter() | |
| 43 | + .withZone(ZoneOffset.UTC); | |
| 44 | + | |
| 45 | + public static OffsetDateTime parseDateTimeString(String str) { | |
| 46 | + return ZonedDateTime.from(Converter.DATE_TIME_FORMATTER.parse(str)).toOffsetDateTime(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + private static final DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder() | |
| 50 | + .appendOptional(DateTimeFormatter.ISO_TIME) | |
| 51 | + .appendOptional(DateTimeFormatter.ISO_OFFSET_TIME) | |
| 52 | + .parseDefaulting(ChronoField.YEAR, 2020) | |
| 53 | + .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1) | |
| 54 | + .parseDefaulting(ChronoField.DAY_OF_MONTH, 1) | |
| 55 | + .toFormatter() | |
| 56 | + .withZone(ZoneOffset.UTC); | |
| 57 | + | |
| 58 | + public static OffsetTime parseTimeString(String str) { | |
| 59 | + return ZonedDateTime.from(Converter.TIME_FORMATTER.parse(str)).toOffsetDateTime().toOffsetTime(); | |
| 60 | + } | |
| 61 | + // Serialize/deserialize helpers | |
| 62 | + | |
| 63 | + public static TopLevel fromJsonString(String json) throws IOException { | |
| 64 | + return getObjectReader().readValue(json); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public static String toJsonString(TopLevel obj) throws JsonProcessingException { | |
| 68 | + return getObjectWriter().writeValueAsString(obj); | |
| 69 | + } | |
| 70 | + | |
| 71 | + private static ObjectReader reader; | |
| 72 | + private static ObjectWriter writer; | |
| 73 | + | |
| 74 | + private static void instantiateMapper() { | |
| 75 | + ObjectMapper mapper = new ObjectMapper(); | |
| 76 | + mapper.findAndRegisterModules(); | |
| 77 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
| 78 | + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
| 79 | + SimpleModule module = new SimpleModule(); | |
| 80 | + module.addDeserializer(OffsetDateTime.class, new JsonDeserializer<OffsetDateTime>() { | |
| 81 | + @Override | |
| 82 | + public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 83 | + String value = jsonParser.getText(); | |
| 84 | + return Converter.parseDateTimeString(value); | |
| 85 | + } | |
| 86 | + }); | |
| 87 | + mapper.registerModule(module); | |
| 88 | + reader = mapper.readerFor(TopLevel.class); | |
| 89 | + writer = mapper.writerFor(TopLevel.class); | |
| 90 | + } | |
| 91 | + | |
| 92 | + private static ObjectReader getObjectReader() { | |
| 93 | + if (reader == null) instantiateMapper(); | |
| 94 | + return reader; | |
| 95 | + } | |
| 96 | + | |
| 97 | + private static ObjectWriter getObjectWriter() { | |
| 98 | + if (writer == null) instantiateMapper(); | |
| 99 | + return writer; | |
| 100 | + } | |
| 101 | +} |
Aschema-javadefault / src / main / java / io / quicktype / DataFormat.java+39 −0
| @@ -0,0 +1,39 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import com.fasterxml.jackson.annotation.*; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * The format of the data. | |
| 8 | + * | |
| 9 | + * Turtle format | |
| 10 | + * | |
| 11 | + * RDF XML format | |
| 12 | + * | |
| 13 | + * N-Triples format | |
| 14 | + * | |
| 15 | + * N-Quads format | |
| 16 | + */ | |
| 17 | +public enum DataFormat { | |
| 18 | + TURTLE, RDF_XML, N_TRIPLES, N_QUADS; | |
| 19 | + | |
| 20 | + @JsonValue | |
| 21 | + public String toValue() { | |
| 22 | + switch (this) { | |
| 23 | + case TURTLE: return "turtle"; | |
| 24 | + case RDF_XML: return "rdf_xml"; | |
| 25 | + case N_TRIPLES: return "n_triples"; | |
| 26 | + case N_QUADS: return "n_quads"; | |
| 27 | + } | |
| 28 | + return null; | |
| 29 | + } | |
| 30 | + | |
| 31 | + @JsonCreator | |
| 32 | + public static DataFormat forValue(String value) throws IOException { | |
| 33 | + if (value.equals("turtle")) return TURTLE; | |
| 34 | + if (value.equals("rdf_xml")) return RDF_XML; | |
| 35 | + if (value.equals("n_triples")) return N_TRIPLES; | |
| 36 | + if (value.equals("n_quads")) return N_QUADS; | |
| 37 | + throw new IOException("Cannot deserialize DataFormat"); | |
| 38 | + } | |
| 39 | +} |
Aschema-javadefault / src / main / java / io / quicktype / TopLevel.java+15 −0
| @@ -0,0 +1,15 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class TopLevel { | |
| 6 | + private DataFormat format; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * The format of the data. | |
| 10 | + */ | |
| 11 | + @JsonProperty("format") | |
| 12 | + public DataFormat getFormat() { return format; } | |
| 13 | + @JsonProperty("format") | |
| 14 | + public void setFormat(DataFormat value) { this.format = value; } | |
| 15 | +} |
Aschema-javascriptdefault / TopLevel.js+188 −0
| @@ -0,0 +1,188 @@ | ||
| 1 | +// To parse this data: | |
| 2 | +// | |
| 3 | +// const Convert = require("./TopLevel"); | |
| 4 | +// | |
| 5 | +// const topLevel = Convert.toTopLevel(json); | |
| 6 | +// | |
| 7 | +// These functions will throw an error if the JSON doesn't | |
| 8 | +// match the expected interface, even if the JSON is valid. | |
| 9 | + | |
| 10 | +// Converts JSON strings to/from your types | |
| 11 | +// and asserts the results of JSON.parse at runtime | |
| 12 | +function toTopLevel(json) { | |
| 13 | + return cast(JSON.parse(json), r("TopLevel")); | |
| 14 | +} | |
| 15 | + | |
| 16 | +function topLevelToJson(value) { | |
| 17 | + return JSON.stringify(uncast(value, r("TopLevel")), null, 2); | |
| 18 | +} | |
| 19 | + | |
| 20 | +function invalidValue(typ, val, key, parent = '') { | |
| 21 | + const prettyTyp = prettyTypeName(typ); | |
| 22 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 23 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 24 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 25 | +} | |
| 26 | + | |
| 27 | +function prettyTypeName(typ) { | |
| 28 | + if (Array.isArray(typ)) { | |
| 29 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 30 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 31 | + } else { | |
| 32 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 33 | + } | |
| 34 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 35 | + return typ.literal; | |
| 36 | + } else { | |
| 37 | + return typeof typ; | |
| 38 | + } | |
| 39 | +} | |
| 40 | + | |
| 41 | +function jsonToJSProps(typ) { | |
| 42 | + if (typ.jsonToJS === undefined) { | |
| 43 | + const map = {}; | |
| 44 | + typ.props.forEach((p) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 45 | + typ.jsonToJS = map; | |
| 46 | + } | |
| 47 | + return typ.jsonToJS; | |
| 48 | +} | |
| 49 | + | |
| 50 | +function jsToJSONProps(typ) { | |
| 51 | + if (typ.jsToJSON === undefined) { | |
| 52 | + const map = {}; | |
| 53 | + typ.props.forEach((p) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 54 | + typ.jsToJSON = map; | |
| 55 | + } | |
| 56 | + return typ.jsToJSON; | |
| 57 | +} | |
| 58 | + | |
| 59 | +function transform(val, typ, getProps, key = '', parent = '') { | |
| 60 | + function transformPrimitive(typ, val) { | |
| 61 | + if (typeof typ === typeof val) return val; | |
| 62 | + return invalidValue(typ, val, key, parent); | |
| 63 | + } | |
| 64 | + | |
| 65 | + function transformUnion(typs, val) { | |
| 66 | + // val must validate against one typ in typs | |
| 67 | + const l = typs.length; | |
| 68 | + for (let i = 0; i < l; i++) { | |
| 69 | + const typ = typs[i]; | |
| 70 | + try { | |
| 71 | + return transform(val, typ, getProps); | |
| 72 | + } catch (_) {} | |
| 73 | + } | |
| 74 | + return invalidValue(typs, val, key, parent); | |
| 75 | + } | |
| 76 | + | |
| 77 | + function transformEnum(cases, val) { | |
| 78 | + if (cases.indexOf(val) !== -1) return val; | |
| 79 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 80 | + } | |
| 81 | + | |
| 82 | + function transformArray(typ, val) { | |
| 83 | + // val must be an array with no invalid elements | |
| 84 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 85 | + return val.map(el => transform(el, typ, getProps)); | |
| 86 | + } | |
| 87 | + | |
| 88 | + function transformDate(val) { | |
| 89 | + if (val === null) { | |
| 90 | + return null; | |
| 91 | + } | |
| 92 | + const d = new Date(val); | |
| 93 | + if (isNaN(d.valueOf())) { | |
| 94 | + return invalidValue(l("Date"), val, key, parent); | |
| 95 | + } | |
| 96 | + return d; | |
| 97 | + } | |
| 98 | + | |
| 99 | + function transformObject(props, additional, val) { | |
| 100 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 101 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 102 | + } | |
| 103 | + const result = {}; | |
| 104 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 105 | + const prop = props[key]; | |
| 106 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 107 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 108 | + }); | |
| 109 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 110 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 111 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 112 | + } | |
| 113 | + }); | |
| 114 | + return result; | |
| 115 | + } | |
| 116 | + | |
| 117 | + if (typ === "any") return val; | |
| 118 | + if (typ === null) { | |
| 119 | + if (val === null) return val; | |
| 120 | + return invalidValue(typ, val, key, parent); | |
| 121 | + } | |
| 122 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 123 | + let ref = undefined; | |
| 124 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 125 | + ref = typ.ref; | |
| 126 | + typ = typeMap[typ.ref]; | |
| 127 | + } | |
| 128 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 129 | + if (typeof typ === "object") { | |
| 130 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 131 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 132 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 133 | + : invalidValue(typ, val, key, parent); | |
| 134 | + } | |
| 135 | + // Numbers can be parsed by Date but shouldn't be. | |
| 136 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 137 | + return transformPrimitive(typ, val); | |
| 138 | +} | |
| 139 | + | |
| 140 | +function cast(val, typ) { | |
| 141 | + return transform(val, typ, jsonToJSProps); | |
| 142 | +} | |
| 143 | + | |
| 144 | +function uncast(val, typ) { | |
| 145 | + return transform(val, typ, jsToJSONProps); | |
| 146 | +} | |
| 147 | + | |
| 148 | +function l(typ) { | |
| 149 | + return { literal: typ }; | |
| 150 | +} | |
| 151 | + | |
| 152 | +function a(typ) { | |
| 153 | + return { arrayItems: typ }; | |
| 154 | +} | |
| 155 | + | |
| 156 | +function u(...typs) { | |
| 157 | + return { unionMembers: typs }; | |
| 158 | +} | |
| 159 | + | |
| 160 | +function o(props, additional) { | |
| 161 | + return { props, additional }; | |
| 162 | +} | |
| 163 | + | |
| 164 | +function m(additional) { | |
| 165 | + const props = []; | |
| 166 | + return { props, additional }; | |
| 167 | +} | |
| 168 | + | |
| 169 | +function r(name) { | |
| 170 | + return { ref: name }; | |
| 171 | +} | |
| 172 | + | |
| 173 | +const typeMap = { | |
| 174 | + "TopLevel": o([ | |
| 175 | + { json: "format", js: "format", typ: u(r("DataFormat"), null) }, | |
| 176 | + ], false), | |
| 177 | + "DataFormat": [ | |
| 178 | + "turtle", | |
| 179 | + "rdf_xml", | |
| 180 | + "n_triples", | |
| 181 | + "n_quads", | |
| 182 | + ], | |
| 183 | +}; | |
| 184 | + | |
| 185 | +module.exports = { | |
| 186 | + "topLevelToJson": topLevelToJson, | |
| 187 | + "toTopLevel": toTopLevel, | |
| 188 | +}; |
Aschema-kotlin-jacksondefault / TopLevel.kt+72 −0
| @@ -0,0 +1,72 @@ | ||
| 1 | +// To parse the JSON, install jackson-module-kotlin and do: | |
| 2 | +// | |
| 3 | +// val topLevel = TopLevel.fromJson(jsonString) | |
| 4 | + | |
| 5 | +package quicktype | |
| 6 | + | |
| 7 | +import com.fasterxml.jackson.annotation.* | |
| 8 | +import com.fasterxml.jackson.core.* | |
| 9 | +import com.fasterxml.jackson.databind.* | |
| 10 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer | |
| 11 | +import com.fasterxml.jackson.databind.module.SimpleModule | |
| 12 | +import com.fasterxml.jackson.databind.node.* | |
| 13 | +import com.fasterxml.jackson.databind.ser.std.StdSerializer | |
| 14 | +import com.fasterxml.jackson.module.kotlin.* | |
| 15 | + | |
| 16 | + | |
| 17 | +@Suppress("UNCHECKED_CAST") | |
| 18 | +private fun <T> ObjectMapper.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonNode) -> T, toJson: (T) -> String, isUnion: Boolean = false) = registerModule(SimpleModule().apply { | |
| 19 | + addSerializer(k.java as Class<T>, object : StdSerializer<T>(k.java as Class<T>) { | |
| 20 | + override fun serialize(value: T, gen: JsonGenerator, provider: SerializerProvider) = gen.writeRawValue(toJson(value)) | |
| 21 | + }) | |
| 22 | + addDeserializer(k.java as Class<T>, object : StdDeserializer<T>(k.java as Class<T>) { | |
| 23 | + override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = fromJson(p.readValueAsTree()) | |
| 24 | + }) | |
| 25 | +}) | |
| 26 | + | |
| 27 | +val mapper = jacksonObjectMapper().apply { | |
| 28 | + propertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE | |
| 29 | + setSerializationInclusion(JsonInclude.Include.NON_NULL) | |
| 30 | + convert(DataFormat::class, { DataFormat.fromValue(it.asText()) }, { "\"${it.value}\"" }) | |
| 31 | +} | |
| 32 | + | |
| 33 | +data class TopLevel ( | |
| 34 | + /** | |
| 35 | + * The format of the data. | |
| 36 | + */ | |
| 37 | + val format: DataFormat? = null | |
| 38 | +) { | |
| 39 | + fun toJson() = mapper.writeValueAsString(this) | |
| 40 | + | |
| 41 | + companion object { | |
| 42 | + fun fromJson(json: String) = mapper.readValue<TopLevel>(json) | |
| 43 | + } | |
| 44 | +} | |
| 45 | + | |
| 46 | +/** | |
| 47 | + * The format of the data. | |
| 48 | + * | |
| 49 | + * Turtle format | |
| 50 | + * | |
| 51 | + * RDF XML format | |
| 52 | + * | |
| 53 | + * N-Triples format | |
| 54 | + * | |
| 55 | + * N-Quads format | |
| 56 | + */ | |
| 57 | +enum class DataFormat(val value: String) { | |
| 58 | + Turtle("turtle"), | |
| 59 | + RDFXML("rdf_xml"), | |
| 60 | + NTriples("n_triples"), | |
| 61 | + NQuads("n_quads"); | |
| 62 | + | |
| 63 | + companion object { | |
| 64 | + fun fromValue(value: String): DataFormat = when (value) { | |
| 65 | + "turtle" -> Turtle | |
| 66 | + "rdf_xml" -> RDFXML | |
| 67 | + "n_triples" -> NTriples | |
| 68 | + "n_quads" -> NQuads | |
| 69 | + else -> throw IllegalArgumentException() | |
| 70 | + } | |
| 71 | + } | |
| 72 | +} |
Aschema-kotlindefault / TopLevel.kt+59 −0
| @@ -0,0 +1,59 @@ | ||
| 1 | +// To parse the JSON, install Klaxon and do: | |
| 2 | +// | |
| 3 | +// val topLevel = TopLevel.fromJson(jsonString) | |
| 4 | + | |
| 5 | +package quicktype | |
| 6 | + | |
| 7 | +import com.beust.klaxon.* | |
| 8 | + | |
| 9 | +private fun <T> Klaxon.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonValue) -> T, toJson: (T) -> String, isUnion: Boolean = false) = | |
| 10 | + this.converter(object: Converter { | |
| 11 | + @Suppress("UNCHECKED_CAST") | |
| 12 | + override fun toJson(value: Any) = toJson(value as T) | |
| 13 | + override fun fromJson(jv: JsonValue) = fromJson(jv) as Any | |
| 14 | + override fun canConvert(cls: Class<*>) = cls == k.java || (isUnion && cls.superclass == k.java) | |
| 15 | + }) | |
| 16 | + | |
| 17 | +private val klaxon = Klaxon() | |
| 18 | + .convert(DataFormat::class, { DataFormat.fromValue(it.string!!) }, { "\"${it.value}\"" }) | |
| 19 | + | |
| 20 | +data class TopLevel ( | |
| 21 | + /** | |
| 22 | + * The format of the data. | |
| 23 | + */ | |
| 24 | + val format: DataFormat? = null | |
| 25 | +) { | |
| 26 | + public fun toJson() = klaxon.toJsonString(this) | |
| 27 | + | |
| 28 | + companion object { | |
| 29 | + public fun fromJson(json: String) = klaxon.parse<TopLevel>(json) | |
| 30 | + } | |
| 31 | +} | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * The format of the data. | |
| 35 | + * | |
| 36 | + * Turtle format | |
| 37 | + * | |
| 38 | + * RDF XML format | |
| 39 | + * | |
| 40 | + * N-Triples format | |
| 41 | + * | |
| 42 | + * N-Quads format | |
| 43 | + */ | |
| 44 | +enum class DataFormat(val value: String) { | |
| 45 | + Turtle("turtle"), | |
| 46 | + RDFXML("rdf_xml"), | |
| 47 | + NTriples("n_triples"), | |
| 48 | + NQuads("n_quads"); | |
| 49 | + | |
| 50 | + companion object { | |
| 51 | + public fun fromValue(value: String): DataFormat = when (value) { | |
| 52 | + "turtle" -> Turtle | |
| 53 | + "rdf_xml" -> RDFXML | |
| 54 | + "n_triples" -> NTriples | |
| 55 | + "n_quads" -> NQuads | |
| 56 | + else -> throw IllegalArgumentException() | |
| 57 | + } | |
| 58 | + } | |
| 59 | +} |
Aschema-kotlinxdefault / TopLevel.kt+38 −0
| @@ -0,0 +1,38 @@ | ||
| 1 | +// To parse the JSON, install kotlin's serialization plugin and do: | |
| 2 | +// | |
| 3 | +// val json = Json { allowStructuredMapKeys = true } | |
| 4 | +// val topLevel = json.parse(TopLevel.serializer(), jsonString) | |
| 5 | + | |
| 6 | +package quicktype | |
| 7 | + | |
| 8 | +import kotlinx.serialization.* | |
| 9 | +import kotlinx.serialization.json.* | |
| 10 | +import kotlinx.serialization.descriptors.* | |
| 11 | +import kotlinx.serialization.encoding.* | |
| 12 | + | |
| 13 | +@Serializable | |
| 14 | +data class TopLevel ( | |
| 15 | + /** | |
| 16 | + * The format of the data. | |
| 17 | + */ | |
| 18 | + val format: DataFormat? = null | |
| 19 | +) | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * The format of the data. | |
| 23 | + * | |
| 24 | + * Turtle format | |
| 25 | + * | |
| 26 | + * RDF XML format | |
| 27 | + * | |
| 28 | + * N-Triples format | |
| 29 | + * | |
| 30 | + * N-Quads format | |
| 31 | + */ | |
| 32 | +@Serializable | |
| 33 | +enum class DataFormat(val value: String) { | |
| 34 | + @SerialName("turtle") Turtle("turtle"), | |
| 35 | + @SerialName("rdf_xml") RDFXML("rdf_xml"), | |
| 36 | + @SerialName("n_triples") NTriples("n_triples"), | |
| 37 | + @SerialName("n_quads") NQuads("n_quads"); | |
| 38 | +} |
Aschema-phpdefault / TopLevel.php+189 −0
| @@ -0,0 +1,189 @@ | ||
| 1 | +<?php | |
| 2 | +declare(strict_types=1); | |
| 3 | + | |
| 4 | +// This is an autogenerated file:TopLevel | |
| 5 | + | |
| 6 | +class TopLevel { | |
| 7 | + private ?DataFormat $format; // json:format Optional | |
| 8 | + | |
| 9 | + /** | |
| 10 | + * @param DataFormat|null $format | |
| 11 | + */ | |
| 12 | + public function __construct(?DataFormat $format) { | |
| 13 | + $this->format = $format; | |
| 14 | + } | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * The format of the data. | |
| 18 | + * | |
| 19 | + * @param ?string $value | |
| 20 | + * @throws Exception | |
| 21 | + * @return ?DataFormat | |
| 22 | + */ | |
| 23 | + public static function fromFormat(?string $value): ?DataFormat { | |
| 24 | + if (!is_null($value)) { | |
| 25 | + return DataFormat::from($value); /*enum*/ | |
| 26 | + } else { | |
| 27 | + return null; | |
| 28 | + } | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * The format of the data. | |
| 33 | + * | |
| 34 | + * @throws Exception | |
| 35 | + * @return ?string | |
| 36 | + */ | |
| 37 | + public function toFormat(): ?string { | |
| 38 | + if (TopLevel::validateFormat($this->format)) { | |
| 39 | + if (!is_null($this->format)) { | |
| 40 | + return DataFormat::to($this->format); /*enum*/ | |
| 41 | + } else { | |
| 42 | + return null; | |
| 43 | + } | |
| 44 | + } | |
| 45 | + throw new Exception('never get to this TopLevel::format'); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * The format of the data. | |
| 50 | + * | |
| 51 | + * @param DataFormat|null | |
| 52 | + * @return bool | |
| 53 | + * @throws Exception | |
| 54 | + */ | |
| 55 | + public static function validateFormat(?DataFormat $value): bool { | |
| 56 | + if (!is_null($value)) { | |
| 57 | + DataFormat::to($value); | |
| 58 | + } | |
| 59 | + return true; | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * The format of the data. | |
| 64 | + * | |
| 65 | + * @throws Exception | |
| 66 | + * @return ?DataFormat | |
| 67 | + */ | |
| 68 | + public function getFormat(): ?DataFormat { | |
| 69 | + if (TopLevel::validateFormat($this->format)) { | |
| 70 | + return $this->format; | |
| 71 | + } | |
| 72 | + throw new Exception('never get to getFormat TopLevel::format'); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * The format of the data. | |
| 77 | + * | |
| 78 | + * @return ?DataFormat | |
| 79 | + */ | |
| 80 | + public static function sampleFormat(): ?DataFormat { | |
| 81 | + return DataFormat::sample(); /*enum*/ | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * @throws Exception | |
| 86 | + * @return bool | |
| 87 | + */ | |
| 88 | + public function validate(): bool { | |
| 89 | + return TopLevel::validateFormat($this->format); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * @return stdClass | |
| 94 | + * @throws Exception | |
| 95 | + */ | |
| 96 | + public function to(): stdClass { | |
| 97 | + $out = new stdClass(); | |
| 98 | + $out->{'format'} = $this->toFormat(); | |
| 99 | + return $out; | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * @param stdClass $obj | |
| 104 | + * @return TopLevel | |
| 105 | + * @throws Exception | |
| 106 | + */ | |
| 107 | + public static function from(stdClass $obj): TopLevel { | |
| 108 | + return new TopLevel( | |
| 109 | + TopLevel::fromFormat($obj->{'format'}) | |
| 110 | + ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * @return TopLevel | |
| 115 | + */ | |
| 116 | + public static function sample(): TopLevel { | |
| 117 | + return new TopLevel( | |
| 118 | + TopLevel::sampleFormat() | |
| 119 | + ); | |
| 120 | + } | |
| 121 | +} | |
| 122 | + | |
| 123 | +// This is an autogenerated file:DataFormat | |
| 124 | + | |
| 125 | +/** | |
| 126 | + * The format of the data. | |
| 127 | + * | |
| 128 | + * Turtle format | |
| 129 | + * | |
| 130 | + * RDF XML format | |
| 131 | + * | |
| 132 | + * N-Triples format | |
| 133 | + * | |
| 134 | + * N-Quads format | |
| 135 | + */ | |
| 136 | +class DataFormat { | |
| 137 | + public static DataFormat $TURTLE; | |
| 138 | + public static DataFormat $RDF_XML; | |
| 139 | + public static DataFormat $N_TRIPLES; | |
| 140 | + public static DataFormat $N_QUADS; | |
| 141 | + public static function init() { | |
| 142 | + DataFormat::$TURTLE = new DataFormat('turtle'); | |
| 143 | + DataFormat::$RDF_XML = new DataFormat('rdf_xml'); | |
| 144 | + DataFormat::$N_TRIPLES = new DataFormat('n_triples'); | |
| 145 | + DataFormat::$N_QUADS = new DataFormat('n_quads'); | |
| 146 | + } | |
| 147 | + private string $enum; | |
| 148 | + public function __construct(string $enum) { | |
| 149 | + $this->enum = $enum; | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * @param DataFormat | |
| 154 | + * @return string | |
| 155 | + * @throws Exception | |
| 156 | + */ | |
| 157 | + public static function to(DataFormat $obj): string { | |
| 158 | + switch ($obj->enum) { | |
| 159 | + case DataFormat::$TURTLE->enum: return 'turtle'; | |
| 160 | + case DataFormat::$RDF_XML->enum: return 'rdf_xml'; | |
| 161 | + case DataFormat::$N_TRIPLES->enum: return 'n_triples'; | |
| 162 | + case DataFormat::$N_QUADS->enum: return 'n_quads'; | |
| 163 | + } | |
| 164 | + throw new Exception('the give value is not an enum-value.'); | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * @param mixed | |
| 169 | + * @return DataFormat | |
| 170 | + * @throws Exception | |
| 171 | + */ | |
| 172 | + public static function from($obj): DataFormat { | |
| 173 | + switch ($obj) { | |
| 174 | + case 'turtle': return DataFormat::$TURTLE; | |
| 175 | + case 'rdf_xml': return DataFormat::$RDF_XML; | |
| 176 | + case 'n_triples': return DataFormat::$N_TRIPLES; | |
| 177 | + case 'n_quads': return DataFormat::$N_QUADS; | |
| 178 | + } | |
| 179 | + throw new Exception("Cannot deserialize DataFormat"); | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * @return DataFormat | |
| 184 | + */ | |
| 185 | + public static function sample(): DataFormat { | |
| 186 | + return DataFormat::$TURTLE; | |
| 187 | + } | |
| 188 | +} | |
| 189 | +DataFormat::init(); |
Aschema-pikedefault / TopLevel.pmod+40 −0
| @@ -0,0 +1,40 @@ | ||
| 1 | +// This source has been automatically generated by quicktype. | |
| 2 | +// ( https://github.com/quicktype/quicktype ) | |
| 3 | +// | |
| 4 | +// To use this code, simply import it into your project as a Pike module. | |
| 5 | +// To JSON-encode your object, you can pass it to `Standards.JSON.encode` | |
| 6 | +// or call `encode_json` on it. | |
| 7 | +// | |
| 8 | +// To decode a JSON string, first pass it to `Standards.JSON.decode`, | |
| 9 | +// and then pass the result to `<YourClass>_from_JSON`. | |
| 10 | +// It will return an instance of <YourClass>. | |
| 11 | +// Bear in mind that these functions have unexpected behavior, | |
| 12 | +// and will likely throw an error, if the JSON string does not | |
| 13 | +// match the expected interface, even if the JSON itself is valid. | |
| 14 | + | |
| 15 | +class TopLevel { | |
| 16 | + DataFormat|mixed format; // json: "format" | |
| 17 | + | |
| 18 | + string encode_json() { | |
| 19 | + mapping(string:mixed) json = ([ | |
| 20 | + "format" : format, | |
| 21 | + ]); | |
| 22 | + | |
| 23 | + return Standards.JSON.encode(json); | |
| 24 | + } | |
| 25 | +} | |
| 26 | + | |
| 27 | +TopLevel TopLevel_from_JSON(mixed json) { | |
| 28 | + TopLevel retval = TopLevel(); | |
| 29 | + | |
| 30 | + retval.format = json["format"]; | |
| 31 | + | |
| 32 | + return retval; | |
| 33 | +} | |
| 34 | + | |
| 35 | +enum DataFormat { | |
| 36 | + TURTLE = "turtle", // json: "turtle" | |
| 37 | + RDF_XML = "rdf_xml", // json: "rdf_xml" | |
| 38 | + N_TRIPLES = "n_triples", // json: "n_triples" | |
| 39 | + N_QUADS = "n_quads", // json: "n_quads" | |
| 40 | +} |
Aschema-pythondefault / quicktype.py+73 −0
| @@ -0,0 +1,73 @@ | ||
| 1 | +from enum import Enum | |
| 2 | +from dataclasses import dataclass | |
| 3 | +from typing import Any, TypeVar, Type, cast | |
| 4 | + | |
| 5 | + | |
| 6 | +T = TypeVar("T") | |
| 7 | +EnumT = TypeVar("EnumT", bound=Enum) | |
| 8 | + | |
| 9 | + | |
| 10 | +def from_none(x: Any) -> Any: | |
| 11 | + assert x is None | |
| 12 | + return x | |
| 13 | + | |
| 14 | + | |
| 15 | +def from_union(fs, x): | |
| 16 | + for f in fs: | |
| 17 | + try: | |
| 18 | + return f(x) | |
| 19 | + except: | |
| 20 | + pass | |
| 21 | + assert False | |
| 22 | + | |
| 23 | + | |
| 24 | +def to_enum(c: Type[EnumT], x: Any) -> EnumT: | |
| 25 | + assert isinstance(x, c) | |
| 26 | + return x.value | |
| 27 | + | |
| 28 | + | |
| 29 | +def to_class(c: Type[T], x: Any) -> dict: | |
| 30 | + assert isinstance(x, c) | |
| 31 | + return cast(Any, x).to_dict() | |
| 32 | + | |
| 33 | + | |
| 34 | +class DataFormat(Enum): | |
| 35 | + """The format of the data. | |
| 36 | + | |
| 37 | + Turtle format | |
| 38 | + | |
| 39 | + RDF XML format | |
| 40 | + | |
| 41 | + N-Triples format | |
| 42 | + | |
| 43 | + N-Quads format | |
| 44 | + """ | |
| 45 | + TURTLE = "turtle" | |
| 46 | + RDF_XML = "rdf_xml" | |
| 47 | + N_TRIPLES = "n_triples" | |
| 48 | + N_QUADS = "n_quads" | |
| 49 | + | |
| 50 | + | |
| 51 | +@dataclass | |
| 52 | +class TopLevel: | |
| 53 | + format: DataFormat | None = None | |
| 54 | + """The format of the data.""" | |
| 55 | + | |
| 56 | + @staticmethod | |
| 57 | + def from_dict(obj: Any) -> 'TopLevel': | |
| 58 | + assert isinstance(obj, dict) | |
| 59 | + format = from_union([from_none, DataFormat], obj.get("format")) | |
| 60 | + return TopLevel(format) | |
| 61 | + | |
| 62 | + def to_dict(self) -> dict: | |
| 63 | + result: dict = {} | |
| 64 | + result["format"] = from_union([from_none, lambda x: to_enum(DataFormat, x)], self.format) | |
| 65 | + return result | |
| 66 | + | |
| 67 | + | |
| 68 | +def top_level_from_dict(s: Any) -> TopLevel: | |
| 69 | + return TopLevel.from_dict(s) | |
| 70 | + | |
| 71 | + | |
| 72 | +def top_level_to_dict(x: TopLevel) -> Any: | |
| 73 | + return to_class(TopLevel, x) |
Aschema-rubydefault / TopLevel.rb+65 −0
| @@ -0,0 +1,65 @@ | ||
| 1 | +# This code may look unusually verbose for Ruby (and it is), but | |
| 2 | +# it performs some subtle and complex validation of JSON data. | |
| 3 | +# | |
| 4 | +# To parse this JSON, add 'dry-struct' and 'dry-types' gems, then do: | |
| 5 | +# | |
| 6 | +# top_level = TopLevel.from_json! "{…}" | |
| 7 | +# puts top_level.top_level_format.nil? | |
| 8 | +# | |
| 9 | +# If from_json! succeeds, the value returned matches the schema. | |
| 10 | + | |
| 11 | +require 'json' | |
| 12 | +require 'dry-types' | |
| 13 | +require 'dry-struct' | |
| 14 | + | |
| 15 | +module Types | |
| 16 | + include Dry.Types(default: :nominal) | |
| 17 | + | |
| 18 | + Nil = Strict::Nil | |
| 19 | + Hash = Strict::Hash | |
| 20 | + String = Strict::String | |
| 21 | + DataFormat = Strict::String.enum("turtle", "rdf_xml", "n_triples", "n_quads") | |
| 22 | +end | |
| 23 | + | |
| 24 | +# The format of the data. | |
| 25 | +# | |
| 26 | +# Turtle format | |
| 27 | +# | |
| 28 | +# RDF XML format | |
| 29 | +# | |
| 30 | +# N-Triples format | |
| 31 | +# | |
| 32 | +# N-Quads format | |
| 33 | +module DataFormat | |
| 34 | + Turtle = "turtle" | |
| 35 | + RDFXML = "rdf_xml" | |
| 36 | + NTriples = "n_triples" | |
| 37 | + NQuads = "n_quads" | |
| 38 | +end | |
| 39 | + | |
| 40 | +class TopLevel < Dry::Struct | |
| 41 | + | |
| 42 | + # The format of the data. | |
| 43 | + attribute :top_level_format, Types::DataFormat.optional | |
| 44 | + | |
| 45 | + def self.from_dynamic!(d) | |
| 46 | + d = Types::Hash[d] | |
| 47 | + new( | |
| 48 | + top_level_format: d.fetch("format"), | |
| 49 | + ) | |
| 50 | + end | |
| 51 | + | |
| 52 | + def self.from_json!(json) | |
| 53 | + from_dynamic!(JSON.parse(json)) | |
| 54 | + end | |
| 55 | + | |
| 56 | + def to_dynamic | |
| 57 | + { | |
| 58 | + "format" => top_level_format, | |
| 59 | + } | |
| 60 | + end | |
| 61 | + | |
| 62 | + def to_json(options = nil) | |
| 63 | + JSON.generate(to_dynamic, options) | |
| 64 | + end | |
| 65 | +end |
Aschema-rustdefault / module_under_test.rs+44 −0
| @@ -0,0 +1,44 @@ | ||
| 1 | +// Example code that deserializes and serializes the model. | |
| 2 | +// extern crate serde; | |
| 3 | +// #[macro_use] | |
| 4 | +// extern crate serde_derive; | |
| 5 | +// extern crate serde_json; | |
| 6 | +// | |
| 7 | +// use generated_module::TopLevel; | |
| 8 | +// | |
| 9 | +// fn main() { | |
| 10 | +// let json = r#"{"answer": 42}"#; | |
| 11 | +// let model: TopLevel = serde_json::from_str(&json).unwrap(); | |
| 12 | +// } | |
| 13 | + | |
| 14 | +use serde::{Serialize, Deserialize}; | |
| 15 | + | |
| 16 | +#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 17 | +pub struct TopLevel { | |
| 18 | + /// The format of the data. | |
| 19 | + pub format: Option<DataFormat>, | |
| 20 | +} | |
| 21 | + | |
| 22 | +/// The format of the data. | |
| 23 | +/// | |
| 24 | +/// Turtle format | |
| 25 | +/// | |
| 26 | +/// RDF XML format | |
| 27 | +/// | |
| 28 | +/// N-Triples format | |
| 29 | +/// | |
| 30 | +/// N-Quads format | |
| 31 | +#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 32 | +#[serde(rename_all = "snake_case")] | |
| 33 | +pub enum DataFormat { | |
| 34 | + Turtle, | |
| 35 | + | |
| 36 | + #[serde(rename = "rdf_xml")] | |
| 37 | + RdfXml, | |
| 38 | + | |
| 39 | + #[serde(rename = "n_triples")] | |
| 40 | + NTriples, | |
| 41 | + | |
| 42 | + #[serde(rename = "n_quads")] | |
| 43 | + NQuads, | |
| 44 | +} |
Aschema-scala3-upickledefault / TopLevel.scala+107 −0
| @@ -0,0 +1,107 @@ | ||
| 1 | +package quicktype | |
| 2 | + | |
| 3 | +// Custom pickler so that missing keys and JSON nulls both read as None, | |
| 4 | +// and None is left out when writing (upickle's default for Option is a | |
| 5 | +// JSON array). | |
| 6 | +object OptionPickler extends upickle.AttributeTagged: | |
| 7 | + import upickle.default.Writer | |
| 8 | + import upickle.default.Reader | |
| 9 | + override implicit def OptionWriter[T: Writer]: Writer[Option[T]] = | |
| 10 | + implicitly[Writer[T]].comap[Option[T]] { | |
| 11 | + case None => null.asInstanceOf[T] | |
| 12 | + case Some(x) => x | |
| 13 | + } | |
| 14 | + | |
| 15 | + override implicit def OptionReader[T: Reader]: Reader[Option[T]] = { | |
| 16 | + new Reader.Delegate[Any, Option[T]](implicitly[Reader[T]].map(Some(_))){ | |
| 17 | + override def visitNull(index: Int) = None | |
| 18 | + } | |
| 19 | + } | |
| 20 | +end OptionPickler | |
| 21 | + | |
| 22 | +// If a union has a null in, then we'll need this too... | |
| 23 | +type NullValue = None.type | |
| 24 | +given OptionPickler.ReadWriter[NullValue] = OptionPickler.readwriter[ujson.Value].bimap[NullValue]( | |
| 25 | + _ => ujson.Null, | |
| 26 | + json => if json.isNull then None else throw new upickle.core.Abort("not null") | |
| 27 | +) | |
| 28 | + | |
| 29 | +object JsonExt: | |
| 30 | + val valueReader = OptionPickler.readwriter[ujson.Value] | |
| 31 | + | |
| 32 | + // upickle's built-in primitive readers are lenient -- the numeric and | |
| 33 | + // boolean readers accept strings, and the string reader accepts | |
| 34 | + // numbers and booleans -- so untagged unions need strict readers to | |
| 35 | + // pick the right member. | |
| 36 | + val strictString: OptionPickler.Reader[String] = valueReader.map { | |
| 37 | + case ujson.Str(s) => s | |
| 38 | + case json => throw new upickle.core.Abort("expected string, got " + json) | |
| 39 | + } | |
| 40 | + val strictLong: OptionPickler.Reader[Long] = valueReader.map { | |
| 41 | + case ujson.Num(n) if n.isWhole => n.toLong | |
| 42 | + case json => throw new upickle.core.Abort("expected integer, got " + json) | |
| 43 | + } | |
| 44 | + val strictDouble: OptionPickler.Reader[Double] = valueReader.map { | |
| 45 | + case ujson.Num(n) => n | |
| 46 | + case json => throw new upickle.core.Abort("expected number, got " + json) | |
| 47 | + } | |
| 48 | + val strictBoolean: OptionPickler.Reader[Boolean] = valueReader.map { | |
| 49 | + case ujson.Bool(b) => b | |
| 50 | + case json => throw new upickle.core.Abort("expected boolean, got " + json) | |
| 51 | + } | |
| 52 | + | |
| 53 | + def badMerge[T](r1: => OptionPickler.Reader[?], rest: OptionPickler.Reader[?]*): OptionPickler.Reader[T] = valueReader.map { json => | |
| 54 | + var t: T | Null = null | |
| 55 | + val stack = Vector.newBuilder[Throwable] | |
| 56 | + (r1 +: rest).foreach { reader => | |
| 57 | + if t == null then | |
| 58 | + try | |
| 59 | + t = OptionPickler.read[T](json, trace = true)(using reader.asInstanceOf[OptionPickler.Reader[T]]) | |
| 60 | + catch | |
| 61 | + case exc => stack += exc | |
| 62 | + } | |
| 63 | + if t != null then t.nn else throw new Exception(json.toString(), stack.result().headOption.getOrElse(null)) | |
| 64 | + } | |
| 65 | +end JsonExt | |
| 66 | + | |
| 67 | + | |
| 68 | +case class TopLevel ( | |
| 69 | + /** | |
| 70 | + * The format of the data. | |
| 71 | + */ | |
| 72 | + val format : Option[DataFormat] = None | |
| 73 | +) derives OptionPickler.ReadWriter | |
| 74 | + | |
| 75 | +/** | |
| 76 | + * The format of the data. | |
| 77 | + * | |
| 78 | + * Turtle format | |
| 79 | + * | |
| 80 | + * RDF XML format | |
| 81 | + * | |
| 82 | + * N-Triples format | |
| 83 | + * | |
| 84 | + * N-Quads format | |
| 85 | + */ | |
| 86 | + | |
| 87 | +enum DataFormat : | |
| 88 | + case Turtle | |
| 89 | + case RDFXML | |
| 90 | + case NTriples | |
| 91 | + case NQuads | |
| 92 | + | |
| 93 | +given OptionPickler.ReadWriter[DataFormat] = OptionPickler.readwriter[String].bimap[DataFormat]( | |
| 94 | + { | |
| 95 | + case DataFormat.Turtle => "turtle" | |
| 96 | + case DataFormat.RDFXML => "rdf_xml" | |
| 97 | + case DataFormat.NTriples => "n_triples" | |
| 98 | + case DataFormat.NQuads => "n_quads" | |
| 99 | + }, | |
| 100 | + { | |
| 101 | + case "turtle" => DataFormat.Turtle | |
| 102 | + case "rdf_xml" => DataFormat.RDFXML | |
| 103 | + case "n_triples" => DataFormat.NTriples | |
| 104 | + case "n_quads" => DataFormat.NQuads | |
| 105 | + case other => throw new upickle.core.Abort("invalid DataFormat: " + other) | |
| 106 | + } | |
| 107 | +) |
Aschema-scala3default / TopLevel.scala+47 −0
| @@ -0,0 +1,47 @@ | ||
| 1 | +package quicktype | |
| 2 | + | |
| 3 | +import io.circe.syntax._ | |
| 4 | +import io.circe._ | |
| 5 | +import cats.syntax.functor._ | |
| 6 | + | |
| 7 | +// If a union has a null in, then we'll need this too... | |
| 8 | +type NullValue = None.type | |
| 9 | + | |
| 10 | +case class TopLevel ( | |
| 11 | + /** | |
| 12 | + * The format of the data. | |
| 13 | + */ | |
| 14 | + val format : Option[DataFormat] = None | |
| 15 | +) derives Encoder.AsObject, Decoder | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * The format of the data. | |
| 19 | + * | |
| 20 | + * Turtle format | |
| 21 | + * | |
| 22 | + * RDF XML format | |
| 23 | + * | |
| 24 | + * N-Triples format | |
| 25 | + * | |
| 26 | + * N-Quads format | |
| 27 | + */ | |
| 28 | + | |
| 29 | +enum DataFormat : | |
| 30 | + case Turtle | |
| 31 | + case RDFXML | |
| 32 | + case NTriples | |
| 33 | + case NQuads | |
| 34 | + | |
| 35 | +given Decoder[DataFormat] = Decoder.decodeString.emap { | |
| 36 | + case "turtle" => scala.Right(DataFormat.Turtle) | |
| 37 | + case "rdf_xml" => scala.Right(DataFormat.RDFXML) | |
| 38 | + case "n_triples" => scala.Right(DataFormat.NTriples) | |
| 39 | + case "n_quads" => scala.Right(DataFormat.NQuads) | |
| 40 | + case other => scala.Left("invalid DataFormat: " + other) | |
| 41 | +} | |
| 42 | +given Encoder[DataFormat] = Encoder.encodeString.contramap { | |
| 43 | + case DataFormat.Turtle => "turtle" | |
| 44 | + case DataFormat.RDFXML => "rdf_xml" | |
| 45 | + case DataFormat.NTriples => "n_triples" | |
| 46 | + case DataFormat.NQuads => "n_quads" | |
| 47 | +} |
Aschema-schemadefault / TopLevel.schema+38 −0
| @@ -0,0 +1,38 @@ | ||
| 1 | +{ | |
| 2 | + "$schema": "http://json-schema.org/draft-06/schema#", | |
| 3 | + "$ref": "#/definitions/TopLevel", | |
| 4 | + "definitions": { | |
| 5 | + "TopLevel": { | |
| 6 | + "type": "object", | |
| 7 | + "additionalProperties": false, | |
| 8 | + "properties": { | |
| 9 | + "format": { | |
| 10 | + "anyOf": [ | |
| 11 | + { | |
| 12 | + "$ref": "#/definitions/DataFormat" | |
| 13 | + }, | |
| 14 | + { | |
| 15 | + "type": "null" | |
| 16 | + } | |
| 17 | + ], | |
| 18 | + "description": "The format of the data." | |
| 19 | + } | |
| 20 | + }, | |
| 21 | + "required": [ | |
| 22 | + "format" | |
| 23 | + ], | |
| 24 | + "title": "TopLevel" | |
| 25 | + }, | |
| 26 | + "DataFormat": { | |
| 27 | + "type": "string", | |
| 28 | + "enum": [ | |
| 29 | + "turtle", | |
| 30 | + "rdf_xml", | |
| 31 | + "n_triples", | |
| 32 | + "n_quads" | |
| 33 | + ], | |
| 34 | + "title": "DataFormat", | |
| 35 | + "description": "The format of the data.\nTurtle format\nRDF XML format\nN-Triples format\nN-Quads format" | |
| 36 | + } | |
| 37 | + } | |
| 38 | +} |
Aschema-typescriptdefault / TopLevel.ts+205 −0
| @@ -0,0 +1,205 @@ | ||
| 1 | +// To parse this data: | |
| 2 | +// | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | +// | |
| 5 | +// const topLevel = Convert.toTopLevel(json); | |
| 6 | +// | |
| 7 | +// These functions will throw an error if the JSON doesn't | |
| 8 | +// match the expected interface, even if the JSON is valid. | |
| 9 | + | |
| 10 | +export interface TopLevel { | |
| 11 | + /** | |
| 12 | + * The format of the data. | |
| 13 | + */ | |
| 14 | + format: DataFormat | null; | |
| 15 | +} | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * The format of the data. | |
| 19 | + * | |
| 20 | + * Turtle format | |
| 21 | + * | |
| 22 | + * RDF XML format | |
| 23 | + * | |
| 24 | + * N-Triples format | |
| 25 | + * | |
| 26 | + * N-Quads format | |
| 27 | + */ | |
| 28 | +export type DataFormat = "turtle" | "rdf_xml" | "n_triples" | "n_quads"; | |
| 29 | + | |
| 30 | +// Converts JSON strings to/from your types | |
| 31 | +// and asserts the results of JSON.parse at runtime | |
| 32 | +export class Convert { | |
| 33 | + public static toTopLevel(json: string): TopLevel { | |
| 34 | + return cast(JSON.parse(json), r("TopLevel")); | |
| 35 | + } | |
| 36 | + | |
| 37 | + public static topLevelToJson(value: TopLevel): string { | |
| 38 | + return JSON.stringify(uncast(value, r("TopLevel")), null, 2); | |
| 39 | + } | |
| 40 | +} | |
| 41 | + | |
| 42 | +function invalidValue(typ: any, val: any, key: any, parent: any = ''): never { | |
| 43 | + const prettyTyp = prettyTypeName(typ); | |
| 44 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 45 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 46 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 47 | +} | |
| 48 | + | |
| 49 | +function prettyTypeName(typ: any): string { | |
| 50 | + if (Array.isArray(typ)) { | |
| 51 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 52 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 53 | + } else { | |
| 54 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 55 | + } | |
| 56 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 57 | + return typ.literal; | |
| 58 | + } else { | |
| 59 | + return typeof typ; | |
| 60 | + } | |
| 61 | +} | |
| 62 | + | |
| 63 | +function jsonToJSProps(typ: any): any { | |
| 64 | + if (typ.jsonToJS === undefined) { | |
| 65 | + const map: any = {}; | |
| 66 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 67 | + typ.jsonToJS = map; | |
| 68 | + } | |
| 69 | + return typ.jsonToJS; | |
| 70 | +} | |
| 71 | + | |
| 72 | +function jsToJSONProps(typ: any): any { | |
| 73 | + if (typ.jsToJSON === undefined) { | |
| 74 | + const map: any = {}; | |
| 75 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 76 | + typ.jsToJSON = map; | |
| 77 | + } | |
| 78 | + return typ.jsToJSON; | |
| 79 | +} | |
| 80 | + | |
| 81 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 82 | + function transformPrimitive(typ: string, val: any): any { | |
| 83 | + if (typeof typ === typeof val) return val; | |
| 84 | + return invalidValue(typ, val, key, parent); | |
| 85 | + } | |
| 86 | + | |
| 87 | + function transformUnion(typs: any[], val: any): any { | |
| 88 | + // val must validate against one typ in typs | |
| 89 | + const l = typs.length; | |
| 90 | + for (let i = 0; i < l; i++) { | |
| 91 | + const typ = typs[i]; | |
| 92 | + try { | |
| 93 | + return transform(val, typ, getProps); | |
| 94 | + } catch (_) {} | |
| 95 | + } | |
| 96 | + return invalidValue(typs, val, key, parent); | |
| 97 | + } | |
| 98 | + | |
| 99 | + function transformEnum(cases: string[], val: any): any { | |
| 100 | + if (cases.indexOf(val) !== -1) return val; | |
| 101 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 102 | + } | |
| 103 | + | |
| 104 | + function transformArray(typ: any, val: any): any { | |
| 105 | + // val must be an array with no invalid elements | |
| 106 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 107 | + return val.map(el => transform(el, typ, getProps)); | |
| 108 | + } | |
| 109 | + | |
| 110 | + function transformDate(val: any): any { | |
| 111 | + if (val === null) { | |
| 112 | + return null; | |
| 113 | + } | |
| 114 | + const d = new Date(val); | |
| 115 | + if (isNaN(d.valueOf())) { | |
| 116 | + return invalidValue(l("Date"), val, key, parent); | |
| 117 | + } | |
| 118 | + return d; | |
| 119 | + } | |
| 120 | + | |
| 121 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 122 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 123 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 124 | + } | |
| 125 | + const result: any = {}; | |
| 126 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 127 | + const prop = props[key]; | |
| 128 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 129 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 130 | + }); | |
| 131 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 132 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 133 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 134 | + } | |
| 135 | + }); | |
| 136 | + return result; | |
| 137 | + } | |
| 138 | + | |
| 139 | + if (typ === "any") return val; | |
| 140 | + if (typ === null) { | |
| 141 | + if (val === null) return val; | |
| 142 | + return invalidValue(typ, val, key, parent); | |
| 143 | + } | |
| 144 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 145 | + let ref: any = undefined; | |
| 146 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 147 | + ref = typ.ref; | |
| 148 | + typ = typeMap[typ.ref]; | |
| 149 | + } | |
| 150 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 151 | + if (typeof typ === "object") { | |
| 152 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 153 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 154 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 155 | + : invalidValue(typ, val, key, parent); | |
| 156 | + } | |
| 157 | + // Numbers can be parsed by Date but shouldn't be. | |
| 158 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 159 | + return transformPrimitive(typ, val); | |
| 160 | +} | |
| 161 | + | |
| 162 | +function cast<T>(val: any, typ: any): T { | |
| 163 | + return transform(val, typ, jsonToJSProps); | |
| 164 | +} | |
| 165 | + | |
| 166 | +function uncast<T>(val: T, typ: any): any { | |
| 167 | + return transform(val, typ, jsToJSONProps); | |
| 168 | +} | |
| 169 | + | |
| 170 | +function l(typ: any) { | |
| 171 | + return { literal: typ }; | |
| 172 | +} | |
| 173 | + | |
| 174 | +function a(typ: any) { | |
| 175 | + return { arrayItems: typ }; | |
| 176 | +} | |
| 177 | + | |
| 178 | +function u(...typs: any[]) { | |
| 179 | + return { unionMembers: typs }; | |
| 180 | +} | |
| 181 | + | |
| 182 | +function o(props: any[], additional: any) { | |
| 183 | + return { props, additional }; | |
| 184 | +} | |
| 185 | + | |
| 186 | +function m(additional: any) { | |
| 187 | + const props: any[] = []; | |
| 188 | + return { props, additional }; | |
| 189 | +} | |
| 190 | + | |
| 191 | +function r(name: string) { | |
| 192 | + return { ref: name }; | |
| 193 | +} | |
| 194 | + | |
| 195 | +const typeMap: any = { | |
| 196 | + "TopLevel": o([ | |
| 197 | + { json: "format", js: "format", typ: u(r("DataFormat"), null) }, | |
| 198 | + ], false), | |
| 199 | + "DataFormat": [ | |
| 200 | + "turtle", | |
| 201 | + "rdf_xml", | |
| 202 | + "n_triples", | |
| 203 | + "n_quads", | |
| 204 | + ], | |
| 205 | +}; |
No generated files match these filters.