Test case
27 generated files · +2,139 −0test/inputs/schema/pattern-properties-value.schema
Aschema-cplusplusdefault / quicktype.hpp+94 −0
| @@ -0,0 +1,94 @@ | ||
| 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 | +namespace quicktype { | |
| 18 | + using nlohmann::json; | |
| 19 | + | |
| 20 | + #ifndef NLOHMANN_UNTYPED_quicktype_HELPER | |
| 21 | + #define NLOHMANN_UNTYPED_quicktype_HELPER | |
| 22 | + inline json get_untyped(const json & j, const char * property) { | |
| 23 | + if (j.find(property) != j.end()) { | |
| 24 | + return j.at(property).get<json>(); | |
| 25 | + } | |
| 26 | + return json(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + inline json get_untyped(const json & j, std::string property) { | |
| 30 | + return get_untyped(j, property.data()); | |
| 31 | + } | |
| 32 | + #endif | |
| 33 | + | |
| 34 | + class Material { | |
| 35 | + public: | |
| 36 | + Material() = default; | |
| 37 | + virtual ~Material() = default; | |
| 38 | + | |
| 39 | + private: | |
| 40 | + std::string roughness; | |
| 41 | + double thickness; | |
| 42 | + | |
| 43 | + public: | |
| 44 | + const std::string & get_roughness() const { return roughness; } | |
| 45 | + std::string & get_mutable_roughness() { return roughness; } | |
| 46 | + void set_roughness(const std::string & value) { this->roughness = value; } | |
| 47 | + | |
| 48 | + const double & get_thickness() const { return thickness; } | |
| 49 | + double & get_mutable_thickness() { return thickness; } | |
| 50 | + void set_thickness(const double & value) { this->thickness = value; } | |
| 51 | + }; | |
| 52 | + | |
| 53 | + class TopLevel { | |
| 54 | + public: | |
| 55 | + TopLevel() = default; | |
| 56 | + virtual ~TopLevel() = default; | |
| 57 | + | |
| 58 | + private: | |
| 59 | + std::map<std::string, Material> materials; | |
| 60 | + | |
| 61 | + public: | |
| 62 | + const std::map<std::string, Material> & get_materials() const { return materials; } | |
| 63 | + std::map<std::string, Material> & get_mutable_materials() { return materials; } | |
| 64 | + void set_materials(const std::map<std::string, Material> & value) { this->materials = value; } | |
| 65 | + }; | |
| 66 | +} | |
| 67 | + | |
| 68 | +namespace quicktype { | |
| 69 | + void from_json(const json & j, Material & x); | |
| 70 | + void to_json(json & j, const Material & x); | |
| 71 | + | |
| 72 | + void from_json(const json & j, TopLevel & x); | |
| 73 | + void to_json(json & j, const TopLevel & x); | |
| 74 | + | |
| 75 | + inline void from_json(const json & j, Material& x) { | |
| 76 | + x.set_roughness(j.at("roughness").get<std::string>()); | |
| 77 | + x.set_thickness(j.at("thickness").get<double>()); | |
| 78 | + } | |
| 79 | + | |
| 80 | + inline void to_json(json & j, const Material & x) { | |
| 81 | + j = json::object(); | |
| 82 | + j["roughness"] = x.get_roughness(); | |
| 83 | + j["thickness"] = x.get_thickness(); | |
| 84 | + } | |
| 85 | + | |
| 86 | + inline void from_json(const json & j, TopLevel& x) { | |
| 87 | + x.set_materials(j.at("materials").get<std::map<std::string, Material>>()); | |
| 88 | + } | |
| 89 | + | |
| 90 | + inline void to_json(json & j, const TopLevel & x) { | |
| 91 | + j = json::object(); | |
| 92 | + j["materials"] = x.get_materials(); | |
| 93 | + } | |
| 94 | +} |
Aschema-csharp-recordsdefault / QuickType.cs+70 −0
| @@ -0,0 +1,70 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | +#pragma warning disable CS8604 | |
| 14 | +#pragma warning disable CS8625 | |
| 15 | +#pragma warning disable CS8765 | |
| 16 | + | |
| 17 | +namespace QuickType | |
| 18 | +{ | |
| 19 | + using System; | |
| 20 | + using System.Collections.Generic; | |
| 21 | + | |
| 22 | + using System.Globalization; | |
| 23 | + using Newtonsoft.Json; | |
| 24 | + using Newtonsoft.Json.Converters; | |
| 25 | + | |
| 26 | + public partial record TopLevel | |
| 27 | + { | |
| 28 | + [JsonProperty("materials", Required = Required.Always)] | |
| 29 | + public Dictionary<string, Material> Materials { get; set; } | |
| 30 | + } | |
| 31 | + | |
| 32 | + public partial record Material | |
| 33 | + { | |
| 34 | + [JsonProperty("roughness", Required = Required.Always)] | |
| 35 | + public string Roughness { get; set; } | |
| 36 | + | |
| 37 | + [JsonProperty("thickness", Required = Required.Always)] | |
| 38 | + public double Thickness { get; set; } | |
| 39 | + } | |
| 40 | + | |
| 41 | + public partial record TopLevel | |
| 42 | + { | |
| 43 | + public static TopLevel FromJson(string json) => JsonConvert.DeserializeObject<TopLevel>(json, QuickType.Converter.Settings); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public static partial class Serialize | |
| 47 | + { | |
| 48 | + public static string ToJson(this TopLevel self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 49 | + } | |
| 50 | + | |
| 51 | + internal static partial class Converter | |
| 52 | + { | |
| 53 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 54 | + { | |
| 55 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 56 | + DateParseHandling = DateParseHandling.None, | |
| 57 | + Converters = | |
| 58 | + { | |
| 59 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 60 | + }, | |
| 61 | + }; | |
| 62 | + } | |
| 63 | +} | |
| 64 | +#pragma warning restore CS8618 | |
| 65 | +#pragma warning restore CS8601 | |
| 66 | +#pragma warning restore CS8602 | |
| 67 | +#pragma warning restore CS8603 | |
| 68 | +#pragma warning restore CS8604 | |
| 69 | +#pragma warning restore CS8625 | |
| 70 | +#pragma warning restore CS8765 |
Aschema-csharp-SystemTextJsondefault / QuickType.cs+177 −0
| @@ -0,0 +1,177 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'System.Text.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | + | |
| 14 | +namespace QuickType | |
| 15 | +{ | |
| 16 | + using System; | |
| 17 | + using System.Collections.Generic; | |
| 18 | + | |
| 19 | + using System.Text.Json; | |
| 20 | + using System.Text.Json.Serialization; | |
| 21 | + using System.Globalization; | |
| 22 | + | |
| 23 | + public partial class TopLevel | |
| 24 | + { | |
| 25 | + [JsonRequired] | |
| 26 | + [JsonPropertyName("materials")] | |
| 27 | + public Dictionary<string, Material> Materials { get; set; } | |
| 28 | + } | |
| 29 | + | |
| 30 | + public partial class Material | |
| 31 | + { | |
| 32 | + [JsonRequired] | |
| 33 | + [JsonPropertyName("roughness")] | |
| 34 | + public string Roughness { get; set; } | |
| 35 | + | |
| 36 | + [JsonRequired] | |
| 37 | + [JsonPropertyName("thickness")] | |
| 38 | + public double Thickness { get; set; } | |
| 39 | + } | |
| 40 | + | |
| 41 | + public partial class TopLevel | |
| 42 | + { | |
| 43 | + public static TopLevel FromJson(string json) => JsonSerializer.Deserialize<TopLevel>(json, QuickType.Converter.Settings); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public static partial class Serialize | |
| 47 | + { | |
| 48 | + public static string ToJson(this TopLevel self) => JsonSerializer.Serialize(self, QuickType.Converter.Settings); | |
| 49 | + } | |
| 50 | + | |
| 51 | + internal static partial class Converter | |
| 52 | + { | |
| 53 | + public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General) | |
| 54 | + { | |
| 55 | + Converters = | |
| 56 | + { | |
| 57 | + new DateOnlyConverter(), | |
| 58 | + new TimeOnlyConverter(), | |
| 59 | + IsoDateTimeOffsetConverter.Singleton | |
| 60 | + }, | |
| 61 | + }; | |
| 62 | + } | |
| 63 | + | |
| 64 | + public class DateOnlyConverter : JsonConverter<DateOnly> | |
| 65 | + { | |
| 66 | + private readonly string serializationFormat; | |
| 67 | + public DateOnlyConverter() : this(null) { } | |
| 68 | + | |
| 69 | + public DateOnlyConverter(string? serializationFormat) | |
| 70 | + { | |
| 71 | + this.serializationFormat = serializationFormat ?? "yyyy-MM-dd"; | |
| 72 | + } | |
| 73 | + | |
| 74 | + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 75 | + { | |
| 76 | + var value = reader.GetString(); | |
| 77 | + return DateOnly.Parse(value!); | |
| 78 | + } | |
| 79 | + | |
| 80 | + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) | |
| 81 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 82 | + } | |
| 83 | + | |
| 84 | + public class TimeOnlyConverter : JsonConverter<TimeOnly> | |
| 85 | + { | |
| 86 | + private readonly string serializationFormat; | |
| 87 | + | |
| 88 | + public TimeOnlyConverter() : this(null) { } | |
| 89 | + | |
| 90 | + public TimeOnlyConverter(string? serializationFormat) | |
| 91 | + { | |
| 92 | + this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff"; | |
| 93 | + } | |
| 94 | + | |
| 95 | + public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 96 | + { | |
| 97 | + var value = reader.GetString(); | |
| 98 | + return TimeOnly.Parse(value!); | |
| 99 | + } | |
| 100 | + | |
| 101 | + public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) | |
| 102 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 103 | + } | |
| 104 | + | |
| 105 | + internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset> | |
| 106 | + { | |
| 107 | + public override bool CanConvert(Type t) => t == typeof(DateTimeOffset); | |
| 108 | + | |
| 109 | + private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"; | |
| 110 | + | |
| 111 | + private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind; | |
| 112 | + private string? _dateTimeFormat; | |
| 113 | + private CultureInfo? _culture; | |
| 114 | + | |
| 115 | + public DateTimeStyles DateTimeStyles | |
| 116 | + { | |
| 117 | + get => _dateTimeStyles; | |
| 118 | + set => _dateTimeStyles = value; | |
| 119 | + } | |
| 120 | + | |
| 121 | + public string? DateTimeFormat | |
| 122 | + { | |
| 123 | + get => _dateTimeFormat ?? string.Empty; | |
| 124 | + set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value; | |
| 125 | + } | |
| 126 | + | |
| 127 | + public CultureInfo Culture | |
| 128 | + { | |
| 129 | + get => _culture ?? CultureInfo.CurrentCulture; | |
| 130 | + set => _culture = value; | |
| 131 | + } | |
| 132 | + | |
| 133 | + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) | |
| 134 | + { | |
| 135 | + string text; | |
| 136 | + | |
| 137 | + | |
| 138 | + if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal | |
| 139 | + || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal) | |
| 140 | + { | |
| 141 | + value = value.ToUniversalTime(); | |
| 142 | + } | |
| 143 | + | |
| 144 | + text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture); | |
| 145 | + | |
| 146 | + writer.WriteStringValue(text); | |
| 147 | + } | |
| 148 | + | |
| 149 | + public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 150 | + { | |
| 151 | + string? dateText = reader.GetString(); | |
| 152 | + | |
| 153 | + if (string.IsNullOrEmpty(dateText) == false) | |
| 154 | + { | |
| 155 | + if (!string.IsNullOrEmpty(_dateTimeFormat)) | |
| 156 | + { | |
| 157 | + return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles); | |
| 158 | + } | |
| 159 | + else | |
| 160 | + { | |
| 161 | + return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + else | |
| 165 | + { | |
| 166 | + return default(DateTimeOffset); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + | |
| 170 | + | |
| 171 | + public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter(); | |
| 172 | + } | |
| 173 | +} | |
| 174 | +#pragma warning restore CS8618 | |
| 175 | +#pragma warning restore CS8601 | |
| 176 | +#pragma warning restore CS8602 | |
| 177 | +#pragma warning restore CS8603 |
Aschema-csharpdefault / QuickType.cs+70 −0
| @@ -0,0 +1,70 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | +#pragma warning disable CS8604 | |
| 14 | +#pragma warning disable CS8625 | |
| 15 | +#pragma warning disable CS8765 | |
| 16 | + | |
| 17 | +namespace QuickType | |
| 18 | +{ | |
| 19 | + using System; | |
| 20 | + using System.Collections.Generic; | |
| 21 | + | |
| 22 | + using System.Globalization; | |
| 23 | + using Newtonsoft.Json; | |
| 24 | + using Newtonsoft.Json.Converters; | |
| 25 | + | |
| 26 | + public partial class TopLevel | |
| 27 | + { | |
| 28 | + [JsonProperty("materials", Required = Required.Always)] | |
| 29 | + public Dictionary<string, Material> Materials { get; set; } | |
| 30 | + } | |
| 31 | + | |
| 32 | + public partial class Material | |
| 33 | + { | |
| 34 | + [JsonProperty("roughness", Required = Required.Always)] | |
| 35 | + public string Roughness { get; set; } | |
| 36 | + | |
| 37 | + [JsonProperty("thickness", Required = Required.Always)] | |
| 38 | + public double Thickness { get; set; } | |
| 39 | + } | |
| 40 | + | |
| 41 | + public partial class TopLevel | |
| 42 | + { | |
| 43 | + public static TopLevel FromJson(string json) => JsonConvert.DeserializeObject<TopLevel>(json, QuickType.Converter.Settings); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public static partial class Serialize | |
| 47 | + { | |
| 48 | + public static string ToJson(this TopLevel self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 49 | + } | |
| 50 | + | |
| 51 | + internal static partial class Converter | |
| 52 | + { | |
| 53 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 54 | + { | |
| 55 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 56 | + DateParseHandling = DateParseHandling.None, | |
| 57 | + Converters = | |
| 58 | + { | |
| 59 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 60 | + }, | |
| 61 | + }; | |
| 62 | + } | |
| 63 | +} | |
| 64 | +#pragma warning restore CS8618 | |
| 65 | +#pragma warning restore CS8601 | |
| 66 | +#pragma warning restore CS8602 | |
| 67 | +#pragma warning restore CS8603 | |
| 68 | +#pragma warning restore CS8604 | |
| 69 | +#pragma warning restore CS8625 | |
| 70 | +#pragma warning restore CS8765 |
Aschema-dartdefault / TopLevel.dart+45 −0
| @@ -0,0 +1,45 @@ | ||
| 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 | + final Map<String, Material> materials; | |
| 13 | + | |
| 14 | + TopLevel({ | |
| 15 | + required this.materials, | |
| 16 | + }); | |
| 17 | + | |
| 18 | + factory TopLevel.fromJson(Map<String, dynamic> json) => TopLevel( | |
| 19 | + materials: Map.from(json["materials"]).map((k, v) => MapEntry<String, Material>(k, Material.fromJson(v))), | |
| 20 | + ); | |
| 21 | + | |
| 22 | + Map<String, dynamic> toJson() => { | |
| 23 | + "materials": Map.from(materials).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())), | |
| 24 | + }; | |
| 25 | +} | |
| 26 | + | |
| 27 | +class Material { | |
| 28 | + final String roughness; | |
| 29 | + final double thickness; | |
| 30 | + | |
| 31 | + Material({ | |
| 32 | + required this.roughness, | |
| 33 | + required this.thickness, | |
| 34 | + }); | |
| 35 | + | |
| 36 | + factory Material.fromJson(Map<String, dynamic> json) => Material( | |
| 37 | + roughness: json["roughness"], | |
| 38 | + thickness: json["thickness"]?.toDouble(), | |
| 39 | + ); | |
| 40 | + | |
| 41 | + Map<String, dynamic> toJson() => { | |
| 42 | + "roughness": roughness, | |
| 43 | + "thickness": thickness, | |
| 44 | + }; | |
| 45 | +} |
Aschema-elmdefault / QuickType.elm+70 −0
| @@ -0,0 +1,70 @@ | ||
| 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 | + , Material | |
| 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 | +type alias QuickType = | |
| 27 | + { materials : Dict String Material | |
| 28 | + } | |
| 29 | + | |
| 30 | +type alias Material = | |
| 31 | + { roughness : String | |
| 32 | + , thickness : Float | |
| 33 | + } | |
| 34 | + | |
| 35 | +-- decoders and encoders | |
| 36 | + | |
| 37 | +quickTypeToString : QuickType -> String | |
| 38 | +quickTypeToString r = Jenc.encode 0 (encodeQuickType r) | |
| 39 | + | |
| 40 | +quickType : Jdec.Decoder QuickType | |
| 41 | +quickType = | |
| 42 | + Jdec.succeed QuickType | |
| 43 | + |> Jpipe.required "materials" (Jdec.dict material) | |
| 44 | + | |
| 45 | +encodeQuickType : QuickType -> Jenc.Value | |
| 46 | +encodeQuickType x = | |
| 47 | + Jenc.object | |
| 48 | + [ ("materials", Jenc.dict identity encodeMaterial x.materials) | |
| 49 | + ] | |
| 50 | + | |
| 51 | +material : Jdec.Decoder Material | |
| 52 | +material = | |
| 53 | + Jdec.succeed Material | |
| 54 | + |> Jpipe.required "roughness" Jdec.string | |
| 55 | + |> Jpipe.required "thickness" Jdec.float | |
| 56 | + | |
| 57 | +encodeMaterial : Material -> Jenc.Value | |
| 58 | +encodeMaterial x = | |
| 59 | + Jenc.object | |
| 60 | + [ ("roughness", Jenc.string x.roughness) | |
| 61 | + , ("thickness", Jenc.float x.thickness) | |
| 62 | + ] | |
| 63 | + | |
| 64 | +--- encoder helpers | |
| 65 | + | |
| 66 | +makeNullableEncoder : (a -> Jenc.Value) -> Maybe a -> Jenc.Value | |
| 67 | +makeNullableEncoder f m = | |
| 68 | + case m of | |
| 69 | + Just x -> f x | |
| 70 | + Nothing -> Jenc.null |
Aschema-flowdefault / TopLevel.js+197 −0
| @@ -0,0 +1,197 @@ | ||
| 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 | + materials: { [key: string]: Material }; | |
| 14 | +}; | |
| 15 | + | |
| 16 | +export type Material = { | |
| 17 | + roughness: string; | |
| 18 | + thickness: number; | |
| 19 | +}; | |
| 20 | + | |
| 21 | +// Converts JSON strings to/from your types | |
| 22 | +// and asserts the results of JSON.parse at runtime | |
| 23 | +function toTopLevel(json: string): TopLevel { | |
| 24 | + return cast(JSON.parse(json), r("TopLevel")); | |
| 25 | +} | |
| 26 | + | |
| 27 | +function topLevelToJson(value: TopLevel): string { | |
| 28 | + return JSON.stringify(uncast(value, r("TopLevel")), null, 2); | |
| 29 | +} | |
| 30 | + | |
| 31 | +function invalidValue(typ: any, val: any, key: any, parent: any = '') { | |
| 32 | + const prettyTyp = prettyTypeName(typ); | |
| 33 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 34 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 35 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 36 | +} | |
| 37 | + | |
| 38 | +function prettyTypeName(typ: any): string { | |
| 39 | + if (Array.isArray(typ)) { | |
| 40 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 41 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 42 | + } else { | |
| 43 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 44 | + } | |
| 45 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 46 | + return typ.literal; | |
| 47 | + } else { | |
| 48 | + return typeof typ; | |
| 49 | + } | |
| 50 | +} | |
| 51 | + | |
| 52 | +function jsonToJSProps(typ: any): any { | |
| 53 | + if (typ.jsonToJS === undefined) { | |
| 54 | + const map: any = {}; | |
| 55 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 56 | + typ.jsonToJS = map; | |
| 57 | + } | |
| 58 | + return typ.jsonToJS; | |
| 59 | +} | |
| 60 | + | |
| 61 | +function jsToJSONProps(typ: any): any { | |
| 62 | + if (typ.jsToJSON === undefined) { | |
| 63 | + const map: any = {}; | |
| 64 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 65 | + typ.jsToJSON = map; | |
| 66 | + } | |
| 67 | + return typ.jsToJSON; | |
| 68 | +} | |
| 69 | + | |
| 70 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 71 | + function transformPrimitive(typ: string, val: any): any { | |
| 72 | + if (typeof typ === typeof val) return val; | |
| 73 | + return invalidValue(typ, val, key, parent); | |
| 74 | + } | |
| 75 | + | |
| 76 | + function transformUnion(typs: any[], val: any): any { | |
| 77 | + // val must validate against one typ in typs | |
| 78 | + const l = typs.length; | |
| 79 | + for (let i = 0; i < l; i++) { | |
| 80 | + const typ = typs[i]; | |
| 81 | + try { | |
| 82 | + return transform(val, typ, getProps); | |
| 83 | + } catch (_) {} | |
| 84 | + } | |
| 85 | + return invalidValue(typs, val, key, parent); | |
| 86 | + } | |
| 87 | + | |
| 88 | + function transformEnum(cases: string[], val: any): any { | |
| 89 | + if (cases.indexOf(val) !== -1) return val; | |
| 90 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 91 | + } | |
| 92 | + | |
| 93 | + function transformArray(typ: any, val: any): any { | |
| 94 | + // val must be an array with no invalid elements | |
| 95 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 96 | + return val.map(el => transform(el, typ, getProps)); | |
| 97 | + } | |
| 98 | + | |
| 99 | + function transformDate(val: any): any { | |
| 100 | + if (val === null) { | |
| 101 | + return null; | |
| 102 | + } | |
| 103 | + const d = new Date(val); | |
| 104 | + if (isNaN(d.valueOf())) { | |
| 105 | + return invalidValue(l("Date"), val, key, parent); | |
| 106 | + } | |
| 107 | + return d; | |
| 108 | + } | |
| 109 | + | |
| 110 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 111 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 112 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 113 | + } | |
| 114 | + const result: any = {}; | |
| 115 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 116 | + const prop = props[key]; | |
| 117 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 118 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 119 | + }); | |
| 120 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 121 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 122 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 123 | + } | |
| 124 | + }); | |
| 125 | + return result; | |
| 126 | + } | |
| 127 | + | |
| 128 | + if (typ === "any") return val; | |
| 129 | + if (typ === null) { | |
| 130 | + if (val === null) return val; | |
| 131 | + return invalidValue(typ, val, key, parent); | |
| 132 | + } | |
| 133 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 134 | + let ref: any = undefined; | |
| 135 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 136 | + ref = typ.ref; | |
| 137 | + typ = typeMap[typ.ref]; | |
| 138 | + } | |
| 139 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 140 | + if (typeof typ === "object") { | |
| 141 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 142 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 143 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 144 | + : invalidValue(typ, val, key, parent); | |
| 145 | + } | |
| 146 | + // Numbers can be parsed by Date but shouldn't be. | |
| 147 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 148 | + return transformPrimitive(typ, val); | |
| 149 | +} | |
| 150 | + | |
| 151 | +function cast<T>(val: any, typ: any): T { | |
| 152 | + return transform(val, typ, jsonToJSProps); | |
| 153 | +} | |
| 154 | + | |
| 155 | +function uncast<T>(val: T, typ: any): any { | |
| 156 | + return transform(val, typ, jsToJSONProps); | |
| 157 | +} | |
| 158 | + | |
| 159 | +function l(typ: any) { | |
| 160 | + return { literal: typ }; | |
| 161 | +} | |
| 162 | + | |
| 163 | +function a(typ: any) { | |
| 164 | + return { arrayItems: typ }; | |
| 165 | +} | |
| 166 | + | |
| 167 | +function u(...typs: any[]) { | |
| 168 | + return { unionMembers: typs }; | |
| 169 | +} | |
| 170 | + | |
| 171 | +function o(props: any[], additional: any) { | |
| 172 | + return { props, additional }; | |
| 173 | +} | |
| 174 | + | |
| 175 | +function m(additional: any) { | |
| 176 | + const props: any[] = []; | |
| 177 | + return { props, additional }; | |
| 178 | +} | |
| 179 | + | |
| 180 | +function r(name: string) { | |
| 181 | + return { ref: name }; | |
| 182 | +} | |
| 183 | + | |
| 184 | +const typeMap: any = { | |
| 185 | + "TopLevel": o([ | |
| 186 | + { json: "materials", js: "materials", typ: m(r("Material")) }, | |
| 187 | + ], false), | |
| 188 | + "Material": o([ | |
| 189 | + { json: "roughness", js: "roughness", typ: "" }, | |
| 190 | + { json: "thickness", js: "thickness", typ: 3.14 }, | |
| 191 | + ], false), | |
| 192 | +}; | |
| 193 | + | |
| 194 | +module.exports = { | |
| 195 | + "topLevelToJson": topLevelToJson, | |
| 196 | + "toTopLevel": toTopLevel, | |
| 197 | +}; |
Aschema-golangdefault / quicktype.go+28 −0
| @@ -0,0 +1,28 @@ | ||
| 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 | + Materials map[string]Material `json:"materials"` | |
| 23 | +} | |
| 24 | + | |
| 25 | +type Material struct { | |
| 26 | + Roughness string `json:"roughness"` | |
| 27 | + Thickness float64 `json:"thickness"` | |
| 28 | +} |
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 / Material.java+18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class Material { | |
| 6 | + private String roughness; | |
| 7 | + private double thickness; | |
| 8 | + | |
| 9 | + @JsonProperty("roughness") | |
| 10 | + public String getRoughness() { return roughness; } | |
| 11 | + @JsonProperty("roughness") | |
| 12 | + public void setRoughness(String value) { this.roughness = value; } | |
| 13 | + | |
| 14 | + @JsonProperty("thickness") | |
| 15 | + public double getThickness() { return thickness; } | |
| 16 | + @JsonProperty("thickness") | |
| 17 | + public void setThickness(double value) { this.thickness = value; } | |
| 18 | +} |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / TopLevel.java+13 −0
| @@ -0,0 +1,13 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.Map; | |
| 5 | + | |
| 6 | +public class TopLevel { | |
| 7 | + private Map<String, Material> materials; | |
| 8 | + | |
| 9 | + @JsonProperty("materials") | |
| 10 | + public Map<String, Material> getMaterials() { return materials; } | |
| 11 | + @JsonProperty("materials") | |
| 12 | + public void setMaterials(Map<String, Material> value) { this.materials = value; } | |
| 13 | +} |
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 / Material.java+18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class Material { | |
| 6 | + private String roughness; | |
| 7 | + private double thickness; | |
| 8 | + | |
| 9 | + @JsonProperty("roughness") | |
| 10 | + public String getRoughness() { return roughness; } | |
| 11 | + @JsonProperty("roughness") | |
| 12 | + public void setRoughness(String value) { this.roughness = value; } | |
| 13 | + | |
| 14 | + @JsonProperty("thickness") | |
| 15 | + public double getThickness() { return thickness; } | |
| 16 | + @JsonProperty("thickness") | |
| 17 | + public void setThickness(double value) { this.thickness = value; } | |
| 18 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / TopLevel.java+13 −0
| @@ -0,0 +1,13 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.Map; | |
| 5 | + | |
| 6 | +public class TopLevel { | |
| 7 | + private Map<String, Material> materials; | |
| 8 | + | |
| 9 | + @JsonProperty("materials") | |
| 10 | + public Map<String, Material> getMaterials() { return materials; } | |
| 11 | + @JsonProperty("materials") | |
| 12 | + public void setMaterials(Map<String, Material> value) { this.materials = value; } | |
| 13 | +} |
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 / Material.java+18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class Material { | |
| 6 | + private String roughness; | |
| 7 | + private double thickness; | |
| 8 | + | |
| 9 | + @JsonProperty("roughness") | |
| 10 | + public String getRoughness() { return roughness; } | |
| 11 | + @JsonProperty("roughness") | |
| 12 | + public void setRoughness(String value) { this.roughness = value; } | |
| 13 | + | |
| 14 | + @JsonProperty("thickness") | |
| 15 | + public double getThickness() { return thickness; } | |
| 16 | + @JsonProperty("thickness") | |
| 17 | + public void setThickness(double value) { this.thickness = value; } | |
| 18 | +} |
Aschema-javadefault / src / main / java / io / quicktype / TopLevel.java+13 −0
| @@ -0,0 +1,13 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.Map; | |
| 5 | + | |
| 6 | +public class TopLevel { | |
| 7 | + private Map<String, Material> materials; | |
| 8 | + | |
| 9 | + @JsonProperty("materials") | |
| 10 | + public Map<String, Material> getMaterials() { return materials; } | |
| 11 | + @JsonProperty("materials") | |
| 12 | + public void setMaterials(Map<String, Material> value) { this.materials = value; } | |
| 13 | +} |
Aschema-javascriptdefault / TopLevel.js+186 −0
| @@ -0,0 +1,186 @@ | ||
| 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: "materials", js: "materials", typ: m(r("Material")) }, | |
| 176 | + ], false), | |
| 177 | + "Material": o([ | |
| 178 | + { json: "roughness", js: "roughness", typ: "" }, | |
| 179 | + { json: "thickness", js: "thickness", typ: 3.14 }, | |
| 180 | + ], false), | |
| 181 | +}; | |
| 182 | + | |
| 183 | +module.exports = { | |
| 184 | + "topLevelToJson": topLevelToJson, | |
| 185 | + "toTopLevel": toTopLevel, | |
| 186 | +}; |
Aschema-kotlinxdefault / TopLevel.kt+22 −0
| @@ -0,0 +1,22 @@ | ||
| 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 | + val materials: Map<String, Material> | |
| 16 | +) | |
| 17 | + | |
| 18 | +@Serializable | |
| 19 | +data class Material ( | |
| 20 | + val roughness: String, | |
| 21 | + val thickness: Double | |
| 22 | +) |
Aschema-phpdefault / TopLevel.php+267 −0
| @@ -0,0 +1,267 @@ | ||
| 1 | +<?php | |
| 2 | +declare(strict_types=1); | |
| 3 | + | |
| 4 | +// This is an autogenerated file:TopLevel | |
| 5 | + | |
| 6 | +class TopLevel { | |
| 7 | + private stdClass $materials; // json:materials Required | |
| 8 | + | |
| 9 | + /** | |
| 10 | + * @param stdClass $materials | |
| 11 | + */ | |
| 12 | + public function __construct(stdClass $materials) { | |
| 13 | + $this->materials = $materials; | |
| 14 | + } | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * @param stdClass $value | |
| 18 | + * @throws Exception | |
| 19 | + * @return stdClass | |
| 20 | + */ | |
| 21 | + public static function fromMaterials(stdClass $value): stdClass { | |
| 22 | + $out = new stdClass(); | |
| 23 | + foreach ($value as $k => $v) { | |
| 24 | + $out->$k = Material::from($v); /*class*/ | |
| 25 | + } | |
| 26 | + return $out; | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @throws Exception | |
| 31 | + * @return stdClass | |
| 32 | + */ | |
| 33 | + public function toMaterials(): stdClass { | |
| 34 | + if (TopLevel::validateMaterials($this->materials)) { | |
| 35 | + $out = new stdClass(); | |
| 36 | + foreach ($this->materials as $k => $v) { | |
| 37 | + $out->$k = $v->to(); /*class*/ | |
| 38 | + } | |
| 39 | + return $out; | |
| 40 | + } | |
| 41 | + throw new Exception('never get to this TopLevel::materials'); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * @param stdClass | |
| 46 | + * @return bool | |
| 47 | + * @throws Exception | |
| 48 | + */ | |
| 49 | + public static function validateMaterials(stdClass $value): bool { | |
| 50 | + foreach ($value as $k => $v) { | |
| 51 | + $v->validate(); | |
| 52 | + } | |
| 53 | + return true; | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * @throws Exception | |
| 58 | + * @return stdClass | |
| 59 | + */ | |
| 60 | + public function getMaterials(): stdClass { | |
| 61 | + if (TopLevel::validateMaterials($this->materials)) { | |
| 62 | + return $this->materials; | |
| 63 | + } | |
| 64 | + throw new Exception('never get to getMaterials TopLevel::materials'); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * @return stdClass | |
| 69 | + */ | |
| 70 | + public static function sampleMaterials(): stdClass { | |
| 71 | + return (function () { | |
| 72 | + $out = new stdClass(); | |
| 73 | + $out->{'TopLevel'} = Material::sample(); /*31:materials*/ | |
| 74 | + return $out; | |
| 75 | + })(); /* 31:materials*/ | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * @throws Exception | |
| 80 | + * @return bool | |
| 81 | + */ | |
| 82 | + public function validate(): bool { | |
| 83 | + return TopLevel::validateMaterials($this->materials); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * @return stdClass | |
| 88 | + * @throws Exception | |
| 89 | + */ | |
| 90 | + public function to(): stdClass { | |
| 91 | + $out = new stdClass(); | |
| 92 | + $out->{'materials'} = $this->toMaterials(); | |
| 93 | + return $out; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * @param stdClass $obj | |
| 98 | + * @return TopLevel | |
| 99 | + * @throws Exception | |
| 100 | + */ | |
| 101 | + public static function from(stdClass $obj): TopLevel { | |
| 102 | + return new TopLevel( | |
| 103 | + TopLevel::fromMaterials($obj->{'materials'}) | |
| 104 | + ); | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * @return TopLevel | |
| 109 | + */ | |
| 110 | + public static function sample(): TopLevel { | |
| 111 | + return new TopLevel( | |
| 112 | + TopLevel::sampleMaterials() | |
| 113 | + ); | |
| 114 | + } | |
| 115 | +} | |
| 116 | + | |
| 117 | +// This is an autogenerated file:Material | |
| 118 | + | |
| 119 | +class Material { | |
| 120 | + private string $roughness; // json:roughness Required | |
| 121 | + private float $thickness; // json:thickness Required | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * @param string $roughness | |
| 125 | + * @param float $thickness | |
| 126 | + */ | |
| 127 | + public function __construct(string $roughness, float $thickness) { | |
| 128 | + $this->roughness = $roughness; | |
| 129 | + $this->thickness = $thickness; | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * @param string $value | |
| 134 | + * @throws Exception | |
| 135 | + * @return string | |
| 136 | + */ | |
| 137 | + public static function fromRoughness(string $value): string { | |
| 138 | + return $value; /*string*/ | |
| 139 | + } | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * @throws Exception | |
| 143 | + * @return string | |
| 144 | + */ | |
| 145 | + public function toRoughness(): string { | |
| 146 | + if (Material::validateRoughness($this->roughness)) { | |
| 147 | + return $this->roughness; /*string*/ | |
| 148 | + } | |
| 149 | + throw new Exception('never get to this Material::roughness'); | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * @param string | |
| 154 | + * @return bool | |
| 155 | + * @throws Exception | |
| 156 | + */ | |
| 157 | + public static function validateRoughness(string $value): bool { | |
| 158 | + return true; | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * @throws Exception | |
| 163 | + * @return string | |
| 164 | + */ | |
| 165 | + public function getRoughness(): string { | |
| 166 | + if (Material::validateRoughness($this->roughness)) { | |
| 167 | + return $this->roughness; | |
| 168 | + } | |
| 169 | + throw new Exception('never get to getRoughness Material::roughness'); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * @return string | |
| 174 | + */ | |
| 175 | + public static function sampleRoughness(): string { | |
| 176 | + return 'Material::roughness::31'; /*31:roughness*/ | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * @param float $value | |
| 181 | + * @throws Exception | |
| 182 | + * @return float | |
| 183 | + */ | |
| 184 | + public static function fromThickness(float $value): float { | |
| 185 | + return $value; /*float*/ | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * @throws Exception | |
| 190 | + * @return float | |
| 191 | + */ | |
| 192 | + public function toThickness(): float { | |
| 193 | + if (Material::validateThickness($this->thickness)) { | |
| 194 | + return $this->thickness; /*float*/ | |
| 195 | + } | |
| 196 | + throw new Exception('never get to this Material::thickness'); | |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * @param float | |
| 201 | + * @return bool | |
| 202 | + * @throws Exception | |
| 203 | + */ | |
| 204 | + public static function validateThickness(float $value): bool { | |
| 205 | + return true; | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * @throws Exception | |
| 210 | + * @return float | |
| 211 | + */ | |
| 212 | + public function getThickness(): float { | |
| 213 | + if (Material::validateThickness($this->thickness)) { | |
| 214 | + return $this->thickness; | |
| 215 | + } | |
| 216 | + throw new Exception('never get to getThickness Material::thickness'); | |
| 217 | + } | |
| 218 | + | |
| 219 | + /** | |
| 220 | + * @return float | |
| 221 | + */ | |
| 222 | + public static function sampleThickness(): float { | |
| 223 | + return 32.032; /*32:thickness*/ | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * @throws Exception | |
| 228 | + * @return bool | |
| 229 | + */ | |
| 230 | + public function validate(): bool { | |
| 231 | + return Material::validateRoughness($this->roughness) | |
| 232 | + || Material::validateThickness($this->thickness); | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 236 | + * @return stdClass | |
| 237 | + * @throws Exception | |
| 238 | + */ | |
| 239 | + public function to(): stdClass { | |
| 240 | + $out = new stdClass(); | |
| 241 | + $out->{'roughness'} = $this->toRoughness(); | |
| 242 | + $out->{'thickness'} = $this->toThickness(); | |
| 243 | + return $out; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * @param stdClass $obj | |
| 248 | + * @return Material | |
| 249 | + * @throws Exception | |
| 250 | + */ | |
| 251 | + public static function from(stdClass $obj): Material { | |
| 252 | + return new Material( | |
| 253 | + Material::fromRoughness($obj->{'roughness'}) | |
| 254 | + ,Material::fromThickness($obj->{'thickness'}) | |
| 255 | + ); | |
| 256 | + } | |
| 257 | + | |
| 258 | + /** | |
| 259 | + * @return Material | |
| 260 | + */ | |
| 261 | + public static function sample(): Material { | |
| 262 | + return new Material( | |
| 263 | + Material::sampleRoughness() | |
| 264 | + ,Material::sampleThickness() | |
| 265 | + ); | |
| 266 | + } | |
| 267 | +} |
Aschema-pythondefault / quicktype.py+73 −0
| @@ -0,0 +1,73 @@ | ||
| 1 | +from dataclasses import dataclass | |
| 2 | +from typing import Any, TypeVar, Callable, Type, cast | |
| 3 | + | |
| 4 | + | |
| 5 | +T = TypeVar("T") | |
| 6 | + | |
| 7 | + | |
| 8 | +def from_str(x: Any) -> str: | |
| 9 | + assert isinstance(x, str) | |
| 10 | + return x | |
| 11 | + | |
| 12 | + | |
| 13 | +def from_float(x: Any) -> float: | |
| 14 | + assert isinstance(x, (float, int)) and not isinstance(x, bool) | |
| 15 | + return float(x) | |
| 16 | + | |
| 17 | + | |
| 18 | +def to_float(x: Any) -> float: | |
| 19 | + assert isinstance(x, (int, float)) | |
| 20 | + return x | |
| 21 | + | |
| 22 | + | |
| 23 | +def from_dict(f: Callable[[Any], T], x: Any) -> dict[str, T]: | |
| 24 | + assert isinstance(x, dict) | |
| 25 | + return { k: f(v) for (k, v) in x.items() } | |
| 26 | + | |
| 27 | + | |
| 28 | +def to_class(c: Type[T], x: Any) -> dict: | |
| 29 | + assert isinstance(x, c) | |
| 30 | + return cast(Any, x).to_dict() | |
| 31 | + | |
| 32 | + | |
| 33 | +@dataclass | |
| 34 | +class Material: | |
| 35 | + roughness: str | |
| 36 | + thickness: float | |
| 37 | + | |
| 38 | + @staticmethod | |
| 39 | + def from_dict(obj: Any) -> 'Material': | |
| 40 | + assert isinstance(obj, dict) | |
| 41 | + roughness = from_str(obj.get("roughness")) | |
| 42 | + thickness = from_float(obj.get("thickness")) | |
| 43 | + return Material(roughness, thickness) | |
| 44 | + | |
| 45 | + def to_dict(self) -> dict: | |
| 46 | + result: dict = {} | |
| 47 | + result["roughness"] = from_str(self.roughness) | |
| 48 | + result["thickness"] = to_float(self.thickness) | |
| 49 | + return result | |
| 50 | + | |
| 51 | + | |
| 52 | +@dataclass | |
| 53 | +class TopLevel: | |
| 54 | + materials: dict[str, Material] | |
| 55 | + | |
| 56 | + @staticmethod | |
| 57 | + def from_dict(obj: Any) -> 'TopLevel': | |
| 58 | + assert isinstance(obj, dict) | |
| 59 | + materials = from_dict(Material.from_dict, obj.get("materials")) | |
| 60 | + return TopLevel(materials) | |
| 61 | + | |
| 62 | + def to_dict(self) -> dict: | |
| 63 | + result: dict = {} | |
| 64 | + result["materials"] = from_dict(lambda x: to_class(Material, x), self.materials) | |
| 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+74 −0
| @@ -0,0 +1,74 @@ | ||
| 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.materials["…"].roughness | |
| 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 | + Hash = Strict::Hash | |
| 19 | + String = Strict::String | |
| 20 | + Double = Strict::Float | Strict::Integer | |
| 21 | +end | |
| 22 | + | |
| 23 | +class Material < Dry::Struct | |
| 24 | + attribute :roughness, Types::String | |
| 25 | + attribute :thickness, Types::Double | |
| 26 | + | |
| 27 | + def self.from_dynamic!(d) | |
| 28 | + d = Types::Hash[d] | |
| 29 | + new( | |
| 30 | + roughness: d.fetch("roughness"), | |
| 31 | + thickness: d.fetch("thickness"), | |
| 32 | + ) | |
| 33 | + end | |
| 34 | + | |
| 35 | + def self.from_json!(json) | |
| 36 | + from_dynamic!(JSON.parse(json)) | |
| 37 | + end | |
| 38 | + | |
| 39 | + def to_dynamic | |
| 40 | + { | |
| 41 | + "roughness" => roughness, | |
| 42 | + "thickness" => thickness, | |
| 43 | + } | |
| 44 | + end | |
| 45 | + | |
| 46 | + def to_json(options = nil) | |
| 47 | + JSON.generate(to_dynamic, options) | |
| 48 | + end | |
| 49 | +end | |
| 50 | + | |
| 51 | +class TopLevel < Dry::Struct | |
| 52 | + attribute :materials, Types::Hash.meta(of: Material) | |
| 53 | + | |
| 54 | + def self.from_dynamic!(d) | |
| 55 | + d = Types::Hash[d] | |
| 56 | + new( | |
| 57 | + materials: Types::Hash[d.fetch("materials")].map { |k, v| [k, Material.from_dynamic!(v)] }.to_h, | |
| 58 | + ) | |
| 59 | + end | |
| 60 | + | |
| 61 | + def self.from_json!(json) | |
| 62 | + from_dynamic!(JSON.parse(json)) | |
| 63 | + end | |
| 64 | + | |
| 65 | + def to_dynamic | |
| 66 | + { | |
| 67 | + "materials" => materials.map { |k, v| [k, v.to_dynamic] }.to_h, | |
| 68 | + } | |
| 69 | + end | |
| 70 | + | |
| 71 | + def to_json(options = nil) | |
| 72 | + JSON.generate(to_dynamic, options) | |
| 73 | + end | |
| 74 | +end |
Aschema-rustdefault / module_under_test.rs+27 −0
| @@ -0,0 +1,27 @@ | ||
| 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 | +use std::collections::HashMap; | |
| 16 | + | |
| 17 | +#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 18 | +pub struct TopLevel { | |
| 19 | + pub materials: HashMap<String, Material>, | |
| 20 | +} | |
| 21 | + | |
| 22 | +#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 23 | +pub struct Material { | |
| 24 | + pub roughness: String, | |
| 25 | + | |
| 26 | + pub thickness: f64, | |
| 27 | +} |
Aschema-scala3-upickledefault / TopLevel.scala+75 −0
| @@ -0,0 +1,75 @@ | ||
| 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 | + val materials : Map[String, Material] | |
| 70 | +) derives OptionPickler.ReadWriter | |
| 71 | + | |
| 72 | +case class Material ( | |
| 73 | + val roughness : String, | |
| 74 | + val thickness : Double | |
| 75 | +) derives OptionPickler.ReadWriter |
Aschema-scala3default / TopLevel.scala+17 −0
| @@ -0,0 +1,17 @@ | ||
| 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 | + val materials : Map[String, Material] | |
| 12 | +) derives Encoder.AsObject, Decoder | |
| 13 | + | |
| 14 | +case class Material ( | |
| 15 | + val roughness : String, | |
| 16 | + val thickness : Double | |
| 17 | +) derives Encoder.AsObject, Decoder |
Aschema-schemadefault / TopLevel.schema+39 −0
| @@ -0,0 +1,39 @@ | ||
| 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 | + "materials": { | |
| 10 | + "type": "object", | |
| 11 | + "additionalProperties": { | |
| 12 | + "$ref": "#/definitions/Material" | |
| 13 | + } | |
| 14 | + } | |
| 15 | + }, | |
| 16 | + "required": [ | |
| 17 | + "materials" | |
| 18 | + ], | |
| 19 | + "title": "TopLevel" | |
| 20 | + }, | |
| 21 | + "Material": { | |
| 22 | + "type": "object", | |
| 23 | + "additionalProperties": false, | |
| 24 | + "properties": { | |
| 25 | + "roughness": { | |
| 26 | + "type": "string" | |
| 27 | + }, | |
| 28 | + "thickness": { | |
| 29 | + "type": "number" | |
| 30 | + } | |
| 31 | + }, | |
| 32 | + "required": [ | |
| 33 | + "roughness", | |
| 34 | + "thickness" | |
| 35 | + ], | |
| 36 | + "title": "Material" | |
| 37 | + } | |
| 38 | + } | |
| 39 | +} |
Aschema-typescriptdefault / TopLevel.ts+192 −0
| @@ -0,0 +1,192 @@ | ||
| 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 | + materials: { [key: string]: Material }; | |
| 12 | +} | |
| 13 | + | |
| 14 | +export interface Material { | |
| 15 | + roughness: string; | |
| 16 | + thickness: number; | |
| 17 | +} | |
| 18 | + | |
| 19 | +// Converts JSON strings to/from your types | |
| 20 | +// and asserts the results of JSON.parse at runtime | |
| 21 | +export class Convert { | |
| 22 | + public static toTopLevel(json: string): TopLevel { | |
| 23 | + return cast(JSON.parse(json), r("TopLevel")); | |
| 24 | + } | |
| 25 | + | |
| 26 | + public static topLevelToJson(value: TopLevel): string { | |
| 27 | + return JSON.stringify(uncast(value, r("TopLevel")), null, 2); | |
| 28 | + } | |
| 29 | +} | |
| 30 | + | |
| 31 | +function invalidValue(typ: any, val: any, key: any, parent: any = ''): never { | |
| 32 | + const prettyTyp = prettyTypeName(typ); | |
| 33 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 34 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 35 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 36 | +} | |
| 37 | + | |
| 38 | +function prettyTypeName(typ: any): string { | |
| 39 | + if (Array.isArray(typ)) { | |
| 40 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 41 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 42 | + } else { | |
| 43 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 44 | + } | |
| 45 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 46 | + return typ.literal; | |
| 47 | + } else { | |
| 48 | + return typeof typ; | |
| 49 | + } | |
| 50 | +} | |
| 51 | + | |
| 52 | +function jsonToJSProps(typ: any): any { | |
| 53 | + if (typ.jsonToJS === undefined) { | |
| 54 | + const map: any = {}; | |
| 55 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 56 | + typ.jsonToJS = map; | |
| 57 | + } | |
| 58 | + return typ.jsonToJS; | |
| 59 | +} | |
| 60 | + | |
| 61 | +function jsToJSONProps(typ: any): any { | |
| 62 | + if (typ.jsToJSON === undefined) { | |
| 63 | + const map: any = {}; | |
| 64 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 65 | + typ.jsToJSON = map; | |
| 66 | + } | |
| 67 | + return typ.jsToJSON; | |
| 68 | +} | |
| 69 | + | |
| 70 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 71 | + function transformPrimitive(typ: string, val: any): any { | |
| 72 | + if (typeof typ === typeof val) return val; | |
| 73 | + return invalidValue(typ, val, key, parent); | |
| 74 | + } | |
| 75 | + | |
| 76 | + function transformUnion(typs: any[], val: any): any { | |
| 77 | + // val must validate against one typ in typs | |
| 78 | + const l = typs.length; | |
| 79 | + for (let i = 0; i < l; i++) { | |
| 80 | + const typ = typs[i]; | |
| 81 | + try { | |
| 82 | + return transform(val, typ, getProps); | |
| 83 | + } catch (_) {} | |
| 84 | + } | |
| 85 | + return invalidValue(typs, val, key, parent); | |
| 86 | + } | |
| 87 | + | |
| 88 | + function transformEnum(cases: string[], val: any): any { | |
| 89 | + if (cases.indexOf(val) !== -1) return val; | |
| 90 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 91 | + } | |
| 92 | + | |
| 93 | + function transformArray(typ: any, val: any): any { | |
| 94 | + // val must be an array with no invalid elements | |
| 95 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 96 | + return val.map(el => transform(el, typ, getProps)); | |
| 97 | + } | |
| 98 | + | |
| 99 | + function transformDate(val: any): any { | |
| 100 | + if (val === null) { | |
| 101 | + return null; | |
| 102 | + } | |
| 103 | + const d = new Date(val); | |
| 104 | + if (isNaN(d.valueOf())) { | |
| 105 | + return invalidValue(l("Date"), val, key, parent); | |
| 106 | + } | |
| 107 | + return d; | |
| 108 | + } | |
| 109 | + | |
| 110 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 111 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 112 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 113 | + } | |
| 114 | + const result: any = {}; | |
| 115 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 116 | + const prop = props[key]; | |
| 117 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 118 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 119 | + }); | |
| 120 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 121 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 122 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 123 | + } | |
| 124 | + }); | |
| 125 | + return result; | |
| 126 | + } | |
| 127 | + | |
| 128 | + if (typ === "any") return val; | |
| 129 | + if (typ === null) { | |
| 130 | + if (val === null) return val; | |
| 131 | + return invalidValue(typ, val, key, parent); | |
| 132 | + } | |
| 133 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 134 | + let ref: any = undefined; | |
| 135 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 136 | + ref = typ.ref; | |
| 137 | + typ = typeMap[typ.ref]; | |
| 138 | + } | |
| 139 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 140 | + if (typeof typ === "object") { | |
| 141 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 142 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 143 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 144 | + : invalidValue(typ, val, key, parent); | |
| 145 | + } | |
| 146 | + // Numbers can be parsed by Date but shouldn't be. | |
| 147 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 148 | + return transformPrimitive(typ, val); | |
| 149 | +} | |
| 150 | + | |
| 151 | +function cast<T>(val: any, typ: any): T { | |
| 152 | + return transform(val, typ, jsonToJSProps); | |
| 153 | +} | |
| 154 | + | |
| 155 | +function uncast<T>(val: T, typ: any): any { | |
| 156 | + return transform(val, typ, jsToJSONProps); | |
| 157 | +} | |
| 158 | + | |
| 159 | +function l(typ: any) { | |
| 160 | + return { literal: typ }; | |
| 161 | +} | |
| 162 | + | |
| 163 | +function a(typ: any) { | |
| 164 | + return { arrayItems: typ }; | |
| 165 | +} | |
| 166 | + | |
| 167 | +function u(...typs: any[]) { | |
| 168 | + return { unionMembers: typs }; | |
| 169 | +} | |
| 170 | + | |
| 171 | +function o(props: any[], additional: any) { | |
| 172 | + return { props, additional }; | |
| 173 | +} | |
| 174 | + | |
| 175 | +function m(additional: any) { | |
| 176 | + const props: any[] = []; | |
| 177 | + return { props, additional }; | |
| 178 | +} | |
| 179 | + | |
| 180 | +function r(name: string) { | |
| 181 | + return { ref: name }; | |
| 182 | +} | |
| 183 | + | |
| 184 | +const typeMap: any = { | |
| 185 | + "TopLevel": o([ | |
| 186 | + { json: "materials", js: "materials", typ: m(r("Material")) }, | |
| 187 | + ], false), | |
| 188 | + "Material": o([ | |
| 189 | + { json: "roughness", js: "roughness", typ: "" }, | |
| 190 | + { json: "thickness", js: "thickness", typ: 3.14 }, | |
| 191 | + ], false), | |
| 192 | +}; |
No generated files match these filters.