Test case
30 generated files · +2,367 −13test/inputs/schema/recursive-ref-to-id.schema
Aschema-cplusplusdefault / quicktype.hpp+151 −0
| @@ -0,0 +1,151 @@ | ||
| 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 <optional> | |
| 12 | +#include "json.hpp" | |
| 13 | + | |
| 14 | +#include <optional> | |
| 15 | +#include <stdexcept> | |
| 16 | +#include <regex> | |
| 17 | + | |
| 18 | +#ifndef NLOHMANN_OPT_HELPER | |
| 19 | +#define NLOHMANN_OPT_HELPER | |
| 20 | +namespace nlohmann { | |
| 21 | + template <typename T> | |
| 22 | + struct adl_serializer<std::shared_ptr<T>> { | |
| 23 | + static void to_json(json & j, const std::shared_ptr<T> & opt) { | |
| 24 | + if (!opt) j = nullptr; else j = *opt; | |
| 25 | + } | |
| 26 | + | |
| 27 | + static std::shared_ptr<T> from_json(const json & j) { | |
| 28 | + if (j.is_null()) return std::shared_ptr<T>(); else return std::make_shared<T>(j.get<T>()); | |
| 29 | + } | |
| 30 | + }; | |
| 31 | + template <typename T> | |
| 32 | + struct adl_serializer<std::optional<T>> { | |
| 33 | + static void to_json(json & j, const std::optional<T> & opt) { | |
| 34 | + if (!opt) j = nullptr; else j = *opt; | |
| 35 | + } | |
| 36 | + | |
| 37 | + static std::optional<T> from_json(const json & j) { | |
| 38 | + if (j.is_null()) return std::optional<T>(); else return std::make_optional<T>(j.get<T>()); | |
| 39 | + } | |
| 40 | + }; | |
| 41 | +} | |
| 42 | +#endif | |
| 43 | + | |
| 44 | +namespace quicktype { | |
| 45 | + using nlohmann::json; | |
| 46 | + | |
| 47 | + #ifndef NLOHMANN_UNTYPED_quicktype_HELPER | |
| 48 | + #define NLOHMANN_UNTYPED_quicktype_HELPER | |
| 49 | + inline json get_untyped(const json & j, const char * property) { | |
| 50 | + if (j.find(property) != j.end()) { | |
| 51 | + return j.at(property).get<json>(); | |
| 52 | + } | |
| 53 | + return json(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + inline json get_untyped(const json & j, std::string property) { | |
| 57 | + return get_untyped(j, property.data()); | |
| 58 | + } | |
| 59 | + #endif | |
| 60 | + | |
| 61 | + #ifndef NLOHMANN_OPTIONAL_quicktype_HELPER | |
| 62 | + #define NLOHMANN_OPTIONAL_quicktype_HELPER | |
| 63 | + template <typename T> | |
| 64 | + inline std::shared_ptr<T> get_heap_optional(const json & j, const char * property) { | |
| 65 | + auto it = j.find(property); | |
| 66 | + if (it != j.end() && !it->is_null()) { | |
| 67 | + return j.at(property).get<std::shared_ptr<T>>(); | |
| 68 | + } | |
| 69 | + return std::shared_ptr<T>(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + template <typename T> | |
| 73 | + inline std::shared_ptr<T> get_heap_optional(const json & j, std::string property) { | |
| 74 | + return get_heap_optional<T>(j, property.data()); | |
| 75 | + } | |
| 76 | + template <typename T> | |
| 77 | + inline std::optional<T> get_stack_optional(const json & j, const char * property) { | |
| 78 | + auto it = j.find(property); | |
| 79 | + if (it != j.end() && !it->is_null()) { | |
| 80 | + return j.at(property).get<std::optional<T>>(); | |
| 81 | + } | |
| 82 | + return std::optional<T>(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + template <typename T> | |
| 86 | + inline std::optional<T> get_stack_optional(const json & j, std::string property) { | |
| 87 | + return get_stack_optional<T>(j, property.data()); | |
| 88 | + } | |
| 89 | + #endif | |
| 90 | + | |
| 91 | + class Data { | |
| 92 | + public: | |
| 93 | + Data() = default; | |
| 94 | + virtual ~Data() = default; | |
| 95 | + | |
| 96 | + private: | |
| 97 | + std::optional<std::string> id; | |
| 98 | + | |
| 99 | + public: | |
| 100 | + const std::optional<std::string> & get_id() const { return id; } | |
| 101 | + std::optional<std::string> & get_mutable_id() { return id; } | |
| 102 | + void set_id(const std::optional<std::string> & value) { this->id = value; } | |
| 103 | + }; | |
| 104 | + | |
| 105 | + class TopLevel { | |
| 106 | + public: | |
| 107 | + TopLevel() = default; | |
| 108 | + virtual ~TopLevel() = default; | |
| 109 | + | |
| 110 | + private: | |
| 111 | + std::optional<std::vector<TopLevel>> children; | |
| 112 | + std::optional<Data> data; | |
| 113 | + | |
| 114 | + public: | |
| 115 | + const std::optional<std::vector<TopLevel>> & get_children() const { return children; } | |
| 116 | + std::optional<std::vector<TopLevel>> & get_mutable_children() { return children; } | |
| 117 | + void set_children(const std::optional<std::vector<TopLevel>> & value) { this->children = value; } | |
| 118 | + | |
| 119 | + const std::optional<Data> & get_data() const { return data; } | |
| 120 | + std::optional<Data> & get_mutable_data() { return data; } | |
| 121 | + void set_data(const std::optional<Data> & value) { this->data = value; } | |
| 122 | + }; | |
| 123 | +} | |
| 124 | + | |
| 125 | +namespace quicktype { | |
| 126 | + void from_json(const json & j, Data & x); | |
| 127 | + void to_json(json & j, const Data & x); | |
| 128 | + | |
| 129 | + void from_json(const json & j, TopLevel & x); | |
| 130 | + void to_json(json & j, const TopLevel & x); | |
| 131 | + | |
| 132 | + inline void from_json(const json & j, Data& x) { | |
| 133 | + x.set_id(get_stack_optional<std::string>(j, "id")); | |
| 134 | + } | |
| 135 | + | |
| 136 | + inline void to_json(json & j, const Data & x) { | |
| 137 | + j = json::object(); | |
| 138 | + j["id"] = x.get_id(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + inline void from_json(const json & j, TopLevel& x) { | |
| 142 | + x.set_children(get_stack_optional<std::vector<TopLevel>>(j, "children")); | |
| 143 | + x.set_data(get_stack_optional<Data>(j, "data")); | |
| 144 | + } | |
| 145 | + | |
| 146 | + inline void to_json(json & j, const TopLevel & x) { | |
| 147 | + j = json::object(); | |
| 148 | + j["children"] = x.get_children(); | |
| 149 | + j["data"] = x.get_data(); | |
| 150 | + } | |
| 151 | +} |
Aschema-csharp-SystemTextJsondefault / 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("children", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 29 | + public TopLevel[]? Children { get; set; } | |
| 30 | + | |
| 31 | + [JsonProperty("data", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 32 | + public Data? Data { get; set; } | |
| 33 | + } | |
| 34 | + | |
| 35 | + public partial class Data | |
| 36 | + { | |
| 37 | + [JsonProperty("id", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)] | |
| 38 | + public string? Id { 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-csharpdefault / 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 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 26 | + [JsonPropertyName("children")] | |
| 27 | + public TopLevel[]? Children { get; set; } | |
| 28 | + | |
| 29 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 30 | + [JsonPropertyName("data")] | |
| 31 | + public Data? Data { get; set; } | |
| 32 | + } | |
| 33 | + | |
| 34 | + public partial class Data | |
| 35 | + { | |
| 36 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | |
| 37 | + [JsonPropertyName("id")] | |
| 38 | + public string? Id { 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-elixirdefault / QuickType.ex+73 −0
| @@ -0,0 +1,73 @@ | ||
| 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 Data do | |
| 9 | + defstruct [:id] | |
| 10 | + | |
| 11 | + @type t :: %__MODULE__{ | |
| 12 | + id: String.t() | nil | |
| 13 | + } | |
| 14 | + | |
| 15 | + def from_map(m) do | |
| 16 | + %Data{ | |
| 17 | + id: m["id"], | |
| 18 | + } | |
| 19 | + end | |
| 20 | + | |
| 21 | + def from_json(json) do | |
| 22 | + json | |
| 23 | + |> Jason.decode!() | |
| 24 | + |> from_map() | |
| 25 | + end | |
| 26 | + | |
| 27 | + def to_map(struct) do | |
| 28 | + %{ | |
| 29 | + "id" => struct.id, | |
| 30 | + } | |
| 31 | + end | |
| 32 | + | |
| 33 | + def to_json(struct) do | |
| 34 | + struct | |
| 35 | + |> to_map() | |
| 36 | + |> Jason.encode!() | |
| 37 | + end | |
| 38 | +end | |
| 39 | + | |
| 40 | +defmodule TopLevel do | |
| 41 | + defstruct [:children, :data] | |
| 42 | + | |
| 43 | + @type t :: %__MODULE__{ | |
| 44 | + children: [TopLevel.t()] | nil, | |
| 45 | + data: Data.t() | nil | |
| 46 | + } | |
| 47 | + | |
| 48 | + def from_map(m) do | |
| 49 | + %TopLevel{ | |
| 50 | + children: m["children"] && Enum.map(m["children"], &TopLevel.from_map/1), | |
| 51 | + data: m["data"] && Data.from_map(m["data"]), | |
| 52 | + } | |
| 53 | + end | |
| 54 | + | |
| 55 | + def from_json(json) do | |
| 56 | + json | |
| 57 | + |> Jason.decode!() | |
| 58 | + |> from_map() | |
| 59 | + end | |
| 60 | + | |
| 61 | + def to_map(struct) do | |
| 62 | + %{ | |
| 63 | + "children" => struct.children && Enum.map(struct.children, &TopLevel.to_map/1), | |
| 64 | + "data" => struct.data && Data.to_map(struct.data), | |
| 65 | + } | |
| 66 | + end | |
| 67 | + | |
| 68 | + def to_json(struct) do | |
| 69 | + struct | |
| 70 | + |> to_map() | |
| 71 | + |> Jason.encode!() | |
| 72 | + end | |
| 73 | +end |
Aschema-flowdefault / TopLevel.js+199 −0
| @@ -0,0 +1,199 @@ | ||
| 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 | + children?: TopLevel[]; | |
| 14 | + data?: Data; | |
| 15 | + [property: string]: mixed; | |
| 16 | +}; | |
| 17 | + | |
| 18 | +export type Data = { | |
| 19 | + id?: string; | |
| 20 | + [property: string]: mixed; | |
| 21 | +}; | |
| 22 | + | |
| 23 | +// Converts JSON strings to/from your types | |
| 24 | +// and asserts the results of JSON.parse at runtime | |
| 25 | +function toTopLevel(json: string): TopLevel { | |
| 26 | + return cast(JSON.parse(json), r("TopLevel")); | |
| 27 | +} | |
| 28 | + | |
| 29 | +function topLevelToJson(value: TopLevel): string { | |
| 30 | + return JSON.stringify(uncast(value, r("TopLevel")), null, 2); | |
| 31 | +} | |
| 32 | + | |
| 33 | +function invalidValue(typ: any, val: any, key: any, parent: any = '') { | |
| 34 | + const prettyTyp = prettyTypeName(typ); | |
| 35 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 36 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 37 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 38 | +} | |
| 39 | + | |
| 40 | +function prettyTypeName(typ: any): string { | |
| 41 | + if (Array.isArray(typ)) { | |
| 42 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 43 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 44 | + } else { | |
| 45 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 46 | + } | |
| 47 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 48 | + return typ.literal; | |
| 49 | + } else { | |
| 50 | + return typeof typ; | |
| 51 | + } | |
| 52 | +} | |
| 53 | + | |
| 54 | +function jsonToJSProps(typ: any): any { | |
| 55 | + if (typ.jsonToJS === undefined) { | |
| 56 | + const map: any = {}; | |
| 57 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 58 | + typ.jsonToJS = map; | |
| 59 | + } | |
| 60 | + return typ.jsonToJS; | |
| 61 | +} | |
| 62 | + | |
| 63 | +function jsToJSONProps(typ: any): any { | |
| 64 | + if (typ.jsToJSON === undefined) { | |
| 65 | + const map: any = {}; | |
| 66 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 67 | + typ.jsToJSON = map; | |
| 68 | + } | |
| 69 | + return typ.jsToJSON; | |
| 70 | +} | |
| 71 | + | |
| 72 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 73 | + function transformPrimitive(typ: string, val: any): any { | |
| 74 | + if (typeof typ === typeof val) return val; | |
| 75 | + return invalidValue(typ, val, key, parent); | |
| 76 | + } | |
| 77 | + | |
| 78 | + function transformUnion(typs: any[], val: any): any { | |
| 79 | + // val must validate against one typ in typs | |
| 80 | + const l = typs.length; | |
| 81 | + for (let i = 0; i < l; i++) { | |
| 82 | + const typ = typs[i]; | |
| 83 | + try { | |
| 84 | + return transform(val, typ, getProps); | |
| 85 | + } catch (_) {} | |
| 86 | + } | |
| 87 | + return invalidValue(typs, val, key, parent); | |
| 88 | + } | |
| 89 | + | |
| 90 | + function transformEnum(cases: string[], val: any): any { | |
| 91 | + if (cases.indexOf(val) !== -1) return val; | |
| 92 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 93 | + } | |
| 94 | + | |
| 95 | + function transformArray(typ: any, val: any): any { | |
| 96 | + // val must be an array with no invalid elements | |
| 97 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 98 | + return val.map(el => transform(el, typ, getProps)); | |
| 99 | + } | |
| 100 | + | |
| 101 | + function transformDate(val: any): any { | |
| 102 | + if (val === null) { | |
| 103 | + return null; | |
| 104 | + } | |
| 105 | + const d = new Date(val); | |
| 106 | + if (isNaN(d.valueOf())) { | |
| 107 | + return invalidValue(l("Date"), val, key, parent); | |
| 108 | + } | |
| 109 | + return d; | |
| 110 | + } | |
| 111 | + | |
| 112 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 113 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 114 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 115 | + } | |
| 116 | + const result: any = {}; | |
| 117 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 118 | + const prop = props[key]; | |
| 119 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 120 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 121 | + }); | |
| 122 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 123 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 124 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 125 | + } | |
| 126 | + }); | |
| 127 | + return result; | |
| 128 | + } | |
| 129 | + | |
| 130 | + if (typ === "any") return val; | |
| 131 | + if (typ === null) { | |
| 132 | + if (val === null) return val; | |
| 133 | + return invalidValue(typ, val, key, parent); | |
| 134 | + } | |
| 135 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 136 | + let ref: any = undefined; | |
| 137 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 138 | + ref = typ.ref; | |
| 139 | + typ = typeMap[typ.ref]; | |
| 140 | + } | |
| 141 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 142 | + if (typeof typ === "object") { | |
| 143 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 144 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 145 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 146 | + : invalidValue(typ, val, key, parent); | |
| 147 | + } | |
| 148 | + // Numbers can be parsed by Date but shouldn't be. | |
| 149 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 150 | + return transformPrimitive(typ, val); | |
| 151 | +} | |
| 152 | + | |
| 153 | +function cast<T>(val: any, typ: any): T { | |
| 154 | + return transform(val, typ, jsonToJSProps); | |
| 155 | +} | |
| 156 | + | |
| 157 | +function uncast<T>(val: T, typ: any): any { | |
| 158 | + return transform(val, typ, jsToJSONProps); | |
| 159 | +} | |
| 160 | + | |
| 161 | +function l(typ: any) { | |
| 162 | + return { literal: typ }; | |
| 163 | +} | |
| 164 | + | |
| 165 | +function a(typ: any) { | |
| 166 | + return { arrayItems: typ }; | |
| 167 | +} | |
| 168 | + | |
| 169 | +function u(...typs: any[]) { | |
| 170 | + return { unionMembers: typs }; | |
| 171 | +} | |
| 172 | + | |
| 173 | +function o(props: any[], additional: any) { | |
| 174 | + return { props, additional }; | |
| 175 | +} | |
| 176 | + | |
| 177 | +function m(additional: any) { | |
| 178 | + const props: any[] = []; | |
| 179 | + return { props, additional }; | |
| 180 | +} | |
| 181 | + | |
| 182 | +function r(name: string) { | |
| 183 | + return { ref: name }; | |
| 184 | +} | |
| 185 | + | |
| 186 | +const typeMap: any = { | |
| 187 | + "TopLevel": o([ | |
| 188 | + { json: "children", js: "children", typ: u(undefined, a(r("TopLevel"))) }, | |
| 189 | + { json: "data", js: "data", typ: u(undefined, r("Data")) }, | |
| 190 | + ], "any"), | |
| 191 | + "Data": o([ | |
| 192 | + { json: "id", js: "id", typ: u(undefined, "") }, | |
| 193 | + ], "any"), | |
| 194 | +}; | |
| 195 | + | |
| 196 | +module.exports = { | |
| 197 | + "topLevelToJson": topLevelToJson, | |
| 198 | + "toTopLevel": toTopLevel, | |
| 199 | +}; |
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 | + Children []TopLevel `json:"children,omitempty"` | |
| 23 | + Data *Data `json:"data,omitempty"` | |
| 24 | +} | |
| 25 | + | |
| 26 | +type Data struct { | |
| 27 | + ID *string `json:"id,omitempty"` | |
| 28 | +} |
Aschema-java-datetime-legacydefault / 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-datetime-legacydefault / src / main / java / io / quicktype / Data.java+12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class Data { | |
| 6 | + private String id; | |
| 7 | + | |
| 8 | + @JsonProperty("id") | |
| 9 | + public String getID() { return id; } | |
| 10 | + @JsonProperty("id") | |
| 11 | + public void setID(String value) { this.id = value; } | |
| 12 | +} |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / TopLevel.java+19 −0
| @@ -0,0 +1,19 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +public class TopLevel { | |
| 7 | + private List<TopLevel> children; | |
| 8 | + private Data data; | |
| 9 | + | |
| 10 | + @JsonProperty("children") | |
| 11 | + public List<TopLevel> getChildren() { return children; } | |
| 12 | + @JsonProperty("children") | |
| 13 | + public void setChildren(List<TopLevel> value) { this.children = value; } | |
| 14 | + | |
| 15 | + @JsonProperty("data") | |
| 16 | + public Data getData() { return data; } | |
| 17 | + @JsonProperty("data") | |
| 18 | + public void setData(Data value) { this.data = value; } | |
| 19 | +} |
Aschema-java-lombokdefault / 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-lombokdefault / src / main / java / io / quicktype / Data.java+12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class Data { | |
| 6 | + private String id; | |
| 7 | + | |
| 8 | + @JsonProperty("id") | |
| 9 | + public String getID() { return id; } | |
| 10 | + @JsonProperty("id") | |
| 11 | + public void setID(String value) { this.id = value; } | |
| 12 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / TopLevel.java+19 −0
| @@ -0,0 +1,19 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +public class TopLevel { | |
| 7 | + private List<TopLevel> children; | |
| 8 | + private Data data; | |
| 9 | + | |
| 10 | + @JsonProperty("children") | |
| 11 | + public List<TopLevel> getChildren() { return children; } | |
| 12 | + @JsonProperty("children") | |
| 13 | + public void setChildren(List<TopLevel> value) { this.children = value; } | |
| 14 | + | |
| 15 | + @JsonProperty("data") | |
| 16 | + public Data getData() { return data; } | |
| 17 | + @JsonProperty("data") | |
| 18 | + public void setData(Data value) { this.data = value; } | |
| 19 | +} |
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 / Data.java+12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class Data { | |
| 6 | + private String id; | |
| 7 | + | |
| 8 | + @JsonProperty("id") | |
| 9 | + public String getID() { return id; } | |
| 10 | + @JsonProperty("id") | |
| 11 | + public void setID(String value) { this.id = value; } | |
| 12 | +} |
Aschema-javadefault / src / main / java / io / quicktype / TopLevel.java+19 −0
| @@ -0,0 +1,19 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +public class TopLevel { | |
| 7 | + private List<TopLevel> children; | |
| 8 | + private Data data; | |
| 9 | + | |
| 10 | + @JsonProperty("children") | |
| 11 | + public List<TopLevel> getChildren() { return children; } | |
| 12 | + @JsonProperty("children") | |
| 13 | + public void setChildren(List<TopLevel> value) { this.children = value; } | |
| 14 | + | |
| 15 | + @JsonProperty("data") | |
| 16 | + public Data getData() { return data; } | |
| 17 | + @JsonProperty("data") | |
| 18 | + public void setData(Data value) { this.data = value; } | |
| 19 | +} |
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: "children", js: "children", typ: u(undefined, a(r("TopLevel"))) }, | |
| 176 | + { json: "data", js: "data", typ: u(undefined, r("Data")) }, | |
| 177 | + ], "any"), | |
| 178 | + "Data": o([ | |
| 179 | + { json: "id", js: "id", typ: u(undefined, "") }, | |
| 180 | + ], "any"), | |
| 181 | +}; | |
| 182 | + | |
| 183 | +module.exports = { | |
| 184 | + "topLevelToJson": topLevelToJson, | |
| 185 | + "toTopLevel": toTopLevel, | |
| 186 | +}; |
Aschema-kotlin-jacksondefault / TopLevel.kt+24 −0
| @@ -0,0 +1,24 @@ | ||
| 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 val klaxon = Klaxon() | |
| 10 | + | |
| 11 | +data class TopLevel ( | |
| 12 | + val children: List<TopLevel>? = null, | |
| 13 | + val data: Data? = null | |
| 14 | +) { | |
| 15 | + public fun toJson() = klaxon.toJsonString(this) | |
| 16 | + | |
| 17 | + companion object { | |
| 18 | + public fun fromJson(json: String) = klaxon.parse<TopLevel>(json) | |
| 19 | + } | |
| 20 | +} | |
| 21 | + | |
| 22 | +data class Data ( | |
| 23 | + val id: String? = null | |
| 24 | +) |
Aschema-kotlindefault / TopLevel.kt+34 −0
| @@ -0,0 +1,34 @@ | ||
| 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 | +val mapper = jacksonObjectMapper().apply { | |
| 17 | + propertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE | |
| 18 | + setSerializationInclusion(JsonInclude.Include.NON_NULL) | |
| 19 | +} | |
| 20 | + | |
| 21 | +data class TopLevel ( | |
| 22 | + val children: List<TopLevel>? = null, | |
| 23 | + val data: Data? = null | |
| 24 | +) { | |
| 25 | + fun toJson() = mapper.writeValueAsString(this) | |
| 26 | + | |
| 27 | + companion object { | |
| 28 | + fun fromJson(json: String) = mapper.readValue<TopLevel>(json) | |
| 29 | + } | |
| 30 | +} | |
| 31 | + | |
| 32 | +data class Data ( | |
| 33 | + val id: String? = null | |
| 34 | +) |
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 children: List<TopLevel>? = null, | |
| 16 | + val data: Data? = null | |
| 17 | +) | |
| 18 | + | |
| 19 | +@Serializable | |
| 20 | +data class Data ( | |
| 21 | + val id: String? = null | |
| 22 | +) |
Aschema-phpdefault / TopLevel.php+295 −0
| @@ -0,0 +1,295 @@ | ||
| 1 | +<?php | |
| 2 | +declare(strict_types=1); | |
| 3 | + | |
| 4 | +// This is an autogenerated file:TopLevel | |
| 5 | + | |
| 6 | +class TopLevel { | |
| 7 | + private ?array $children; // json:children Optional | |
| 8 | + private ?Data $data; // json:data Optional | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * @param array|null $children | |
| 12 | + * @param Data|null $data | |
| 13 | + */ | |
| 14 | + public function __construct(?array $children, ?Data $data) { | |
| 15 | + $this->children = $children; | |
| 16 | + $this->data = $data; | |
| 17 | + } | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @param ?array $value | |
| 21 | + * @throws Exception | |
| 22 | + * @return ?array | |
| 23 | + */ | |
| 24 | + public static function fromChildren(?array $value): ?array { | |
| 25 | + if (!is_null($value)) { | |
| 26 | + return array_map(function ($value) { | |
| 27 | + return TopLevel::from($value); /*class*/ | |
| 28 | + }, $value); | |
| 29 | + } else { | |
| 30 | + return null; | |
| 31 | + } | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * @throws Exception | |
| 36 | + * @return ?array | |
| 37 | + */ | |
| 38 | + public function toChildren(): ?array { | |
| 39 | + if (TopLevel::validateChildren($this->children)) { | |
| 40 | + if (!is_null($this->children)) { | |
| 41 | + return array_map(function ($value) { | |
| 42 | + return $value->to(); /*class*/ | |
| 43 | + }, $this->children); | |
| 44 | + } else { | |
| 45 | + return null; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + throw new Exception('never get to this TopLevel::children'); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @param array|null | |
| 53 | + * @return bool | |
| 54 | + * @throws Exception | |
| 55 | + */ | |
| 56 | + public static function validateChildren(?array $value): bool { | |
| 57 | + if (!is_null($value)) { | |
| 58 | + if (!is_array($value)) { | |
| 59 | + throw new Exception("Attribute Error:TopLevel::children"); | |
| 60 | + } | |
| 61 | + array_walk($value, function($value_v) { | |
| 62 | + $value_v->validate(); | |
| 63 | + }); | |
| 64 | + } | |
| 65 | + return true; | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * @throws Exception | |
| 70 | + * @return ?array | |
| 71 | + */ | |
| 72 | + public function getChildren(): ?array { | |
| 73 | + if (TopLevel::validateChildren($this->children)) { | |
| 74 | + return $this->children; | |
| 75 | + } | |
| 76 | + throw new Exception('never get to getChildren TopLevel::children'); | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * @return ?array | |
| 81 | + */ | |
| 82 | + public static function sampleChildren(): ?array { | |
| 83 | + return array( | |
| 84 | + TopLevel::sample() /*31:*/ | |
| 85 | + ); /* 31:children*/ | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * @param ?stdClass $value | |
| 90 | + * @throws Exception | |
| 91 | + * @return ?Data | |
| 92 | + */ | |
| 93 | + public static function fromData(?stdClass $value): ?Data { | |
| 94 | + if (!is_null($value)) { | |
| 95 | + return Data::from($value); /*class*/ | |
| 96 | + } else { | |
| 97 | + return null; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * @throws Exception | |
| 103 | + * @return ?stdClass | |
| 104 | + */ | |
| 105 | + public function toData(): ?stdClass { | |
| 106 | + if (TopLevel::validateData($this->data)) { | |
| 107 | + if (!is_null($this->data)) { | |
| 108 | + return $this->data->to(); /*class*/ | |
| 109 | + } else { | |
| 110 | + return null; | |
| 111 | + } | |
| 112 | + } | |
| 113 | + throw new Exception('never get to this TopLevel::data'); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * @param Data|null | |
| 118 | + * @return bool | |
| 119 | + * @throws Exception | |
| 120 | + */ | |
| 121 | + public static function validateData(?Data $value): bool { | |
| 122 | + if (!is_null($value)) { | |
| 123 | + $value->validate(); | |
| 124 | + } | |
| 125 | + return true; | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * @throws Exception | |
| 130 | + * @return ?Data | |
| 131 | + */ | |
| 132 | + public function getData(): ?Data { | |
| 133 | + if (TopLevel::validateData($this->data)) { | |
| 134 | + return $this->data; | |
| 135 | + } | |
| 136 | + throw new Exception('never get to getData TopLevel::data'); | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * @return ?Data | |
| 141 | + */ | |
| 142 | + public static function sampleData(): ?Data { | |
| 143 | + return Data::sample(); /*32:data*/ | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * @throws Exception | |
| 148 | + * @return bool | |
| 149 | + */ | |
| 150 | + public function validate(): bool { | |
| 151 | + return TopLevel::validateChildren($this->children) | |
| 152 | + || TopLevel::validateData($this->data); | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * @return stdClass | |
| 157 | + * @throws Exception | |
| 158 | + */ | |
| 159 | + public function to(): stdClass { | |
| 160 | + $out = new stdClass(); | |
| 161 | + $out->{'children'} = $this->toChildren(); | |
| 162 | + $out->{'data'} = $this->toData(); | |
| 163 | + return $out; | |
| 164 | + } | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * @param stdClass $obj | |
| 168 | + * @return TopLevel | |
| 169 | + * @throws Exception | |
| 170 | + */ | |
| 171 | + public static function from(stdClass $obj): TopLevel { | |
| 172 | + return new TopLevel( | |
| 173 | + TopLevel::fromChildren($obj->{'children'}) | |
| 174 | + ,TopLevel::fromData($obj->{'data'}) | |
| 175 | + ); | |
| 176 | + } | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * @return TopLevel | |
| 180 | + */ | |
| 181 | + public static function sample(): TopLevel { | |
| 182 | + return new TopLevel( | |
| 183 | + TopLevel::sampleChildren() | |
| 184 | + ,TopLevel::sampleData() | |
| 185 | + ); | |
| 186 | + } | |
| 187 | +} | |
| 188 | + | |
| 189 | +// This is an autogenerated file:Data | |
| 190 | + | |
| 191 | +class Data { | |
| 192 | + private ?string $id; // json:id Optional | |
| 193 | + | |
| 194 | + /** | |
| 195 | + * @param string|null $id | |
| 196 | + */ | |
| 197 | + public function __construct(?string $id) { | |
| 198 | + $this->id = $id; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * @param ?string $value | |
| 203 | + * @throws Exception | |
| 204 | + * @return ?string | |
| 205 | + */ | |
| 206 | + public static function fromID(?string $value): ?string { | |
| 207 | + if (!is_null($value)) { | |
| 208 | + return $value; /*string*/ | |
| 209 | + } else { | |
| 210 | + return null; | |
| 211 | + } | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * @throws Exception | |
| 216 | + * @return ?string | |
| 217 | + */ | |
| 218 | + public function toID(): ?string { | |
| 219 | + if (Data::validateID($this->id)) { | |
| 220 | + if (!is_null($this->id)) { | |
| 221 | + return $this->id; /*string*/ | |
| 222 | + } else { | |
| 223 | + return null; | |
| 224 | + } | |
| 225 | + } | |
| 226 | + throw new Exception('never get to this Data::id'); | |
| 227 | + } | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * @param string|null | |
| 231 | + * @return bool | |
| 232 | + * @throws Exception | |
| 233 | + */ | |
| 234 | + public static function validateID(?string $value): bool { | |
| 235 | + if (!is_null($value)) { | |
| 236 | + } | |
| 237 | + return true; | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * @throws Exception | |
| 242 | + * @return ?string | |
| 243 | + */ | |
| 244 | + public function getID(): ?string { | |
| 245 | + if (Data::validateID($this->id)) { | |
| 246 | + return $this->id; | |
| 247 | + } | |
| 248 | + throw new Exception('never get to getID Data::id'); | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * @return ?string | |
| 253 | + */ | |
| 254 | + public static function sampleID(): ?string { | |
| 255 | + return 'Data::id::31'; /*31:id*/ | |
| 256 | + } | |
| 257 | + | |
| 258 | + /** | |
| 259 | + * @throws Exception | |
| 260 | + * @return bool | |
| 261 | + */ | |
| 262 | + public function validate(): bool { | |
| 263 | + return Data::validateID($this->id); | |
| 264 | + } | |
| 265 | + | |
| 266 | + /** | |
| 267 | + * @return stdClass | |
| 268 | + * @throws Exception | |
| 269 | + */ | |
| 270 | + public function to(): stdClass { | |
| 271 | + $out = new stdClass(); | |
| 272 | + $out->{'id'} = $this->toID(); | |
| 273 | + return $out; | |
| 274 | + } | |
| 275 | + | |
| 276 | + /** | |
| 277 | + * @param stdClass $obj | |
| 278 | + * @return Data | |
| 279 | + * @throws Exception | |
| 280 | + */ | |
| 281 | + public static function from(stdClass $obj): Data { | |
| 282 | + return new Data( | |
| 283 | + Data::fromID($obj->{'id'}) | |
| 284 | + ); | |
| 285 | + } | |
| 286 | + | |
| 287 | + /** | |
| 288 | + * @return Data | |
| 289 | + */ | |
| 290 | + public static function sample(): Data { | |
| 291 | + return new Data( | |
| 292 | + Data::sampleID() | |
| 293 | + ); | |
| 294 | + } | |
| 295 | +} |
Aschema-pikedefault / TopLevel.pmod+56 −0
| @@ -0,0 +1,56 @@ | ||
| 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 | + array(TopLevel)|mixed children; // json: "children" | |
| 17 | + Data|mixed data; // json: "data" | |
| 18 | + | |
| 19 | + string encode_json() { | |
| 20 | + mapping(string:mixed) json = ([ | |
| 21 | + "children" : children, | |
| 22 | + "data" : data, | |
| 23 | + ]); | |
| 24 | + | |
| 25 | + return Standards.JSON.encode(json); | |
| 26 | + } | |
| 27 | +} | |
| 28 | + | |
| 29 | +TopLevel TopLevel_from_JSON(mixed json) { | |
| 30 | + TopLevel retval = TopLevel(); | |
| 31 | + | |
| 32 | + retval.children = json["children"]; | |
| 33 | + retval.data = json["data"]; | |
| 34 | + | |
| 35 | + return retval; | |
| 36 | +} | |
| 37 | + | |
| 38 | +class Data { | |
| 39 | + mixed|string id; // json: "id" | |
| 40 | + | |
| 41 | + string encode_json() { | |
| 42 | + mapping(string:mixed) json = ([ | |
| 43 | + "id" : id, | |
| 44 | + ]); | |
| 45 | + | |
| 46 | + return Standards.JSON.encode(json); | |
| 47 | + } | |
| 48 | +} | |
| 49 | + | |
| 50 | +Data Data_from_JSON(mixed json) { | |
| 51 | + Data retval = Data(); | |
| 52 | + | |
| 53 | + retval.id = json["id"]; | |
| 54 | + | |
| 55 | + return retval; | |
| 56 | +} |
Aschema-pythondefault / quicktype.py+80 −0
| @@ -0,0 +1,80 @@ | ||
| 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_none(x: Any) -> Any: | |
| 14 | + assert x is None | |
| 15 | + return x | |
| 16 | + | |
| 17 | + | |
| 18 | +def from_union(fs, x): | |
| 19 | + for f in fs: | |
| 20 | + try: | |
| 21 | + return f(x) | |
| 22 | + except: | |
| 23 | + pass | |
| 24 | + assert False | |
| 25 | + | |
| 26 | + | |
| 27 | +def from_list(f: Callable[[Any], T], x: Any) -> list[T]: | |
| 28 | + assert isinstance(x, list) | |
| 29 | + return [f(y) for y in x] | |
| 30 | + | |
| 31 | + | |
| 32 | +def to_class(c: Type[T], x: Any) -> dict: | |
| 33 | + assert isinstance(x, c) | |
| 34 | + return cast(Any, x).to_dict() | |
| 35 | + | |
| 36 | + | |
| 37 | +@dataclass | |
| 38 | +class Data: | |
| 39 | + id: str | None = None | |
| 40 | + | |
| 41 | + @staticmethod | |
| 42 | + def from_dict(obj: Any) -> 'Data': | |
| 43 | + assert isinstance(obj, dict) | |
| 44 | + id = from_union([from_str, from_none], obj.get("id")) | |
| 45 | + return Data(id) | |
| 46 | + | |
| 47 | + def to_dict(self) -> dict: | |
| 48 | + result: dict = {} | |
| 49 | + if self.id is not None: | |
| 50 | + result["id"] = from_union([from_str, from_none], self.id) | |
| 51 | + return result | |
| 52 | + | |
| 53 | + | |
| 54 | +@dataclass | |
| 55 | +class TopLevel: | |
| 56 | + children: 'list[TopLevel] | None' = None | |
| 57 | + data: Data | None = None | |
| 58 | + | |
| 59 | + @staticmethod | |
| 60 | + def from_dict(obj: Any) -> 'TopLevel': | |
| 61 | + assert isinstance(obj, dict) | |
| 62 | + children = from_union([lambda x: from_list(TopLevel.from_dict, x), from_none], obj.get("children")) | |
| 63 | + data = from_union([Data.from_dict, from_none], obj.get("data")) | |
| 64 | + return TopLevel(children, data) | |
| 65 | + | |
| 66 | + def to_dict(self) -> dict: | |
| 67 | + result: dict = {} | |
| 68 | + if self.children is not None: | |
| 69 | + result["children"] = from_union([lambda x: from_list(lambda x: to_class(TopLevel, x), x), from_none], self.children) | |
| 70 | + if self.data is not None: | |
| 71 | + result["data"] = from_union([lambda x: to_class(Data, x), from_none], self.data) | |
| 72 | + return result | |
| 73 | + | |
| 74 | + | |
| 75 | +def top_level_from_dict(s: Any) -> TopLevel: | |
| 76 | + return TopLevel.from_dict(s) | |
| 77 | + | |
| 78 | + | |
| 79 | +def top_level_to_dict(x: TopLevel) -> Any: | |
| 80 | + return to_class(TopLevel, x) |
Aschema-rubydefault / TopLevel.rb+73 −0
| @@ -0,0 +1,73 @@ | ||
| 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.data&.id | |
| 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 | +end | |
| 21 | + | |
| 22 | +class DataClass < Dry::Struct | |
| 23 | + attribute :id, Types::String.optional | |
| 24 | + | |
| 25 | + def self.from_dynamic!(d) | |
| 26 | + d = Types::Hash[d] | |
| 27 | + new( | |
| 28 | + id: d["id"], | |
| 29 | + ) | |
| 30 | + end | |
| 31 | + | |
| 32 | + def self.from_json!(json) | |
| 33 | + from_dynamic!(JSON.parse(json)) | |
| 34 | + end | |
| 35 | + | |
| 36 | + def to_dynamic | |
| 37 | + { | |
| 38 | + "id" => id, | |
| 39 | + } | |
| 40 | + end | |
| 41 | + | |
| 42 | + def to_json(options = nil) | |
| 43 | + JSON.generate(to_dynamic, options) | |
| 44 | + end | |
| 45 | +end | |
| 46 | + | |
| 47 | +class TopLevel < Dry::Struct | |
| 48 | + attribute :children, Types.Array(TopLevel).optional | |
| 49 | + attribute :data, DataClass.optional | |
| 50 | + | |
| 51 | + def self.from_dynamic!(d) | |
| 52 | + d = Types::Hash[d] | |
| 53 | + new( | |
| 54 | + children: d["children"]&.map { |x| TopLevel.from_dynamic!(x) }, | |
| 55 | + data: d["data"] ? DataClass.from_dynamic!(d["data"]) : nil, | |
| 56 | + ) | |
| 57 | + end | |
| 58 | + | |
| 59 | + def self.from_json!(json) | |
| 60 | + from_dynamic!(JSON.parse(json)) | |
| 61 | + end | |
| 62 | + | |
| 63 | + def to_dynamic | |
| 64 | + { | |
| 65 | + "children" => children&.map { |x| x.to_dynamic }, | |
| 66 | + "data" => data&.to_dynamic, | |
| 67 | + } | |
| 68 | + end | |
| 69 | + | |
| 70 | + def to_json(options = nil) | |
| 71 | + JSON.generate(to_dynamic, options) | |
| 72 | + end | |
| 73 | +end |
Aschema-rustdefault / module_under_test.rs+26 −0
| @@ -0,0 +1,26 @@ | ||
| 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 | + pub children: Option<Vec<TopLevel>>, | |
| 19 | + | |
| 20 | + pub data: Option<Data>, | |
| 21 | +} | |
| 22 | + | |
| 23 | +#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 24 | +pub struct Data { | |
| 25 | + pub id: Option<String>, | |
| 26 | +} |
Aschema-scala3-upickledefault / 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 children : Option[Seq[TopLevel]] = None, | |
| 12 | + val data : Option[Data] = None | |
| 13 | +) derives Encoder.AsObject, Decoder | |
| 14 | + | |
| 15 | +case class Data ( | |
| 16 | + val id : Option[String] = None | |
| 17 | +) derives Encoder.AsObject, Decoder |
Aschema-scala3default / 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 children : Option[Seq[TopLevel]] = None, | |
| 70 | + val data : Option[Data] = None | |
| 71 | +) derives OptionPickler.ReadWriter | |
| 72 | + | |
| 73 | +case class Data ( | |
| 74 | + val id : Option[String] = None | |
| 75 | +) derives OptionPickler.ReadWriter |
Aschema-schemadefault / TopLevel.schema+34 −0
| @@ -0,0 +1,34 @@ | ||
| 1 | +{ | |
| 2 | + "$schema": "http://json-schema.org/draft-06/schema#", | |
| 3 | + "$ref": "#/definitions/TopLevel", | |
| 4 | + "definitions": { | |
| 5 | + "TopLevel": { | |
| 6 | + "type": "object", | |
| 7 | + "additionalProperties": {}, | |
| 8 | + "properties": { | |
| 9 | + "children": { | |
| 10 | + "type": "array", | |
| 11 | + "items": { | |
| 12 | + "$ref": "#/definitions/TopLevel" | |
| 13 | + } | |
| 14 | + }, | |
| 15 | + "data": { | |
| 16 | + "$ref": "#/definitions/Data" | |
| 17 | + } | |
| 18 | + }, | |
| 19 | + "required": [], | |
| 20 | + "title": "TopLevel" | |
| 21 | + }, | |
| 22 | + "Data": { | |
| 23 | + "type": "object", | |
| 24 | + "additionalProperties": {}, | |
| 25 | + "properties": { | |
| 26 | + "id": { | |
| 27 | + "type": "string" | |
| 28 | + } | |
| 29 | + }, | |
| 30 | + "required": [], | |
| 31 | + "title": "Data" | |
| 32 | + } | |
| 33 | + } | |
| 34 | +} |
Aschema-swiftdefault / quicktype.swift+134 −0
| @@ -0,0 +1,134 @@ | ||
| 1 | +// This file was generated from JSON Schema using quicktype, do not modify it directly. | |
| 2 | +// To parse the JSON, add this file to your project and do: | |
| 3 | +// | |
| 4 | +// let topLevel = try TopLevel(json) | |
| 5 | + | |
| 6 | +import Foundation | |
| 7 | + | |
| 8 | +// MARK: - TopLevel | |
| 9 | +struct TopLevel: Codable { | |
| 10 | + let children: [TopLevel]? | |
| 11 | + let data: DataClass? | |
| 12 | + | |
| 13 | + enum CodingKeys: String, CodingKey { | |
| 14 | + case children = "children" | |
| 15 | + case data = "data" | |
| 16 | + } | |
| 17 | +} | |
| 18 | + | |
| 19 | +// MARK: TopLevel convenience initializers and mutators | |
| 20 | + | |
| 21 | +extension TopLevel { | |
| 22 | + init(data: Data) throws { | |
| 23 | + self = try newJSONDecoder().decode(TopLevel.self, from: data) | |
| 24 | + } | |
| 25 | + | |
| 26 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 27 | + guard let data = json.data(using: encoding) else { | |
| 28 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 29 | + } | |
| 30 | + try self.init(data: data) | |
| 31 | + } | |
| 32 | + | |
| 33 | + init(fromURL url: URL) throws { | |
| 34 | + try self.init(data: try Data(contentsOf: url)) | |
| 35 | + } | |
| 36 | + | |
| 37 | + func with( | |
| 38 | + children: [TopLevel]?? = nil, | |
| 39 | + data: DataClass?? = nil | |
| 40 | + ) -> TopLevel { | |
| 41 | + return TopLevel( | |
| 42 | + children: children ?? self.children, | |
| 43 | + data: data ?? self.data | |
| 44 | + ) | |
| 45 | + } | |
| 46 | + | |
| 47 | + func jsonData() throws -> Data { | |
| 48 | + return try newJSONEncoder().encode(self) | |
| 49 | + } | |
| 50 | + | |
| 51 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 52 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 53 | + } | |
| 54 | +} | |
| 55 | + | |
| 56 | +// MARK: - DataClass | |
| 57 | +struct DataClass: Codable { | |
| 58 | + let id: String? | |
| 59 | + | |
| 60 | + enum CodingKeys: String, CodingKey { | |
| 61 | + case id = "id" | |
| 62 | + } | |
| 63 | +} | |
| 64 | + | |
| 65 | +// MARK: DataClass convenience initializers and mutators | |
| 66 | + | |
| 67 | +extension DataClass { | |
| 68 | + init(data: Data) throws { | |
| 69 | + self = try newJSONDecoder().decode(DataClass.self, from: data) | |
| 70 | + } | |
| 71 | + | |
| 72 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 73 | + guard let data = json.data(using: encoding) else { | |
| 74 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 75 | + } | |
| 76 | + try self.init(data: data) | |
| 77 | + } | |
| 78 | + | |
| 79 | + init(fromURL url: URL) throws { | |
| 80 | + try self.init(data: try Data(contentsOf: url)) | |
| 81 | + } | |
| 82 | + | |
| 83 | + func with( | |
| 84 | + id: String?? = nil | |
| 85 | + ) -> DataClass { | |
| 86 | + return DataClass( | |
| 87 | + id: id ?? self.id | |
| 88 | + ) | |
| 89 | + } | |
| 90 | + | |
| 91 | + func jsonData() throws -> Data { | |
| 92 | + return try newJSONEncoder().encode(self) | |
| 93 | + } | |
| 94 | + | |
| 95 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 96 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 97 | + } | |
| 98 | +} | |
| 99 | + | |
| 100 | +// MARK: - Helper functions for creating encoders and decoders | |
| 101 | + | |
| 102 | +func newJSONDecoder() -> JSONDecoder { | |
| 103 | + let decoder = JSONDecoder() | |
| 104 | + decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in | |
| 105 | + let container = try decoder.singleValueContainer() | |
| 106 | + let dateStr = try container.decode(String.self) | |
| 107 | + | |
| 108 | + let formatter = DateFormatter() | |
| 109 | + formatter.calendar = Calendar(identifier: .iso8601) | |
| 110 | + formatter.locale = Locale(identifier: "en_US_POSIX") | |
| 111 | + formatter.timeZone = TimeZone(secondsFromGMT: 0) | |
| 112 | + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" | |
| 113 | + if let date = formatter.date(from: dateStr) { | |
| 114 | + return date | |
| 115 | + } | |
| 116 | + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX" | |
| 117 | + if let date = formatter.date(from: dateStr) { | |
| 118 | + return date | |
| 119 | + } | |
| 120 | + throw DecodingError.typeMismatch(Date.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Could not decode date")) | |
| 121 | + }) | |
| 122 | + return decoder | |
| 123 | +} | |
| 124 | + | |
| 125 | +func newJSONEncoder() -> JSONEncoder { | |
| 126 | + let encoder = JSONEncoder() | |
| 127 | + let formatter = DateFormatter() | |
| 128 | + formatter.calendar = Calendar(identifier: .iso8601) | |
| 129 | + formatter.locale = Locale(identifier: "en_US_POSIX") | |
| 130 | + formatter.timeZone = TimeZone(secondsFromGMT: 0) | |
| 131 | + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX" | |
| 132 | + encoder.dateEncodingStrategy = .formatted(formatter) | |
| 133 | + return encoder | |
| 134 | +} |
Aschema-typescript-zoddefault / TopLevel.ts+194 −0
| @@ -0,0 +1,194 @@ | ||
| 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 | + children?: TopLevel[]; | |
| 12 | + data?: Data; | |
| 13 | + [property: string]: unknown; | |
| 14 | +} | |
| 15 | + | |
| 16 | +export interface Data { | |
| 17 | + id?: string; | |
| 18 | + [property: string]: unknown; | |
| 19 | +} | |
| 20 | + | |
| 21 | +// Converts JSON strings to/from your types | |
| 22 | +// and asserts the results of JSON.parse at runtime | |
| 23 | +export class Convert { | |
| 24 | + public static toTopLevel(json: string): TopLevel { | |
| 25 | + return cast(JSON.parse(json), r("TopLevel")); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public static topLevelToJson(value: TopLevel): string { | |
| 29 | + return JSON.stringify(uncast(value, r("TopLevel")), null, 2); | |
| 30 | + } | |
| 31 | +} | |
| 32 | + | |
| 33 | +function invalidValue(typ: any, val: any, key: any, parent: any = ''): never { | |
| 34 | + const prettyTyp = prettyTypeName(typ); | |
| 35 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 36 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 37 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 38 | +} | |
| 39 | + | |
| 40 | +function prettyTypeName(typ: any): string { | |
| 41 | + if (Array.isArray(typ)) { | |
| 42 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 43 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 44 | + } else { | |
| 45 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 46 | + } | |
| 47 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 48 | + return typ.literal; | |
| 49 | + } else { | |
| 50 | + return typeof typ; | |
| 51 | + } | |
| 52 | +} | |
| 53 | + | |
| 54 | +function jsonToJSProps(typ: any): any { | |
| 55 | + if (typ.jsonToJS === undefined) { | |
| 56 | + const map: any = {}; | |
| 57 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 58 | + typ.jsonToJS = map; | |
| 59 | + } | |
| 60 | + return typ.jsonToJS; | |
| 61 | +} | |
| 62 | + | |
| 63 | +function jsToJSONProps(typ: any): any { | |
| 64 | + if (typ.jsToJSON === undefined) { | |
| 65 | + const map: any = {}; | |
| 66 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 67 | + typ.jsToJSON = map; | |
| 68 | + } | |
| 69 | + return typ.jsToJSON; | |
| 70 | +} | |
| 71 | + | |
| 72 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 73 | + function transformPrimitive(typ: string, val: any): any { | |
| 74 | + if (typeof typ === typeof val) return val; | |
| 75 | + return invalidValue(typ, val, key, parent); | |
| 76 | + } | |
| 77 | + | |
| 78 | + function transformUnion(typs: any[], val: any): any { | |
| 79 | + // val must validate against one typ in typs | |
| 80 | + const l = typs.length; | |
| 81 | + for (let i = 0; i < l; i++) { | |
| 82 | + const typ = typs[i]; | |
| 83 | + try { | |
| 84 | + return transform(val, typ, getProps); | |
| 85 | + } catch (_) {} | |
| 86 | + } | |
| 87 | + return invalidValue(typs, val, key, parent); | |
| 88 | + } | |
| 89 | + | |
| 90 | + function transformEnum(cases: string[], val: any): any { | |
| 91 | + if (cases.indexOf(val) !== -1) return val; | |
| 92 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 93 | + } | |
| 94 | + | |
| 95 | + function transformArray(typ: any, val: any): any { | |
| 96 | + // val must be an array with no invalid elements | |
| 97 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 98 | + return val.map(el => transform(el, typ, getProps)); | |
| 99 | + } | |
| 100 | + | |
| 101 | + function transformDate(val: any): any { | |
| 102 | + if (val === null) { | |
| 103 | + return null; | |
| 104 | + } | |
| 105 | + const d = new Date(val); | |
| 106 | + if (isNaN(d.valueOf())) { | |
| 107 | + return invalidValue(l("Date"), val, key, parent); | |
| 108 | + } | |
| 109 | + return d; | |
| 110 | + } | |
| 111 | + | |
| 112 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 113 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 114 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 115 | + } | |
| 116 | + const result: any = {}; | |
| 117 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 118 | + const prop = props[key]; | |
| 119 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 120 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 121 | + }); | |
| 122 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 123 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 124 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 125 | + } | |
| 126 | + }); | |
| 127 | + return result; | |
| 128 | + } | |
| 129 | + | |
| 130 | + if (typ === "any") return val; | |
| 131 | + if (typ === null) { | |
| 132 | + if (val === null) return val; | |
| 133 | + return invalidValue(typ, val, key, parent); | |
| 134 | + } | |
| 135 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 136 | + let ref: any = undefined; | |
| 137 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 138 | + ref = typ.ref; | |
| 139 | + typ = typeMap[typ.ref]; | |
| 140 | + } | |
| 141 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 142 | + if (typeof typ === "object") { | |
| 143 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 144 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 145 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 146 | + : invalidValue(typ, val, key, parent); | |
| 147 | + } | |
| 148 | + // Numbers can be parsed by Date but shouldn't be. | |
| 149 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 150 | + return transformPrimitive(typ, val); | |
| 151 | +} | |
| 152 | + | |
| 153 | +function cast<T>(val: any, typ: any): T { | |
| 154 | + return transform(val, typ, jsonToJSProps); | |
| 155 | +} | |
| 156 | + | |
| 157 | +function uncast<T>(val: T, typ: any): any { | |
| 158 | + return transform(val, typ, jsToJSONProps); | |
| 159 | +} | |
| 160 | + | |
| 161 | +function l(typ: any) { | |
| 162 | + return { literal: typ }; | |
| 163 | +} | |
| 164 | + | |
| 165 | +function a(typ: any) { | |
| 166 | + return { arrayItems: typ }; | |
| 167 | +} | |
| 168 | + | |
| 169 | +function u(...typs: any[]) { | |
| 170 | + return { unionMembers: typs }; | |
| 171 | +} | |
| 172 | + | |
| 173 | +function o(props: any[], additional: any) { | |
| 174 | + return { props, additional }; | |
| 175 | +} | |
| 176 | + | |
| 177 | +function m(additional: any) { | |
| 178 | + const props: any[] = []; | |
| 179 | + return { props, additional }; | |
| 180 | +} | |
| 181 | + | |
| 182 | +function r(name: string) { | |
| 183 | + return { ref: name }; | |
| 184 | +} | |
| 185 | + | |
| 186 | +const typeMap: any = { | |
| 187 | + "TopLevel": o([ | |
| 188 | + { json: "children", js: "children", typ: u(undefined, a(r("TopLevel"))) }, | |
| 189 | + { json: "data", js: "data", typ: u(undefined, r("Data")) }, | |
| 190 | + ], "any"), | |
| 191 | + "Data": o([ | |
| 192 | + { json: "id", js: "id", typ: u(undefined, "") }, | |
| 193 | + ], "any"), | |
| 194 | +}; |
Aschema-typescriptdefault / TopLevel.ts+3 −13
| @@ -7,20 +7,14 @@ | ||
| 7 | 7 | // These functions will throw an error if the JSON doesn't |
| 8 | 8 | // match the expected interface, even if the JSON is valid. |
| 9 | 9 | |
| 10 | -export interface TopLevel { | |
| 11 | - next?: Next; | |
| 12 | - value: string; | |
| 13 | - [property: string]: unknown; | |
| 14 | -} | |
| 10 | +export type Next = null | TopLevel | string; | |
| 15 | 11 | |
| 16 | -export interface Node { | |
| 12 | +export interface TopLevel { | |
| 17 | 13 | next?: Next; |
| 18 | 14 | value: string; |
| 19 | 15 | [property: string]: unknown; |
| 20 | 16 | } |
| 21 | 17 | |
| 22 | -export type Next = null | Node | string; | |
| 23 | - | |
| 24 | 18 | // Converts JSON strings to/from your types |
| 25 | 19 | // and asserts the results of JSON.parse at runtime |
| 26 | 20 | export class Convert { |
| @@ -188,11 +182,7 @@ function r(name: string) { | ||
| 188 | 182 | |
| 189 | 183 | const typeMap: any = { |
| 190 | 184 | "TopLevel": o([ |
| 191 | - { json: "next", js: "next", typ: u(undefined, u(null, r("Node"), "")) }, | |
| 192 | - { json: "value", js: "value", typ: "" }, | |
| 193 | - ], "any"), | |
| 194 | - "Node": o([ | |
| 195 | - { json: "next", js: "next", typ: u(undefined, u(null, r("Node"), "")) }, | |
| 185 | + { json: "next", js: "next", typ: u(undefined, u(null, r("TopLevel"), "")) }, | |
| 196 | 186 | { json: "value", js: "value", typ: "" }, |
| 197 | 187 | ], "any"), |
| 198 | 188 | }; |
Test case
37 generated files · +603 −1,575test/inputs/schema/recursive-union-flattening.schema
Mschema-csharp-SystemTextJsondefault / QuickType.cs+40 −142
| @@ -23,70 +23,46 @@ namespace QuickType | ||
| 23 | 23 | using Newtonsoft.Json; |
| 24 | 24 | using Newtonsoft.Json.Converters; |
| 25 | 25 | |
| 26 | - public partial class TopLevelClass | |
| 26 | + public partial class A | |
| 27 | 27 | { |
| 28 | 28 | [JsonProperty("x")] |
| 29 | - public X? X { get; set; } | |
| 29 | + public PurpleTopLevel? X { get; set; } | |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - public partial class XClass | |
| 33 | - { | |
| 34 | - [JsonProperty("x")] | |
| 35 | - public X? X { get; set; } | |
| 36 | - } | |
| 37 | - | |
| 38 | - public partial struct X | |
| 39 | - { | |
| 40 | - public XElement[]? AnythingArray; | |
| 41 | - public Dictionary<string, object>? AnythingMap; | |
| 42 | - public bool? Bool; | |
| 43 | - public double? Double; | |
| 44 | - public long? Integer; | |
| 45 | - public string? String; | |
| 46 | - | |
| 47 | - public static implicit operator X(XElement[] AnythingArray) => new X { AnythingArray = AnythingArray }; | |
| 48 | - public static implicit operator X(Dictionary<string, object> AnythingMap) => new X { AnythingMap = AnythingMap }; | |
| 49 | - public static implicit operator X(bool Bool) => new X { Bool = Bool }; | |
| 50 | - public static implicit operator X(double Double) => new X { Double = Double }; | |
| 51 | - public static implicit operator X(long Integer) => new X { Integer = Integer }; | |
| 52 | - public static implicit operator X(string String) => new X { String = String }; | |
| 53 | - public bool IsNull => AnythingArray == null && Bool == null && Double == null && Integer == null && AnythingMap == null && String == null; | |
| 54 | - } | |
| 55 | - | |
| 56 | - public partial struct XElement | |
| 32 | + public partial struct TopLevelElement | |
| 57 | 33 | { |
| 34 | + public A? A; | |
| 58 | 35 | public object[]? AnythingArray; |
| 59 | 36 | public bool? Bool; |
| 60 | 37 | public double? Double; |
| 61 | 38 | public long? Integer; |
| 62 | 39 | public string? String; |
| 63 | - public XClass? XClass; | |
| 64 | 40 | |
| 65 | - public static implicit operator XElement(object[] AnythingArray) => new XElement { AnythingArray = AnythingArray }; | |
| 66 | - public static implicit operator XElement(bool Bool) => new XElement { Bool = Bool }; | |
| 67 | - public static implicit operator XElement(double Double) => new XElement { Double = Double }; | |
| 68 | - public static implicit operator XElement(long Integer) => new XElement { Integer = Integer }; | |
| 69 | - public static implicit operator XElement(string String) => new XElement { String = String }; | |
| 70 | - public static implicit operator XElement(XClass XClass) => new XElement { XClass = XClass }; | |
| 71 | - public bool IsNull => AnythingArray == null && Bool == null && XClass == null && Double == null && Integer == null && String == null; | |
| 41 | + public static implicit operator TopLevelElement(A A) => new TopLevelElement { A = A }; | |
| 42 | + public static implicit operator TopLevelElement(object[] AnythingArray) => new TopLevelElement { AnythingArray = AnythingArray }; | |
| 43 | + public static implicit operator TopLevelElement(bool Bool) => new TopLevelElement { Bool = Bool }; | |
| 44 | + public static implicit operator TopLevelElement(double Double) => new TopLevelElement { Double = Double }; | |
| 45 | + public static implicit operator TopLevelElement(long Integer) => new TopLevelElement { Integer = Integer }; | |
| 46 | + public static implicit operator TopLevelElement(string String) => new TopLevelElement { String = String }; | |
| 47 | + public bool IsNull => AnythingArray == null && Bool == null && A == null && Double == null && Integer == null && String == null; | |
| 72 | 48 | } |
| 73 | 49 | |
| 74 | - public partial struct TopLevelElement | |
| 50 | + public partial struct PurpleTopLevel | |
| 75 | 51 | { |
| 76 | - public object[]? AnythingArray; | |
| 52 | + public TopLevelElement[]? AnythingArray; | |
| 53 | + public Dictionary<string, object>? AnythingMap; | |
| 77 | 54 | public bool? Bool; |
| 78 | 55 | public double? Double; |
| 79 | 56 | public long? Integer; |
| 80 | 57 | public string? String; |
| 81 | - public TopLevelClass? TopLevelClass; | |
| 82 | 58 | |
| 83 | - public static implicit operator TopLevelElement(object[] AnythingArray) => new TopLevelElement { AnythingArray = AnythingArray }; | |
| 84 | - public static implicit operator TopLevelElement(bool Bool) => new TopLevelElement { Bool = Bool }; | |
| 85 | - public static implicit operator TopLevelElement(double Double) => new TopLevelElement { Double = Double }; | |
| 86 | - public static implicit operator TopLevelElement(long Integer) => new TopLevelElement { Integer = Integer }; | |
| 87 | - public static implicit operator TopLevelElement(string String) => new TopLevelElement { String = String }; | |
| 88 | - public static implicit operator TopLevelElement(TopLevelClass TopLevelClass) => new TopLevelElement { TopLevelClass = TopLevelClass }; | |
| 89 | - public bool IsNull => AnythingArray == null && Bool == null && TopLevelClass == null && Double == null && Integer == null && String == null; | |
| 59 | + public static implicit operator PurpleTopLevel(TopLevelElement[] AnythingArray) => new PurpleTopLevel { AnythingArray = AnythingArray }; | |
| 60 | + public static implicit operator PurpleTopLevel(Dictionary<string, object> AnythingMap) => new PurpleTopLevel { AnythingMap = AnythingMap }; | |
| 61 | + public static implicit operator PurpleTopLevel(bool Bool) => new PurpleTopLevel { Bool = Bool }; | |
| 62 | + public static implicit operator PurpleTopLevel(double Double) => new PurpleTopLevel { Double = Double }; | |
| 63 | + public static implicit operator PurpleTopLevel(long Integer) => new PurpleTopLevel { Integer = Integer }; | |
| 64 | + public static implicit operator PurpleTopLevel(string String) => new PurpleTopLevel { String = String }; | |
| 65 | + public bool IsNull => AnythingArray == null && Bool == null && Double == null && Integer == null && AnythingMap == null && String == null; | |
| 90 | 66 | } |
| 91 | 67 | |
| 92 | 68 | public partial struct TopLevelUnion |
| @@ -127,8 +103,7 @@ namespace QuickType | ||
| 127 | 103 | { |
| 128 | 104 | TopLevelUnionConverter.Singleton, |
| 129 | 105 | TopLevelElementConverter.Singleton, |
| 130 | - XConverter.Singleton, | |
| 131 | - XElementConverter.Singleton, | |
| 106 | + PurpleTopLevelConverter.Singleton, | |
| 132 | 107 | new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } |
| 133 | 108 | }, |
| 134 | 109 | }; |
| @@ -235,8 +210,8 @@ namespace QuickType | ||
| 235 | 210 | var stringValue = serializer.Deserialize<string>(reader); |
| 236 | 211 | return new TopLevelElement { String = stringValue }; |
| 237 | 212 | case JsonToken.StartObject: |
| 238 | - var objectValue = serializer.Deserialize<TopLevelClass>(reader); | |
| 239 | - return new TopLevelElement { TopLevelClass = objectValue }; | |
| 213 | + var objectValue = serializer.Deserialize<A>(reader); | |
| 214 | + return new TopLevelElement { A = objectValue }; | |
| 240 | 215 | case JsonToken.StartArray: |
| 241 | 216 | var arrayValue = serializer.Deserialize<object[]>(reader); |
| 242 | 217 | return new TopLevelElement { AnythingArray = arrayValue }; |
| @@ -277,9 +252,9 @@ namespace QuickType | ||
| 277 | 252 | serializer.Serialize(writer, value.AnythingArray); |
| 278 | 253 | return; |
| 279 | 254 | } |
| 280 | - if (value.TopLevelClass != null) | |
| 255 | + if (value.A != null) | |
| 281 | 256 | { |
| 282 | - serializer.Serialize(writer, value.TopLevelClass); | |
| 257 | + serializer.Serialize(writer, value.A); | |
| 283 | 258 | return; |
| 284 | 259 | } |
| 285 | 260 | throw new Exception("Cannot marshal type TopLevelElement"); |
| @@ -288,42 +263,42 @@ namespace QuickType | ||
| 288 | 263 | public static readonly TopLevelElementConverter Singleton = new TopLevelElementConverter(); |
| 289 | 264 | } |
| 290 | 265 | |
| 291 | - internal class XConverter : JsonConverter | |
| 266 | + internal class PurpleTopLevelConverter : JsonConverter | |
| 292 | 267 | { |
| 293 | - public override bool CanConvert(Type t) => t == typeof(X) || t == typeof(X?); | |
| 268 | + public override bool CanConvert(Type t) => t == typeof(PurpleTopLevel) || t == typeof(PurpleTopLevel?); | |
| 294 | 269 | |
| 295 | 270 | public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) |
| 296 | 271 | { |
| 297 | 272 | switch (reader.TokenType) |
| 298 | 273 | { |
| 299 | 274 | case JsonToken.Null: |
| 300 | - return new X { }; | |
| 275 | + return new PurpleTopLevel { }; | |
| 301 | 276 | case JsonToken.Integer: |
| 302 | 277 | var integerValue = serializer.Deserialize<long>(reader); |
| 303 | - return new X { Integer = integerValue }; | |
| 278 | + return new PurpleTopLevel { Integer = integerValue }; | |
| 304 | 279 | case JsonToken.Float: |
| 305 | 280 | var doubleValue = serializer.Deserialize<double>(reader); |
| 306 | - return new X { Double = doubleValue }; | |
| 281 | + return new PurpleTopLevel { Double = doubleValue }; | |
| 307 | 282 | case JsonToken.Boolean: |
| 308 | 283 | var boolValue = serializer.Deserialize<bool>(reader); |
| 309 | - return new X { Bool = boolValue }; | |
| 284 | + return new PurpleTopLevel { Bool = boolValue }; | |
| 310 | 285 | case JsonToken.String: |
| 311 | 286 | case JsonToken.Date: |
| 312 | 287 | var stringValue = serializer.Deserialize<string>(reader); |
| 313 | - return new X { String = stringValue }; | |
| 288 | + return new PurpleTopLevel { String = stringValue }; | |
| 314 | 289 | case JsonToken.StartObject: |
| 315 | 290 | var objectValue = serializer.Deserialize<Dictionary<string, object>>(reader); |
| 316 | - return new X { AnythingMap = objectValue }; | |
| 291 | + return new PurpleTopLevel { AnythingMap = objectValue }; | |
| 317 | 292 | case JsonToken.StartArray: |
| 318 | - var arrayValue = serializer.Deserialize<XElement[]>(reader); | |
| 319 | - return new X { AnythingArray = arrayValue }; | |
| 293 | + var arrayValue = serializer.Deserialize<TopLevelElement[]>(reader); | |
| 294 | + return new PurpleTopLevel { AnythingArray = arrayValue }; | |
| 320 | 295 | } |
| 321 | - throw new Exception("Cannot unmarshal type X"); | |
| 296 | + throw new Exception("Cannot unmarshal type PurpleTopLevel"); | |
| 322 | 297 | } |
| 323 | 298 | |
| 324 | 299 | public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) |
| 325 | 300 | { |
| 326 | - var value = (X)untypedValue; | |
| 301 | + var value = (PurpleTopLevel)untypedValue; | |
| 327 | 302 | if (value.IsNull) |
| 328 | 303 | { |
| 329 | 304 | serializer.Serialize(writer, null); |
| @@ -359,87 +334,10 @@ namespace QuickType | ||
| 359 | 334 | serializer.Serialize(writer, value.AnythingMap); |
| 360 | 335 | return; |
| 361 | 336 | } |
| 362 | - throw new Exception("Cannot marshal type X"); | |
| 363 | - } | |
| 364 | - | |
| 365 | - public static readonly XConverter Singleton = new XConverter(); | |
| 366 | - } | |
| 367 | - | |
| 368 | - internal class XElementConverter : JsonConverter | |
| 369 | - { | |
| 370 | - public override bool CanConvert(Type t) => t == typeof(XElement) || t == typeof(XElement?); | |
| 371 | - | |
| 372 | - public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer) | |
| 373 | - { | |
| 374 | - switch (reader.TokenType) | |
| 375 | - { | |
| 376 | - case JsonToken.Null: | |
| 377 | - return new XElement { }; | |
| 378 | - case JsonToken.Integer: | |
| 379 | - var integerValue = serializer.Deserialize<long>(reader); | |
| 380 | - return new XElement { Integer = integerValue }; | |
| 381 | - case JsonToken.Float: | |
| 382 | - var doubleValue = serializer.Deserialize<double>(reader); | |
| 383 | - return new XElement { Double = doubleValue }; | |
| 384 | - case JsonToken.Boolean: | |
| 385 | - var boolValue = serializer.Deserialize<bool>(reader); | |
| 386 | - return new XElement { Bool = boolValue }; | |
| 387 | - case JsonToken.String: | |
| 388 | - case JsonToken.Date: | |
| 389 | - var stringValue = serializer.Deserialize<string>(reader); | |
| 390 | - return new XElement { String = stringValue }; | |
| 391 | - case JsonToken.StartObject: | |
| 392 | - var objectValue = serializer.Deserialize<XClass>(reader); | |
| 393 | - return new XElement { XClass = objectValue }; | |
| 394 | - case JsonToken.StartArray: | |
| 395 | - var arrayValue = serializer.Deserialize<object[]>(reader); | |
| 396 | - return new XElement { AnythingArray = arrayValue }; | |
| 397 | - } | |
| 398 | - throw new Exception("Cannot unmarshal type XElement"); | |
| 399 | - } | |
| 400 | - | |
| 401 | - public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer) | |
| 402 | - { | |
| 403 | - var value = (XElement)untypedValue; | |
| 404 | - if (value.IsNull) | |
| 405 | - { | |
| 406 | - serializer.Serialize(writer, null); | |
| 407 | - return; | |
| 408 | - } | |
| 409 | - if (value.Integer != null) | |
| 410 | - { | |
| 411 | - serializer.Serialize(writer, value.Integer.Value); | |
| 412 | - return; | |
| 413 | - } | |
| 414 | - if (value.Double != null) | |
| 415 | - { | |
| 416 | - serializer.Serialize(writer, value.Double.Value); | |
| 417 | - return; | |
| 418 | - } | |
| 419 | - if (value.Bool != null) | |
| 420 | - { | |
| 421 | - serializer.Serialize(writer, value.Bool.Value); | |
| 422 | - return; | |
| 423 | - } | |
| 424 | - if (value.String != null) | |
| 425 | - { | |
| 426 | - serializer.Serialize(writer, value.String); | |
| 427 | - return; | |
| 428 | - } | |
| 429 | - if (value.AnythingArray != null) | |
| 430 | - { | |
| 431 | - serializer.Serialize(writer, value.AnythingArray); | |
| 432 | - return; | |
| 433 | - } | |
| 434 | - if (value.XClass != null) | |
| 435 | - { | |
| 436 | - serializer.Serialize(writer, value.XClass); | |
| 437 | - return; | |
| 438 | - } | |
| 439 | - throw new Exception("Cannot marshal type XElement"); | |
| 337 | + throw new Exception("Cannot marshal type PurpleTopLevel"); | |
| 440 | 338 | } |
| 441 | 339 | |
| 442 | - public static readonly XElementConverter Singleton = new XElementConverter(); | |
| 340 | + public static readonly PurpleTopLevelConverter Singleton = new PurpleTopLevelConverter(); | |
| 443 | 341 | } |
| 444 | 342 | } |
| 445 | 343 | #pragma warning restore CS8618 |
Mschema-csharpdefault / QuickType.cs+42 −148
| @@ -20,70 +20,46 @@ namespace QuickType | ||
| 20 | 20 | using System.Text.Json.Serialization; |
| 21 | 21 | using System.Globalization; |
| 22 | 22 | |
| 23 | - public partial class TopLevelClass | |
| 23 | + public partial class A | |
| 24 | 24 | { |
| 25 | 25 | [JsonPropertyName("x")] |
| 26 | - public X? X { get; set; } | |
| 26 | + public PurpleTopLevel? X { get; set; } | |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - public partial class XClass | |
| 30 | - { | |
| 31 | - [JsonPropertyName("x")] | |
| 32 | - public X? X { get; set; } | |
| 33 | - } | |
| 34 | - | |
| 35 | - public partial struct X | |
| 36 | - { | |
| 37 | - public XElement[]? AnythingArray; | |
| 38 | - public Dictionary<string, object>? AnythingMap; | |
| 39 | - public bool? Bool; | |
| 40 | - public double? Double; | |
| 41 | - public long? Integer; | |
| 42 | - public string? String; | |
| 43 | - | |
| 44 | - public static implicit operator X(XElement[] AnythingArray) => new X { AnythingArray = AnythingArray }; | |
| 45 | - public static implicit operator X(Dictionary<string, object> AnythingMap) => new X { AnythingMap = AnythingMap }; | |
| 46 | - public static implicit operator X(bool Bool) => new X { Bool = Bool }; | |
| 47 | - public static implicit operator X(double Double) => new X { Double = Double }; | |
| 48 | - public static implicit operator X(long Integer) => new X { Integer = Integer }; | |
| 49 | - public static implicit operator X(string String) => new X { String = String }; | |
| 50 | - public bool IsNull => AnythingArray == null && Bool == null && Double == null && Integer == null && AnythingMap == null && String == null; | |
| 51 | - } | |
| 52 | - | |
| 53 | - public partial struct XElement | |
| 29 | + public partial struct TopLevelElement | |
| 54 | 30 | { |
| 31 | + public A? A; | |
| 55 | 32 | public object[]? AnythingArray; |
| 56 | 33 | public bool? Bool; |
| 57 | 34 | public double? Double; |
| 58 | 35 | public long? Integer; |
| 59 | 36 | public string? String; |
| 60 | - public XClass? XClass; | |
| 61 | - | |
| 62 | - public static implicit operator XElement(object[] AnythingArray) => new XElement { AnythingArray = AnythingArray }; | |
| 63 | - public static implicit operator XElement(bool Bool) => new XElement { Bool = Bool }; | |
| 64 | - public static implicit operator XElement(double Double) => new XElement { Double = Double }; | |
| 65 | - public static implicit operator XElement(long Integer) => new XElement { Integer = Integer }; | |
| 66 | - public static implicit operator XElement(string String) => new XElement { String = String }; | |
| 67 | - public static implicit operator XElement(XClass XClass) => new XElement { XClass = XClass }; | |
| 68 | - public bool IsNull => AnythingArray == null && Bool == null && XClass == null && Double == null && Integer == null && String == null; | |
| 37 | + | |
| 38 | + public static implicit operator TopLevelElement(A A) => new TopLevelElement { A = A }; | |
| 39 | + public static implicit operator TopLevelElement(object[] AnythingArray) => new TopLevelElement { AnythingArray = AnythingArray }; | |
| 40 | + public static implicit operator TopLevelElement(bool Bool) => new TopLevelElement { Bool = Bool }; | |
| 41 | + public static implicit operator TopLevelElement(double Double) => new TopLevelElement { Double = Double }; | |
| 42 | + public static implicit operator TopLevelElement(long Integer) => new TopLevelElement { Integer = Integer }; | |
| 43 | + public static implicit operator TopLevelElement(string String) => new TopLevelElement { String = String }; | |
| 44 | + public bool IsNull => AnythingArray == null && Bool == null && A == null && Double == null && Integer == null && String == null; | |
| 69 | 45 | } |
| 70 | 46 | |
| 71 | - public partial struct TopLevelElement | |
| 47 | + public partial struct PurpleTopLevel | |
| 72 | 48 | { |
| 73 | - public object[]? AnythingArray; | |
| 49 | + public TopLevelElement[]? AnythingArray; | |
| 50 | + public Dictionary<string, object>? AnythingMap; | |
| 74 | 51 | public bool? Bool; |
| 75 | 52 | public double? Double; |
| 76 | 53 | public long? Integer; |
| 77 | 54 | public string? String; |
| 78 | - public TopLevelClass? TopLevelClass; | |
| 79 | 55 | |
| 80 | - public static implicit operator TopLevelElement(object[] AnythingArray) => new TopLevelElement { AnythingArray = AnythingArray }; | |
| 81 | - public static implicit operator TopLevelElement(bool Bool) => new TopLevelElement { Bool = Bool }; | |
| 82 | - public static implicit operator TopLevelElement(double Double) => new TopLevelElement { Double = Double }; | |
| 83 | - public static implicit operator TopLevelElement(long Integer) => new TopLevelElement { Integer = Integer }; | |
| 84 | - public static implicit operator TopLevelElement(string String) => new TopLevelElement { String = String }; | |
| 85 | - public static implicit operator TopLevelElement(TopLevelClass TopLevelClass) => new TopLevelElement { TopLevelClass = TopLevelClass }; | |
| 86 | - public bool IsNull => AnythingArray == null && Bool == null && TopLevelClass == null && Double == null && Integer == null && String == null; | |
| 56 | + public static implicit operator PurpleTopLevel(TopLevelElement[] AnythingArray) => new PurpleTopLevel { AnythingArray = AnythingArray }; | |
| 57 | + public static implicit operator PurpleTopLevel(Dictionary<string, object> AnythingMap) => new PurpleTopLevel { AnythingMap = AnythingMap }; | |
| 58 | + public static implicit operator PurpleTopLevel(bool Bool) => new PurpleTopLevel { Bool = Bool }; | |
| 59 | + public static implicit operator PurpleTopLevel(double Double) => new PurpleTopLevel { Double = Double }; | |
| 60 | + public static implicit operator PurpleTopLevel(long Integer) => new PurpleTopLevel { Integer = Integer }; | |
| 61 | + public static implicit operator PurpleTopLevel(string String) => new PurpleTopLevel { String = String }; | |
| 62 | + public bool IsNull => AnythingArray == null && Bool == null && Double == null && Integer == null && AnythingMap == null && String == null; | |
| 87 | 63 | } |
| 88 | 64 | |
| 89 | 65 | public partial struct TopLevelUnion |
| @@ -122,8 +98,7 @@ namespace QuickType | ||
| 122 | 98 | { |
| 123 | 99 | TopLevelUnionConverter.Singleton, |
| 124 | 100 | TopLevelElementConverter.Singleton, |
| 125 | - XConverter.Singleton, | |
| 126 | - XElementConverter.Singleton, | |
| 101 | + PurpleTopLevelConverter.Singleton, | |
| 127 | 102 | new DateOnlyConverter(), |
| 128 | 103 | new TimeOnlyConverter(), |
| 129 | 104 | IsoDateTimeOffsetConverter.Singleton |
| @@ -241,8 +216,8 @@ namespace QuickType | ||
| 241 | 216 | var stringValue = reader.GetString(); |
| 242 | 217 | return new TopLevelElement { String = stringValue }; |
| 243 | 218 | case JsonTokenType.StartObject: |
| 244 | - var objectValue = JsonSerializer.Deserialize<TopLevelClass>(ref reader, options); | |
| 245 | - return new TopLevelElement { TopLevelClass = objectValue }; | |
| 219 | + var objectValue = JsonSerializer.Deserialize<A>(ref reader, options); | |
| 220 | + return new TopLevelElement { A = objectValue }; | |
| 246 | 221 | case JsonTokenType.StartArray: |
| 247 | 222 | var arrayValue = JsonSerializer.Deserialize<object[]>(ref reader, options); |
| 248 | 223 | return new TopLevelElement { AnythingArray = arrayValue }; |
| @@ -282,9 +257,9 @@ namespace QuickType | ||
| 282 | 257 | JsonSerializer.Serialize(writer, value.AnythingArray, options); |
| 283 | 258 | return; |
| 284 | 259 | } |
| 285 | - if (value.TopLevelClass != null) | |
| 260 | + if (value.A != null) | |
| 286 | 261 | { |
| 287 | - JsonSerializer.Serialize(writer, value.TopLevelClass, options); | |
| 262 | + JsonSerializer.Serialize(writer, value.A, options); | |
| 288 | 263 | return; |
| 289 | 264 | } |
| 290 | 265 | throw new NotSupportedException("Cannot marshal type TopLevelElement"); |
| @@ -293,45 +268,45 @@ namespace QuickType | ||
| 293 | 268 | public static readonly TopLevelElementConverter Singleton = new TopLevelElementConverter(); |
| 294 | 269 | } |
| 295 | 270 | |
| 296 | - internal class XConverter : JsonConverter<X> | |
| 271 | + internal class PurpleTopLevelConverter : JsonConverter<PurpleTopLevel> | |
| 297 | 272 | { |
| 298 | - public override bool CanConvert(Type t) => t == typeof(X); | |
| 273 | + public override bool CanConvert(Type t) => t == typeof(PurpleTopLevel); | |
| 299 | 274 | |
| 300 | - public override X Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 275 | + public override PurpleTopLevel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 301 | 276 | { |
| 302 | 277 | switch (reader.TokenType) |
| 303 | 278 | { |
| 304 | 279 | case JsonTokenType.Null: |
| 305 | - return new X { }; | |
| 280 | + return new PurpleTopLevel { }; | |
| 306 | 281 | case JsonTokenType.Number: |
| 307 | 282 | if (reader.TryGetInt64(out long intTryValue1)) |
| 308 | 283 | { |
| 309 | 284 | var intValue2 = reader.GetInt64(); |
| 310 | - return new X { Integer = intValue2 }; | |
| 285 | + return new PurpleTopLevel { Integer = intValue2 }; | |
| 311 | 286 | } |
| 312 | 287 | else |
| 313 | 288 | { |
| 314 | 289 | var doubleValue3 = reader.GetDouble(); |
| 315 | - return new X { Double = doubleValue3 }; | |
| 290 | + return new PurpleTopLevel { Double = doubleValue3 }; | |
| 316 | 291 | } |
| 317 | 292 | case JsonTokenType.True: |
| 318 | 293 | case JsonTokenType.False: |
| 319 | 294 | var boolValue = reader.GetBoolean(); |
| 320 | - return new X { Bool = boolValue }; | |
| 295 | + return new PurpleTopLevel { Bool = boolValue }; | |
| 321 | 296 | case JsonTokenType.String: |
| 322 | 297 | var stringValue = reader.GetString(); |
| 323 | - return new X { String = stringValue }; | |
| 298 | + return new PurpleTopLevel { String = stringValue }; | |
| 324 | 299 | case JsonTokenType.StartObject: |
| 325 | 300 | var objectValue = JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options); |
| 326 | - return new X { AnythingMap = objectValue }; | |
| 301 | + return new PurpleTopLevel { AnythingMap = objectValue }; | |
| 327 | 302 | case JsonTokenType.StartArray: |
| 328 | - var arrayValue = JsonSerializer.Deserialize<XElement[]>(ref reader, options); | |
| 329 | - return new X { AnythingArray = arrayValue }; | |
| 303 | + var arrayValue = JsonSerializer.Deserialize<TopLevelElement[]>(ref reader, options); | |
| 304 | + return new PurpleTopLevel { AnythingArray = arrayValue }; | |
| 330 | 305 | } |
| 331 | - throw new JsonException("Cannot unmarshal type X"); | |
| 306 | + throw new JsonException("Cannot unmarshal type PurpleTopLevel"); | |
| 332 | 307 | } |
| 333 | 308 | |
| 334 | - public override void Write(Utf8JsonWriter writer, X value, JsonSerializerOptions options) | |
| 309 | + public override void Write(Utf8JsonWriter writer, PurpleTopLevel value, JsonSerializerOptions options) | |
| 335 | 310 | { |
| 336 | 311 | if (value.IsNull) |
| 337 | 312 | { |
| @@ -368,91 +343,10 @@ namespace QuickType | ||
| 368 | 343 | JsonSerializer.Serialize(writer, value.AnythingMap, options); |
| 369 | 344 | return; |
| 370 | 345 | } |
| 371 | - throw new NotSupportedException("Cannot marshal type X"); | |
| 372 | - } | |
| 373 | - | |
| 374 | - public static readonly XConverter Singleton = new XConverter(); | |
| 375 | - } | |
| 376 | - | |
| 377 | - internal class XElementConverter : JsonConverter<XElement> | |
| 378 | - { | |
| 379 | - public override bool CanConvert(Type t) => t == typeof(XElement); | |
| 380 | - | |
| 381 | - public override XElement Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 382 | - { | |
| 383 | - switch (reader.TokenType) | |
| 384 | - { | |
| 385 | - case JsonTokenType.Null: | |
| 386 | - return new XElement { }; | |
| 387 | - case JsonTokenType.Number: | |
| 388 | - if (reader.TryGetInt64(out long intTryValue1)) | |
| 389 | - { | |
| 390 | - var intValue2 = reader.GetInt64(); | |
| 391 | - return new XElement { Integer = intValue2 }; | |
| 392 | - } | |
| 393 | - else | |
| 394 | - { | |
| 395 | - var doubleValue3 = reader.GetDouble(); | |
| 396 | - return new XElement { Double = doubleValue3 }; | |
| 397 | - } | |
| 398 | - case JsonTokenType.True: | |
| 399 | - case JsonTokenType.False: | |
| 400 | - var boolValue = reader.GetBoolean(); | |
| 401 | - return new XElement { Bool = boolValue }; | |
| 402 | - case JsonTokenType.String: | |
| 403 | - var stringValue = reader.GetString(); | |
| 404 | - return new XElement { String = stringValue }; | |
| 405 | - case JsonTokenType.StartObject: | |
| 406 | - var objectValue = JsonSerializer.Deserialize<XClass>(ref reader, options); | |
| 407 | - return new XElement { XClass = objectValue }; | |
| 408 | - case JsonTokenType.StartArray: | |
| 409 | - var arrayValue = JsonSerializer.Deserialize<object[]>(ref reader, options); | |
| 410 | - return new XElement { AnythingArray = arrayValue }; | |
| 411 | - } | |
| 412 | - throw new JsonException("Cannot unmarshal type XElement"); | |
| 413 | - } | |
| 414 | - | |
| 415 | - public override void Write(Utf8JsonWriter writer, XElement value, JsonSerializerOptions options) | |
| 416 | - { | |
| 417 | - if (value.IsNull) | |
| 418 | - { | |
| 419 | - writer.WriteNullValue(); | |
| 420 | - return; | |
| 421 | - } | |
| 422 | - if (value.Integer != null) | |
| 423 | - { | |
| 424 | - JsonSerializer.Serialize(writer, value.Integer.Value, options); | |
| 425 | - return; | |
| 426 | - } | |
| 427 | - if (value.Double != null) | |
| 428 | - { | |
| 429 | - JsonSerializer.Serialize(writer, value.Double.Value, options); | |
| 430 | - return; | |
| 431 | - } | |
| 432 | - if (value.Bool != null) | |
| 433 | - { | |
| 434 | - JsonSerializer.Serialize(writer, value.Bool.Value, options); | |
| 435 | - return; | |
| 436 | - } | |
| 437 | - if (value.String != null) | |
| 438 | - { | |
| 439 | - JsonSerializer.Serialize(writer, value.String, options); | |
| 440 | - return; | |
| 441 | - } | |
| 442 | - if (value.AnythingArray != null) | |
| 443 | - { | |
| 444 | - JsonSerializer.Serialize(writer, value.AnythingArray, options); | |
| 445 | - return; | |
| 446 | - } | |
| 447 | - if (value.XClass != null) | |
| 448 | - { | |
| 449 | - JsonSerializer.Serialize(writer, value.XClass, options); | |
| 450 | - return; | |
| 451 | - } | |
| 452 | - throw new NotSupportedException("Cannot marshal type XElement"); | |
| 346 | + throw new NotSupportedException("Cannot marshal type PurpleTopLevel"); | |
| 453 | 347 | } |
| 454 | 348 | |
| 455 | - public static readonly XElementConverter Singleton = new XElementConverter(); | |
| 349 | + public static readonly PurpleTopLevelConverter Singleton = new PurpleTopLevelConverter(); | |
| 456 | 350 | } |
| 457 | 351 | |
| 458 | 352 | public class DateOnlyConverter : JsonConverter<DateOnly> |
Mschema-dartdefault / TopLevel.dart+3 −19
| @@ -8,30 +8,14 @@ dynamic topLevelFromJson(String str) => json.decode(str); | ||
| 8 | 8 | |
| 9 | 9 | String topLevelToJson(dynamic data) => json.encode(data); |
| 10 | 10 | |
| 11 | -class TopLevelClass { | |
| 11 | +class A { | |
| 12 | 12 | final dynamic x; |
| 13 | 13 | |
| 14 | - TopLevelClass({ | |
| 14 | + A({ | |
| 15 | 15 | this.x, |
| 16 | 16 | }); |
| 17 | 17 | |
| 18 | - factory TopLevelClass.fromJson(Map<String, dynamic> json) => TopLevelClass( | |
| 19 | - x: json["x"], | |
| 20 | - ); | |
| 21 | - | |
| 22 | - Map<String, dynamic> toJson() => { | |
| 23 | - "x": x, | |
| 24 | - }; | |
| 25 | -} | |
| 26 | - | |
| 27 | -class XClass { | |
| 28 | - final dynamic x; | |
| 29 | - | |
| 30 | - XClass({ | |
| 31 | - this.x, | |
| 32 | - }); | |
| 33 | - | |
| 34 | - factory XClass.fromJson(Map<String, dynamic> json) => XClass( | |
| 18 | + factory A.fromJson(Map<String, dynamic> json) => A( | |
| 35 | 19 | x: json["x"], |
| 36 | 20 | ); |
Mschema-flowdefault / TopLevel.js+8 −18
| @@ -11,30 +11,23 @@ | ||
| 11 | 11 | |
| 12 | 12 | export type TopLevel = TopLevelElement[] | boolean | number | number | { [key: string]: mixed } | null | string; |
| 13 | 13 | |
| 14 | -export type TopLevelElement = mixed[] | boolean | number | null | TopLevelObject | string; | |
| 14 | +export type PurpleTopLevel = TopLevelElement[] | boolean | number | { [key: string]: mixed } | null | string; | |
| 15 | 15 | |
| 16 | -export type TopLevelObject = { | |
| 17 | - x?: X; | |
| 16 | +export type A = { | |
| 17 | + x?: PurpleTopLevel; | |
| 18 | 18 | [property: string]: mixed; |
| 19 | 19 | }; |
| 20 | 20 | |
| 21 | -export type XObject = { | |
| 22 | - x?: X; | |
| 23 | - [property: string]: mixed; | |
| 24 | -}; | |
| 25 | - | |
| 26 | -export type XElement = mixed[] | boolean | number | null | XObject | string; | |
| 27 | - | |
| 28 | -export type X = XElement[] | boolean | number | { [key: string]: mixed } | null | string; | |
| 21 | +export type TopLevelElement = mixed[] | boolean | number | null | A | string; | |
| 29 | 22 | |
| 30 | 23 | // Converts JSON strings to/from your types |
| 31 | 24 | // and asserts the results of JSON.parse at runtime |
| 32 | 25 | function toTopLevel(json: string): TopLevel { |
| 33 | - return cast(JSON.parse(json), u(a(u(a("any"), true, 3.14, null, r("TopLevelObject"), "")), true, 3.14, 0, m("any"), null, "")); | |
| 26 | + return cast(JSON.parse(json), u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, 0, m("any"), null, "")); | |
| 34 | 27 | } |
| 35 | 28 | |
| 36 | 29 | function topLevelToJson(value: TopLevel): string { |
| 37 | - return JSON.stringify(uncast(value, u(a(u(a("any"), true, 3.14, null, r("TopLevelObject"), "")), true, 3.14, 0, m("any"), null, "")), null, 2); | |
| 30 | + return JSON.stringify(uncast(value, u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, 0, m("any"), null, "")), null, 2); | |
| 38 | 31 | } |
| 39 | 32 | |
| 40 | 33 | function invalidValue(typ: any, val: any, key: any, parent: any = '') { |
| @@ -191,11 +184,8 @@ function r(name: string) { | ||
| 191 | 184 | } |
| 192 | 185 | |
| 193 | 186 | const typeMap: any = { |
| 194 | - "TopLevelObject": o([ | |
| 195 | - { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("XObject"), "")), true, 3.14, m("any"), null, "")) }, | |
| 196 | - ], "any"), | |
| 197 | - "XObject": o([ | |
| 198 | - { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("XObject"), "")), true, 3.14, m("any"), null, "")) }, | |
| 187 | + "A": o([ | |
| 188 | + { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, m("any"), null, "")) }, | |
| 199 | 189 | ], "any"), |
| 200 | 190 | }; |
Mschema-golangdefault / quicktype.go+23 −54
| @@ -21,12 +21,8 @@ func (r *TopLevel) Marshal() ([]byte, error) { | ||
| 21 | 21 | return json.Marshal(r) |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | -type TopLevelClass struct { | |
| 25 | - X *X `json:"x"` | |
| 26 | -} | |
| 27 | - | |
| 28 | -type XClass struct { | |
| 29 | - X *X `json:"x"` | |
| 24 | +type A struct { | |
| 25 | + X *PurpleTopLevel `json:"x"` | |
| 30 | 26 | } |
| 31 | 27 | |
| 32 | 28 | type TopLevel struct { |
| @@ -54,83 +50,56 @@ func (x *TopLevel) MarshalJSON() ([]byte, error) { | ||
| 54 | 50 | return marshalUnion(x.Integer, x.Double, x.Bool, x.String, x.UnionArray != nil, x.UnionArray, false, nil, x.AnythingMap != nil, x.AnythingMap, false, nil, true) |
| 55 | 51 | } |
| 56 | 52 | |
| 57 | -type TopLevelElement struct { | |
| 58 | - AnythingArray []interface{} | |
| 59 | - Bool *bool | |
| 60 | - Double *float64 | |
| 61 | - Integer *int64 | |
| 62 | - String *string | |
| 63 | - TopLevelClass *TopLevelClass | |
| 53 | +type PurpleTopLevel struct { | |
| 54 | + AnythingMap map[string]interface{} | |
| 55 | + Bool *bool | |
| 56 | + Double *float64 | |
| 57 | + Integer *int64 | |
| 58 | + String *string | |
| 59 | + UnionArray []TopLevelElement | |
| 64 | 60 | } |
| 65 | 61 | |
| 66 | -func (x *TopLevelElement) UnmarshalJSON(data []byte) error { | |
| 67 | - x.AnythingArray = nil | |
| 68 | - x.TopLevelClass = nil | |
| 69 | - var c TopLevelClass | |
| 70 | - object, err := unmarshalUnion(data, &x.Integer, &x.Double, &x.Bool, &x.String, true, &x.AnythingArray, true, &c, false, nil, false, nil, true) | |
| 62 | +func (x *PurpleTopLevel) UnmarshalJSON(data []byte) error { | |
| 63 | + x.UnionArray = nil | |
| 64 | + x.AnythingMap = nil | |
| 65 | + object, err := unmarshalUnion(data, &x.Integer, &x.Double, &x.Bool, &x.String, true, &x.UnionArray, false, nil, true, &x.AnythingMap, false, nil, true) | |
| 71 | 66 | if err != nil { |
| 72 | 67 | return err |
| 73 | 68 | } |
| 74 | 69 | if object { |
| 75 | - x.TopLevelClass = &c | |
| 76 | 70 | } |
| 77 | 71 | return nil |
| 78 | 72 | } |
| 79 | 73 | |
| 80 | -func (x *TopLevelElement) MarshalJSON() ([]byte, error) { | |
| 81 | - return marshalUnion(x.Integer, x.Double, x.Bool, x.String, x.AnythingArray != nil, x.AnythingArray, x.TopLevelClass != nil, x.TopLevelClass, false, nil, false, nil, true) | |
| 74 | +func (x *PurpleTopLevel) MarshalJSON() ([]byte, error) { | |
| 75 | + return marshalUnion(x.Integer, x.Double, x.Bool, x.String, x.UnionArray != nil, x.UnionArray, false, nil, x.AnythingMap != nil, x.AnythingMap, false, nil, true) | |
| 82 | 76 | } |
| 83 | 77 | |
| 84 | -type XElement struct { | |
| 78 | +type TopLevelElement struct { | |
| 79 | + A *A | |
| 85 | 80 | AnythingArray []interface{} |
| 86 | 81 | Bool *bool |
| 87 | 82 | Double *float64 |
| 88 | 83 | Integer *int64 |
| 89 | 84 | String *string |
| 90 | - XClass *XClass | |
| 91 | 85 | } |
| 92 | 86 | |
| 93 | -func (x *XElement) UnmarshalJSON(data []byte) error { | |
| 87 | +func (x *TopLevelElement) UnmarshalJSON(data []byte) error { | |
| 94 | 88 | x.AnythingArray = nil |
| 95 | - x.XClass = nil | |
| 96 | - var c XClass | |
| 89 | + x.A = nil | |
| 90 | + var c A | |
| 97 | 91 | object, err := unmarshalUnion(data, &x.Integer, &x.Double, &x.Bool, &x.String, true, &x.AnythingArray, true, &c, false, nil, false, nil, true) |
| 98 | 92 | if err != nil { |
| 99 | 93 | return err |
| 100 | 94 | } |
| 101 | 95 | if object { |
| 102 | - x.XClass = &c | |
| 96 | + x.A = &c | |
| 103 | 97 | } |
| 104 | 98 | return nil |
| 105 | 99 | } |
| 106 | 100 | |
| 107 | -func (x *XElement) MarshalJSON() ([]byte, error) { | |
| 108 | - return marshalUnion(x.Integer, x.Double, x.Bool, x.String, x.AnythingArray != nil, x.AnythingArray, x.XClass != nil, x.XClass, false, nil, false, nil, true) | |
| 109 | -} | |
| 110 | - | |
| 111 | -type X struct { | |
| 112 | - AnythingMap map[string]interface{} | |
| 113 | - Bool *bool | |
| 114 | - Double *float64 | |
| 115 | - Integer *int64 | |
| 116 | - String *string | |
| 117 | - UnionArray []XElement | |
| 118 | -} | |
| 119 | - | |
| 120 | -func (x *X) UnmarshalJSON(data []byte) error { | |
| 121 | - x.UnionArray = nil | |
| 122 | - x.AnythingMap = nil | |
| 123 | - object, err := unmarshalUnion(data, &x.Integer, &x.Double, &x.Bool, &x.String, true, &x.UnionArray, false, nil, true, &x.AnythingMap, false, nil, true) | |
| 124 | - if err != nil { | |
| 125 | - return err | |
| 126 | - } | |
| 127 | - if object { | |
| 128 | - } | |
| 129 | - return nil | |
| 130 | -} | |
| 131 | - | |
| 132 | -func (x *X) MarshalJSON() ([]byte, error) { | |
| 133 | - return marshalUnion(x.Integer, x.Double, x.Bool, x.String, x.UnionArray != nil, x.UnionArray, false, nil, x.AnythingMap != nil, x.AnythingMap, false, nil, true) | |
| 101 | +func (x *TopLevelElement) MarshalJSON() ([]byte, error) { | |
| 102 | + return marshalUnion(x.Integer, x.Double, x.Bool, x.String, x.AnythingArray != nil, x.AnythingArray, x.A != nil, x.A, false, nil, false, nil, true) | |
| 134 | 103 | } |
| 135 | 104 | |
| 136 | 105 | func unmarshalUnion(data []byte, pi **int64, pf **float64, pb **bool, ps **string, haveArray bool, pa interface{}, haveObject bool, pc interface{}, haveMap bool, pm interface{}, haveEnum bool, pe interface{}, nullable bool) (bool, error) { |
Mschema-haskelldefault / QuickType.hs+48 −92
| @@ -3,11 +3,9 @@ | ||
| 3 | 3 | |
| 4 | 4 | module QuickType |
| 5 | 5 | ( QuickType (..) |
| 6 | - , QuickTypeClass (..) | |
| 7 | - , XClass (..) | |
| 6 | + , A (..) | |
| 7 | + , PurpleQuickType (..) | |
| 8 | 8 | , QuickTypeElement (..) |
| 9 | - , XElement (..) | |
| 10 | - , X (..) | |
| 11 | 9 | , decodeTopLevel |
| 12 | 10 | ) where |
| 13 | 11 | |
| @@ -27,44 +25,30 @@ data QuickType | ||
| 27 | 25 | | UnionArrayInQuickType ([QuickTypeElement]) |
| 28 | 26 | deriving (Show) |
| 29 | 27 | |
| 28 | +data PurpleQuickType | |
| 29 | + = AnythingMapInPurpleQuickType (HashMap Text Value) | |
| 30 | + | BoolInPurpleQuickType Bool | |
| 31 | + | DoubleInPurpleQuickType Float | |
| 32 | + | IntegerInPurpleQuickType Int | |
| 33 | + | NullInPurpleQuickType | |
| 34 | + | StringInPurpleQuickType Text | |
| 35 | + | UnionArrayInPurpleQuickType ([QuickTypeElement]) | |
| 36 | + deriving (Show) | |
| 37 | + | |
| 38 | +data A = A | |
| 39 | + { xA :: Maybe PurpleQuickType | |
| 40 | + } deriving (Show) | |
| 41 | + | |
| 30 | 42 | data QuickTypeElement |
| 31 | - = AnythingArrayInQuickTypeElement ([Value]) | |
| 43 | + = AInQuickTypeElement A | |
| 44 | + | AnythingArrayInQuickTypeElement ([Value]) | |
| 32 | 45 | | BoolInQuickTypeElement Bool |
| 33 | 46 | | DoubleInQuickTypeElement Float |
| 34 | 47 | | IntegerInQuickTypeElement Int |
| 35 | 48 | | NullInQuickTypeElement |
| 36 | - | QuickTypeClassInQuickTypeElement QuickTypeClass | |
| 37 | 49 | | StringInQuickTypeElement Text |
| 38 | 50 | deriving (Show) |
| 39 | 51 | |
| 40 | -data QuickTypeClass = QuickTypeClass | |
| 41 | - { xQuickTypeClass :: Maybe X | |
| 42 | - } deriving (Show) | |
| 43 | - | |
| 44 | -data XClass = XClass | |
| 45 | - { xXClass :: Maybe X | |
| 46 | - } deriving (Show) | |
| 47 | - | |
| 48 | -data XElement | |
| 49 | - = AnythingArrayInXElement ([Value]) | |
| 50 | - | BoolInXElement Bool | |
| 51 | - | DoubleInXElement Float | |
| 52 | - | IntegerInXElement Int | |
| 53 | - | NullInXElement | |
| 54 | - | StringInXElement Text | |
| 55 | - | XClassInXElement XClass | |
| 56 | - deriving (Show) | |
| 57 | - | |
| 58 | -data X | |
| 59 | - = AnythingMapInX (HashMap Text Value) | |
| 60 | - | BoolInX Bool | |
| 61 | - | DoubleInX Float | |
| 62 | - | IntegerInX Int | |
| 63 | - | NullInX | |
| 64 | - | StringInX Text | |
| 65 | - | UnionArrayInX ([XElement]) | |
| 66 | - deriving (Show) | |
| 67 | - | |
| 68 | 52 | decodeTopLevel :: ByteString -> Maybe QuickType |
| 69 | 53 | decodeTopLevel = decode |
| 70 | 54 | |
| @@ -86,76 +70,48 @@ instance FromJSON QuickType where | ||
| 86 | 70 | parseJSON xs@(String _) = (fmap StringInQuickType . parseJSON) xs |
| 87 | 71 | parseJSON xs@(Array _) = (fmap UnionArrayInQuickType . parseJSON) xs |
| 88 | 72 | |
| 73 | +instance ToJSON PurpleQuickType where | |
| 74 | + toJSON (AnythingMapInPurpleQuickType x) = toJSON x | |
| 75 | + toJSON (BoolInPurpleQuickType x) = toJSON x | |
| 76 | + toJSON (DoubleInPurpleQuickType x) = toJSON x | |
| 77 | + toJSON (IntegerInPurpleQuickType x) = toJSON x | |
| 78 | + toJSON NullInPurpleQuickType = Null | |
| 79 | + toJSON (StringInPurpleQuickType x) = toJSON x | |
| 80 | + toJSON (UnionArrayInPurpleQuickType x) = toJSON x | |
| 81 | + | |
| 82 | +instance FromJSON PurpleQuickType where | |
| 83 | + parseJSON xs@(Object _) = (fmap AnythingMapInPurpleQuickType . parseJSON) xs | |
| 84 | + parseJSON xs@(Bool _) = (fmap BoolInPurpleQuickType . parseJSON) xs | |
| 85 | + parseJSON xs@(Number _) = (fmap DoubleInPurpleQuickType . parseJSON) xs | |
| 86 | + parseJSON xs@(Number _) = (fmap IntegerInPurpleQuickType . parseJSON) xs | |
| 87 | + parseJSON Null = return NullInPurpleQuickType | |
| 88 | + parseJSON xs@(String _) = (fmap StringInPurpleQuickType . parseJSON) xs | |
| 89 | + parseJSON xs@(Array _) = (fmap UnionArrayInPurpleQuickType . parseJSON) xs | |
| 90 | + | |
| 91 | +instance ToJSON A where | |
| 92 | + toJSON (A xA) = | |
| 93 | + object | |
| 94 | + [ "x" .= xA | |
| 95 | + ] | |
| 96 | + | |
| 97 | +instance FromJSON A where | |
| 98 | + parseJSON (Object v) = A | |
| 99 | + <$> v .:? "x" | |
| 100 | + | |
| 89 | 101 | instance ToJSON QuickTypeElement where |
| 102 | + toJSON (AInQuickTypeElement x) = toJSON x | |
| 90 | 103 | toJSON (AnythingArrayInQuickTypeElement x) = toJSON x |
| 91 | 104 | toJSON (BoolInQuickTypeElement x) = toJSON x |
| 92 | 105 | toJSON (DoubleInQuickTypeElement x) = toJSON x |
| 93 | 106 | toJSON (IntegerInQuickTypeElement x) = toJSON x |
| 94 | 107 | toJSON NullInQuickTypeElement = Null |
| 95 | - toJSON (QuickTypeClassInQuickTypeElement x) = toJSON x | |
| 96 | 108 | toJSON (StringInQuickTypeElement x) = toJSON x |
| 97 | 109 | |
| 98 | 110 | instance FromJSON QuickTypeElement where |
| 111 | + parseJSON xs@(Object _) = (fmap AInQuickTypeElement . parseJSON) xs | |
| 99 | 112 | parseJSON xs@(Array _) = (fmap AnythingArrayInQuickTypeElement . parseJSON) xs |
| 100 | 113 | parseJSON xs@(Bool _) = (fmap BoolInQuickTypeElement . parseJSON) xs |
| 101 | 114 | parseJSON xs@(Number _) = (fmap DoubleInQuickTypeElement . parseJSON) xs |
| 102 | 115 | parseJSON xs@(Number _) = (fmap IntegerInQuickTypeElement . parseJSON) xs |
| 103 | 116 | parseJSON Null = return NullInQuickTypeElement |
| 104 | - parseJSON xs@(Object _) = (fmap QuickTypeClassInQuickTypeElement . parseJSON) xs | |
| 105 | 117 | parseJSON xs@(String _) = (fmap StringInQuickTypeElement . parseJSON) xs |
| 106 | - | |
| 107 | -instance ToJSON QuickTypeClass where | |
| 108 | - toJSON (QuickTypeClass xQuickTypeClass) = | |
| 109 | - object | |
| 110 | - [ "x" .= xQuickTypeClass | |
| 111 | - ] | |
| 112 | - | |
| 113 | -instance FromJSON QuickTypeClass where | |
| 114 | - parseJSON (Object v) = QuickTypeClass | |
| 115 | - <$> v .:? "x" | |
| 116 | - | |
| 117 | -instance ToJSON XClass where | |
| 118 | - toJSON (XClass xXClass) = | |
| 119 | - object | |
| 120 | - [ "x" .= xXClass | |
| 121 | - ] | |
| 122 | - | |
| 123 | -instance FromJSON XClass where | |
| 124 | - parseJSON (Object v) = XClass | |
| 125 | - <$> v .:? "x" | |
| 126 | - | |
| 127 | -instance ToJSON XElement where | |
| 128 | - toJSON (AnythingArrayInXElement x) = toJSON x | |
| 129 | - toJSON (BoolInXElement x) = toJSON x | |
| 130 | - toJSON (DoubleInXElement x) = toJSON x | |
| 131 | - toJSON (IntegerInXElement x) = toJSON x | |
| 132 | - toJSON NullInXElement = Null | |
| 133 | - toJSON (StringInXElement x) = toJSON x | |
| 134 | - toJSON (XClassInXElement x) = toJSON x | |
| 135 | - | |
| 136 | -instance FromJSON XElement where | |
| 137 | - parseJSON xs@(Array _) = (fmap AnythingArrayInXElement . parseJSON) xs | |
| 138 | - parseJSON xs@(Bool _) = (fmap BoolInXElement . parseJSON) xs | |
| 139 | - parseJSON xs@(Number _) = (fmap DoubleInXElement . parseJSON) xs | |
| 140 | - parseJSON xs@(Number _) = (fmap IntegerInXElement . parseJSON) xs | |
| 141 | - parseJSON Null = return NullInXElement | |
| 142 | - parseJSON xs@(String _) = (fmap StringInXElement . parseJSON) xs | |
| 143 | - parseJSON xs@(Object _) = (fmap XClassInXElement . parseJSON) xs | |
| 144 | - | |
| 145 | -instance ToJSON X where | |
| 146 | - toJSON (AnythingMapInX x) = toJSON x | |
| 147 | - toJSON (BoolInX x) = toJSON x | |
| 148 | - toJSON (DoubleInX x) = toJSON x | |
| 149 | - toJSON (IntegerInX x) = toJSON x | |
| 150 | - toJSON NullInX = Null | |
| 151 | - toJSON (StringInX x) = toJSON x | |
| 152 | - toJSON (UnionArrayInX x) = toJSON x | |
| 153 | - | |
| 154 | -instance FromJSON X where | |
| 155 | - parseJSON xs@(Object _) = (fmap AnythingMapInX . parseJSON) xs | |
| 156 | - parseJSON xs@(Bool _) = (fmap BoolInX . parseJSON) xs | |
| 157 | - parseJSON xs@(Number _) = (fmap DoubleInX . parseJSON) xs | |
| 158 | - parseJSON xs@(Number _) = (fmap IntegerInX . parseJSON) xs | |
| 159 | - parseJSON Null = return NullInX | |
| 160 | - parseJSON xs@(String _) = (fmap StringInX . parseJSON) xs | |
| 161 | - parseJSON xs@(Array _) = (fmap UnionArrayInX . parseJSON) xs |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / A.java+14 −0
| @@ -0,0 +1,14 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.List; | |
| 5 | +import java.util.Map; | |
| 6 | + | |
| 7 | +public class A { | |
| 8 | + private PurpleTopLevel x; | |
| 9 | + | |
| 10 | + @JsonProperty("x") | |
| 11 | + public PurpleTopLevel getX() { return x; } | |
| 12 | + @JsonProperty("x") | |
| 13 | + public void setX(PurpleTopLevel value) { this.x = value; } | |
| 14 | +} |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / PurpleTopLevel.java+85 −0
| @@ -0,0 +1,85 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.io.IOException; | |
| 5 | +import com.fasterxml.jackson.core.*; | |
| 6 | +import com.fasterxml.jackson.databind.*; | |
| 7 | +import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | +import com.fasterxml.jackson.core.type.*; | |
| 9 | +import java.util.List; | |
| 10 | +import java.util.Map; | |
| 11 | + | |
| 12 | +@JsonDeserialize(using = PurpleTopLevel.Deserializer.class) | |
| 13 | +@JsonSerialize(using = PurpleTopLevel.Serializer.class) | |
| 14 | +public class PurpleTopLevel { | |
| 15 | + public Double doubleValue; | |
| 16 | + public Long integerValue; | |
| 17 | + public Boolean boolValue; | |
| 18 | + public List<TopLevelElement> unionArrayValue; | |
| 19 | + public Map<String, Object> anythingMapValue; | |
| 20 | + public String stringValue; | |
| 21 | + | |
| 22 | + static class Deserializer extends JsonDeserializer<PurpleTopLevel> { | |
| 23 | + @Override | |
| 24 | + public PurpleTopLevel deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 25 | + PurpleTopLevel value = new PurpleTopLevel(); | |
| 26 | + switch (jsonParser.currentToken()) { | |
| 27 | + case VALUE_NULL: | |
| 28 | + break; | |
| 29 | + case VALUE_NUMBER_INT: | |
| 30 | + value.integerValue = jsonParser.readValueAs(Long.class); | |
| 31 | + break; | |
| 32 | + case VALUE_NUMBER_FLOAT: | |
| 33 | + value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 34 | + break; | |
| 35 | + case VALUE_TRUE: | |
| 36 | + case VALUE_FALSE: | |
| 37 | + value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 38 | + break; | |
| 39 | + case VALUE_STRING: | |
| 40 | + String string = jsonParser.readValueAs(String.class); | |
| 41 | + value.stringValue = string; | |
| 42 | + break; | |
| 43 | + case START_ARRAY: | |
| 44 | + value.unionArrayValue = jsonParser.readValueAs(new TypeReference<List<TopLevelElement>>() {}); | |
| 45 | + break; | |
| 46 | + case START_OBJECT: | |
| 47 | + value.anythingMapValue = jsonParser.readValueAs(Map.class); | |
| 48 | + break; | |
| 49 | + default: throw new IOException("Cannot deserialize PurpleTopLevel"); | |
| 50 | + } | |
| 51 | + return value; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + static class Serializer extends JsonSerializer<PurpleTopLevel> { | |
| 56 | + @Override | |
| 57 | + public void serialize(PurpleTopLevel obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 58 | + if (obj.doubleValue != null) { | |
| 59 | + jsonGenerator.writeObject(obj.doubleValue); | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + if (obj.integerValue != null) { | |
| 63 | + jsonGenerator.writeObject(obj.integerValue); | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + if (obj.boolValue != null) { | |
| 67 | + jsonGenerator.writeObject(obj.boolValue); | |
| 68 | + return; | |
| 69 | + } | |
| 70 | + if (obj.unionArrayValue != null) { | |
| 71 | + jsonGenerator.writeObject(obj.unionArrayValue); | |
| 72 | + return; | |
| 73 | + } | |
| 74 | + if (obj.anythingMapValue != null) { | |
| 75 | + jsonGenerator.writeObject(obj.anythingMapValue); | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + if (obj.stringValue != null) { | |
| 79 | + jsonGenerator.writeObject(obj.stringValue); | |
| 80 | + return; | |
| 81 | + } | |
| 82 | + jsonGenerator.writeNull(); | |
| 83 | + } | |
| 84 | + } | |
| 85 | +} |
Dschema-java-datetime-legacydefault / src / main / java / io / quicktype / TopLevelClass.java+0 −14
| @@ -1,14 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | -import java.util.List; | |
| 5 | -import java.util.Map; | |
| 6 | - | |
| 7 | -public class TopLevelClass { | |
| 8 | - private X x; | |
| 9 | - | |
| 10 | - @JsonProperty("x") | |
| 11 | - public X getX() { return x; } | |
| 12 | - @JsonProperty("x") | |
| 13 | - public void setX(X value) { this.x = value; } | |
| 14 | -} |
Mschema-java-datetime-legacydefault / src / main / java / io / quicktype / TopLevelElement.java+4 −4
| @@ -15,7 +15,7 @@ public class TopLevelElement { | ||
| 15 | 15 | public Long integerValue; |
| 16 | 16 | public Boolean boolValue; |
| 17 | 17 | public List<Object> anythingArrayValue; |
| 18 | - public TopLevelClass topLevelClassValue; | |
| 18 | + public A aValue; | |
| 19 | 19 | public String stringValue; |
| 20 | 20 | |
| 21 | 21 | static class Deserializer extends JsonDeserializer<TopLevelElement> { |
| @@ -43,7 +43,7 @@ public class TopLevelElement { | ||
| 43 | 43 | value.anythingArrayValue = jsonParser.readValueAs(new TypeReference<List<Object>>() {}); |
| 44 | 44 | break; |
| 45 | 45 | case START_OBJECT: |
| 46 | - value.topLevelClassValue = jsonParser.readValueAs(TopLevelClass.class); | |
| 46 | + value.aValue = jsonParser.readValueAs(A.class); | |
| 47 | 47 | break; |
| 48 | 48 | default: throw new IOException("Cannot deserialize TopLevelElement"); |
| 49 | 49 | } |
| @@ -70,8 +70,8 @@ public class TopLevelElement { | ||
| 70 | 70 | jsonGenerator.writeObject(obj.anythingArrayValue); |
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | - if (obj.topLevelClassValue != null) { | |
| 74 | - jsonGenerator.writeObject(obj.topLevelClassValue); | |
| 73 | + if (obj.aValue != null) { | |
| 74 | + jsonGenerator.writeObject(obj.aValue); | |
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | if (obj.stringValue != null) { |
Dschema-java-datetime-legacydefault / src / main / java / io / quicktype / X.java+0 −85
| @@ -1,85 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.io.IOException; | |
| 5 | -import com.fasterxml.jackson.core.*; | |
| 6 | -import com.fasterxml.jackson.databind.*; | |
| 7 | -import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | -import com.fasterxml.jackson.core.type.*; | |
| 9 | -import java.util.List; | |
| 10 | -import java.util.Map; | |
| 11 | - | |
| 12 | -@JsonDeserialize(using = X.Deserializer.class) | |
| 13 | -@JsonSerialize(using = X.Serializer.class) | |
| 14 | -public class X { | |
| 15 | - public Double doubleValue; | |
| 16 | - public Long integerValue; | |
| 17 | - public Boolean boolValue; | |
| 18 | - public List<XElement> unionArrayValue; | |
| 19 | - public Map<String, Object> anythingMapValue; | |
| 20 | - public String stringValue; | |
| 21 | - | |
| 22 | - static class Deserializer extends JsonDeserializer<X> { | |
| 23 | - @Override | |
| 24 | - public X deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 25 | - X value = new X(); | |
| 26 | - switch (jsonParser.currentToken()) { | |
| 27 | - case VALUE_NULL: | |
| 28 | - break; | |
| 29 | - case VALUE_NUMBER_INT: | |
| 30 | - value.integerValue = jsonParser.readValueAs(Long.class); | |
| 31 | - break; | |
| 32 | - case VALUE_NUMBER_FLOAT: | |
| 33 | - value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 34 | - break; | |
| 35 | - case VALUE_TRUE: | |
| 36 | - case VALUE_FALSE: | |
| 37 | - value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 38 | - break; | |
| 39 | - case VALUE_STRING: | |
| 40 | - String string = jsonParser.readValueAs(String.class); | |
| 41 | - value.stringValue = string; | |
| 42 | - break; | |
| 43 | - case START_ARRAY: | |
| 44 | - value.unionArrayValue = jsonParser.readValueAs(new TypeReference<List<XElement>>() {}); | |
| 45 | - break; | |
| 46 | - case START_OBJECT: | |
| 47 | - value.anythingMapValue = jsonParser.readValueAs(Map.class); | |
| 48 | - break; | |
| 49 | - default: throw new IOException("Cannot deserialize X"); | |
| 50 | - } | |
| 51 | - return value; | |
| 52 | - } | |
| 53 | - } | |
| 54 | - | |
| 55 | - static class Serializer extends JsonSerializer<X> { | |
| 56 | - @Override | |
| 57 | - public void serialize(X obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 58 | - if (obj.doubleValue != null) { | |
| 59 | - jsonGenerator.writeObject(obj.doubleValue); | |
| 60 | - return; | |
| 61 | - } | |
| 62 | - if (obj.integerValue != null) { | |
| 63 | - jsonGenerator.writeObject(obj.integerValue); | |
| 64 | - return; | |
| 65 | - } | |
| 66 | - if (obj.boolValue != null) { | |
| 67 | - jsonGenerator.writeObject(obj.boolValue); | |
| 68 | - return; | |
| 69 | - } | |
| 70 | - if (obj.unionArrayValue != null) { | |
| 71 | - jsonGenerator.writeObject(obj.unionArrayValue); | |
| 72 | - return; | |
| 73 | - } | |
| 74 | - if (obj.anythingMapValue != null) { | |
| 75 | - jsonGenerator.writeObject(obj.anythingMapValue); | |
| 76 | - return; | |
| 77 | - } | |
| 78 | - if (obj.stringValue != null) { | |
| 79 | - jsonGenerator.writeObject(obj.stringValue); | |
| 80 | - return; | |
| 81 | - } | |
| 82 | - jsonGenerator.writeNull(); | |
| 83 | - } | |
| 84 | - } | |
| 85 | -} |
Dschema-java-datetime-legacydefault / src / main / java / io / quicktype / XClass.java+0 −14
| @@ -1,14 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | -import java.util.List; | |
| 5 | -import java.util.Map; | |
| 6 | - | |
| 7 | -public class XClass { | |
| 8 | - private X x; | |
| 9 | - | |
| 10 | - @JsonProperty("x") | |
| 11 | - public X getX() { return x; } | |
| 12 | - @JsonProperty("x") | |
| 13 | - public void setX(X value) { this.x = value; } | |
| 14 | -} |
Dschema-java-datetime-legacydefault / src / main / java / io / quicktype / XElement.java+0 −84
| @@ -1,84 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.io.IOException; | |
| 5 | -import com.fasterxml.jackson.core.*; | |
| 6 | -import com.fasterxml.jackson.databind.*; | |
| 7 | -import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | -import com.fasterxml.jackson.core.type.*; | |
| 9 | -import java.util.List; | |
| 10 | - | |
| 11 | -@JsonDeserialize(using = XElement.Deserializer.class) | |
| 12 | -@JsonSerialize(using = XElement.Serializer.class) | |
| 13 | -public class XElement { | |
| 14 | - public Double doubleValue; | |
| 15 | - public Long integerValue; | |
| 16 | - public Boolean boolValue; | |
| 17 | - public List<Object> anythingArrayValue; | |
| 18 | - public XClass xClassValue; | |
| 19 | - public String stringValue; | |
| 20 | - | |
| 21 | - static class Deserializer extends JsonDeserializer<XElement> { | |
| 22 | - @Override | |
| 23 | - public XElement deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 24 | - XElement value = new XElement(); | |
| 25 | - switch (jsonParser.currentToken()) { | |
| 26 | - case VALUE_NULL: | |
| 27 | - break; | |
| 28 | - case VALUE_NUMBER_INT: | |
| 29 | - value.integerValue = jsonParser.readValueAs(Long.class); | |
| 30 | - break; | |
| 31 | - case VALUE_NUMBER_FLOAT: | |
| 32 | - value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 33 | - break; | |
| 34 | - case VALUE_TRUE: | |
| 35 | - case VALUE_FALSE: | |
| 36 | - value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 37 | - break; | |
| 38 | - case VALUE_STRING: | |
| 39 | - String string = jsonParser.readValueAs(String.class); | |
| 40 | - value.stringValue = string; | |
| 41 | - break; | |
| 42 | - case START_ARRAY: | |
| 43 | - value.anythingArrayValue = jsonParser.readValueAs(new TypeReference<List<Object>>() {}); | |
| 44 | - break; | |
| 45 | - case START_OBJECT: | |
| 46 | - value.xClassValue = jsonParser.readValueAs(XClass.class); | |
| 47 | - break; | |
| 48 | - default: throw new IOException("Cannot deserialize XElement"); | |
| 49 | - } | |
| 50 | - return value; | |
| 51 | - } | |
| 52 | - } | |
| 53 | - | |
| 54 | - static class Serializer extends JsonSerializer<XElement> { | |
| 55 | - @Override | |
| 56 | - public void serialize(XElement obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 57 | - if (obj.doubleValue != null) { | |
| 58 | - jsonGenerator.writeObject(obj.doubleValue); | |
| 59 | - return; | |
| 60 | - } | |
| 61 | - if (obj.integerValue != null) { | |
| 62 | - jsonGenerator.writeObject(obj.integerValue); | |
| 63 | - return; | |
| 64 | - } | |
| 65 | - if (obj.boolValue != null) { | |
| 66 | - jsonGenerator.writeObject(obj.boolValue); | |
| 67 | - return; | |
| 68 | - } | |
| 69 | - if (obj.anythingArrayValue != null) { | |
| 70 | - jsonGenerator.writeObject(obj.anythingArrayValue); | |
| 71 | - return; | |
| 72 | - } | |
| 73 | - if (obj.xClassValue != null) { | |
| 74 | - jsonGenerator.writeObject(obj.xClassValue); | |
| 75 | - return; | |
| 76 | - } | |
| 77 | - if (obj.stringValue != null) { | |
| 78 | - jsonGenerator.writeObject(obj.stringValue); | |
| 79 | - return; | |
| 80 | - } | |
| 81 | - jsonGenerator.writeNull(); | |
| 82 | - } | |
| 83 | - } | |
| 84 | -} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / A.java+14 −0
| @@ -0,0 +1,14 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.List; | |
| 5 | +import java.util.Map; | |
| 6 | + | |
| 7 | +public class A { | |
| 8 | + private PurpleTopLevel x; | |
| 9 | + | |
| 10 | + @JsonProperty("x") | |
| 11 | + public PurpleTopLevel getX() { return x; } | |
| 12 | + @JsonProperty("x") | |
| 13 | + public void setX(PurpleTopLevel value) { this.x = value; } | |
| 14 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / PurpleTopLevel.java+85 −0
| @@ -0,0 +1,85 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.io.IOException; | |
| 5 | +import com.fasterxml.jackson.core.*; | |
| 6 | +import com.fasterxml.jackson.databind.*; | |
| 7 | +import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | +import com.fasterxml.jackson.core.type.*; | |
| 9 | +import java.util.List; | |
| 10 | +import java.util.Map; | |
| 11 | + | |
| 12 | +@JsonDeserialize(using = PurpleTopLevel.Deserializer.class) | |
| 13 | +@JsonSerialize(using = PurpleTopLevel.Serializer.class) | |
| 14 | +public class PurpleTopLevel { | |
| 15 | + public Double doubleValue; | |
| 16 | + public Long integerValue; | |
| 17 | + public Boolean boolValue; | |
| 18 | + public List<TopLevelElement> unionArrayValue; | |
| 19 | + public Map<String, Object> anythingMapValue; | |
| 20 | + public String stringValue; | |
| 21 | + | |
| 22 | + static class Deserializer extends JsonDeserializer<PurpleTopLevel> { | |
| 23 | + @Override | |
| 24 | + public PurpleTopLevel deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 25 | + PurpleTopLevel value = new PurpleTopLevel(); | |
| 26 | + switch (jsonParser.currentToken()) { | |
| 27 | + case VALUE_NULL: | |
| 28 | + break; | |
| 29 | + case VALUE_NUMBER_INT: | |
| 30 | + value.integerValue = jsonParser.readValueAs(Long.class); | |
| 31 | + break; | |
| 32 | + case VALUE_NUMBER_FLOAT: | |
| 33 | + value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 34 | + break; | |
| 35 | + case VALUE_TRUE: | |
| 36 | + case VALUE_FALSE: | |
| 37 | + value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 38 | + break; | |
| 39 | + case VALUE_STRING: | |
| 40 | + String string = jsonParser.readValueAs(String.class); | |
| 41 | + value.stringValue = string; | |
| 42 | + break; | |
| 43 | + case START_ARRAY: | |
| 44 | + value.unionArrayValue = jsonParser.readValueAs(new TypeReference<List<TopLevelElement>>() {}); | |
| 45 | + break; | |
| 46 | + case START_OBJECT: | |
| 47 | + value.anythingMapValue = jsonParser.readValueAs(Map.class); | |
| 48 | + break; | |
| 49 | + default: throw new IOException("Cannot deserialize PurpleTopLevel"); | |
| 50 | + } | |
| 51 | + return value; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + static class Serializer extends JsonSerializer<PurpleTopLevel> { | |
| 56 | + @Override | |
| 57 | + public void serialize(PurpleTopLevel obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 58 | + if (obj.doubleValue != null) { | |
| 59 | + jsonGenerator.writeObject(obj.doubleValue); | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + if (obj.integerValue != null) { | |
| 63 | + jsonGenerator.writeObject(obj.integerValue); | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + if (obj.boolValue != null) { | |
| 67 | + jsonGenerator.writeObject(obj.boolValue); | |
| 68 | + return; | |
| 69 | + } | |
| 70 | + if (obj.unionArrayValue != null) { | |
| 71 | + jsonGenerator.writeObject(obj.unionArrayValue); | |
| 72 | + return; | |
| 73 | + } | |
| 74 | + if (obj.anythingMapValue != null) { | |
| 75 | + jsonGenerator.writeObject(obj.anythingMapValue); | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + if (obj.stringValue != null) { | |
| 79 | + jsonGenerator.writeObject(obj.stringValue); | |
| 80 | + return; | |
| 81 | + } | |
| 82 | + jsonGenerator.writeNull(); | |
| 83 | + } | |
| 84 | + } | |
| 85 | +} |
Dschema-java-lombokdefault / src / main / java / io / quicktype / TopLevelClass.java+0 −14
| @@ -1,14 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | -import java.util.List; | |
| 5 | -import java.util.Map; | |
| 6 | - | |
| 7 | -public class TopLevelClass { | |
| 8 | - private X x; | |
| 9 | - | |
| 10 | - @JsonProperty("x") | |
| 11 | - public X getX() { return x; } | |
| 12 | - @JsonProperty("x") | |
| 13 | - public void setX(X value) { this.x = value; } | |
| 14 | -} |
Mschema-java-lombokdefault / src / main / java / io / quicktype / TopLevelElement.java+4 −4
| @@ -15,7 +15,7 @@ public class TopLevelElement { | ||
| 15 | 15 | public Long integerValue; |
| 16 | 16 | public Boolean boolValue; |
| 17 | 17 | public List<Object> anythingArrayValue; |
| 18 | - public TopLevelClass topLevelClassValue; | |
| 18 | + public A aValue; | |
| 19 | 19 | public String stringValue; |
| 20 | 20 | |
| 21 | 21 | static class Deserializer extends JsonDeserializer<TopLevelElement> { |
| @@ -43,7 +43,7 @@ public class TopLevelElement { | ||
| 43 | 43 | value.anythingArrayValue = jsonParser.readValueAs(new TypeReference<List<Object>>() {}); |
| 44 | 44 | break; |
| 45 | 45 | case START_OBJECT: |
| 46 | - value.topLevelClassValue = jsonParser.readValueAs(TopLevelClass.class); | |
| 46 | + value.aValue = jsonParser.readValueAs(A.class); | |
| 47 | 47 | break; |
| 48 | 48 | default: throw new IOException("Cannot deserialize TopLevelElement"); |
| 49 | 49 | } |
| @@ -70,8 +70,8 @@ public class TopLevelElement { | ||
| 70 | 70 | jsonGenerator.writeObject(obj.anythingArrayValue); |
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | - if (obj.topLevelClassValue != null) { | |
| 74 | - jsonGenerator.writeObject(obj.topLevelClassValue); | |
| 73 | + if (obj.aValue != null) { | |
| 74 | + jsonGenerator.writeObject(obj.aValue); | |
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | if (obj.stringValue != null) { |
Dschema-java-lombokdefault / src / main / java / io / quicktype / X.java+0 −85
| @@ -1,85 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.io.IOException; | |
| 5 | -import com.fasterxml.jackson.core.*; | |
| 6 | -import com.fasterxml.jackson.databind.*; | |
| 7 | -import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | -import com.fasterxml.jackson.core.type.*; | |
| 9 | -import java.util.List; | |
| 10 | -import java.util.Map; | |
| 11 | - | |
| 12 | -@JsonDeserialize(using = X.Deserializer.class) | |
| 13 | -@JsonSerialize(using = X.Serializer.class) | |
| 14 | -public class X { | |
| 15 | - public Double doubleValue; | |
| 16 | - public Long integerValue; | |
| 17 | - public Boolean boolValue; | |
| 18 | - public List<XElement> unionArrayValue; | |
| 19 | - public Map<String, Object> anythingMapValue; | |
| 20 | - public String stringValue; | |
| 21 | - | |
| 22 | - static class Deserializer extends JsonDeserializer<X> { | |
| 23 | - @Override | |
| 24 | - public X deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 25 | - X value = new X(); | |
| 26 | - switch (jsonParser.currentToken()) { | |
| 27 | - case VALUE_NULL: | |
| 28 | - break; | |
| 29 | - case VALUE_NUMBER_INT: | |
| 30 | - value.integerValue = jsonParser.readValueAs(Long.class); | |
| 31 | - break; | |
| 32 | - case VALUE_NUMBER_FLOAT: | |
| 33 | - value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 34 | - break; | |
| 35 | - case VALUE_TRUE: | |
| 36 | - case VALUE_FALSE: | |
| 37 | - value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 38 | - break; | |
| 39 | - case VALUE_STRING: | |
| 40 | - String string = jsonParser.readValueAs(String.class); | |
| 41 | - value.stringValue = string; | |
| 42 | - break; | |
| 43 | - case START_ARRAY: | |
| 44 | - value.unionArrayValue = jsonParser.readValueAs(new TypeReference<List<XElement>>() {}); | |
| 45 | - break; | |
| 46 | - case START_OBJECT: | |
| 47 | - value.anythingMapValue = jsonParser.readValueAs(Map.class); | |
| 48 | - break; | |
| 49 | - default: throw new IOException("Cannot deserialize X"); | |
| 50 | - } | |
| 51 | - return value; | |
| 52 | - } | |
| 53 | - } | |
| 54 | - | |
| 55 | - static class Serializer extends JsonSerializer<X> { | |
| 56 | - @Override | |
| 57 | - public void serialize(X obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 58 | - if (obj.doubleValue != null) { | |
| 59 | - jsonGenerator.writeObject(obj.doubleValue); | |
| 60 | - return; | |
| 61 | - } | |
| 62 | - if (obj.integerValue != null) { | |
| 63 | - jsonGenerator.writeObject(obj.integerValue); | |
| 64 | - return; | |
| 65 | - } | |
| 66 | - if (obj.boolValue != null) { | |
| 67 | - jsonGenerator.writeObject(obj.boolValue); | |
| 68 | - return; | |
| 69 | - } | |
| 70 | - if (obj.unionArrayValue != null) { | |
| 71 | - jsonGenerator.writeObject(obj.unionArrayValue); | |
| 72 | - return; | |
| 73 | - } | |
| 74 | - if (obj.anythingMapValue != null) { | |
| 75 | - jsonGenerator.writeObject(obj.anythingMapValue); | |
| 76 | - return; | |
| 77 | - } | |
| 78 | - if (obj.stringValue != null) { | |
| 79 | - jsonGenerator.writeObject(obj.stringValue); | |
| 80 | - return; | |
| 81 | - } | |
| 82 | - jsonGenerator.writeNull(); | |
| 83 | - } | |
| 84 | - } | |
| 85 | -} |
Dschema-java-lombokdefault / src / main / java / io / quicktype / XClass.java+0 −14
| @@ -1,14 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | -import java.util.List; | |
| 5 | -import java.util.Map; | |
| 6 | - | |
| 7 | -public class XClass { | |
| 8 | - private X x; | |
| 9 | - | |
| 10 | - @JsonProperty("x") | |
| 11 | - public X getX() { return x; } | |
| 12 | - @JsonProperty("x") | |
| 13 | - public void setX(X value) { this.x = value; } | |
| 14 | -} |
Dschema-java-lombokdefault / src / main / java / io / quicktype / XElement.java+0 −84
| @@ -1,84 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.io.IOException; | |
| 5 | -import com.fasterxml.jackson.core.*; | |
| 6 | -import com.fasterxml.jackson.databind.*; | |
| 7 | -import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | -import com.fasterxml.jackson.core.type.*; | |
| 9 | -import java.util.List; | |
| 10 | - | |
| 11 | -@JsonDeserialize(using = XElement.Deserializer.class) | |
| 12 | -@JsonSerialize(using = XElement.Serializer.class) | |
| 13 | -public class XElement { | |
| 14 | - public Double doubleValue; | |
| 15 | - public Long integerValue; | |
| 16 | - public Boolean boolValue; | |
| 17 | - public List<Object> anythingArrayValue; | |
| 18 | - public XClass xClassValue; | |
| 19 | - public String stringValue; | |
| 20 | - | |
| 21 | - static class Deserializer extends JsonDeserializer<XElement> { | |
| 22 | - @Override | |
| 23 | - public XElement deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 24 | - XElement value = new XElement(); | |
| 25 | - switch (jsonParser.currentToken()) { | |
| 26 | - case VALUE_NULL: | |
| 27 | - break; | |
| 28 | - case VALUE_NUMBER_INT: | |
| 29 | - value.integerValue = jsonParser.readValueAs(Long.class); | |
| 30 | - break; | |
| 31 | - case VALUE_NUMBER_FLOAT: | |
| 32 | - value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 33 | - break; | |
| 34 | - case VALUE_TRUE: | |
| 35 | - case VALUE_FALSE: | |
| 36 | - value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 37 | - break; | |
| 38 | - case VALUE_STRING: | |
| 39 | - String string = jsonParser.readValueAs(String.class); | |
| 40 | - value.stringValue = string; | |
| 41 | - break; | |
| 42 | - case START_ARRAY: | |
| 43 | - value.anythingArrayValue = jsonParser.readValueAs(new TypeReference<List<Object>>() {}); | |
| 44 | - break; | |
| 45 | - case START_OBJECT: | |
| 46 | - value.xClassValue = jsonParser.readValueAs(XClass.class); | |
| 47 | - break; | |
| 48 | - default: throw new IOException("Cannot deserialize XElement"); | |
| 49 | - } | |
| 50 | - return value; | |
| 51 | - } | |
| 52 | - } | |
| 53 | - | |
| 54 | - static class Serializer extends JsonSerializer<XElement> { | |
| 55 | - @Override | |
| 56 | - public void serialize(XElement obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 57 | - if (obj.doubleValue != null) { | |
| 58 | - jsonGenerator.writeObject(obj.doubleValue); | |
| 59 | - return; | |
| 60 | - } | |
| 61 | - if (obj.integerValue != null) { | |
| 62 | - jsonGenerator.writeObject(obj.integerValue); | |
| 63 | - return; | |
| 64 | - } | |
| 65 | - if (obj.boolValue != null) { | |
| 66 | - jsonGenerator.writeObject(obj.boolValue); | |
| 67 | - return; | |
| 68 | - } | |
| 69 | - if (obj.anythingArrayValue != null) { | |
| 70 | - jsonGenerator.writeObject(obj.anythingArrayValue); | |
| 71 | - return; | |
| 72 | - } | |
| 73 | - if (obj.xClassValue != null) { | |
| 74 | - jsonGenerator.writeObject(obj.xClassValue); | |
| 75 | - return; | |
| 76 | - } | |
| 77 | - if (obj.stringValue != null) { | |
| 78 | - jsonGenerator.writeObject(obj.stringValue); | |
| 79 | - return; | |
| 80 | - } | |
| 81 | - jsonGenerator.writeNull(); | |
| 82 | - } | |
| 83 | - } | |
| 84 | -} |
Aschema-javadefault / src / main / java / io / quicktype / A.java+14 −0
| @@ -0,0 +1,14 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | +import java.util.List; | |
| 5 | +import java.util.Map; | |
| 6 | + | |
| 7 | +public class A { | |
| 8 | + private PurpleTopLevel x; | |
| 9 | + | |
| 10 | + @JsonProperty("x") | |
| 11 | + public PurpleTopLevel getX() { return x; } | |
| 12 | + @JsonProperty("x") | |
| 13 | + public void setX(PurpleTopLevel value) { this.x = value; } | |
| 14 | +} |
Aschema-javadefault / src / main / java / io / quicktype / PurpleTopLevel.java+85 −0
| @@ -0,0 +1,85 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.io.IOException; | |
| 5 | +import com.fasterxml.jackson.core.*; | |
| 6 | +import com.fasterxml.jackson.databind.*; | |
| 7 | +import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | +import com.fasterxml.jackson.core.type.*; | |
| 9 | +import java.util.List; | |
| 10 | +import java.util.Map; | |
| 11 | + | |
| 12 | +@JsonDeserialize(using = PurpleTopLevel.Deserializer.class) | |
| 13 | +@JsonSerialize(using = PurpleTopLevel.Serializer.class) | |
| 14 | +public class PurpleTopLevel { | |
| 15 | + public Double doubleValue; | |
| 16 | + public Long integerValue; | |
| 17 | + public Boolean boolValue; | |
| 18 | + public List<TopLevelElement> unionArrayValue; | |
| 19 | + public Map<String, Object> anythingMapValue; | |
| 20 | + public String stringValue; | |
| 21 | + | |
| 22 | + static class Deserializer extends JsonDeserializer<PurpleTopLevel> { | |
| 23 | + @Override | |
| 24 | + public PurpleTopLevel deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 25 | + PurpleTopLevel value = new PurpleTopLevel(); | |
| 26 | + switch (jsonParser.currentToken()) { | |
| 27 | + case VALUE_NULL: | |
| 28 | + break; | |
| 29 | + case VALUE_NUMBER_INT: | |
| 30 | + value.integerValue = jsonParser.readValueAs(Long.class); | |
| 31 | + break; | |
| 32 | + case VALUE_NUMBER_FLOAT: | |
| 33 | + value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 34 | + break; | |
| 35 | + case VALUE_TRUE: | |
| 36 | + case VALUE_FALSE: | |
| 37 | + value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 38 | + break; | |
| 39 | + case VALUE_STRING: | |
| 40 | + String string = jsonParser.readValueAs(String.class); | |
| 41 | + value.stringValue = string; | |
| 42 | + break; | |
| 43 | + case START_ARRAY: | |
| 44 | + value.unionArrayValue = jsonParser.readValueAs(new TypeReference<List<TopLevelElement>>() {}); | |
| 45 | + break; | |
| 46 | + case START_OBJECT: | |
| 47 | + value.anythingMapValue = jsonParser.readValueAs(Map.class); | |
| 48 | + break; | |
| 49 | + default: throw new IOException("Cannot deserialize PurpleTopLevel"); | |
| 50 | + } | |
| 51 | + return value; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + static class Serializer extends JsonSerializer<PurpleTopLevel> { | |
| 56 | + @Override | |
| 57 | + public void serialize(PurpleTopLevel obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 58 | + if (obj.doubleValue != null) { | |
| 59 | + jsonGenerator.writeObject(obj.doubleValue); | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + if (obj.integerValue != null) { | |
| 63 | + jsonGenerator.writeObject(obj.integerValue); | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + if (obj.boolValue != null) { | |
| 67 | + jsonGenerator.writeObject(obj.boolValue); | |
| 68 | + return; | |
| 69 | + } | |
| 70 | + if (obj.unionArrayValue != null) { | |
| 71 | + jsonGenerator.writeObject(obj.unionArrayValue); | |
| 72 | + return; | |
| 73 | + } | |
| 74 | + if (obj.anythingMapValue != null) { | |
| 75 | + jsonGenerator.writeObject(obj.anythingMapValue); | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + if (obj.stringValue != null) { | |
| 79 | + jsonGenerator.writeObject(obj.stringValue); | |
| 80 | + return; | |
| 81 | + } | |
| 82 | + jsonGenerator.writeNull(); | |
| 83 | + } | |
| 84 | + } | |
| 85 | +} |
Dschema-javadefault / src / main / java / io / quicktype / TopLevelClass.java+0 −14
| @@ -1,14 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | -import java.util.List; | |
| 5 | -import java.util.Map; | |
| 6 | - | |
| 7 | -public class TopLevelClass { | |
| 8 | - private X x; | |
| 9 | - | |
| 10 | - @JsonProperty("x") | |
| 11 | - public X getX() { return x; } | |
| 12 | - @JsonProperty("x") | |
| 13 | - public void setX(X value) { this.x = value; } | |
| 14 | -} |
Mschema-javadefault / src / main / java / io / quicktype / TopLevelElement.java+4 −4
| @@ -15,7 +15,7 @@ public class TopLevelElement { | ||
| 15 | 15 | public Long integerValue; |
| 16 | 16 | public Boolean boolValue; |
| 17 | 17 | public List<Object> anythingArrayValue; |
| 18 | - public TopLevelClass topLevelClassValue; | |
| 18 | + public A aValue; | |
| 19 | 19 | public String stringValue; |
| 20 | 20 | |
| 21 | 21 | static class Deserializer extends JsonDeserializer<TopLevelElement> { |
| @@ -43,7 +43,7 @@ public class TopLevelElement { | ||
| 43 | 43 | value.anythingArrayValue = jsonParser.readValueAs(new TypeReference<List<Object>>() {}); |
| 44 | 44 | break; |
| 45 | 45 | case START_OBJECT: |
| 46 | - value.topLevelClassValue = jsonParser.readValueAs(TopLevelClass.class); | |
| 46 | + value.aValue = jsonParser.readValueAs(A.class); | |
| 47 | 47 | break; |
| 48 | 48 | default: throw new IOException("Cannot deserialize TopLevelElement"); |
| 49 | 49 | } |
| @@ -70,8 +70,8 @@ public class TopLevelElement { | ||
| 70 | 70 | jsonGenerator.writeObject(obj.anythingArrayValue); |
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | - if (obj.topLevelClassValue != null) { | |
| 74 | - jsonGenerator.writeObject(obj.topLevelClassValue); | |
| 73 | + if (obj.aValue != null) { | |
| 74 | + jsonGenerator.writeObject(obj.aValue); | |
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | if (obj.stringValue != null) { |
Dschema-javadefault / src / main / java / io / quicktype / X.java+0 −85
| @@ -1,85 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.io.IOException; | |
| 5 | -import com.fasterxml.jackson.core.*; | |
| 6 | -import com.fasterxml.jackson.databind.*; | |
| 7 | -import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | -import com.fasterxml.jackson.core.type.*; | |
| 9 | -import java.util.List; | |
| 10 | -import java.util.Map; | |
| 11 | - | |
| 12 | -@JsonDeserialize(using = X.Deserializer.class) | |
| 13 | -@JsonSerialize(using = X.Serializer.class) | |
| 14 | -public class X { | |
| 15 | - public Double doubleValue; | |
| 16 | - public Long integerValue; | |
| 17 | - public Boolean boolValue; | |
| 18 | - public List<XElement> unionArrayValue; | |
| 19 | - public Map<String, Object> anythingMapValue; | |
| 20 | - public String stringValue; | |
| 21 | - | |
| 22 | - static class Deserializer extends JsonDeserializer<X> { | |
| 23 | - @Override | |
| 24 | - public X deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 25 | - X value = new X(); | |
| 26 | - switch (jsonParser.currentToken()) { | |
| 27 | - case VALUE_NULL: | |
| 28 | - break; | |
| 29 | - case VALUE_NUMBER_INT: | |
| 30 | - value.integerValue = jsonParser.readValueAs(Long.class); | |
| 31 | - break; | |
| 32 | - case VALUE_NUMBER_FLOAT: | |
| 33 | - value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 34 | - break; | |
| 35 | - case VALUE_TRUE: | |
| 36 | - case VALUE_FALSE: | |
| 37 | - value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 38 | - break; | |
| 39 | - case VALUE_STRING: | |
| 40 | - String string = jsonParser.readValueAs(String.class); | |
| 41 | - value.stringValue = string; | |
| 42 | - break; | |
| 43 | - case START_ARRAY: | |
| 44 | - value.unionArrayValue = jsonParser.readValueAs(new TypeReference<List<XElement>>() {}); | |
| 45 | - break; | |
| 46 | - case START_OBJECT: | |
| 47 | - value.anythingMapValue = jsonParser.readValueAs(Map.class); | |
| 48 | - break; | |
| 49 | - default: throw new IOException("Cannot deserialize X"); | |
| 50 | - } | |
| 51 | - return value; | |
| 52 | - } | |
| 53 | - } | |
| 54 | - | |
| 55 | - static class Serializer extends JsonSerializer<X> { | |
| 56 | - @Override | |
| 57 | - public void serialize(X obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 58 | - if (obj.doubleValue != null) { | |
| 59 | - jsonGenerator.writeObject(obj.doubleValue); | |
| 60 | - return; | |
| 61 | - } | |
| 62 | - if (obj.integerValue != null) { | |
| 63 | - jsonGenerator.writeObject(obj.integerValue); | |
| 64 | - return; | |
| 65 | - } | |
| 66 | - if (obj.boolValue != null) { | |
| 67 | - jsonGenerator.writeObject(obj.boolValue); | |
| 68 | - return; | |
| 69 | - } | |
| 70 | - if (obj.unionArrayValue != null) { | |
| 71 | - jsonGenerator.writeObject(obj.unionArrayValue); | |
| 72 | - return; | |
| 73 | - } | |
| 74 | - if (obj.anythingMapValue != null) { | |
| 75 | - jsonGenerator.writeObject(obj.anythingMapValue); | |
| 76 | - return; | |
| 77 | - } | |
| 78 | - if (obj.stringValue != null) { | |
| 79 | - jsonGenerator.writeObject(obj.stringValue); | |
| 80 | - return; | |
| 81 | - } | |
| 82 | - jsonGenerator.writeNull(); | |
| 83 | - } | |
| 84 | - } | |
| 85 | -} |
Dschema-javadefault / src / main / java / io / quicktype / XClass.java+0 −14
| @@ -1,14 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | -import java.util.List; | |
| 5 | -import java.util.Map; | |
| 6 | - | |
| 7 | -public class XClass { | |
| 8 | - private X x; | |
| 9 | - | |
| 10 | - @JsonProperty("x") | |
| 11 | - public X getX() { return x; } | |
| 12 | - @JsonProperty("x") | |
| 13 | - public void setX(X value) { this.x = value; } | |
| 14 | -} |
Dschema-javadefault / src / main / java / io / quicktype / XElement.java+0 −84
| @@ -1,84 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.io.IOException; | |
| 5 | -import com.fasterxml.jackson.core.*; | |
| 6 | -import com.fasterxml.jackson.databind.*; | |
| 7 | -import com.fasterxml.jackson.databind.annotation.*; | |
| 8 | -import com.fasterxml.jackson.core.type.*; | |
| 9 | -import java.util.List; | |
| 10 | - | |
| 11 | -@JsonDeserialize(using = XElement.Deserializer.class) | |
| 12 | -@JsonSerialize(using = XElement.Serializer.class) | |
| 13 | -public class XElement { | |
| 14 | - public Double doubleValue; | |
| 15 | - public Long integerValue; | |
| 16 | - public Boolean boolValue; | |
| 17 | - public List<Object> anythingArrayValue; | |
| 18 | - public XClass xClassValue; | |
| 19 | - public String stringValue; | |
| 20 | - | |
| 21 | - static class Deserializer extends JsonDeserializer<XElement> { | |
| 22 | - @Override | |
| 23 | - public XElement deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | |
| 24 | - XElement value = new XElement(); | |
| 25 | - switch (jsonParser.currentToken()) { | |
| 26 | - case VALUE_NULL: | |
| 27 | - break; | |
| 28 | - case VALUE_NUMBER_INT: | |
| 29 | - value.integerValue = jsonParser.readValueAs(Long.class); | |
| 30 | - break; | |
| 31 | - case VALUE_NUMBER_FLOAT: | |
| 32 | - value.doubleValue = jsonParser.readValueAs(Double.class); | |
| 33 | - break; | |
| 34 | - case VALUE_TRUE: | |
| 35 | - case VALUE_FALSE: | |
| 36 | - value.boolValue = jsonParser.readValueAs(Boolean.class); | |
| 37 | - break; | |
| 38 | - case VALUE_STRING: | |
| 39 | - String string = jsonParser.readValueAs(String.class); | |
| 40 | - value.stringValue = string; | |
| 41 | - break; | |
| 42 | - case START_ARRAY: | |
| 43 | - value.anythingArrayValue = jsonParser.readValueAs(new TypeReference<List<Object>>() {}); | |
| 44 | - break; | |
| 45 | - case START_OBJECT: | |
| 46 | - value.xClassValue = jsonParser.readValueAs(XClass.class); | |
| 47 | - break; | |
| 48 | - default: throw new IOException("Cannot deserialize XElement"); | |
| 49 | - } | |
| 50 | - return value; | |
| 51 | - } | |
| 52 | - } | |
| 53 | - | |
| 54 | - static class Serializer extends JsonSerializer<XElement> { | |
| 55 | - @Override | |
| 56 | - public void serialize(XElement obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | |
| 57 | - if (obj.doubleValue != null) { | |
| 58 | - jsonGenerator.writeObject(obj.doubleValue); | |
| 59 | - return; | |
| 60 | - } | |
| 61 | - if (obj.integerValue != null) { | |
| 62 | - jsonGenerator.writeObject(obj.integerValue); | |
| 63 | - return; | |
| 64 | - } | |
| 65 | - if (obj.boolValue != null) { | |
| 66 | - jsonGenerator.writeObject(obj.boolValue); | |
| 67 | - return; | |
| 68 | - } | |
| 69 | - if (obj.anythingArrayValue != null) { | |
| 70 | - jsonGenerator.writeObject(obj.anythingArrayValue); | |
| 71 | - return; | |
| 72 | - } | |
| 73 | - if (obj.xClassValue != null) { | |
| 74 | - jsonGenerator.writeObject(obj.xClassValue); | |
| 75 | - return; | |
| 76 | - } | |
| 77 | - if (obj.stringValue != null) { | |
| 78 | - jsonGenerator.writeObject(obj.stringValue); | |
| 79 | - return; | |
| 80 | - } | |
| 81 | - jsonGenerator.writeNull(); | |
| 82 | - } | |
| 83 | - } | |
| 84 | -} |
Mschema-javascriptdefault / TopLevel.js+4 −7
| @@ -10,11 +10,11 @@ | ||
| 10 | 10 | // Converts JSON strings to/from your types |
| 11 | 11 | // and asserts the results of JSON.parse at runtime |
| 12 | 12 | function toTopLevel(json) { |
| 13 | - return cast(JSON.parse(json), u(a(u(a("any"), true, 3.14, null, r("TopLevelObject"), "")), true, 3.14, 0, m("any"), null, "")); | |
| 13 | + return cast(JSON.parse(json), u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, 0, m("any"), null, "")); | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | function topLevelToJson(value) { |
| 17 | - return JSON.stringify(uncast(value, u(a(u(a("any"), true, 3.14, null, r("TopLevelObject"), "")), true, 3.14, 0, m("any"), null, "")), null, 2); | |
| 17 | + return JSON.stringify(uncast(value, u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, 0, m("any"), null, "")), null, 2); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | function invalidValue(typ, val, key, parent = '') { |
| @@ -171,11 +171,8 @@ function r(name) { | ||
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | const typeMap = { |
| 174 | - "TopLevelObject": o([ | |
| 175 | - { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("XObject"), "")), true, 3.14, m("any"), null, "")) }, | |
| 176 | - ], "any"), | |
| 177 | - "XObject": o([ | |
| 178 | - { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("XObject"), "")), true, 3.14, m("any"), null, "")) }, | |
| 174 | + "A": o([ | |
| 175 | + { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, m("any"), null, "")) }, | |
| 179 | 176 | ], "any"), |
| 180 | 177 | }; |
Mschema-pikedefault / TopLevel.pmod+8 −34
| @@ -18,34 +18,14 @@ TopLevel TopLevel_from_JSON(mixed json) { | ||
| 18 | 18 | return json; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | -typedef array(mixed)|bool|float|string|TopLevelClass TopLevelElement; | |
| 21 | +typedef mapping(string:mixed)|bool|float|string|array(TopLevelElement) PurpleTopLevel; | |
| 22 | 22 | |
| 23 | -TopLevelElement TopLevelElement_from_JSON(mixed json) { | |
| 23 | +PurpleTopLevel PurpleTopLevel_from_JSON(mixed json) { | |
| 24 | 24 | return json; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | -class TopLevelClass { | |
| 28 | - X x; // json: "x" | |
| 29 | - | |
| 30 | - string encode_json() { | |
| 31 | - mapping(string:mixed) json = ([ | |
| 32 | - "x" : x, | |
| 33 | - ]); | |
| 34 | - | |
| 35 | - return Standards.JSON.encode(json); | |
| 36 | - } | |
| 37 | -} | |
| 38 | - | |
| 39 | -TopLevelClass TopLevelClass_from_JSON(mixed json) { | |
| 40 | - TopLevelClass retval = TopLevelClass(); | |
| 41 | - | |
| 42 | - retval.x = json["x"]; | |
| 43 | - | |
| 44 | - return retval; | |
| 45 | -} | |
| 46 | - | |
| 47 | -class XClass { | |
| 48 | - X x; // json: "x" | |
| 27 | +class A { | |
| 28 | + PurpleTopLevel x; // json: "x" | |
| 49 | 29 | |
| 50 | 30 | string encode_json() { |
| 51 | 31 | mapping(string:mixed) json = ([ |
| @@ -56,22 +36,16 @@ class XClass { | ||
| 56 | 36 | } |
| 57 | 37 | } |
| 58 | 38 | |
| 59 | -XClass XClass_from_JSON(mixed json) { | |
| 60 | - XClass retval = XClass(); | |
| 39 | +A A_from_JSON(mixed json) { | |
| 40 | + A retval = A(); | |
| 61 | 41 | |
| 62 | 42 | retval.x = json["x"]; |
| 63 | 43 | |
| 64 | 44 | return retval; |
| 65 | 45 | } |
| 66 | 46 | |
| 67 | -typedef array(mixed)|bool|float|string|XClass XElement; | |
| 47 | +typedef A|array(mixed)|bool|float|string TopLevelElement; | |
| 68 | 48 | |
| 69 | -XElement XElement_from_JSON(mixed json) { | |
| 70 | - return json; | |
| 71 | -} | |
| 72 | - | |
| 73 | -typedef mapping(string:mixed)|bool|float|string|array(XElement) X; | |
| 74 | - | |
| 75 | -X X_from_JSON(mixed json) { | |
| 49 | +TopLevelElement TopLevelElement_from_JSON(mixed json) { | |
| 76 | 50 | return json; |
| 77 | 51 | } |
Mschema-pythondefault / quicktype.py+10 −27
| @@ -60,42 +60,25 @@ def to_class(c: Type[T], x: Any) -> dict: | ||
| 60 | 60 | |
| 61 | 61 | |
| 62 | 62 | @dataclass |
| 63 | -class XClass: | |
| 64 | - x: 'float | int | bool | list[float | int | bool | list[Any] | XClass | str | None] | dict[str, Any] | str | None' = None | |
| 63 | +class A: | |
| 64 | + x: 'float | int | bool | list[float | int | bool | list[Any] | A | str | None] | dict[str, Any] | str | None' = None | |
| 65 | 65 | |
| 66 | 66 | @staticmethod |
| 67 | - def from_dict(obj: Any) -> 'XClass': | |
| 67 | + def from_dict(obj: Any) -> 'A': | |
| 68 | 68 | assert isinstance(obj, dict) |
| 69 | - x = from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: x, x), XClass.from_dict, from_str], x), x), lambda x: from_dict(lambda x: x, x), from_str], obj.get("x")) | |
| 70 | - return XClass(x) | |
| 69 | + x = from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: x, x), A.from_dict, from_str], x), x), lambda x: from_dict(lambda x: x, x), from_str], obj.get("x")) | |
| 70 | + return A(x) | |
| 71 | 71 | |
| 72 | 72 | def to_dict(self) -> dict: |
| 73 | 73 | result: dict = {} |
| 74 | 74 | if self.x is not None: |
| 75 | - result["x"] = from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: x, x), lambda x: to_class(XClass, x), from_str], x), x), lambda x: from_dict(lambda x: x, x), from_str], self.x) | |
| 75 | + result["x"] = from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: x, x), lambda x: to_class(A, x), from_str], x), x), lambda x: from_dict(lambda x: x, x), from_str], self.x) | |
| 76 | 76 | return result |
| 77 | 77 | |
| 78 | 78 | |
| 79 | -@dataclass | |
| 80 | -class TopLevelClass: | |
| 81 | - x: float | int | bool | list[float | int | bool | list[Any] | XClass | str | None] | dict[str, Any] | str | None = None | |
| 82 | - | |
| 83 | - @staticmethod | |
| 84 | - def from_dict(obj: Any) -> 'TopLevelClass': | |
| 85 | - assert isinstance(obj, dict) | |
| 86 | - x = from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: x, x), XClass.from_dict, from_str], x), x), lambda x: from_dict(lambda x: x, x), from_str], obj.get("x")) | |
| 87 | - return TopLevelClass(x) | |
| 88 | - | |
| 89 | - def to_dict(self) -> dict: | |
| 90 | - result: dict = {} | |
| 91 | - if self.x is not None: | |
| 92 | - result["x"] = from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: x, x), lambda x: to_class(XClass, x), from_str], x), x), lambda x: from_dict(lambda x: x, x), from_str], self.x) | |
| 93 | - return result | |
| 94 | - | |
| 95 | - | |
| 96 | -def top_level_from_dict(s: Any) -> float | int | bool | str | list[float | int | bool | list[Any] | TopLevelClass | str | None] | dict[str, Any] | None: | |
| 97 | - return from_union([from_none, from_int, from_float, from_bool, from_str, lambda x: from_list(lambda x: from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: x, x), TopLevelClass.from_dict, from_str], x), x), lambda x: from_dict(lambda x: x, x)], s) | |
| 79 | +def top_level_from_dict(s: Any) -> float | int | bool | str | list[float | int | bool | list[Any] | A | str | None] | dict[str, Any] | None: | |
| 80 | + return from_union([from_none, from_int, from_float, from_bool, from_str, lambda x: from_list(lambda x: from_union([from_none, from_int, from_float, from_bool, lambda x: from_list(lambda x: x, x), A.from_dict, from_str], x), x), lambda x: from_dict(lambda x: x, x)], s) | |
| 98 | 81 | |
| 99 | 82 | |
| 100 | -def top_level_to_dict(x: float | int | bool | str | list[float | int | bool | list[Any] | TopLevelClass | str | None] | dict[str, Any] | None) -> Any: | |
| 101 | - return from_union([from_none, from_int, to_float, from_bool, from_str, lambda x: from_list(lambda x: from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: x, x), lambda x: to_class(TopLevelClass, x), from_str], x), x), lambda x: from_dict(lambda x: x, x)], x) | |
| 83 | +def top_level_to_dict(x: float | int | bool | str | list[float | int | bool | list[Any] | A | str | None] | dict[str, Any] | None) -> Any: | |
| 84 | + return from_union([from_none, from_int, to_float, from_bool, from_str, lambda x: from_list(lambda x: from_union([from_none, from_int, to_float, from_bool, lambda x: from_list(lambda x: x, x), lambda x: to_class(A, x), from_str], x), x), lambda x: from_dict(lambda x: x, x)], x) |
Mschema-rubydefault / TopLevel.rb+24 −108
| @@ -24,15 +24,15 @@ module Types | ||
| 24 | 24 | end |
| 25 | 25 | |
| 26 | 26 | # (forward declaration) |
| 27 | -class XClass < Dry::Struct; end | |
| 27 | +class A < Dry::Struct; end | |
| 28 | 28 | |
| 29 | -class X < Dry::Struct | |
| 29 | +class TopLevel1 < Dry::Struct | |
| 30 | 30 | attribute :anything_map, Types::Hash.meta(of: Types::Any).optional |
| 31 | 31 | attribute :bool, Types::Bool.optional |
| 32 | 32 | attribute :double, Types::Double.optional |
| 33 | 33 | attribute :null, Types::Nil.optional |
| 34 | 34 | attribute :string, Types::String.optional |
| 35 | - attribute :union_array, Types.Array(Types.Instance(XElement)).optional | |
| 35 | + attribute :union_array, Types.Array(Types.Instance(TopLevelElement)).optional | |
| 36 | 36 | |
| 37 | 37 | def self.from_dynamic!(d) |
| 38 | 38 | begin |
| @@ -55,7 +55,7 @@ class X < Dry::Struct | ||
| 55 | 55 | return new(string: d, union_array: nil, bool: nil, double: nil, anything_map: nil, null: nil) |
| 56 | 56 | end |
| 57 | 57 | begin |
| 58 | - value = d.map { |x| XElement.from_dynamic!(x) } | |
| 58 | + value = d.map { |x| TopLevelElement.from_dynamic!(x) } | |
| 59 | 59 | if schema[:union_array].right.valid? value |
| 60 | 60 | return new(union_array: value, bool: nil, double: nil, anything_map: nil, null: nil, string: nil) |
| 61 | 61 | end |
| @@ -89,36 +89,36 @@ class X < Dry::Struct | ||
| 89 | 89 | end |
| 90 | 90 | end |
| 91 | 91 | |
| 92 | -class XElement < Dry::Struct | |
| 92 | +class TopLevelElement < Dry::Struct | |
| 93 | + attribute :a, A.optional | |
| 93 | 94 | attribute :anything_array, Types.Array(Types::Any).optional |
| 94 | 95 | attribute :bool, Types::Bool.optional |
| 95 | 96 | attribute :double, Types::Double.optional |
| 96 | 97 | attribute :null, Types::Nil.optional |
| 97 | 98 | attribute :string, Types::String.optional |
| 98 | - attribute :x_class, XClass.optional | |
| 99 | 99 | |
| 100 | 100 | def self.from_dynamic!(d) |
| 101 | + begin | |
| 102 | + value = A.from_dynamic!(d) | |
| 103 | + if schema[:a].right.valid? value | |
| 104 | + return new(a: value, anything_array: nil, bool: nil, double: nil, null: nil, string: nil) | |
| 105 | + end | |
| 106 | + rescue | |
| 107 | + end | |
| 101 | 108 | if schema[:anything_array].right.valid? d |
| 102 | - return new(anything_array: d, bool: nil, x_class: nil, double: nil, null: nil, string: nil) | |
| 109 | + return new(anything_array: d, bool: nil, a: nil, double: nil, null: nil, string: nil) | |
| 103 | 110 | end |
| 104 | 111 | if schema[:bool].right.valid? d |
| 105 | - return new(bool: d, anything_array: nil, x_class: nil, double: nil, null: nil, string: nil) | |
| 112 | + return new(bool: d, anything_array: nil, a: nil, double: nil, null: nil, string: nil) | |
| 106 | 113 | end |
| 107 | 114 | if schema[:double].right.valid? d |
| 108 | - return new(double: d, anything_array: nil, bool: nil, x_class: nil, null: nil, string: nil) | |
| 115 | + return new(double: d, anything_array: nil, bool: nil, a: nil, null: nil, string: nil) | |
| 109 | 116 | end |
| 110 | 117 | if schema[:null].right.valid? d |
| 111 | - return new(null: d, anything_array: nil, bool: nil, x_class: nil, double: nil, string: nil) | |
| 118 | + return new(null: d, anything_array: nil, bool: nil, a: nil, double: nil, string: nil) | |
| 112 | 119 | end |
| 113 | 120 | if schema[:string].right.valid? d |
| 114 | - return new(string: d, anything_array: nil, bool: nil, x_class: nil, double: nil, null: nil) | |
| 115 | - end | |
| 116 | - begin | |
| 117 | - value = XClass.from_dynamic!(d) | |
| 118 | - if schema[:x_class].right.valid? value | |
| 119 | - return new(x_class: value, anything_array: nil, bool: nil, double: nil, null: nil, string: nil) | |
| 120 | - end | |
| 121 | - rescue | |
| 121 | + return new(string: d, anything_array: nil, bool: nil, a: nil, double: nil, null: nil) | |
| 122 | 122 | end |
| 123 | 123 | raise "Invalid union" |
| 124 | 124 | end |
| @@ -128,7 +128,9 @@ class XElement < Dry::Struct | ||
| 128 | 128 | end |
| 129 | 129 | |
| 130 | 130 | def to_dynamic |
| 131 | - if anything_array != nil | |
| 131 | + if a != nil | |
| 132 | + a.to_dynamic | |
| 133 | + elsif anything_array != nil | |
| 132 | 134 | anything_array |
| 133 | 135 | elsif bool != nil |
| 134 | 136 | bool |
| @@ -136,8 +138,6 @@ class XElement < Dry::Struct | ||
| 136 | 138 | double |
| 137 | 139 | elsif string != nil |
| 138 | 140 | string |
| 139 | - elsif x_class != nil | |
| 140 | - x_class.to_dynamic | |
| 141 | 141 | else |
| 142 | 142 | nil |
| 143 | 143 | end |
| @@ -148,38 +148,13 @@ class XElement < Dry::Struct | ||
| 148 | 148 | end |
| 149 | 149 | end |
| 150 | 150 | |
| 151 | -class XClass < Dry::Struct | |
| 152 | - attribute :x, Types.Instance(X).optional | |
| 153 | - | |
| 154 | - def self.from_dynamic!(d) | |
| 155 | - d = Types::Hash[d] | |
| 156 | - new( | |
| 157 | - x: d["x"] ? X.from_dynamic!(d["x"]) : nil, | |
| 158 | - ) | |
| 159 | - end | |
| 160 | - | |
| 161 | - def self.from_json!(json) | |
| 162 | - from_dynamic!(JSON.parse(json)) | |
| 163 | - end | |
| 164 | - | |
| 165 | - def to_dynamic | |
| 166 | - { | |
| 167 | - "x" => x&.to_dynamic, | |
| 168 | - } | |
| 169 | - end | |
| 170 | - | |
| 171 | - def to_json(options = nil) | |
| 172 | - JSON.generate(to_dynamic, options) | |
| 173 | - end | |
| 174 | -end | |
| 175 | - | |
| 176 | -class TopLevelClass < Dry::Struct | |
| 177 | - attribute :x, Types.Instance(X).optional | |
| 151 | +class A < Dry::Struct | |
| 152 | + attribute :x, Types.Instance(TopLevel1).optional | |
| 178 | 153 | |
| 179 | 154 | def self.from_dynamic!(d) |
| 180 | 155 | d = Types::Hash[d] |
| 181 | 156 | new( |
| 182 | - x: d["x"] ? X.from_dynamic!(d["x"]) : nil, | |
| 157 | + x: d["x"] ? TopLevel1.from_dynamic!(d["x"]) : nil, | |
| 183 | 158 | ) |
| 184 | 159 | end |
| 185 | 160 | |
| @@ -198,65 +173,6 @@ class TopLevelClass < Dry::Struct | ||
| 198 | 173 | end |
| 199 | 174 | end |
| 200 | 175 | |
| 201 | -class TopLevelElement < Dry::Struct | |
| 202 | - attribute :anything_array, Types.Array(Types::Any).optional | |
| 203 | - attribute :bool, Types::Bool.optional | |
| 204 | - attribute :double, Types::Double.optional | |
| 205 | - attribute :null, Types::Nil.optional | |
| 206 | - attribute :string, Types::String.optional | |
| 207 | - attribute :top_level_class, TopLevelClass.optional | |
| 208 | - | |
| 209 | - def self.from_dynamic!(d) | |
| 210 | - if schema[:anything_array].right.valid? d | |
| 211 | - return new(anything_array: d, bool: nil, top_level_class: nil, double: nil, null: nil, string: nil) | |
| 212 | - end | |
| 213 | - if schema[:bool].right.valid? d | |
| 214 | - return new(bool: d, anything_array: nil, top_level_class: nil, double: nil, null: nil, string: nil) | |
| 215 | - end | |
| 216 | - if schema[:double].right.valid? d | |
| 217 | - return new(double: d, anything_array: nil, bool: nil, top_level_class: nil, null: nil, string: nil) | |
| 218 | - end | |
| 219 | - if schema[:null].right.valid? d | |
| 220 | - return new(null: d, anything_array: nil, bool: nil, top_level_class: nil, double: nil, string: nil) | |
| 221 | - end | |
| 222 | - if schema[:string].right.valid? d | |
| 223 | - return new(string: d, anything_array: nil, bool: nil, top_level_class: nil, double: nil, null: nil) | |
| 224 | - end | |
| 225 | - begin | |
| 226 | - value = TopLevelClass.from_dynamic!(d) | |
| 227 | - if schema[:top_level_class].right.valid? value | |
| 228 | - return new(top_level_class: value, anything_array: nil, bool: nil, double: nil, null: nil, string: nil) | |
| 229 | - end | |
| 230 | - rescue | |
| 231 | - end | |
| 232 | - raise "Invalid union" | |
| 233 | - end | |
| 234 | - | |
| 235 | - def self.from_json!(json) | |
| 236 | - from_dynamic!(JSON.parse(json)) | |
| 237 | - end | |
| 238 | - | |
| 239 | - def to_dynamic | |
| 240 | - if anything_array != nil | |
| 241 | - anything_array | |
| 242 | - elsif bool != nil | |
| 243 | - bool | |
| 244 | - elsif double != nil | |
| 245 | - double | |
| 246 | - elsif string != nil | |
| 247 | - string | |
| 248 | - elsif top_level_class != nil | |
| 249 | - top_level_class.to_dynamic | |
| 250 | - else | |
| 251 | - nil | |
| 252 | - end | |
| 253 | - end | |
| 254 | - | |
| 255 | - def to_json(options = nil) | |
| 256 | - JSON.generate(to_dynamic, options) | |
| 257 | - end | |
| 258 | -end | |
| 259 | - | |
| 260 | 176 | class TopLevel < Dry::Struct |
| 261 | 177 | attribute :anything_map, Types::Hash.meta(of: Types::Any).optional |
| 262 | 178 | attribute :bool, Types::Bool.optional |
Mschema-rustdefault / module_under_test.rs+8 −27
| @@ -32,8 +32,8 @@ pub enum TopLevel { | ||
| 32 | 32 | |
| 33 | 33 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 34 | 34 | #[serde(untagged)] |
| 35 | -pub enum TopLevelElement { | |
| 36 | - AnythingArray(Vec<Option<serde_json::Value>>), | |
| 35 | +pub enum PurpleTopLevel { | |
| 36 | + AnythingMap(HashMap<String, Option<serde_json::Value>>), | |
| 37 | 37 | |
| 38 | 38 | Bool(bool), |
| 39 | 39 | |
| @@ -41,43 +41,24 @@ pub enum TopLevelElement { | ||
| 41 | 41 | |
| 42 | 42 | PurpleString(String), |
| 43 | 43 | |
| 44 | - TopLevelClass(TopLevelClass), | |
| 45 | -} | |
| 46 | - | |
| 47 | -#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 48 | -pub struct TopLevelClass { | |
| 49 | - pub x: Option<X>, | |
| 44 | + UnionArray(Vec<Option<TopLevelElement>>), | |
| 50 | 45 | } |
| 51 | 46 | |
| 52 | 47 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 53 | -pub struct XClass { | |
| 54 | - pub x: Option<X>, | |
| 48 | +pub struct A { | |
| 49 | + pub x: Option<PurpleTopLevel>, | |
| 55 | 50 | } |
| 56 | 51 | |
| 57 | 52 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 58 | 53 | #[serde(untagged)] |
| 59 | -pub enum XElement { | |
| 60 | - AnythingArray(Vec<Option<serde_json::Value>>), | |
| 61 | - | |
| 62 | - Bool(bool), | |
| 63 | - | |
| 64 | - Double(f64), | |
| 65 | - | |
| 66 | - PurpleString(String), | |
| 67 | - | |
| 68 | - XClass(XClass), | |
| 69 | -} | |
| 54 | +pub enum TopLevelElement { | |
| 55 | + A(A), | |
| 70 | 56 | |
| 71 | -#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 72 | -#[serde(untagged)] | |
| 73 | -pub enum X { | |
| 74 | - AnythingMap(HashMap<String, Option<serde_json::Value>>), | |
| 57 | + AnythingArray(Vec<Option<serde_json::Value>>), | |
| 75 | 58 | |
| 76 | 59 | Bool(bool), |
| 77 | 60 | |
| 78 | 61 | Double(f64), |
| 79 | 62 | |
| 80 | 63 | PurpleString(String), |
| 81 | - | |
| 82 | - UnionArray(Vec<Option<XElement>>), | |
| 83 | 64 | } |
Mschema-scala3-upickledefault / TopLevel.scala+8 −57
| @@ -30,7 +30,12 @@ given Encoder[TopLevel] = Encoder.instance { | ||
| 30 | 30 | case enc6 : NullValue => Encoder.encodeNone(enc6) |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | -type TopLevelElement = Seq[Option[Json]] | Boolean | Double | Long | String | TopLevelClass | NullValue | |
| 33 | +type PurpleTopLevel = Map[String, Option[Json]] | Boolean | Double | Long | String | Seq[TopLevelElement] | NullValue | |
| 34 | +case class A ( | |
| 35 | + val x : Option[PurpleTopLevel] = None | |
| 36 | +) derives Encoder.AsObject, Decoder | |
| 37 | + | |
| 38 | +type TopLevelElement = A | Seq[Option[Json]] | Boolean | Double | Long | String | NullValue | |
| 34 | 39 | given Decoder[TopLevelElement] = { |
| 35 | 40 | List[Decoder[TopLevelElement]]( |
| 36 | 41 | Decoder[String].widen, |
| @@ -38,7 +43,7 @@ given Decoder[TopLevelElement] = { | ||
| 38 | 43 | Decoder[Long].widen, |
| 39 | 44 | Decoder[Double].widen, |
| 40 | 45 | Decoder[Seq[Option[Json]]].widen, |
| 41 | - Decoder[TopLevelClass].widen, | |
| 46 | + Decoder[A].widen, | |
| 42 | 47 | Decoder[NullValue].widen, |
| 43 | 48 | ).reduceLeft(_ or _) |
| 44 | 49 | } |
| @@ -49,60 +54,6 @@ given Encoder[TopLevelElement] = Encoder.instance { | ||
| 49 | 54 | case enc2 : Long => Encoder.encodeLong(enc2) |
| 50 | 55 | case enc3 : Double => Encoder.encodeDouble(enc3) |
| 51 | 56 | case enc4 : Seq[Option[Json]] => Encoder.encodeSeq[Option[Json]].apply(enc4) |
| 52 | - case enc5 : TopLevelClass => Encoder.AsObject[TopLevelClass].apply(enc5) | |
| 53 | - case enc6 : NullValue => Encoder.encodeNone(enc6) | |
| 54 | -} | |
| 55 | - | |
| 56 | -case class TopLevelClass ( | |
| 57 | - val x : Option[X] = None | |
| 58 | -) derives Encoder.AsObject, Decoder | |
| 59 | - | |
| 60 | -case class XClass ( | |
| 61 | - val x : Option[X] = None | |
| 62 | -) derives Encoder.AsObject, Decoder | |
| 63 | - | |
| 64 | -type XElement = Seq[Option[Json]] | Boolean | Double | Long | String | XClass | NullValue | |
| 65 | -given Decoder[XElement] = { | |
| 66 | - List[Decoder[XElement]]( | |
| 67 | - Decoder[String].widen, | |
| 68 | - Decoder[Boolean].widen, | |
| 69 | - Decoder[Long].widen, | |
| 70 | - Decoder[Double].widen, | |
| 71 | - Decoder[Seq[Option[Json]]].widen, | |
| 72 | - Decoder[XClass].widen, | |
| 73 | - Decoder[NullValue].widen, | |
| 74 | - ).reduceLeft(_ or _) | |
| 75 | -} | |
| 76 | - | |
| 77 | -given Encoder[XElement] = Encoder.instance { | |
| 78 | - case enc0 : String => Encoder.encodeString(enc0) | |
| 79 | - case enc1 : Boolean => Encoder.encodeBoolean(enc1) | |
| 80 | - case enc2 : Long => Encoder.encodeLong(enc2) | |
| 81 | - case enc3 : Double => Encoder.encodeDouble(enc3) | |
| 82 | - case enc4 : Seq[Option[Json]] => Encoder.encodeSeq[Option[Json]].apply(enc4) | |
| 83 | - case enc5 : XClass => Encoder.AsObject[XClass].apply(enc5) | |
| 84 | - case enc6 : NullValue => Encoder.encodeNone(enc6) | |
| 85 | -} | |
| 86 | - | |
| 87 | -type X = Map[String, Option[Json]] | Boolean | Double | Long | String | Seq[XElement] | NullValue | |
| 88 | -given Decoder[X] = { | |
| 89 | - List[Decoder[X]]( | |
| 90 | - Decoder[String].widen, | |
| 91 | - Decoder[Boolean].widen, | |
| 92 | - Decoder[Long].widen, | |
| 93 | - Decoder[Double].widen, | |
| 94 | - Decoder[Seq[XElement]].widen, | |
| 95 | - Decoder[Map[String, Option[Json]]].widen, | |
| 96 | - Decoder[NullValue].widen, | |
| 97 | - ).reduceLeft(_ or _) | |
| 98 | -} | |
| 99 | - | |
| 100 | -given Encoder[X] = Encoder.instance { | |
| 101 | - case enc0 : String => Encoder.encodeString(enc0) | |
| 102 | - case enc1 : Boolean => Encoder.encodeBoolean(enc1) | |
| 103 | - case enc2 : Long => Encoder.encodeLong(enc2) | |
| 104 | - case enc3 : Double => Encoder.encodeDouble(enc3) | |
| 105 | - case enc4 : Seq[XElement] => Encoder.encodeSeq[XElement].apply(enc4) | |
| 106 | - case enc5 : Map[String, Option[Json]] => Encoder.encodeMap[String,Option[Json]].apply(enc5) | |
| 57 | + case enc5 : A => Encoder.AsObject[A].apply(enc5) | |
| 107 | 58 | case enc6 : NullValue => Encoder.encodeNone(enc6) |
| 108 | 59 | } |
Mschema-scala3default / TopLevel.scala+8 −55
| @@ -87,76 +87,29 @@ given unionWriterTopLevel: OptionPickler.Writer[TopLevel] = OptionPickler.writer | ||
| 87 | 87 | case v: NullValue => OptionPickler.writeJs[NullValue](v) |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | -type TopLevelElement = Seq[Option[ujson.Value]] | Boolean | Double | Long | String | TopLevelClass | NullValue | |
| 91 | -given unionReaderTopLevelElement: OptionPickler.Reader[TopLevelElement] = JsonExt.badMerge[TopLevelElement]( | |
| 92 | - JsonExt.strictString, | |
| 93 | - JsonExt.strictBoolean, | |
| 94 | - JsonExt.strictLong, | |
| 95 | - JsonExt.strictDouble, | |
| 96 | - summon[OptionPickler.Reader[Seq[Option[ujson.Value]]]], | |
| 97 | - summon[OptionPickler.Reader[TopLevelClass]], | |
| 98 | - summon[OptionPickler.Reader[NullValue]], | |
| 99 | - ) | |
| 100 | - | |
| 101 | -given unionWriterTopLevelElement: OptionPickler.Writer[TopLevelElement] = OptionPickler.writer[ujson.Value].comap[TopLevelElement]{ _v => | |
| 102 | - (_v: @unchecked) match | |
| 103 | - case v: String => OptionPickler.writeJs[String](v) | |
| 104 | - case v: Boolean => OptionPickler.writeJs[Boolean](v) | |
| 105 | - case v: Long => OptionPickler.writeJs[Long](v) | |
| 106 | - case v: Double => OptionPickler.writeJs[Double](v) | |
| 107 | - case v: Seq[Option[ujson.Value]] => OptionPickler.writeJs[Seq[Option[ujson.Value]]](v) | |
| 108 | - case v: TopLevelClass => OptionPickler.writeJs[TopLevelClass](v) | |
| 109 | - case v: NullValue => OptionPickler.writeJs[NullValue](v) | |
| 110 | -} | |
| 111 | - | |
| 112 | -case class TopLevelClass ( | |
| 113 | - val x : Option[X] = None | |
| 114 | -) derives OptionPickler.ReadWriter | |
| 115 | - | |
| 116 | -case class XClass ( | |
| 117 | - val x : Option[X] = None | |
| 90 | +type PurpleTopLevel = Map[String, Option[ujson.Value]] | Boolean | Double | Long | String | Seq[TopLevelElement] | NullValue | |
| 91 | +case class A ( | |
| 92 | + val x : Option[PurpleTopLevel] = None | |
| 118 | 93 | ) derives OptionPickler.ReadWriter |
| 119 | 94 | |
| 120 | -type XElement = Seq[Option[ujson.Value]] | Boolean | Double | Long | String | XClass | NullValue | |
| 121 | -given unionReaderXElement: OptionPickler.Reader[XElement] = JsonExt.badMerge[XElement]( | |
| 95 | +type TopLevelElement = A | Seq[Option[ujson.Value]] | Boolean | Double | Long | String | NullValue | |
| 96 | +given unionReaderTopLevelElement: OptionPickler.Reader[TopLevelElement] = JsonExt.badMerge[TopLevelElement]( | |
| 122 | 97 | JsonExt.strictString, |
| 123 | 98 | JsonExt.strictBoolean, |
| 124 | 99 | JsonExt.strictLong, |
| 125 | 100 | JsonExt.strictDouble, |
| 126 | 101 | summon[OptionPickler.Reader[Seq[Option[ujson.Value]]]], |
| 127 | - summon[OptionPickler.Reader[XClass]], | |
| 102 | + summon[OptionPickler.Reader[A]], | |
| 128 | 103 | summon[OptionPickler.Reader[NullValue]], |
| 129 | 104 | ) |
| 130 | 105 | |
| 131 | -given unionWriterXElement: OptionPickler.Writer[XElement] = OptionPickler.writer[ujson.Value].comap[XElement]{ _v => | |
| 106 | +given unionWriterTopLevelElement: OptionPickler.Writer[TopLevelElement] = OptionPickler.writer[ujson.Value].comap[TopLevelElement]{ _v => | |
| 132 | 107 | (_v: @unchecked) match |
| 133 | 108 | case v: String => OptionPickler.writeJs[String](v) |
| 134 | 109 | case v: Boolean => OptionPickler.writeJs[Boolean](v) |
| 135 | 110 | case v: Long => OptionPickler.writeJs[Long](v) |
| 136 | 111 | case v: Double => OptionPickler.writeJs[Double](v) |
| 137 | 112 | case v: Seq[Option[ujson.Value]] => OptionPickler.writeJs[Seq[Option[ujson.Value]]](v) |
| 138 | - case v: XClass => OptionPickler.writeJs[XClass](v) | |
| 139 | - case v: NullValue => OptionPickler.writeJs[NullValue](v) | |
| 140 | -} | |
| 141 | - | |
| 142 | -type X = Map[String, Option[ujson.Value]] | Boolean | Double | Long | String | Seq[XElement] | NullValue | |
| 143 | -given unionReaderX: OptionPickler.Reader[X] = JsonExt.badMerge[X]( | |
| 144 | - JsonExt.strictString, | |
| 145 | - JsonExt.strictBoolean, | |
| 146 | - JsonExt.strictLong, | |
| 147 | - JsonExt.strictDouble, | |
| 148 | - summon[OptionPickler.Reader[Seq[XElement]]], | |
| 149 | - summon[OptionPickler.Reader[Map[String, Option[ujson.Value]]]], | |
| 150 | - summon[OptionPickler.Reader[NullValue]], | |
| 151 | - ) | |
| 152 | - | |
| 153 | -given unionWriterX: OptionPickler.Writer[X] = OptionPickler.writer[ujson.Value].comap[X]{ _v => | |
| 154 | - (_v: @unchecked) match | |
| 155 | - case v: String => OptionPickler.writeJs[String](v) | |
| 156 | - case v: Boolean => OptionPickler.writeJs[Boolean](v) | |
| 157 | - case v: Long => OptionPickler.writeJs[Long](v) | |
| 158 | - case v: Double => OptionPickler.writeJs[Double](v) | |
| 159 | - case v: Seq[XElement] => OptionPickler.writeJs[Seq[XElement]](v) | |
| 160 | - case v: Map[String, Option[ujson.Value]] => OptionPickler.writeJs[Map[String, Option[ujson.Value]]](v) | |
| 113 | + case v: A => OptionPickler.writeJs[A](v) | |
| 161 | 114 | case v: NullValue => OptionPickler.writeJs[NullValue](v) |
| 162 | 115 | } |
Mschema-schemadefault / TopLevel.schema+14 −49
| @@ -2,27 +2,16 @@ | ||
| 2 | 2 | "$schema": "http://json-schema.org/draft-06/schema#", |
| 3 | 3 | "$ref": "#/definitions/TopLevel", |
| 4 | 4 | "definitions": { |
| 5 | - "TopLevelObject": { | |
| 5 | + "A": { | |
| 6 | 6 | "type": "object", |
| 7 | 7 | "additionalProperties": {}, |
| 8 | 8 | "properties": { |
| 9 | 9 | "x": { |
| 10 | - "$ref": "#/definitions/X" | |
| 10 | + "$ref": "#/definitions/PurpleTopLevel" | |
| 11 | 11 | } |
| 12 | 12 | }, |
| 13 | 13 | "required": [], |
| 14 | - "title": "TopLevelObject" | |
| 15 | - }, | |
| 16 | - "XObject": { | |
| 17 | - "type": "object", | |
| 18 | - "additionalProperties": {}, | |
| 19 | - "properties": { | |
| 20 | - "x": { | |
| 21 | - "$ref": "#/definitions/X" | |
| 22 | - } | |
| 23 | - }, | |
| 24 | - "required": [], | |
| 25 | - "title": "XObject" | |
| 14 | + "title": "A" | |
| 26 | 15 | }, |
| 27 | 16 | "TopLevel": { |
| 28 | 17 | "anyOf": [ |
| @@ -54,11 +43,13 @@ | ||
| 54 | 43 | ], |
| 55 | 44 | "title": "TopLevel" |
| 56 | 45 | }, |
| 57 | - "TopLevelElement": { | |
| 46 | + "PurpleTopLevel": { | |
| 58 | 47 | "anyOf": [ |
| 59 | 48 | { |
| 60 | 49 | "type": "array", |
| 61 | - "items": {} | |
| 50 | + "items": { | |
| 51 | + "$ref": "#/definitions/TopLevelElement" | |
| 52 | + } | |
| 62 | 53 | }, |
| 63 | 54 | { |
| 64 | 55 | "type": "boolean" |
| @@ -67,18 +58,19 @@ | ||
| 67 | 58 | "type": "number" |
| 68 | 59 | }, |
| 69 | 60 | { |
| 70 | - "type": "null" | |
| 61 | + "type": "object", | |
| 62 | + "additionalProperties": {} | |
| 71 | 63 | }, |
| 72 | 64 | { |
| 73 | - "$ref": "#/definitions/TopLevelObject" | |
| 65 | + "type": "null" | |
| 74 | 66 | }, |
| 75 | 67 | { |
| 76 | 68 | "type": "string" |
| 77 | 69 | } |
| 78 | 70 | ], |
| 79 | - "title": "TopLevelElement" | |
| 71 | + "title": "PurpleTopLevel" | |
| 80 | 72 | }, |
| 81 | - "XElement": { | |
| 73 | + "TopLevelElement": { | |
| 82 | 74 | "anyOf": [ |
| 83 | 75 | { |
| 84 | 76 | "type": "array", |
| @@ -94,40 +86,13 @@ | ||
| 94 | 86 | "type": "null" |
| 95 | 87 | }, |
| 96 | 88 | { |
| 97 | - "$ref": "#/definitions/XObject" | |
| 98 | - }, | |
| 99 | - { | |
| 100 | - "type": "string" | |
| 101 | - } | |
| 102 | - ], | |
| 103 | - "title": "XElement" | |
| 104 | - }, | |
| 105 | - "X": { | |
| 106 | - "anyOf": [ | |
| 107 | - { | |
| 108 | - "type": "array", | |
| 109 | - "items": { | |
| 110 | - "$ref": "#/definitions/XElement" | |
| 111 | - } | |
| 112 | - }, | |
| 113 | - { | |
| 114 | - "type": "boolean" | |
| 115 | - }, | |
| 116 | - { | |
| 117 | - "type": "number" | |
| 118 | - }, | |
| 119 | - { | |
| 120 | - "type": "object", | |
| 121 | - "additionalProperties": {} | |
| 122 | - }, | |
| 123 | - { | |
| 124 | - "type": "null" | |
| 89 | + "$ref": "#/definitions/A" | |
| 125 | 90 | }, |
| 126 | 91 | { |
| 127 | 92 | "type": "string" |
| 128 | 93 | } |
| 129 | 94 | ], |
| 130 | - "title": "X" | |
| 95 | + "title": "TopLevelElement" | |
| 131 | 96 | } |
| 132 | 97 | } |
| 133 | 98 | } |
Mschema-swiftdefault / quicktype.swift+28 −135
| @@ -68,13 +68,13 @@ enum TopLevel: Codable { | ||
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | -enum TopLevelElement: Codable { | |
| 72 | - case anythingArray([JSONAny]) | |
| 71 | +enum PurpleTopLevel: Codable { | |
| 72 | + case anythingMap([String: JSONAny]) | |
| 73 | 73 | case bool(Bool) |
| 74 | 74 | case double(Double) |
| 75 | 75 | case integer(Int) |
| 76 | 76 | case string(String) |
| 77 | - case topLevelClass(TopLevelClass) | |
| 77 | + case unionArray([TopLevelElement]) | |
| 78 | 78 | case null |
| 79 | 79 | |
| 80 | 80 | init(from decoder: Decoder) throws { |
| @@ -87,33 +87,33 @@ enum TopLevelElement: Codable { | ||
| 87 | 87 | self = .integer(x) |
| 88 | 88 | return |
| 89 | 89 | } |
| 90 | - if let x = try? container.decode([JSONAny].self) { | |
| 91 | - self = .anythingArray(x) | |
| 90 | + if let x = try? container.decode([TopLevelElement].self) { | |
| 91 | + self = .unionArray(x) | |
| 92 | 92 | return |
| 93 | 93 | } |
| 94 | 94 | if let x = try? container.decode(Double.self) { |
| 95 | 95 | self = .double(x) |
| 96 | 96 | return |
| 97 | 97 | } |
| 98 | - if let x = try? container.decode(String.self) { | |
| 99 | - self = .string(x) | |
| 98 | + if let x = try? container.decode([String: JSONAny].self) { | |
| 99 | + self = .anythingMap(x) | |
| 100 | 100 | return |
| 101 | 101 | } |
| 102 | - if let x = try? container.decode(TopLevelClass.self) { | |
| 103 | - self = .topLevelClass(x) | |
| 102 | + if let x = try? container.decode(String.self) { | |
| 103 | + self = .string(x) | |
| 104 | 104 | return |
| 105 | 105 | } |
| 106 | 106 | if container.decodeNil() { |
| 107 | 107 | self = .null |
| 108 | 108 | return |
| 109 | 109 | } |
| 110 | - throw DecodingError.typeMismatch(TopLevelElement.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for TopLevelElement")) | |
| 110 | + throw DecodingError.typeMismatch(PurpleTopLevel.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for PurpleTopLevel")) | |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | func encode(to encoder: Encoder) throws { |
| 114 | 114 | var container = encoder.singleValueContainer() |
| 115 | 115 | switch self { |
| 116 | - case .anythingArray(let x): | |
| 116 | + case .anythingMap(let x): | |
| 117 | 117 | try container.encode(x) |
| 118 | 118 | case .bool(let x): |
| 119 | 119 | try container.encode(x) |
| @@ -123,7 +123,7 @@ enum TopLevelElement: Codable { | ||
| 123 | 123 | try container.encode(x) |
| 124 | 124 | case .string(let x): |
| 125 | 125 | try container.encode(x) |
| 126 | - case .topLevelClass(let x): | |
| 126 | + case .unionArray(let x): | |
| 127 | 127 | try container.encode(x) |
| 128 | 128 | case .null: |
| 129 | 129 | try container.encodeNil() |
| @@ -131,64 +131,20 @@ enum TopLevelElement: Codable { | ||
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | -// MARK: - TopLevelClass | |
| 135 | -struct TopLevelClass: Codable { | |
| 136 | - let x: X? | |
| 137 | - | |
| 138 | - enum CodingKeys: String, CodingKey { | |
| 139 | - case x = "x" | |
| 140 | - } | |
| 141 | -} | |
| 142 | - | |
| 143 | -// MARK: TopLevelClass convenience initializers and mutators | |
| 144 | - | |
| 145 | -extension TopLevelClass { | |
| 146 | - init(data: Data) throws { | |
| 147 | - self = try newJSONDecoder().decode(TopLevelClass.self, from: data) | |
| 148 | - } | |
| 149 | - | |
| 150 | - init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 151 | - guard let data = json.data(using: encoding) else { | |
| 152 | - throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 153 | - } | |
| 154 | - try self.init(data: data) | |
| 155 | - } | |
| 156 | - | |
| 157 | - init(fromURL url: URL) throws { | |
| 158 | - try self.init(data: try Data(contentsOf: url)) | |
| 159 | - } | |
| 160 | - | |
| 161 | - func with( | |
| 162 | - x: X?? = nil | |
| 163 | - ) -> TopLevelClass { | |
| 164 | - return TopLevelClass( | |
| 165 | - x: x ?? self.x | |
| 166 | - ) | |
| 167 | - } | |
| 168 | - | |
| 169 | - func jsonData() throws -> Data { | |
| 170 | - return try newJSONEncoder().encode(self) | |
| 171 | - } | |
| 172 | - | |
| 173 | - func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 174 | - return String(data: try self.jsonData(), encoding: encoding) | |
| 175 | - } | |
| 176 | -} | |
| 177 | - | |
| 178 | -// MARK: - XClass | |
| 179 | -struct XClass: Codable { | |
| 180 | - let x: X? | |
| 134 | +// MARK: - A | |
| 135 | +struct A: Codable { | |
| 136 | + let x: PurpleTopLevel? | |
| 181 | 137 | |
| 182 | 138 | enum CodingKeys: String, CodingKey { |
| 183 | 139 | case x = "x" |
| 184 | 140 | } |
| 185 | 141 | } |
| 186 | 142 | |
| 187 | -// MARK: XClass convenience initializers and mutators | |
| 143 | +// MARK: A convenience initializers and mutators | |
| 188 | 144 | |
| 189 | -extension XClass { | |
| 145 | +extension A { | |
| 190 | 146 | init(data: Data) throws { |
| 191 | - self = try newJSONDecoder().decode(XClass.self, from: data) | |
| 147 | + self = try newJSONDecoder().decode(A.self, from: data) | |
| 192 | 148 | } |
| 193 | 149 | |
| 194 | 150 | init(_ json: String, using encoding: String.Encoding = .utf8) throws { |
| @@ -203,9 +159,9 @@ extension XClass { | ||
| 203 | 159 | } |
| 204 | 160 | |
| 205 | 161 | func with( |
| 206 | - x: X?? = nil | |
| 207 | - ) -> XClass { | |
| 208 | - return XClass( | |
| 162 | + x: PurpleTopLevel?? = nil | |
| 163 | + ) -> A { | |
| 164 | + return A( | |
| 209 | 165 | x: x ?? self.x |
| 210 | 166 | ) |
| 211 | 167 | } |
| @@ -219,13 +175,13 @@ extension XClass { | ||
| 219 | 175 | } |
| 220 | 176 | } |
| 221 | 177 | |
| 222 | -enum XElement: Codable { | |
| 178 | +enum TopLevelElement: Codable { | |
| 179 | + case a(A) | |
| 223 | 180 | case anythingArray([JSONAny]) |
| 224 | 181 | case bool(Bool) |
| 225 | 182 | case double(Double) |
| 226 | 183 | case integer(Int) |
| 227 | 184 | case string(String) |
| 228 | - case xClass(XClass) | |
| 229 | 185 | case null |
| 230 | 186 | |
| 231 | 187 | init(from decoder: Decoder) throws { |
| @@ -250,84 +206,23 @@ enum XElement: Codable { | ||
| 250 | 206 | self = .string(x) |
| 251 | 207 | return |
| 252 | 208 | } |
| 253 | - if let x = try? container.decode(XClass.self) { | |
| 254 | - self = .xClass(x) | |
| 209 | + if let x = try? container.decode(A.self) { | |
| 210 | + self = .a(x) | |
| 255 | 211 | return |
| 256 | 212 | } |
| 257 | 213 | if container.decodeNil() { |
| 258 | 214 | self = .null |
| 259 | 215 | return |
| 260 | 216 | } |
| 261 | - throw DecodingError.typeMismatch(XElement.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for XElement")) | |
| 217 | + throw DecodingError.typeMismatch(TopLevelElement.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for TopLevelElement")) | |
| 262 | 218 | } |
| 263 | 219 | |
| 264 | 220 | func encode(to encoder: Encoder) throws { |
| 265 | 221 | var container = encoder.singleValueContainer() |
| 266 | 222 | switch self { |
| 267 | - case .anythingArray(let x): | |
| 268 | - try container.encode(x) | |
| 269 | - case .bool(let x): | |
| 223 | + case .a(let x): | |
| 270 | 224 | try container.encode(x) |
| 271 | - case .double(let x): | |
| 272 | - try container.encode(x) | |
| 273 | - case .integer(let x): | |
| 274 | - try container.encode(x) | |
| 275 | - case .string(let x): | |
| 276 | - try container.encode(x) | |
| 277 | - case .xClass(let x): | |
| 278 | - try container.encode(x) | |
| 279 | - case .null: | |
| 280 | - try container.encodeNil() | |
| 281 | - } | |
| 282 | - } | |
| 283 | -} | |
| 284 | - | |
| 285 | -enum X: Codable { | |
| 286 | - case anythingMap([String: JSONAny]) | |
| 287 | - case bool(Bool) | |
| 288 | - case double(Double) | |
| 289 | - case integer(Int) | |
| 290 | - case string(String) | |
| 291 | - case unionArray([XElement]) | |
| 292 | - case null | |
| 293 | - | |
| 294 | - init(from decoder: Decoder) throws { | |
| 295 | - let container = try decoder.singleValueContainer() | |
| 296 | - if let x = try? container.decode(Bool.self) { | |
| 297 | - self = .bool(x) | |
| 298 | - return | |
| 299 | - } | |
| 300 | - if let x = try? container.decode(Int.self) { | |
| 301 | - self = .integer(x) | |
| 302 | - return | |
| 303 | - } | |
| 304 | - if let x = try? container.decode([XElement].self) { | |
| 305 | - self = .unionArray(x) | |
| 306 | - return | |
| 307 | - } | |
| 308 | - if let x = try? container.decode(Double.self) { | |
| 309 | - self = .double(x) | |
| 310 | - return | |
| 311 | - } | |
| 312 | - if let x = try? container.decode([String: JSONAny].self) { | |
| 313 | - self = .anythingMap(x) | |
| 314 | - return | |
| 315 | - } | |
| 316 | - if let x = try? container.decode(String.self) { | |
| 317 | - self = .string(x) | |
| 318 | - return | |
| 319 | - } | |
| 320 | - if container.decodeNil() { | |
| 321 | - self = .null | |
| 322 | - return | |
| 323 | - } | |
| 324 | - throw DecodingError.typeMismatch(X.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for X")) | |
| 325 | - } | |
| 326 | - | |
| 327 | - func encode(to encoder: Encoder) throws { | |
| 328 | - var container = encoder.singleValueContainer() | |
| 329 | - switch self { | |
| 330 | - case .anythingMap(let x): | |
| 225 | + case .anythingArray(let x): | |
| 331 | 226 | try container.encode(x) |
| 332 | 227 | case .bool(let x): |
| 333 | 228 | try container.encode(x) |
| @@ -337,8 +232,6 @@ enum X: Codable { | ||
| 337 | 232 | try container.encode(x) |
| 338 | 233 | case .string(let x): |
| 339 | 234 | try container.encode(x) |
| 340 | - case .unionArray(let x): | |
| 341 | - try container.encode(x) | |
| 342 | 235 | case .null: |
| 343 | 236 | try container.encodeNil() |
| 344 | 237 | } |
Mschema-typescriptdefault / TopLevel.ts+18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +import * as z from "zod"; | |
| 2 | + | |
| 3 | + | |
| 4 | +export type TopLevel = { | |
| 5 | + "children"?: Array<TopLevel>; | |
| 6 | + "data"?: Data; | |
| 7 | +}; | |
| 8 | +export const TopLevelSchema: z.ZodType<TopLevel> = z.lazy(() => | |
| 9 | + z.object({ | |
| 10 | + "children": z.array(TopLevelSchema).optional(), | |
| 11 | + "data": DataSchema.optional(), | |
| 12 | + }) | |
| 13 | +); | |
| 14 | + | |
| 15 | +export const DataSchema = z.object({ | |
| 16 | + "id": z.string().optional(), | |
| 17 | +}); | |
| 18 | +export type Data = z.infer<typeof DataSchema>; |
Test case
28 generated files · +221 −784test/inputs/schema/rust-cycle-breaker-union.schema
Mschema-cplusplusdefault / quicktype.hpp+10 −42
| @@ -89,27 +89,9 @@ namespace quicktype { | ||
| 89 | 89 | } |
| 90 | 90 | #endif |
| 91 | 91 | |
| 92 | - class Node; | |
| 92 | + class TopLevel; | |
| 93 | 93 | |
| 94 | - using Next = std::shared_ptr<std::variant<std::shared_ptr<Node>, std::string>>; | |
| 95 | - | |
| 96 | - class Node { | |
| 97 | - public: | |
| 98 | - Node() = default; | |
| 99 | - virtual ~Node() = default; | |
| 100 | - | |
| 101 | - private: | |
| 102 | - Next next; | |
| 103 | - std::string value; | |
| 104 | - | |
| 105 | - public: | |
| 106 | - Next get_next() const { return next; } | |
| 107 | - void set_next(Next value) { this->next = value; } | |
| 108 | - | |
| 109 | - const std::string & get_value() const { return value; } | |
| 110 | - std::string & get_mutable_value() { return value; } | |
| 111 | - void set_value(const std::string & value) { this->value = value; } | |
| 112 | - }; | |
| 94 | + using Next = std::optional<std::variant<std::shared_ptr<TopLevel>, std::string>>; | |
| 113 | 95 | |
| 114 | 96 | class TopLevel { |
| 115 | 97 | public: |
| @@ -131,33 +113,19 @@ namespace quicktype { | ||
| 131 | 113 | } |
| 132 | 114 | |
| 133 | 115 | namespace quicktype { |
| 134 | -void from_json(const json & j, Node & x); | |
| 135 | -void to_json(json & j, const Node & x); | |
| 136 | - | |
| 137 | 116 | void from_json(const json & j, TopLevel & x); |
| 138 | 117 | void to_json(json & j, const TopLevel & x); |
| 139 | 118 | } |
| 140 | 119 | namespace nlohmann { |
| 141 | 120 | template <> |
| 142 | -struct adl_serializer<std::variant<std::shared_ptr<quicktype::Node>, std::string>> { | |
| 143 | - static void from_json(const json & j, std::variant<std::shared_ptr<quicktype::Node>, std::string> & x); | |
| 144 | - static void to_json(json & j, const std::variant<std::shared_ptr<quicktype::Node>, std::string> & x); | |
| 121 | +struct adl_serializer<std::variant<std::shared_ptr<quicktype::TopLevel>, std::string>> { | |
| 122 | + static void from_json(const json & j, std::variant<std::shared_ptr<quicktype::TopLevel>, std::string> & x); | |
| 123 | + static void to_json(json & j, const std::variant<std::shared_ptr<quicktype::TopLevel>, std::string> & x); | |
| 145 | 124 | }; |
| 146 | 125 | } |
| 147 | 126 | namespace quicktype { |
| 148 | - inline void from_json(const json & j, Node& x) { | |
| 149 | - x.set_next(get_heap_optional<std::variant<std::shared_ptr<Node>, std::string>>(j, "next")); | |
| 150 | - x.set_value(j.at("value").get<std::string>()); | |
| 151 | - } | |
| 152 | - | |
| 153 | - inline void to_json(json & j, const Node & x) { | |
| 154 | - j = json::object(); | |
| 155 | - j["next"] = x.get_next(); | |
| 156 | - j["value"] = x.get_value(); | |
| 157 | - } | |
| 158 | - | |
| 159 | 127 | inline void from_json(const json & j, TopLevel& x) { |
| 160 | - x.set_next(get_heap_optional<std::variant<std::shared_ptr<Node>, std::string>>(j, "next")); | |
| 128 | + x.set_next(get_stack_optional<std::variant<std::shared_ptr<TopLevel>, std::string>>(j, "next")); | |
| 161 | 129 | x.set_value(j.at("value").get<std::string>()); |
| 162 | 130 | } |
| 163 | 131 | |
| @@ -168,18 +136,18 @@ namespace quicktype { | ||
| 168 | 136 | } |
| 169 | 137 | } |
| 170 | 138 | namespace nlohmann { |
| 171 | - inline void adl_serializer<std::variant<std::shared_ptr<quicktype::Node>, std::string>>::from_json(const json & j, std::variant<std::shared_ptr<quicktype::Node>, std::string> & x) { | |
| 139 | + inline void adl_serializer<std::variant<std::shared_ptr<quicktype::TopLevel>, std::string>>::from_json(const json & j, std::variant<std::shared_ptr<quicktype::TopLevel>, std::string> & x) { | |
| 172 | 140 | if (j.is_string()) |
| 173 | 141 | x = j.get<std::string>(); |
| 174 | 142 | else if (j.is_object()) |
| 175 | - x = j.get<std::shared_ptr<quicktype::Node>>(); | |
| 143 | + x = j.get<std::shared_ptr<quicktype::TopLevel>>(); | |
| 176 | 144 | else throw std::runtime_error("Could not deserialise!"); |
| 177 | 145 | } |
| 178 | 146 | |
| 179 | - inline void adl_serializer<std::variant<std::shared_ptr<quicktype::Node>, std::string>>::to_json(json & j, const std::variant<std::shared_ptr<quicktype::Node>, std::string> & x) { | |
| 147 | + inline void adl_serializer<std::variant<std::shared_ptr<quicktype::TopLevel>, std::string>>::to_json(json & j, const std::variant<std::shared_ptr<quicktype::TopLevel>, std::string> & x) { | |
| 180 | 148 | switch (x.index()) { |
| 181 | 149 | case 0: |
| 182 | - j = std::get<std::shared_ptr<quicktype::Node>>(x); | |
| 150 | + j = std::get<std::shared_ptr<quicktype::TopLevel>>(x); | |
| 183 | 151 | break; |
| 184 | 152 | case 1: |
| 185 | 153 | j = std::get<std::string>(x); |
Mschema-csharp-SystemTextJsondefault / QuickType.cs+7 −16
| @@ -32,23 +32,14 @@ namespace QuickType | ||
| 32 | 32 | public string Value { get; set; } |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - public partial class Node | |
| 36 | - { | |
| 37 | - [JsonProperty("next")] | |
| 38 | - public Next? Next { get; set; } | |
| 39 | - | |
| 40 | - [JsonProperty("value", Required = Required.Always)] | |
| 41 | - public string Value { get; set; } | |
| 42 | - } | |
| 43 | - | |
| 44 | 35 | public partial struct Next |
| 45 | 36 | { |
| 46 | - public Node? Node; | |
| 47 | 37 | public string? String; |
| 38 | + public TopLevel? TopLevel; | |
| 48 | 39 | |
| 49 | - public static implicit operator Next(Node Node) => new Next { Node = Node }; | |
| 50 | 40 | public static implicit operator Next(string String) => new Next { String = String }; |
| 51 | - public bool IsNull => Node == null && String == null; | |
| 41 | + public static implicit operator Next(TopLevel TopLevel) => new Next { TopLevel = TopLevel }; | |
| 42 | + public bool IsNull => TopLevel == null && String == null; | |
| 52 | 43 | } |
| 53 | 44 | |
| 54 | 45 | public partial class TopLevel |
| @@ -90,8 +81,8 @@ namespace QuickType | ||
| 90 | 81 | var stringValue = serializer.Deserialize<string>(reader); |
| 91 | 82 | return new Next { String = stringValue }; |
| 92 | 83 | case JsonToken.StartObject: |
| 93 | - var objectValue = serializer.Deserialize<Node>(reader); | |
| 94 | - return new Next { Node = objectValue }; | |
| 84 | + var objectValue = serializer.Deserialize<TopLevel>(reader); | |
| 85 | + return new Next { TopLevel = objectValue }; | |
| 95 | 86 | } |
| 96 | 87 | throw new Exception("Cannot unmarshal type Next"); |
| 97 | 88 | } |
| @@ -109,9 +100,9 @@ namespace QuickType | ||
| 109 | 100 | serializer.Serialize(writer, value.String); |
| 110 | 101 | return; |
| 111 | 102 | } |
| 112 | - if (value.Node != null) | |
| 103 | + if (value.TopLevel != null) | |
| 113 | 104 | { |
| 114 | - serializer.Serialize(writer, value.Node); | |
| 105 | + serializer.Serialize(writer, value.TopLevel); | |
| 115 | 106 | return; |
| 116 | 107 | } |
| 117 | 108 | throw new Exception("Cannot marshal type Next"); |
Mschema-csharpdefault / QuickType.cs+7 −17
| @@ -30,24 +30,14 @@ namespace QuickType | ||
| 30 | 30 | public string Value { get; set; } |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public partial class Node | |
| 34 | - { | |
| 35 | - [JsonPropertyName("next")] | |
| 36 | - public Next? Next { get; set; } | |
| 37 | - | |
| 38 | - [JsonRequired] | |
| 39 | - [JsonPropertyName("value")] | |
| 40 | - public string Value { get; set; } | |
| 41 | - } | |
| 42 | - | |
| 43 | 33 | public partial struct Next |
| 44 | 34 | { |
| 45 | - public Node? Node; | |
| 46 | 35 | public string? String; |
| 36 | + public TopLevel? TopLevel; | |
| 47 | 37 | |
| 48 | - public static implicit operator Next(Node Node) => new Next { Node = Node }; | |
| 49 | 38 | public static implicit operator Next(string String) => new Next { String = String }; |
| 50 | - public bool IsNull => Node == null && String == null; | |
| 39 | + public static implicit operator Next(TopLevel TopLevel) => new Next { TopLevel = TopLevel }; | |
| 40 | + public bool IsNull => TopLevel == null && String == null; | |
| 51 | 41 | } |
| 52 | 42 | |
| 53 | 43 | public partial class TopLevel |
| @@ -88,8 +78,8 @@ namespace QuickType | ||
| 88 | 78 | var stringValue = reader.GetString(); |
| 89 | 79 | return new Next { String = stringValue }; |
| 90 | 80 | case JsonTokenType.StartObject: |
| 91 | - var objectValue = JsonSerializer.Deserialize<Node>(ref reader, options); | |
| 92 | - return new Next { Node = objectValue }; | |
| 81 | + var objectValue = JsonSerializer.Deserialize<TopLevel>(ref reader, options); | |
| 82 | + return new Next { TopLevel = objectValue }; | |
| 93 | 83 | } |
| 94 | 84 | throw new JsonException("Cannot unmarshal type Next"); |
| 95 | 85 | } |
| @@ -106,9 +96,9 @@ namespace QuickType | ||
| 106 | 96 | JsonSerializer.Serialize(writer, value.String, options); |
| 107 | 97 | return; |
| 108 | 98 | } |
| 109 | - if (value.Node != null) | |
| 99 | + if (value.TopLevel != null) | |
| 110 | 100 | { |
| 111 | - JsonSerializer.Serialize(writer, value.Node, options); | |
| 101 | + JsonSerializer.Serialize(writer, value.TopLevel, options); | |
| 112 | 102 | return; |
| 113 | 103 | } |
| 114 | 104 | throw new NotSupportedException("Cannot marshal type Next"); |
Mschema-dartdefault / TopLevel.dart+0 −20
| @@ -27,23 +27,3 @@ class TopLevel { | ||
| 27 | 27 | "value": value, |
| 28 | 28 | }; |
| 29 | 29 | } |
| 30 | - | |
| 31 | -class Node { | |
| 32 | - final dynamic next; | |
| 33 | - final String value; | |
| 34 | - | |
| 35 | - Node({ | |
| 36 | - this.next, | |
| 37 | - required this.value, | |
| 38 | - }); | |
| 39 | - | |
| 40 | - factory Node.fromJson(Map<String, dynamic> json) => Node( | |
| 41 | - next: json["next"], | |
| 42 | - value: json["value"], | |
| 43 | - ); | |
| 44 | - | |
| 45 | - Map<String, dynamic> toJson() => { | |
| 46 | - "next": next, | |
| 47 | - "value": value, | |
| 48 | - }; | |
| 49 | -} |
Mschema-elixirdefault / QuickType.ex+3 −49
| @@ -5,67 +5,21 @@ | ||
| 5 | 5 | # Decode a JSON string: TopLevel.from_json(data) |
| 6 | 6 | # Encode into a JSON string: TopLevel.to_json(struct) |
| 7 | 7 | |
| 8 | -defmodule NodeClass do | |
| 9 | - @enforce_keys [:value] | |
| 10 | - defstruct [:next, :value] | |
| 11 | - | |
| 12 | - @type t :: %__MODULE__{ | |
| 13 | - next: NodeClass.t() | nil | String.t() | nil, | |
| 14 | - value: String.t() | |
| 15 | - } | |
| 16 | - | |
| 17 | - def decode_next(%{"value" => _,} = value), do: NodeClass.from_map(value) | |
| 18 | - def decode_next(value) when is_nil(value), do: value | |
| 19 | - def decode_next(value) when is_binary(value), do: value | |
| 20 | - def decode_next(_), do: {:error, "Unexpected type when decoding NodeClass.next"} | |
| 21 | - | |
| 22 | - def encode_next(%NodeClass{} = value), do: NodeClass.to_map(value) | |
| 23 | - def encode_next(value) when is_nil(value), do: value | |
| 24 | - def encode_next(value) when is_binary(value), do: value | |
| 25 | - def encode_next(_), do: {:error, "Unexpected type when encoding NodeClass.next"} | |
| 26 | - | |
| 27 | - def from_map(m) do | |
| 28 | - %NodeClass{ | |
| 29 | - next: decode_next(m["next"]), | |
| 30 | - value: m["value"], | |
| 31 | - } | |
| 32 | - end | |
| 33 | - | |
| 34 | - def from_json(json) do | |
| 35 | - json | |
| 36 | - |> Jason.decode!() | |
| 37 | - |> from_map() | |
| 38 | - end | |
| 39 | - | |
| 40 | - def to_map(struct) do | |
| 41 | - %{ | |
| 42 | - "next" => encode_next(struct.next), | |
| 43 | - "value" => struct.value, | |
| 44 | - } | |
| 45 | - end | |
| 46 | - | |
| 47 | - def to_json(struct) do | |
| 48 | - struct | |
| 49 | - |> to_map() | |
| 50 | - |> Jason.encode!() | |
| 51 | - end | |
| 52 | -end | |
| 53 | - | |
| 54 | 8 | defmodule TopLevel do |
| 55 | 9 | @enforce_keys [:value] |
| 56 | 10 | defstruct [:next, :value] |
| 57 | 11 | |
| 58 | 12 | @type t :: %__MODULE__{ |
| 59 | - next: NodeClass.t() | nil | String.t() | nil, | |
| 13 | + next: TopLevel.t() | nil | String.t() | nil, | |
| 60 | 14 | value: String.t() |
| 61 | 15 | } |
| 62 | 16 | |
| 63 | - def decode_next(%{"value" => _,} = value), do: NodeClass.from_map(value) | |
| 17 | + def decode_next(%{"value" => _,} = value), do: TopLevel.from_map(value) | |
| 64 | 18 | def decode_next(value) when is_nil(value), do: value |
| 65 | 19 | def decode_next(value) when is_binary(value), do: value |
| 66 | 20 | def decode_next(_), do: {:error, "Unexpected type when decoding TopLevel.next"} |
| 67 | 21 | |
| 68 | - def encode_next(%NodeClass{} = value), do: NodeClass.to_map(value) | |
| 22 | + def encode_next(%TopLevel{} = value), do: TopLevel.to_map(value) | |
| 69 | 23 | def encode_next(value) when is_nil(value), do: value |
| 70 | 24 | def encode_next(value) when is_binary(value), do: value |
| 71 | 25 | def encode_next(_), do: {:error, "Unexpected type when encoding TopLevel.next"} |
Mschema-flowdefault / TopLevel.js+3 −13
| @@ -9,20 +9,14 @@ | ||
| 9 | 9 | // These functions will throw an error if the JSON doesn't |
| 10 | 10 | // match the expected interface, even if the JSON is valid. |
| 11 | 11 | |
| 12 | -export type TopLevel = { | |
| 13 | - next?: Next; | |
| 14 | - value: string; | |
| 15 | - [property: string]: mixed; | |
| 16 | -}; | |
| 12 | +export type Next = null | TopLevel | string; | |
| 17 | 13 | |
| 18 | -export type Node = { | |
| 14 | +export type TopLevel = { | |
| 19 | 15 | next?: Next; |
| 20 | 16 | value: string; |
| 21 | 17 | [property: string]: mixed; |
| 22 | 18 | }; |
| 23 | 19 | |
| 24 | -export type Next = null | Node | string; | |
| 25 | - | |
| 26 | 20 | // Converts JSON strings to/from your types |
| 27 | 21 | // and asserts the results of JSON.parse at runtime |
| 28 | 22 | function toTopLevel(json: string): TopLevel { |
| @@ -188,11 +182,7 @@ function r(name: string) { | ||
| 188 | 182 | |
| 189 | 183 | const typeMap: any = { |
| 190 | 184 | "TopLevel": o([ |
| 191 | - { json: "next", js: "next", typ: u(undefined, u(null, r("Node"), "")) }, | |
| 192 | - { json: "value", js: "value", typ: "" }, | |
| 193 | - ], "any"), | |
| 194 | - "Node": o([ | |
| 195 | - { json: "next", js: "next", typ: u(undefined, u(null, r("Node"), "")) }, | |
| 185 | + { json: "next", js: "next", typ: u(undefined, u(null, r("TopLevel"), "")) }, | |
| 196 | 186 | { json: "value", js: "value", typ: "" }, |
| 197 | 187 | ], "any"), |
| 198 | 188 | }; |
Mschema-golangdefault / quicktype.go+6 −11
| @@ -26,31 +26,26 @@ type TopLevel struct { | ||
| 26 | 26 | Value string `json:"value"` |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | -type Node struct { | |
| 30 | - Next *Next `json:"next"` | |
| 31 | - Value string `json:"value"` | |
| 32 | -} | |
| 33 | - | |
| 34 | 29 | type Next struct { |
| 35 | - Node *Node | |
| 36 | - String *string | |
| 30 | + String *string | |
| 31 | + TopLevel *TopLevel | |
| 37 | 32 | } |
| 38 | 33 | |
| 39 | 34 | func (x *Next) UnmarshalJSON(data []byte) error { |
| 40 | - x.Node = nil | |
| 41 | - var c Node | |
| 35 | + x.TopLevel = nil | |
| 36 | + var c TopLevel | |
| 42 | 37 | object, err := unmarshalUnion(data, nil, nil, nil, &x.String, false, nil, true, &c, false, nil, false, nil, true) |
| 43 | 38 | if err != nil { |
| 44 | 39 | return err |
| 45 | 40 | } |
| 46 | 41 | if object { |
| 47 | - x.Node = &c | |
| 42 | + x.TopLevel = &c | |
| 48 | 43 | } |
| 49 | 44 | return nil |
| 50 | 45 | } |
| 51 | 46 | |
| 52 | 47 | func (x *Next) MarshalJSON() ([]byte, error) { |
| 53 | - return marshalUnion(nil, nil, nil, x.String, false, nil, x.Node != nil, x.Node, false, nil, false, nil, true) | |
| 48 | + return marshalUnion(nil, nil, nil, x.String, false, nil, x.TopLevel != nil, x.TopLevel, false, nil, false, nil, true) | |
| 54 | 49 | } |
| 55 | 50 | |
| 56 | 51 | func unmarshalUnion(data []byte, pi **int64, pf **float64, pb **bool, ps **string, haveArray bool, pa interface{}, haveObject bool, pc interface{}, haveMap bool, pm interface{}, haveEnum bool, pe interface{}, nullable bool) (bool, error) { |
Mschema-haskelldefault / QuickType.hs+16 −34
| @@ -3,7 +3,6 @@ | ||
| 3 | 3 | |
| 4 | 4 | module QuickType |
| 5 | 5 | ( QuickType (..) |
| 6 | - , Node (..) | |
| 7 | 6 | , Next (..) |
| 8 | 7 | , decodeTopLevel |
| 9 | 8 | ) where |
| @@ -14,25 +13,30 @@ import Data.ByteString.Lazy (ByteString) | ||
| 14 | 13 | import Data.HashMap.Strict (HashMap) |
| 15 | 14 | import Data.Text (Text) |
| 16 | 15 | |
| 16 | +data Next | |
| 17 | + = NullInNext | |
| 18 | + | QuickTypeInNext QuickType | |
| 19 | + | StringInNext Text | |
| 20 | + deriving (Show) | |
| 21 | + | |
| 17 | 22 | data QuickType = QuickType |
| 18 | 23 | { nextQuickType :: Maybe Next |
| 19 | 24 | , valueQuickType :: Text |
| 20 | 25 | } deriving (Show) |
| 21 | 26 | |
| 22 | -data Node = Node | |
| 23 | - { nextNode :: Maybe Next | |
| 24 | - , valueNode :: Text | |
| 25 | - } deriving (Show) | |
| 26 | - | |
| 27 | -data Next | |
| 28 | - = NodeInNext Node | |
| 29 | - | NullInNext | |
| 30 | - | StringInNext Text | |
| 31 | - deriving (Show) | |
| 32 | - | |
| 33 | 27 | decodeTopLevel :: ByteString -> Maybe QuickType |
| 34 | 28 | decodeTopLevel = decode |
| 35 | 29 | |
| 30 | +instance ToJSON Next where | |
| 31 | + toJSON NullInNext = Null | |
| 32 | + toJSON (QuickTypeInNext x) = toJSON x | |
| 33 | + toJSON (StringInNext x) = toJSON x | |
| 34 | + | |
| 35 | +instance FromJSON Next where | |
| 36 | + parseJSON Null = return NullInNext | |
| 37 | + parseJSON xs@(Object _) = (fmap QuickTypeInNext . parseJSON) xs | |
| 38 | + parseJSON xs@(String _) = (fmap StringInNext . parseJSON) xs | |
| 39 | + | |
| 36 | 40 | instance ToJSON QuickType where |
| 37 | 41 | toJSON (QuickType nextQuickType valueQuickType) = |
| 38 | 42 | object |
| @@ -44,25 +48,3 @@ instance FromJSON QuickType where | ||
| 44 | 48 | parseJSON (Object v) = QuickType |
| 45 | 49 | <$> v .:? "next" |
| 46 | 50 | <*> v .: "value" |
| 47 | - | |
| 48 | -instance ToJSON Node where | |
| 49 | - toJSON (Node nextNode valueNode) = | |
| 50 | - object | |
| 51 | - [ "next" .= nextNode | |
| 52 | - , "value" .= valueNode | |
| 53 | - ] | |
| 54 | - | |
| 55 | -instance FromJSON Node where | |
| 56 | - parseJSON (Object v) = Node | |
| 57 | - <$> v .:? "next" | |
| 58 | - <*> v .: "value" | |
| 59 | - | |
| 60 | -instance ToJSON Next where | |
| 61 | - toJSON (NodeInNext x) = toJSON x | |
| 62 | - toJSON NullInNext = Null | |
| 63 | - toJSON (StringInNext x) = toJSON x | |
| 64 | - | |
| 65 | -instance FromJSON Next where | |
| 66 | - parseJSON xs@(Object _) = (fmap NodeInNext . parseJSON) xs | |
| 67 | - parseJSON Null = return NullInNext | |
| 68 | - parseJSON xs@(String _) = (fmap StringInNext . parseJSON) xs |
Mschema-java-datetime-legacydefault / src / main / java / io / quicktype / Next.java+4 −4
| @@ -10,7 +10,7 @@ import com.fasterxml.jackson.core.type.*; | ||
| 10 | 10 | @JsonDeserialize(using = Next.Deserializer.class) |
| 11 | 11 | @JsonSerialize(using = Next.Serializer.class) |
| 12 | 12 | public class Next { |
| 13 | - public Node nodeValue; | |
| 13 | + public TopLevel topLevelValue; | |
| 14 | 14 | public String stringValue; |
| 15 | 15 | |
| 16 | 16 | static class Deserializer extends JsonDeserializer<Next> { |
| @@ -25,7 +25,7 @@ public class Next { | ||
| 25 | 25 | value.stringValue = string; |
| 26 | 26 | break; |
| 27 | 27 | case START_OBJECT: |
| 28 | - value.nodeValue = jsonParser.readValueAs(Node.class); | |
| 28 | + value.topLevelValue = jsonParser.readValueAs(TopLevel.class); | |
| 29 | 29 | break; |
| 30 | 30 | default: throw new IOException("Cannot deserialize Next"); |
| 31 | 31 | } |
| @@ -36,8 +36,8 @@ public class Next { | ||
| 36 | 36 | static class Serializer extends JsonSerializer<Next> { |
| 37 | 37 | @Override |
| 38 | 38 | public void serialize(Next obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { |
| 39 | - if (obj.nodeValue != null) { | |
| 40 | - jsonGenerator.writeObject(obj.nodeValue); | |
| 39 | + if (obj.topLevelValue != null) { | |
| 40 | + jsonGenerator.writeObject(obj.topLevelValue); | |
| 41 | 41 | return; |
| 42 | 42 | } |
| 43 | 43 | if (obj.stringValue != null) { |
Dschema-java-datetime-legacydefault / src / main / java / io / quicktype / Node.java+0 −18
| @@ -1,18 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | - | |
| 5 | -public class Node { | |
| 6 | - private Next next; | |
| 7 | - private String value; | |
| 8 | - | |
| 9 | - @JsonProperty("next") | |
| 10 | - public Next getNext() { return next; } | |
| 11 | - @JsonProperty("next") | |
| 12 | - public void setNext(Next value) { this.next = value; } | |
| 13 | - | |
| 14 | - @JsonProperty("value") | |
| 15 | - public String getValue() { return value; } | |
| 16 | - @JsonProperty("value") | |
| 17 | - public void setValue(String value) { this.value = value; } | |
| 18 | -} |
Mschema-java-lombokdefault / src / main / java / io / quicktype / Next.java+4 −4
| @@ -10,7 +10,7 @@ import com.fasterxml.jackson.core.type.*; | ||
| 10 | 10 | @JsonDeserialize(using = Next.Deserializer.class) |
| 11 | 11 | @JsonSerialize(using = Next.Serializer.class) |
| 12 | 12 | public class Next { |
| 13 | - public Node nodeValue; | |
| 13 | + public TopLevel topLevelValue; | |
| 14 | 14 | public String stringValue; |
| 15 | 15 | |
| 16 | 16 | static class Deserializer extends JsonDeserializer<Next> { |
| @@ -25,7 +25,7 @@ public class Next { | ||
| 25 | 25 | value.stringValue = string; |
| 26 | 26 | break; |
| 27 | 27 | case START_OBJECT: |
| 28 | - value.nodeValue = jsonParser.readValueAs(Node.class); | |
| 28 | + value.topLevelValue = jsonParser.readValueAs(TopLevel.class); | |
| 29 | 29 | break; |
| 30 | 30 | default: throw new IOException("Cannot deserialize Next"); |
| 31 | 31 | } |
| @@ -36,8 +36,8 @@ public class Next { | ||
| 36 | 36 | static class Serializer extends JsonSerializer<Next> { |
| 37 | 37 | @Override |
| 38 | 38 | public void serialize(Next obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { |
| 39 | - if (obj.nodeValue != null) { | |
| 40 | - jsonGenerator.writeObject(obj.nodeValue); | |
| 39 | + if (obj.topLevelValue != null) { | |
| 40 | + jsonGenerator.writeObject(obj.topLevelValue); | |
| 41 | 41 | return; |
| 42 | 42 | } |
| 43 | 43 | if (obj.stringValue != null) { |
Dschema-java-lombokdefault / src / main / java / io / quicktype / Node.java+0 −18
| @@ -1,18 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | - | |
| 5 | -public class Node { | |
| 6 | - private Next next; | |
| 7 | - private String value; | |
| 8 | - | |
| 9 | - @JsonProperty("next") | |
| 10 | - public Next getNext() { return next; } | |
| 11 | - @JsonProperty("next") | |
| 12 | - public void setNext(Next value) { this.next = value; } | |
| 13 | - | |
| 14 | - @JsonProperty("value") | |
| 15 | - public String getValue() { return value; } | |
| 16 | - @JsonProperty("value") | |
| 17 | - public void setValue(String value) { this.value = value; } | |
| 18 | -} |
Mschema-javadefault / src / main / java / io / quicktype / Next.java+4 −4
| @@ -10,7 +10,7 @@ import com.fasterxml.jackson.core.type.*; | ||
| 10 | 10 | @JsonDeserialize(using = Next.Deserializer.class) |
| 11 | 11 | @JsonSerialize(using = Next.Serializer.class) |
| 12 | 12 | public class Next { |
| 13 | - public Node nodeValue; | |
| 13 | + public TopLevel topLevelValue; | |
| 14 | 14 | public String stringValue; |
| 15 | 15 | |
| 16 | 16 | static class Deserializer extends JsonDeserializer<Next> { |
| @@ -25,7 +25,7 @@ public class Next { | ||
| 25 | 25 | value.stringValue = string; |
| 26 | 26 | break; |
| 27 | 27 | case START_OBJECT: |
| 28 | - value.nodeValue = jsonParser.readValueAs(Node.class); | |
| 28 | + value.topLevelValue = jsonParser.readValueAs(TopLevel.class); | |
| 29 | 29 | break; |
| 30 | 30 | default: throw new IOException("Cannot deserialize Next"); |
| 31 | 31 | } |
| @@ -36,8 +36,8 @@ public class Next { | ||
| 36 | 36 | static class Serializer extends JsonSerializer<Next> { |
| 37 | 37 | @Override |
| 38 | 38 | public void serialize(Next obj, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { |
| 39 | - if (obj.nodeValue != null) { | |
| 40 | - jsonGenerator.writeObject(obj.nodeValue); | |
| 39 | + if (obj.topLevelValue != null) { | |
| 40 | + jsonGenerator.writeObject(obj.topLevelValue); | |
| 41 | 41 | return; |
| 42 | 42 | } |
| 43 | 43 | if (obj.stringValue != null) { |
Dschema-javadefault / src / main / java / io / quicktype / Node.java+0 −18
| @@ -1,18 +0,0 @@ | ||
| 1 | -package io.quicktype; | |
| 2 | - | |
| 3 | -import com.fasterxml.jackson.annotation.*; | |
| 4 | - | |
| 5 | -public class Node { | |
| 6 | - private Next next; | |
| 7 | - private String value; | |
| 8 | - | |
| 9 | - @JsonProperty("next") | |
| 10 | - public Next getNext() { return next; } | |
| 11 | - @JsonProperty("next") | |
| 12 | - public void setNext(Next value) { this.next = value; } | |
| 13 | - | |
| 14 | - @JsonProperty("value") | |
| 15 | - public String getValue() { return value; } | |
| 16 | - @JsonProperty("value") | |
| 17 | - public void setValue(String value) { this.value = value; } | |
| 18 | -} |
Mschema-javascriptdefault / TopLevel.js+1 −5
| @@ -172,11 +172,7 @@ function r(name) { | ||
| 172 | 172 | |
| 173 | 173 | const typeMap = { |
| 174 | 174 | "TopLevel": o([ |
| 175 | - { json: "next", js: "next", typ: u(undefined, u(null, r("Node"), "")) }, | |
| 176 | - { json: "value", js: "value", typ: "" }, | |
| 177 | - ], "any"), | |
| 178 | - "Node": o([ | |
| 179 | - { json: "next", js: "next", typ: u(undefined, u(null, r("Node"), "")) }, | |
| 175 | + { json: "next", js: "next", typ: u(undefined, u(null, r("TopLevel"), "")) }, | |
| 180 | 176 | { json: "value", js: "value", typ: "" }, |
| 181 | 177 | ], "any"), |
| 182 | 178 | }; |
Mschema-kotlin-jacksondefault / TopLevel.kt+18 −23
| @@ -17,39 +17,34 @@ private fun <T> Klaxon.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonValue | ||
| 17 | 17 | private val klaxon = Klaxon() |
| 18 | 18 | .convert(Next::class, { Next.fromJson(it) }, { it.toJson() }, true) |
| 19 | 19 | |
| 20 | -data class TopLevel ( | |
| 21 | - val next: Next? = null, | |
| 22 | - val value: String | |
| 23 | -) { | |
| 24 | - public fun toJson() = klaxon.toJsonString(this) | |
| 25 | - | |
| 26 | - companion object { | |
| 27 | - public fun fromJson(json: String) = klaxon.parse<TopLevel>(json) | |
| 28 | - } | |
| 29 | -} | |
| 30 | - | |
| 31 | -data class Node ( | |
| 32 | - val next: Next? = null, | |
| 33 | - val value: String | |
| 34 | -) | |
| 35 | - | |
| 36 | 20 | sealed class Next { |
| 37 | - class NodeValue(val value: Node) : Next() | |
| 38 | - class StringValue(val value: String) : Next() | |
| 39 | - class NullValue() : Next() | |
| 21 | + class StringValue(val value: String) : Next() | |
| 22 | + class TopLevelValue(val value: TopLevel) : Next() | |
| 23 | + class NullValue() : Next() | |
| 40 | 24 | |
| 41 | 25 | public fun toJson(): String = klaxon.toJsonString(when (this) { |
| 42 | - is NodeValue -> this.value | |
| 43 | - is StringValue -> this.value | |
| 44 | - is NullValue -> "null" | |
| 26 | + is StringValue -> this.value | |
| 27 | + is TopLevelValue -> this.value | |
| 28 | + is NullValue -> "null" | |
| 45 | 29 | }) |
| 46 | 30 | |
| 47 | 31 | companion object { |
| 48 | 32 | public fun fromJson(jv: JsonValue): Next = when (jv.inside) { |
| 49 | - is JsonObject -> NodeValue(jv.obj?.let { klaxon.parseFromJsonObject<Node>(it) }!!) | |
| 50 | 33 | is String -> StringValue(jv.string!!) |
| 34 | + is JsonObject -> TopLevelValue(jv.obj?.let { klaxon.parseFromJsonObject<TopLevel>(it) }!!) | |
| 51 | 35 | null -> NullValue() |
| 52 | 36 | else -> throw IllegalArgumentException() |
| 53 | 37 | } |
| 54 | 38 | } |
| 55 | 39 | } |
| 40 | + | |
| 41 | +data class TopLevel ( | |
| 42 | + val next: Next? = null, | |
| 43 | + val value: String | |
| 44 | +) { | |
| 45 | + public fun toJson() = klaxon.toJsonString(this) | |
| 46 | + | |
| 47 | + companion object { | |
| 48 | + public fun fromJson(json: String) = klaxon.parse<TopLevel>(json) | |
| 49 | + } | |
| 50 | +} |
Mschema-kotlindefault / TopLevel.kt+19 −26
| @@ -30,43 +30,36 @@ val mapper = jacksonObjectMapper().apply { | ||
| 30 | 30 | convert(Next::class, { Next.fromJson(it) }, { it.toJson() }, true) |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | -data class TopLevel ( | |
| 34 | - val next: Next? = null, | |
| 33 | +sealed class Next { | |
| 34 | + class StringValue(val value: String) : Next() | |
| 35 | + class TopLevelValue(val value: TopLevel) : Next() | |
| 36 | + class NullValue() : Next() | |
| 35 | 37 | |
| 36 | - @get:JsonProperty(required=true)@field:JsonProperty(required=true) | |
| 37 | - val value: String | |
| 38 | -) { | |
| 39 | - fun toJson() = mapper.writeValueAsString(this) | |
| 38 | + fun toJson(): String = mapper.writeValueAsString(when (this) { | |
| 39 | + is StringValue -> this.value | |
| 40 | + is TopLevelValue -> this.value | |
| 41 | + is NullValue -> "null" | |
| 42 | + }) | |
| 40 | 43 | |
| 41 | 44 | companion object { |
| 42 | - fun fromJson(json: String) = mapper.readValue<TopLevel>(json) | |
| 45 | + fun fromJson(jn: JsonNode): Next = when (jn) { | |
| 46 | + is TextNode -> StringValue(mapper.treeToValue(jn)) | |
| 47 | + is ObjectNode -> TopLevelValue(mapper.treeToValue(jn)) | |
| 48 | + null -> NullValue() | |
| 49 | + else -> throw IllegalArgumentException() | |
| 50 | + } | |
| 43 | 51 | } |
| 44 | 52 | } |
| 45 | 53 | |
| 46 | -data class Node ( | |
| 54 | +data class TopLevel ( | |
| 47 | 55 | val next: Next? = null, |
| 48 | 56 | |
| 49 | 57 | @get:JsonProperty(required=true)@field:JsonProperty(required=true) |
| 50 | 58 | val value: String |
| 51 | -) | |
| 52 | - | |
| 53 | -sealed class Next { | |
| 54 | - class NodeValue(val value: Node) : Next() | |
| 55 | - class StringValue(val value: String) : Next() | |
| 56 | - class NullValue() : Next() | |
| 57 | - | |
| 58 | - fun toJson(): String = mapper.writeValueAsString(when (this) { | |
| 59 | - is NodeValue -> this.value | |
| 60 | - is StringValue -> this.value | |
| 61 | - is NullValue -> "null" | |
| 62 | - }) | |
| 59 | +) { | |
| 60 | + fun toJson() = mapper.writeValueAsString(this) | |
| 63 | 61 | |
| 64 | 62 | companion object { |
| 65 | - fun fromJson(jn: JsonNode): Next = when (jn) { | |
| 66 | - is ObjectNode -> NodeValue(mapper.treeToValue(jn)) | |
| 67 | - is TextNode -> StringValue(mapper.treeToValue(jn)) | |
| 68 | - null -> NullValue() | |
| 69 | - else -> throw IllegalArgumentException() | |
| 70 | - } | |
| 63 | + fun fromJson(json: String) = mapper.readValue<TopLevel>(json) | |
| 71 | 64 | } |
| 72 | 65 | } |
Mschema-phpdefault / TopLevel.php+15 −196
| @@ -4,14 +4,14 @@ declare(strict_types=1); | ||
| 4 | 4 | // This is an autogenerated file:TopLevel |
| 5 | 5 | |
| 6 | 6 | class TopLevel { |
| 7 | - private Node|string|null $next; // json:next Optional | |
| 7 | + private TopLevel|string|null $next; // json:next Optional | |
| 8 | 8 | private string $value; // json:value Required |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | - * @param Node|string|null $next | |
| 11 | + * @param TopLevel|string|null $next | |
| 12 | 12 | * @param string $value |
| 13 | 13 | */ |
| 14 | - public function __construct(Node|string|null $next, string $value) { | |
| 14 | + public function __construct(TopLevel|string|null $next, string $value) { | |
| 15 | 15 | $this->next = $next; |
| 16 | 16 | $this->value = $value; |
| 17 | 17 | } |
| @@ -19,13 +19,13 @@ class TopLevel { | ||
| 19 | 19 | /** |
| 20 | 20 | * @param stdClass|string|null $value |
| 21 | 21 | * @throws Exception |
| 22 | - * @return Node|string|null | |
| 22 | + * @return TopLevel|string|null | |
| 23 | 23 | */ |
| 24 | - public static function fromNext(stdClass|string|null $value): Node|string|null { | |
| 24 | + public static function fromNext(stdClass|string|null $value): TopLevel|string|null { | |
| 25 | 25 | if (is_null($value)) { |
| 26 | 26 | return $value; /*null*/ |
| 27 | 27 | } elseif (is_object($value)) { |
| 28 | - return Node::from($value); /*class*/ | |
| 28 | + return TopLevel::from($value); /*class*/ | |
| 29 | 29 | } elseif (is_string($value)) { |
| 30 | 30 | return $value; /*string*/ |
| 31 | 31 | } else { |
| @@ -41,7 +41,7 @@ class TopLevel { | ||
| 41 | 41 | if (TopLevel::validateNext($this->next)) { |
| 42 | 42 | if (is_null($this->next)) { |
| 43 | 43 | return $this->next; /*null*/ |
| 44 | - } elseif ($this->next instanceof Node) { | |
| 44 | + } elseif ($this->next instanceof TopLevel) { | |
| 45 | 45 | return $this->next->to(); /*class*/ |
| 46 | 46 | } elseif (is_string($this->next)) { |
| 47 | 47 | return $this->next; /*string*/ |
| @@ -53,16 +53,16 @@ class TopLevel { | ||
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | - * @param Node|string|null | |
| 56 | + * @param TopLevel|string|null | |
| 57 | 57 | * @return bool |
| 58 | 58 | * @throws Exception |
| 59 | 59 | */ |
| 60 | - public static function validateNext(Node|string|null $value): bool { | |
| 60 | + public static function validateNext(TopLevel|string|null $value): bool { | |
| 61 | 61 | if (is_null($value)) { |
| 62 | 62 | if (!is_null($value)) { |
| 63 | 63 | throw new Exception("Attribute Error:TopLevel::next"); |
| 64 | 64 | } |
| 65 | - } elseif ($value instanceof Node) { | |
| 65 | + } elseif ($value instanceof TopLevel) { | |
| 66 | 66 | $value->validate(); |
| 67 | 67 | } elseif (is_string($value)) { |
| 68 | 68 | if (!is_string($value)) { |
| @@ -76,9 +76,9 @@ class TopLevel { | ||
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * @throws Exception |
| 79 | - * @return Node|string|null | |
| 79 | + * @return TopLevel|string|null | |
| 80 | 80 | */ |
| 81 | - public function getNext(): Node|string|null { | |
| 81 | + public function getNext(): TopLevel|string|null { | |
| 82 | 82 | if (TopLevel::validateNext($this->next)) { |
| 83 | 83 | return $this->next; |
| 84 | 84 | } |
| @@ -86,10 +86,10 @@ class TopLevel { | ||
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | - * @return Node|string|null | |
| 89 | + * @return TopLevel|string|null | |
| 90 | 90 | */ |
| 91 | - public static function sampleNext(): Node|string|null { | |
| 92 | - return Node::sample(); /*31:next*/ | |
| 91 | + public static function sampleNext(): TopLevel|string|null { | |
| 92 | + return TopLevel::sample(); /*31:next*/ | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
| @@ -181,184 +181,3 @@ class TopLevel { | ||
| 181 | 181 | ); |
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | - | |
| 185 | -// This is an autogenerated file:Node | |
| 186 | - | |
| 187 | -class Node { | |
| 188 | - private Node|string|null $next; // json:next Optional | |
| 189 | - private string $value; // json:value Required | |
| 190 | - | |
| 191 | - /** | |
| 192 | - * @param Node|string|null $next | |
| 193 | - * @param string $value | |
| 194 | - */ | |
| 195 | - public function __construct(Node|string|null $next, string $value) { | |
| 196 | - $this->next = $next; | |
| 197 | - $this->value = $value; | |
| 198 | - } | |
| 199 | - | |
| 200 | - /** | |
| 201 | - * @param stdClass|string|null $value | |
| 202 | - * @throws Exception | |
| 203 | - * @return Node|string|null | |
| 204 | - */ | |
| 205 | - public static function fromNext(stdClass|string|null $value): Node|string|null { | |
| 206 | - if (is_null($value)) { | |
| 207 | - return $value; /*null*/ | |
| 208 | - } elseif (is_object($value)) { | |
| 209 | - return Node::from($value); /*class*/ | |
| 210 | - } elseif (is_string($value)) { | |
| 211 | - return $value; /*string*/ | |
| 212 | - } else { | |
| 213 | - throw new Exception('Cannot deserialize union value in Node'); | |
| 214 | - } | |
| 215 | - } | |
| 216 | - | |
| 217 | - /** | |
| 218 | - * @throws Exception | |
| 219 | - * @return stdClass|string|null | |
| 220 | - */ | |
| 221 | - public function toNext(): stdClass|string|null { | |
| 222 | - if (Node::validateNext($this->next)) { | |
| 223 | - if (is_null($this->next)) { | |
| 224 | - return $this->next; /*null*/ | |
| 225 | - } elseif ($this->next instanceof Node) { | |
| 226 | - return $this->next->to(); /*class*/ | |
| 227 | - } elseif (is_string($this->next)) { | |
| 228 | - return $this->next; /*string*/ | |
| 229 | - } else { | |
| 230 | - throw new Exception('Union value has no matching member in Node'); | |
| 231 | - } | |
| 232 | - } | |
| 233 | - throw new Exception('never get to this Node::next'); | |
| 234 | - } | |
| 235 | - | |
| 236 | - /** | |
| 237 | - * @param Node|string|null | |
| 238 | - * @return bool | |
| 239 | - * @throws Exception | |
| 240 | - */ | |
| 241 | - public static function validateNext(Node|string|null $value): bool { | |
| 242 | - if (is_null($value)) { | |
| 243 | - if (!is_null($value)) { | |
| 244 | - throw new Exception("Attribute Error:Node::next"); | |
| 245 | - } | |
| 246 | - } elseif ($value instanceof Node) { | |
| 247 | - $value->validate(); | |
| 248 | - } elseif (is_string($value)) { | |
| 249 | - if (!is_string($value)) { | |
| 250 | - throw new Exception("Attribute Error:Node::next"); | |
| 251 | - } | |
| 252 | - } else { | |
| 253 | - throw new Exception("Attribute Error:Node::next"); | |
| 254 | - } | |
| 255 | - return true; | |
| 256 | - } | |
| 257 | - | |
| 258 | - /** | |
| 259 | - * @throws Exception | |
| 260 | - * @return Node|string|null | |
| 261 | - */ | |
| 262 | - public function getNext(): Node|string|null { | |
| 263 | - if (Node::validateNext($this->next)) { | |
| 264 | - return $this->next; | |
| 265 | - } | |
| 266 | - throw new Exception('never get to getNext Node::next'); | |
| 267 | - } | |
| 268 | - | |
| 269 | - /** | |
| 270 | - * @return Node|string|null | |
| 271 | - */ | |
| 272 | - public static function sampleNext(): Node|string|null { | |
| 273 | - return Node::sample(); /*31:next*/ | |
| 274 | - } | |
| 275 | - | |
| 276 | - /** | |
| 277 | - * @param string $value | |
| 278 | - * @throws Exception | |
| 279 | - * @return string | |
| 280 | - */ | |
| 281 | - public static function fromValue(string $value): string { | |
| 282 | - return $value; /*string*/ | |
| 283 | - } | |
| 284 | - | |
| 285 | - /** | |
| 286 | - * @throws Exception | |
| 287 | - * @return string | |
| 288 | - */ | |
| 289 | - public function toValue(): string { | |
| 290 | - if (Node::validateValue($this->value)) { | |
| 291 | - return $this->value; /*string*/ | |
| 292 | - } | |
| 293 | - throw new Exception('never get to this Node::value'); | |
| 294 | - } | |
| 295 | - | |
| 296 | - /** | |
| 297 | - * @param string | |
| 298 | - * @return bool | |
| 299 | - * @throws Exception | |
| 300 | - */ | |
| 301 | - public static function validateValue(string $value): bool { | |
| 302 | - return true; | |
| 303 | - } | |
| 304 | - | |
| 305 | - /** | |
| 306 | - * @throws Exception | |
| 307 | - * @return string | |
| 308 | - */ | |
| 309 | - public function getValue(): string { | |
| 310 | - if (Node::validateValue($this->value)) { | |
| 311 | - return $this->value; | |
| 312 | - } | |
| 313 | - throw new Exception('never get to getValue Node::value'); | |
| 314 | - } | |
| 315 | - | |
| 316 | - /** | |
| 317 | - * @return string | |
| 318 | - */ | |
| 319 | - public static function sampleValue(): string { | |
| 320 | - return 'Node::value::32'; /*32:value*/ | |
| 321 | - } | |
| 322 | - | |
| 323 | - /** | |
| 324 | - * @throws Exception | |
| 325 | - * @return bool | |
| 326 | - */ | |
| 327 | - public function validate(): bool { | |
| 328 | - return Node::validateNext($this->next) | |
| 329 | - || Node::validateValue($this->value); | |
| 330 | - } | |
| 331 | - | |
| 332 | - /** | |
| 333 | - * @return stdClass | |
| 334 | - * @throws Exception | |
| 335 | - */ | |
| 336 | - public function to(): stdClass { | |
| 337 | - $out = new stdClass(); | |
| 338 | - $out->{'next'} = $this->toNext(); | |
| 339 | - $out->{'value'} = $this->toValue(); | |
| 340 | - return $out; | |
| 341 | - } | |
| 342 | - | |
| 343 | - /** | |
| 344 | - * @param stdClass $obj | |
| 345 | - * @return Node | |
| 346 | - * @throws Exception | |
| 347 | - */ | |
| 348 | - public static function from(stdClass $obj): Node { | |
| 349 | - return new Node( | |
| 350 | - Node::fromNext($obj->{'next'}) | |
| 351 | - ,Node::fromValue($obj->{'value'}) | |
| 352 | - ); | |
| 353 | - } | |
| 354 | - | |
| 355 | - /** | |
| 356 | - * @return Node | |
| 357 | - */ | |
| 358 | - public static function sample(): Node { | |
| 359 | - return new Node( | |
| 360 | - Node::sampleNext() | |
| 361 | - ,Node::sampleValue() | |
| 362 | - ); | |
| 363 | - } | |
| 364 | -} |
Mschema-pikedefault / TopLevel.pmod+6 −29
| @@ -12,30 +12,13 @@ | ||
| 12 | 12 | // and will likely throw an error, if the JSON string does not |
| 13 | 13 | // match the expected interface, even if the JSON itself is valid. |
| 14 | 14 | |
| 15 | -class TopLevel { | |
| 16 | - Next next; // json: "next" | |
| 17 | - string value; // json: "value" | |
| 18 | - | |
| 19 | - string encode_json() { | |
| 20 | - mapping(string:mixed) json = ([ | |
| 21 | - "next" : next, | |
| 22 | - "value" : value, | |
| 23 | - ]); | |
| 24 | - | |
| 25 | - return Standards.JSON.encode(json); | |
| 26 | - } | |
| 27 | -} | |
| 28 | - | |
| 29 | -TopLevel TopLevel_from_JSON(mixed json) { | |
| 30 | - TopLevel retval = TopLevel(); | |
| 31 | - | |
| 32 | - retval.next = json["next"]; | |
| 33 | - retval.value = json["value"]; | |
| 15 | +typedef string|TopLevel Next; | |
| 34 | 16 | |
| 35 | - return retval; | |
| 17 | +Next Next_from_JSON(mixed json) { | |
| 18 | + return json; | |
| 36 | 19 | } |
| 37 | 20 | |
| 38 | -class Node { | |
| 21 | +class TopLevel { | |
| 39 | 22 | Next next; // json: "next" |
| 40 | 23 | string value; // json: "value" |
| 41 | 24 | |
| @@ -49,17 +32,11 @@ class Node { | ||
| 49 | 32 | } |
| 50 | 33 | } |
| 51 | 34 | |
| 52 | -Node Node_from_JSON(mixed json) { | |
| 53 | - Node retval = Node(); | |
| 35 | +TopLevel TopLevel_from_JSON(mixed json) { | |
| 36 | + TopLevel retval = TopLevel(); | |
| 54 | 37 | |
| 55 | 38 | retval.next = json["next"]; |
| 56 | 39 | retval.value = json["value"]; |
| 57 | 40 | |
| 58 | 41 | return retval; |
| 59 | 42 | } |
| 60 | - | |
| 61 | -typedef Node|string Next; | |
| 62 | - | |
| 63 | -Next Next_from_JSON(mixed json) { | |
| 64 | - return json; | |
| 65 | -} |
Mschema-pythondefault / quicktype.py+3 −23
| @@ -29,43 +29,23 @@ def to_class(c: Type[T], x: Any) -> dict: | ||
| 29 | 29 | return cast(Any, x).to_dict() |
| 30 | 30 | |
| 31 | 31 | |
| 32 | -@dataclass | |
| 33 | -class Node: | |
| 34 | - value: str | |
| 35 | - next: 'Node | str | None' = None | |
| 36 | - | |
| 37 | - @staticmethod | |
| 38 | - def from_dict(obj: Any) -> 'Node': | |
| 39 | - assert isinstance(obj, dict) | |
| 40 | - value = from_str(obj.get("value")) | |
| 41 | - next = from_union([Node.from_dict, from_none, from_str], obj.get("next")) | |
| 42 | - return Node(value, next) | |
| 43 | - | |
| 44 | - def to_dict(self) -> dict: | |
| 45 | - result: dict = {} | |
| 46 | - result["value"] = from_str(self.value) | |
| 47 | - if self.next is not None: | |
| 48 | - result["next"] = from_union([lambda x: to_class(Node, x), from_none, from_str], self.next) | |
| 49 | - return result | |
| 50 | - | |
| 51 | - | |
| 52 | 32 | @dataclass |
| 53 | 33 | class TopLevel: |
| 54 | 34 | value: str |
| 55 | - next: Node | str | None = None | |
| 35 | + next: 'TopLevel | str | None' = None | |
| 56 | 36 | |
| 57 | 37 | @staticmethod |
| 58 | 38 | def from_dict(obj: Any) -> 'TopLevel': |
| 59 | 39 | assert isinstance(obj, dict) |
| 60 | 40 | value = from_str(obj.get("value")) |
| 61 | - next = from_union([Node.from_dict, from_none, from_str], obj.get("next")) | |
| 41 | + next = from_union([TopLevel.from_dict, from_none, from_str], obj.get("next")) | |
| 62 | 42 | return TopLevel(value, next) |
| 63 | 43 | |
| 64 | 44 | def to_dict(self) -> dict: |
| 65 | 45 | result: dict = {} |
| 66 | 46 | result["value"] = from_str(self.value) |
| 67 | 47 | if self.next is not None: |
| 68 | - result["next"] = from_union([lambda x: to_class(Node, x), from_none, from_str], self.next) | |
| 48 | + result["next"] = from_union([lambda x: to_class(TopLevel, x), from_none, from_str], self.next) | |
| 69 | 49 | return result |
Mschema-rubydefault / TopLevel.rb+16 −44
| @@ -21,26 +21,26 @@ module Types | ||
| 21 | 21 | end |
| 22 | 22 | |
| 23 | 23 | # (forward declaration) |
| 24 | -class Node < Dry::Struct; end | |
| 24 | +class TopLevel < Dry::Struct; end | |
| 25 | 25 | |
| 26 | 26 | class Next < Dry::Struct |
| 27 | - attribute :node, Node.optional | |
| 28 | - attribute :null, Types::Nil.optional | |
| 29 | - attribute :string, Types::String.optional | |
| 27 | + attribute :null, Types::Nil.optional | |
| 28 | + attribute :string, Types::String.optional | |
| 29 | + attribute :top_level, TopLevel.optional | |
| 30 | 30 | |
| 31 | 31 | def self.from_dynamic!(d) |
| 32 | - begin | |
| 33 | - value = Node.from_dynamic!(d) | |
| 34 | - if schema[:node].right.valid? value | |
| 35 | - return new(node: value, null: nil, string: nil) | |
| 36 | - end | |
| 37 | - rescue | |
| 38 | - end | |
| 39 | 32 | if schema[:null].right.valid? d |
| 40 | - return new(null: d, node: nil, string: nil) | |
| 33 | + return new(null: d, top_level: nil, string: nil) | |
| 41 | 34 | end |
| 42 | 35 | if schema[:string].right.valid? d |
| 43 | - return new(string: d, node: nil, null: nil) | |
| 36 | + return new(string: d, top_level: nil, null: nil) | |
| 37 | + end | |
| 38 | + begin | |
| 39 | + value = TopLevel.from_dynamic!(d) | |
| 40 | + if schema[:top_level].right.valid? value | |
| 41 | + return new(top_level: value, null: nil, string: nil) | |
| 42 | + end | |
| 43 | + rescue | |
| 44 | 44 | end |
| 45 | 45 | raise "Invalid union" |
| 46 | 46 | end |
| @@ -50,10 +50,10 @@ class Next < Dry::Struct | ||
| 50 | 50 | end |
| 51 | 51 | |
| 52 | 52 | def to_dynamic |
| 53 | - if node != nil | |
| 54 | - node.to_dynamic | |
| 55 | - elsif string != nil | |
| 53 | + if string != nil | |
| 56 | 54 | string |
| 55 | + elsif top_level != nil | |
| 56 | + top_level.to_dynamic | |
| 57 | 57 | else |
| 58 | 58 | nil |
| 59 | 59 | end |
| @@ -64,34 +64,6 @@ class Next < Dry::Struct | ||
| 64 | 64 | end |
| 65 | 65 | end |
| 66 | 66 | |
| 67 | -class Node < Dry::Struct | |
| 68 | - attribute :node_next, Types.Instance(Next).optional | |
| 69 | - attribute :value, Types::String | |
| 70 | - | |
| 71 | - def self.from_dynamic!(d) | |
| 72 | - d = Types::Hash[d] | |
| 73 | - new( | |
| 74 | - node_next: d["next"] ? Next.from_dynamic!(d["next"]) : nil, | |
| 75 | - value: d.fetch("value"), | |
| 76 | - ) | |
| 77 | - end | |
| 78 | - | |
| 79 | - def self.from_json!(json) | |
| 80 | - from_dynamic!(JSON.parse(json)) | |
| 81 | - end | |
| 82 | - | |
| 83 | - def to_dynamic | |
| 84 | - { | |
| 85 | - "next" => node_next&.to_dynamic, | |
| 86 | - "value" => value, | |
| 87 | - } | |
| 88 | - end | |
| 89 | - | |
| 90 | - def to_json(options = nil) | |
| 91 | - JSON.generate(to_dynamic, options) | |
| 92 | - end | |
| 93 | -end | |
| 94 | - | |
| 95 | 67 | class TopLevel < Dry::Struct |
| 96 | 68 | attribute :top_level_next, Types.Instance(Next).optional |
| 97 | 69 | attribute :value, Types::String |
Mschema-rustdefault / module_under_test.rs+6 −13
| @@ -14,23 +14,16 @@ | ||
| 14 | 14 | use serde::{Serialize, Deserialize}; |
| 15 | 15 | |
| 16 | 16 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | -pub struct TopLevel { | |
| 18 | - pub next: Option<Box<Next>>, | |
| 17 | +#[serde(untagged)] | |
| 18 | +pub enum Next { | |
| 19 | + PurpleString(String), | |
| 19 | 20 | |
| 20 | - pub value: String, | |
| 21 | + TopLevel(Box<TopLevel>), | |
| 21 | 22 | } |
| 22 | 23 | |
| 23 | 24 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 24 | -pub struct Node { | |
| 25 | - pub next: Option<Box<Next>>, | |
| 25 | +pub struct TopLevel { | |
| 26 | + pub next: Option<Next>, | |
| 26 | 27 | |
| 27 | 28 | pub value: String, |
| 28 | 29 | } |
| 29 | - | |
| 30 | -#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 31 | -#[serde(untagged)] | |
| 32 | -pub enum Next { | |
| 33 | - Node(Node), | |
| 34 | - | |
| 35 | - PurpleString(String), | |
| 36 | -} |
Mschema-scala3-upickledefault / TopLevel.scala+8 −13
| @@ -7,27 +7,22 @@ import cats.syntax.functor._ | ||
| 7 | 7 | // If a union has a null in, then we'll need this too... |
| 8 | 8 | type NullValue = None.type |
| 9 | 9 | |
| 10 | -case class TopLevel ( | |
| 11 | - val next : Option[Next] = None, | |
| 12 | - val value : String | |
| 13 | -) derives Encoder.AsObject, Decoder | |
| 14 | - | |
| 15 | -case class Node ( | |
| 16 | - val next : Option[Next] = None, | |
| 17 | - val value : String | |
| 18 | -) derives Encoder.AsObject, Decoder | |
| 19 | - | |
| 20 | -type Next = Node | String | NullValue | |
| 10 | +type Next = String | TopLevel | NullValue | |
| 21 | 11 | given Decoder[Next] = { |
| 22 | 12 | List[Decoder[Next]]( |
| 23 | 13 | Decoder[String].widen, |
| 24 | - Decoder[Node].widen, | |
| 14 | + Decoder[TopLevel].widen, | |
| 25 | 15 | Decoder[NullValue].widen, |
| 26 | 16 | ).reduceLeft(_ or _) |
| 27 | 17 | } |
| 28 | 18 | |
| 29 | 19 | given Encoder[Next] = Encoder.instance { |
| 30 | 20 | case enc0 : String => Encoder.encodeString(enc0) |
| 31 | - case enc1 : Node => Encoder.AsObject[Node].apply(enc1) | |
| 21 | + case enc1 : TopLevel => Encoder.AsObject[TopLevel].apply(enc1) | |
| 32 | 22 | case enc2 : NullValue => Encoder.encodeNone(enc2) |
| 33 | 23 | } |
| 24 | + | |
| 25 | +case class TopLevel ( | |
| 26 | + val next : Option[Next] = None, | |
| 27 | + val value : String | |
| 28 | +) derives Encoder.AsObject, Decoder |
Mschema-scala3default / TopLevel.scala+8 −13
| @@ -65,26 +65,21 @@ object JsonExt: | ||
| 65 | 65 | end JsonExt |
| 66 | 66 | |
| 67 | 67 | |
| 68 | -case class TopLevel ( | |
| 69 | - val next : Option[Next] = None, | |
| 70 | - val value : String | |
| 71 | -) derives OptionPickler.ReadWriter | |
| 72 | - | |
| 73 | -case class Node ( | |
| 74 | - val next : Option[Next] = None, | |
| 75 | - val value : String | |
| 76 | -) derives OptionPickler.ReadWriter | |
| 77 | - | |
| 78 | -type Next = Node | String | NullValue | |
| 68 | +type Next = String | TopLevel | NullValue | |
| 79 | 69 | given unionReaderNext: OptionPickler.Reader[Next] = JsonExt.badMerge[Next]( |
| 80 | 70 | JsonExt.strictString, |
| 81 | - summon[OptionPickler.Reader[Node]], | |
| 71 | + summon[OptionPickler.Reader[TopLevel]], | |
| 82 | 72 | summon[OptionPickler.Reader[NullValue]], |
| 83 | 73 | ) |
| 84 | 74 | |
| 85 | 75 | given unionWriterNext: OptionPickler.Writer[Next] = OptionPickler.writer[ujson.Value].comap[Next]{ _v => |
| 86 | 76 | (_v: @unchecked) match |
| 87 | 77 | case v: String => OptionPickler.writeJs[String](v) |
| 88 | - case v: Node => OptionPickler.writeJs[Node](v) | |
| 78 | + case v: TopLevel => OptionPickler.writeJs[TopLevel](v) | |
| 89 | 79 | case v: NullValue => OptionPickler.writeJs[NullValue](v) |
| 90 | 80 | } |
| 81 | + | |
| 82 | +case class TopLevel ( | |
| 83 | + val next : Option[Next] = None, | |
| 84 | + val value : String | |
| 85 | +) derives OptionPickler.ReadWriter |
Mschema-schemadefault / TopLevel.schema+1 −17
| @@ -18,29 +18,13 @@ | ||
| 18 | 18 | ], |
| 19 | 19 | "title": "TopLevel" |
| 20 | 20 | }, |
| 21 | - "Node": { | |
| 22 | - "type": "object", | |
| 23 | - "additionalProperties": {}, | |
| 24 | - "properties": { | |
| 25 | - "next": { | |
| 26 | - "$ref": "#/definitions/Next" | |
| 27 | - }, | |
| 28 | - "value": { | |
| 29 | - "type": "string" | |
| 30 | - } | |
| 31 | - }, | |
| 32 | - "required": [ | |
| 33 | - "value" | |
| 34 | - ], | |
| 35 | - "title": "Node" | |
| 36 | - }, | |
| 37 | 21 | "Next": { |
| 38 | 22 | "anyOf": [ |
| 39 | 23 | { |
| 40 | 24 | "type": "null" |
| 41 | 25 | }, |
| 42 | 26 | { |
| 43 | - "$ref": "#/definitions/Node" | |
| 27 | + "$ref": "#/definitions/TopLevel" | |
| 44 | 28 | }, |
| 45 | 29 | { |
| 46 | 30 | "type": "string" |
Mschema-swiftdefault / quicktype.swift+44 −86
| @@ -5,56 +5,43 @@ | ||
| 5 | 5 | |
| 6 | 6 | import Foundation |
| 7 | 7 | |
| 8 | -// MARK: - TopLevel | |
| 9 | -struct TopLevel: Codable { | |
| 10 | - let next: Next? | |
| 11 | - let value: String | |
| 12 | - | |
| 13 | - enum CodingKeys: String, CodingKey { | |
| 14 | - case next = "next" | |
| 15 | - case value = "value" | |
| 16 | - } | |
| 17 | -} | |
| 18 | - | |
| 19 | -// MARK: TopLevel convenience initializers and mutators | |
| 20 | - | |
| 21 | -extension TopLevel { | |
| 22 | - init(data: Data) throws { | |
| 23 | - self = try newJSONDecoder().decode(TopLevel.self, from: data) | |
| 24 | - } | |
| 8 | +enum Next: Codable { | |
| 9 | + case string(String) | |
| 10 | + case topLevel(TopLevel) | |
| 11 | + case null | |
| 25 | 12 | |
| 26 | - init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 27 | - guard let data = json.data(using: encoding) else { | |
| 28 | - throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 13 | + init(from decoder: Decoder) throws { | |
| 14 | + let container = try decoder.singleValueContainer() | |
| 15 | + if let x = try? container.decode(String.self) { | |
| 16 | + self = .string(x) | |
| 17 | + return | |
| 29 | 18 | } |
| 30 | - try self.init(data: data) | |
| 31 | - } | |
| 32 | - | |
| 33 | - init(fromURL url: URL) throws { | |
| 34 | - try self.init(data: try Data(contentsOf: url)) | |
| 35 | - } | |
| 36 | - | |
| 37 | - func with( | |
| 38 | - next: Next?? = nil, | |
| 39 | - value: String? = nil | |
| 40 | - ) -> TopLevel { | |
| 41 | - return TopLevel( | |
| 42 | - next: next ?? self.next, | |
| 43 | - value: value ?? self.value | |
| 44 | - ) | |
| 45 | - } | |
| 46 | - | |
| 47 | - func jsonData() throws -> Data { | |
| 48 | - return try newJSONEncoder().encode(self) | |
| 19 | + if let x = try? container.decode(TopLevel.self) { | |
| 20 | + self = .topLevel(x) | |
| 21 | + return | |
| 22 | + } | |
| 23 | + if container.decodeNil() { | |
| 24 | + self = .null | |
| 25 | + return | |
| 26 | + } | |
| 27 | + throw DecodingError.typeMismatch(Next.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Next")) | |
| 49 | 28 | } |
| 50 | 29 | |
| 51 | - func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 52 | - return String(data: try self.jsonData(), encoding: encoding) | |
| 30 | + func encode(to encoder: Encoder) throws { | |
| 31 | + var container = encoder.singleValueContainer() | |
| 32 | + switch self { | |
| 33 | + case .string(let x): | |
| 34 | + try container.encode(x) | |
| 35 | + case .topLevel(let x): | |
| 36 | + try container.encode(x) | |
| 37 | + case .null: | |
| 38 | + try container.encodeNil() | |
| 39 | + } | |
| 53 | 40 | } |
| 54 | 41 | } |
| 55 | 42 | |
| 56 | -// MARK: - Node | |
| 57 | -struct Node: Codable { | |
| 43 | +// MARK: - TopLevel | |
| 44 | +class TopLevel: Codable { | |
| 58 | 45 | let next: Next? |
| 59 | 46 | let value: String |
| 60 | 47 | |
| @@ -62,31 +49,37 @@ struct Node: Codable { | ||
| 62 | 49 | case next = "next" |
| 63 | 50 | case value = "value" |
| 64 | 51 | } |
| 52 | + | |
| 53 | + init(next: Next?, value: String) { | |
| 54 | + self.next = next | |
| 55 | + self.value = value | |
| 56 | + } | |
| 65 | 57 | } |
| 66 | 58 | |
| 67 | -// MARK: Node convenience initializers and mutators | |
| 59 | +// MARK: TopLevel convenience initializers and mutators | |
| 68 | 60 | |
| 69 | -extension Node { | |
| 70 | - init(data: Data) throws { | |
| 71 | - self = try newJSONDecoder().decode(Node.self, from: data) | |
| 61 | +extension TopLevel { | |
| 62 | + convenience init(data: Data) throws { | |
| 63 | + let me = try newJSONDecoder().decode(TopLevel.self, from: data) | |
| 64 | + self.init(next: me.next, value: me.value) | |
| 72 | 65 | } |
| 73 | 66 | |
| 74 | - init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 67 | + convenience init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 75 | 68 | guard let data = json.data(using: encoding) else { |
| 76 | 69 | throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) |
| 77 | 70 | } |
| 78 | 71 | try self.init(data: data) |
| 79 | 72 | } |
| 80 | 73 | |
| 81 | - init(fromURL url: URL) throws { | |
| 74 | + convenience init(fromURL url: URL) throws { | |
| 82 | 75 | try self.init(data: try Data(contentsOf: url)) |
| 83 | 76 | } |
| 84 | 77 | |
| 85 | 78 | func with( |
| 86 | 79 | next: Next?? = nil, |
| 87 | 80 | value: String? = nil |
| 88 | - ) -> Node { | |
| 89 | - return Node( | |
| 81 | + ) -> TopLevel { | |
| 82 | + return TopLevel( | |
| 90 | 83 | next: next ?? self.next, |
| 91 | 84 | value: value ?? self.value |
| 92 | 85 | ) |
| @@ -101,41 +94,6 @@ extension Node { | ||
| 101 | 94 | } |
| 102 | 95 | } |
| 103 | 96 | |
| 104 | -indirect enum Next: Codable { | |
| 105 | - case node(Node) | |
| 106 | - case string(String) | |
| 107 | - case null | |
| 108 | - | |
| 109 | - init(from decoder: Decoder) throws { | |
| 110 | - let container = try decoder.singleValueContainer() | |
| 111 | - if let x = try? container.decode(String.self) { | |
| 112 | - self = .string(x) | |
| 113 | - return | |
| 114 | - } | |
| 115 | - if let x = try? container.decode(Node.self) { | |
| 116 | - self = .node(x) | |
| 117 | - return | |
| 118 | - } | |
| 119 | - if container.decodeNil() { | |
| 120 | - self = .null | |
| 121 | - return | |
| 122 | - } | |
| 123 | - throw DecodingError.typeMismatch(Next.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Next")) | |
| 124 | - } | |
| 125 | - | |
| 126 | - func encode(to encoder: Encoder) throws { | |
| 127 | - var container = encoder.singleValueContainer() | |
| 128 | - switch self { | |
| 129 | - case .node(let x): | |
| 130 | - try container.encode(x) | |
| 131 | - case .string(let x): | |
| 132 | - try container.encode(x) | |
| 133 | - case .null: | |
| 134 | - try container.encodeNil() | |
| 135 | - } | |
| 136 | - } | |
| 137 | -} | |
| 138 | - | |
| 139 | 97 | // MARK: - Helper functions for creating encoders and decoders |
| 140 | 98 | |
| 141 | 99 | func newJSONDecoder() -> JSONDecoder { |
Mschema-typescript-zoddefault / TopLevel.ts+8 −18
| @@ -9,31 +9,24 @@ | ||
| 9 | 9 | |
| 10 | 10 | export type TopLevel = TopLevelElement[] | boolean | number | number | { [key: string]: unknown } | null | string; |
| 11 | 11 | |
| 12 | -export type TopLevelElement = unknown[] | boolean | number | null | TopLevelObject | string; | |
| 12 | +export type PurpleTopLevel = TopLevelElement[] | boolean | number | { [key: string]: unknown } | null | string; | |
| 13 | 13 | |
| 14 | -export interface TopLevelObject { | |
| 15 | - x?: X; | |
| 14 | +export interface A { | |
| 15 | + x?: PurpleTopLevel; | |
| 16 | 16 | [property: string]: unknown; |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | -export interface XObject { | |
| 20 | - x?: X; | |
| 21 | - [property: string]: unknown; | |
| 22 | -} | |
| 23 | - | |
| 24 | -export type XElement = unknown[] | boolean | number | null | XObject | string; | |
| 25 | - | |
| 26 | -export type X = XElement[] | boolean | number | { [key: string]: unknown } | null | string; | |
| 19 | +export type TopLevelElement = unknown[] | boolean | number | null | A | string; | |
| 27 | 20 | |
| 28 | 21 | // Converts JSON strings to/from your types |
| 29 | 22 | // and asserts the results of JSON.parse at runtime |
| 30 | 23 | export class Convert { |
| 31 | 24 | public static toTopLevel(json: string): TopLevel { |
| 32 | - return cast(JSON.parse(json), u(a(u(a("any"), true, 3.14, null, r("TopLevelObject"), "")), true, 3.14, 0, m("any"), null, "")); | |
| 25 | + return cast(JSON.parse(json), u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, 0, m("any"), null, "")); | |
| 33 | 26 | } |
| 34 | 27 | |
| 35 | 28 | public static topLevelToJson(value: TopLevel): string { |
| 36 | - return JSON.stringify(uncast(value, u(a(u(a("any"), true, 3.14, null, r("TopLevelObject"), "")), true, 3.14, 0, m("any"), null, "")), null, 2); | |
| 29 | + return JSON.stringify(uncast(value, u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, 0, m("any"), null, "")), null, 2); | |
| 37 | 30 | } |
| 38 | 31 | } |
| 39 | 32 | |
| @@ -191,10 +184,7 @@ function r(name: string) { | ||
| 191 | 184 | } |
| 192 | 185 | |
| 193 | 186 | const typeMap: any = { |
| 194 | - "TopLevelObject": o([ | |
| 195 | - { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("XObject"), "")), true, 3.14, m("any"), null, "")) }, | |
| 196 | - ], "any"), | |
| 197 | - "XObject": o([ | |
| 198 | - { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("XObject"), "")), true, 3.14, m("any"), null, "")) }, | |
| 187 | + "A": o([ | |
| 188 | + { json: "x", js: "x", typ: u(undefined, u(a(u(a("any"), true, 3.14, null, r("A"), "")), true, 3.14, m("any"), null, "")) }, | |
| 199 | 189 | ], "any"), |
| 200 | 190 | }; |
Mschema-typescriptdefault / TopLevel.ts+4 −10
| @@ -1,19 +1,13 @@ | ||
| 1 | 1 | import * as z from "zod"; |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -export type Node = { | |
| 5 | - "next"?: Node | null | string; | |
| 4 | +export type TopLevel = { | |
| 5 | + "next"?: TopLevel | null | string; | |
| 6 | 6 | "value": string; |
| 7 | 7 | }; |
| 8 | -export const NodeSchema: z.ZodType<Node> = z.lazy(() => | |
| 8 | +export const TopLevelSchema: z.ZodType<TopLevel> = z.lazy(() => | |
| 9 | 9 | z.object({ |
| 10 | - "next": z.union([z.null(), NodeSchema, z.string()]).optional(), | |
| 10 | + "next": z.union([z.null(), TopLevelSchema, z.string()]).optional(), | |
| 11 | 11 | "value": z.string(), |
| 12 | 12 | }) |
| 13 | 13 | ); |
| 14 | - | |
| 15 | -export const TopLevelSchema = z.object({ | |
| 16 | - "next": z.union([z.null(), NodeSchema, z.string()]).optional(), | |
| 17 | - "value": z.string(), | |
| 18 | -}); | |
| 19 | -export type TopLevel = z.infer<typeof TopLevelSchema>; |
No generated files match these filters.