Test case
1 generated file · +12 −10test/inputs/json/misc/00c36.json
Mtypescriptdefault / TopLevel.ts+12 −10
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = TopLevelElement[] | PurpleTopLevel; | |
| 10 | +export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel; | |
| 11 | 11 | |
| 12 | -export interface TopLevelElement { | |
| 12 | +export interface PurpleTopLevel { | |
| 13 | 13 | country: Country; |
| 14 | 14 | date: string; |
| 15 | 15 | decimal: string; |
| @@ -26,22 +26,24 @@ export type ID = "US" | "NY.GDP.MKTP.CD"; | ||
| 26 | 26 | |
| 27 | 27 | export type Value = "United States" | "GDP (current US$)"; |
| 28 | 28 | |
| 29 | -export interface PurpleTopLevel { | |
| 29 | +export interface FluffyTopLevel { | |
| 30 | 30 | page: number; |
| 31 | 31 | pages: number; |
| 32 | 32 | per_page: string; |
| 33 | 33 | total: number; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | +export type TopLevel = TopLevelUnion[]; | |
| 37 | + | |
| 36 | 38 | // Converts JSON strings to/from your types |
| 37 | 39 | // and asserts the results of JSON.parse at runtime |
| 38 | 40 | export class Convert { |
| 39 | - public static toTopLevel(json: string): TopLevel[] { | |
| 40 | - return cast(JSON.parse(json), a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))); | |
| 41 | + public static toTopLevel(json: string): TopLevel { | |
| 42 | + return cast(JSON.parse(json), a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))); | |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 44 | - return JSON.stringify(uncast(value, a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))), null, 2); | |
| 45 | + public static topLevelToJson(value: TopLevel): string { | |
| 46 | + return JSON.stringify(uncast(value, a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))), null, 2); | |
| 45 | 47 | } |
| 46 | 48 | } |
| 47 | 49 | |
| @@ -199,7 +201,7 @@ function r(name: string) { | ||
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | const typeMap: any = { |
| 202 | - "TopLevelElement": o([ | |
| 204 | + "PurpleTopLevel": o([ | |
| 203 | 205 | { json: "country", js: "country", typ: r("Country") }, |
| 204 | 206 | { json: "date", js: "date", typ: "" }, |
| 205 | 207 | { json: "decimal", js: "decimal", typ: "" }, |
| @@ -210,7 +212,7 @@ const typeMap: any = { | ||
| 210 | 212 | { json: "id", js: "id", typ: r("ID") }, |
| 211 | 213 | { json: "value", js: "value", typ: r("Value") }, |
| 212 | 214 | ], false), |
| 213 | - "PurpleTopLevel": o([ | |
| 215 | + "FluffyTopLevel": o([ | |
| 214 | 216 | { json: "page", js: "page", typ: 0 }, |
| 215 | 217 | { json: "pages", js: "pages", typ: 0 }, |
| 216 | 218 | { json: "per_page", js: "per_page", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/010b1.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | age: number; |
| 12 | 12 | country: Country; |
| 13 | 13 | females: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | |
| 19 | 19 | export type Country = "United States"; |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "age", js: "age", typ: 0 }, |
| 189 | 191 | { json: "country", js: "country", typ: r("Country") }, |
| 190 | 192 | { json: "females", js: "females", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/050b0.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -49,15 +49,17 @@ export type MediaType = "text/html" | "text/plain"; | ||
| 49 | 49 | |
| 50 | 50 | export type Title = "HTML" | "Plain Text"; |
| 51 | 51 | |
| 52 | +export type TopLevel = TopLevelElement[]; | |
| 53 | + | |
| 52 | 54 | // Converts JSON strings to/from your types |
| 53 | 55 | // and asserts the results of JSON.parse at runtime |
| 54 | 56 | export class Convert { |
| 55 | - public static toTopLevel(json: string): TopLevel[] { | |
| 56 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 57 | + public static toTopLevel(json: string): TopLevel { | |
| 58 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 57 | 59 | } |
| 58 | 60 | |
| 59 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 60 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 61 | + public static topLevelToJson(value: TopLevel): string { | |
| 62 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 61 | 63 | } |
| 62 | 64 | } |
| 63 | 65 | |
| @@ -215,7 +217,7 @@ function r(name: string) { | ||
| 215 | 217 | } |
| 216 | 218 | |
| 217 | 219 | const typeMap: any = { |
| 218 | - "TopLevel": o([ | |
| 220 | + "TopLevelElement": o([ | |
| 219 | 221 | { json: "id", js: "id", typ: "" }, |
| 220 | 222 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 221 | 223 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/06bee.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -44,15 +44,17 @@ export type MediaType = "text/html"; | ||
| 44 | 44 | |
| 45 | 45 | export type Title = "HTML"; |
| 46 | 46 | |
| 47 | +export type TopLevel = TopLevelElement[]; | |
| 48 | + | |
| 47 | 49 | // Converts JSON strings to/from your types |
| 48 | 50 | // and asserts the results of JSON.parse at runtime |
| 49 | 51 | export class Convert { |
| 50 | - public static toTopLevel(json: string): TopLevel[] { | |
| 51 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 52 | + public static toTopLevel(json: string): TopLevel { | |
| 53 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 55 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 56 | + public static topLevelToJson(value: TopLevel): string { | |
| 57 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 56 | 58 | } |
| 57 | 59 | } |
| 58 | 60 | |
| @@ -210,7 +212,7 @@ function r(name: string) { | ||
| 210 | 212 | } |
| 211 | 213 | |
| 212 | 214 | const typeMap: any = { |
| 213 | - "TopLevel": o([ | |
| 215 | + "TopLevelElement": o([ | |
| 214 | 216 | { json: "id", js: "id", typ: "" }, |
| 215 | 217 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 216 | 218 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/07c75.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: string[]; |
| @@ -34,15 +34,17 @@ export interface Text { | ||
| 34 | 34 | url: string; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | +export type TopLevel = TopLevelElement[]; | |
| 38 | + | |
| 37 | 39 | // Converts JSON strings to/from your types |
| 38 | 40 | // and asserts the results of JSON.parse at runtime |
| 39 | 41 | export class Convert { |
| 40 | - public static toTopLevel(json: string): TopLevel[] { | |
| 41 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 42 | + public static toTopLevel(json: string): TopLevel { | |
| 43 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 42 | 44 | } |
| 43 | 45 | |
| 44 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 45 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 46 | + public static topLevelToJson(value: TopLevel): string { | |
| 47 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 46 | 48 | } |
| 47 | 49 | } |
| 48 | 50 | |
| @@ -200,7 +202,7 @@ function r(name: string) { | ||
| 200 | 202 | } |
| 201 | 203 | |
| 202 | 204 | const typeMap: any = { |
| 203 | - "TopLevel": o([ | |
| 205 | + "TopLevelElement": o([ | |
| 204 | 206 | { json: "id", js: "id", typ: "" }, |
| 205 | 207 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 206 | 208 | { json: "keywords", js: "keywords", typ: a("") }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/0a91a.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | actor: Actor; |
| 12 | 12 | created_at: Date; |
| 13 | 13 | id: string; |
| @@ -224,15 +224,17 @@ export interface TopLevelRepo { | ||
| 224 | 224 | |
| 225 | 225 | export type Type = "PushEvent" | "CreateEvent" | "WatchEvent" | "PullRequestEvent" | "DeleteEvent"; |
| 226 | 226 | |
| 227 | +export type TopLevel = TopLevelElement[]; | |
| 228 | + | |
| 227 | 229 | // Converts JSON strings to/from your types |
| 228 | 230 | // and asserts the results of JSON.parse at runtime |
| 229 | 231 | export class Convert { |
| 230 | - public static toTopLevel(json: string): TopLevel[] { | |
| 231 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 232 | + public static toTopLevel(json: string): TopLevel { | |
| 233 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 232 | 234 | } |
| 233 | 235 | |
| 234 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 235 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 236 | + public static topLevelToJson(value: TopLevel): string { | |
| 237 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 236 | 238 | } |
| 237 | 239 | } |
| 238 | 240 | |
| @@ -390,7 +392,7 @@ function r(name: string) { | ||
| 390 | 392 | } |
| 391 | 393 | |
| 392 | 394 | const typeMap: any = { |
| 393 | - "TopLevel": o([ | |
| 395 | + "TopLevelElement": o([ | |
| 394 | 396 | { json: "actor", js: "actor", typ: r("Actor") }, |
| 395 | 397 | { json: "created_at", js: "created_at", typ: Date }, |
| 396 | 398 | { json: "id", js: "id", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/10be4.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -49,15 +49,17 @@ export type MediaType = "text/html" | "text/plain" | "application/pdf"; | ||
| 49 | 49 | |
| 50 | 50 | export type Title = "HTML" | "Plain Text" | "PDF"; |
| 51 | 51 | |
| 52 | +export type TopLevel = TopLevelElement[]; | |
| 53 | + | |
| 52 | 54 | // Converts JSON strings to/from your types |
| 53 | 55 | // and asserts the results of JSON.parse at runtime |
| 54 | 56 | export class Convert { |
| 55 | - public static toTopLevel(json: string): TopLevel[] { | |
| 56 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 57 | + public static toTopLevel(json: string): TopLevel { | |
| 58 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 57 | 59 | } |
| 58 | 60 | |
| 59 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 60 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 61 | + public static topLevelToJson(value: TopLevel): string { | |
| 62 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 61 | 63 | } |
| 62 | 64 | } |
| 63 | 65 | |
| @@ -215,7 +217,7 @@ function r(name: string) { | ||
| 215 | 217 | } |
| 216 | 218 | |
| 217 | 219 | const typeMap: any = { |
| 218 | - "TopLevel": o([ | |
| 220 | + "TopLevelElement": o([ | |
| 219 | 221 | { json: "id", js: "id", typ: "" }, |
| 220 | 222 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 221 | 223 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/13d8d.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | category: string; |
| 12 | 12 | context: string; |
| 13 | 13 | id: number; |
| @@ -35,15 +35,17 @@ export interface OutcomeStatus { | ||
| 35 | 35 | date: string; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | +export type TopLevel = TopLevelElement[]; | |
| 39 | + | |
| 38 | 40 | // Converts JSON strings to/from your types |
| 39 | 41 | // and asserts the results of JSON.parse at runtime |
| 40 | 42 | export class Convert { |
| 41 | - public static toTopLevel(json: string): TopLevel[] { | |
| 42 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 43 | + public static toTopLevel(json: string): TopLevel { | |
| 44 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 43 | 45 | } |
| 44 | 46 | |
| 45 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 46 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 47 | + public static topLevelToJson(value: TopLevel): string { | |
| 48 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 47 | 49 | } |
| 48 | 50 | } |
| 49 | 51 | |
| @@ -201,7 +203,7 @@ function r(name: string) { | ||
| 201 | 203 | } |
| 202 | 204 | |
| 203 | 205 | const typeMap: any = { |
| 204 | - "TopLevel": o([ | |
| 206 | + "TopLevelElement": o([ | |
| 205 | 207 | { json: "category", js: "category", typ: "" }, |
| 206 | 208 | { json: "context", js: "context", typ: "" }, |
| 207 | 209 | { json: "id", js: "id", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/1a7f5.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | age: number; |
| 12 | 12 | country: Country; |
| 13 | 13 | females: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | |
| 19 | 19 | export type Country = "United States"; |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "age", js: "age", typ: 0 }, |
| 189 | 191 | { json: "country", js: "country", typ: r("Country") }, |
| 190 | 192 | { json: "females", js: "females", typ: 0 }, |
Test case
1 generated file · +12 −10test/inputs/json/misc/2df80.json
Mtypescriptdefault / TopLevel.ts+12 −10
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = TopLevelElement[] | PurpleTopLevel; | |
| 10 | +export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel; | |
| 11 | 11 | |
| 12 | -export interface TopLevelElement { | |
| 12 | +export interface PurpleTopLevel { | |
| 13 | 13 | adminregion: Adminregion; |
| 14 | 14 | capitalCity: string; |
| 15 | 15 | id: string; |
| @@ -31,22 +31,24 @@ export type ID = "" | "SAS" | "SSA" | "ECA" | "LAC" | "EAP" | "MNA" | "HIC" | "L | ||
| 31 | 31 | |
| 32 | 32 | export type Value = "" | "South Asia" | "Sub-Saharan Africa (excluding high income)" | "Europe & Central Asia (excluding high income)" | "Latin America & Caribbean (excluding high income)" | "East Asia & Pacific (excluding high income)" | "Middle East & North Africa (excluding high income)" | "High income" | "Low income" | "Aggregates" | "Lower middle income" | "Upper middle income" | "Not classified" | "IDA" | "IBRD" | "Blend" | "Latin America & Caribbean " | "Sub-Saharan Africa " | "Europe & Central Asia" | "Middle East & North Africa" | "East Asia & Pacific" | "North America"; |
| 33 | 33 | |
| 34 | -export interface PurpleTopLevel { | |
| 34 | +export interface FluffyTopLevel { | |
| 35 | 35 | page: number; |
| 36 | 36 | pages: number; |
| 37 | 37 | per_page: string; |
| 38 | 38 | total: number; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | +export type TopLevel = TopLevelUnion[]; | |
| 42 | + | |
| 41 | 43 | // Converts JSON strings to/from your types |
| 42 | 44 | // and asserts the results of JSON.parse at runtime |
| 43 | 45 | export class Convert { |
| 44 | - public static toTopLevel(json: string): TopLevel[] { | |
| 45 | - return cast(JSON.parse(json), a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))); | |
| 46 | + public static toTopLevel(json: string): TopLevel { | |
| 47 | + return cast(JSON.parse(json), a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))); | |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 49 | - return JSON.stringify(uncast(value, a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))), null, 2); | |
| 50 | + public static topLevelToJson(value: TopLevel): string { | |
| 51 | + return JSON.stringify(uncast(value, a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))), null, 2); | |
| 50 | 52 | } |
| 51 | 53 | } |
| 52 | 54 | |
| @@ -204,7 +206,7 @@ function r(name: string) { | ||
| 204 | 206 | } |
| 205 | 207 | |
| 206 | 208 | const typeMap: any = { |
| 207 | - "TopLevelElement": o([ | |
| 209 | + "PurpleTopLevel": o([ | |
| 208 | 210 | { json: "adminregion", js: "adminregion", typ: r("Adminregion") }, |
| 209 | 211 | { json: "capitalCity", js: "capitalCity", typ: "" }, |
| 210 | 212 | { json: "id", js: "id", typ: "" }, |
| @@ -220,7 +222,7 @@ const typeMap: any = { | ||
| 220 | 222 | { json: "id", js: "id", typ: r("ID") }, |
| 221 | 223 | { json: "value", js: "value", typ: r("Value") }, |
| 222 | 224 | ], false), |
| 223 | - "PurpleTopLevel": o([ | |
| 225 | + "FluffyTopLevel": o([ | |
| 224 | 226 | { json: "page", js: "page", typ: 0 }, |
| 225 | 227 | { json: "pages", js: "pages", typ: 0 }, |
| 226 | 228 | { json: "per_page", js: "per_page", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/32d5c.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | BirthDate: Date | null; |
| 12 | 12 | BirthDateIsProtected: boolean; |
| 13 | 13 | GenderTypeID: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | PreferredName: string; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "BirthDate", js: "BirthDate", typ: u(Date, null) }, |
| 189 | 191 | { json: "BirthDateIsProtected", js: "BirthDateIsProtected", typ: true }, |
| 190 | 192 | { json: "GenderTypeID", js: "GenderTypeID", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/3536b.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -38,15 +38,17 @@ export interface Text { | ||
| 38 | 38 | url: string; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | +export type TopLevel = TopLevelElement[]; | |
| 42 | + | |
| 41 | 43 | // Converts JSON strings to/from your types |
| 42 | 44 | // and asserts the results of JSON.parse at runtime |
| 43 | 45 | export class Convert { |
| 44 | - public static toTopLevel(json: string): TopLevel[] { | |
| 45 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 46 | + public static toTopLevel(json: string): TopLevel { | |
| 47 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 49 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 50 | + public static topLevelToJson(value: TopLevel): string { | |
| 51 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 50 | 52 | } |
| 51 | 53 | } |
| 52 | 54 | |
| @@ -204,7 +206,7 @@ function r(name: string) { | ||
| 204 | 206 | } |
| 205 | 207 | |
| 206 | 208 | const typeMap: any = { |
| 207 | - "TopLevel": o([ | |
| 209 | + "TopLevelElement": o([ | |
| 208 | 210 | { json: "id", js: "id", typ: "" }, |
| 209 | 211 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 210 | 212 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/43970.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,27 +1,29 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | ID: number; |
| 12 | 12 | Name: string; |
| 13 | 13 | Notes: string; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | +export type TopLevel = TopLevelElement[]; | |
| 17 | + | |
| 16 | 18 | // Converts JSON strings to/from your types |
| 17 | 19 | // and asserts the results of JSON.parse at runtime |
| 18 | 20 | export class Convert { |
| 19 | - public static toTopLevel(json: string): TopLevel[] { | |
| 20 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 21 | + public static toTopLevel(json: string): TopLevel { | |
| 22 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 24 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 25 | + public static topLevelToJson(value: TopLevel): string { | |
| 26 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 25 | 27 | } |
| 26 | 28 | } |
| 27 | 29 | |
| @@ -179,7 +181,7 @@ function r(name: string) { | ||
| 179 | 181 | } |
| 180 | 182 | |
| 181 | 183 | const typeMap: any = { |
| 182 | - "TopLevel": o([ | |
| 184 | + "TopLevelElement": o([ | |
| 183 | 185 | { json: "ID", js: "ID", typ: 0 }, |
| 184 | 186 | { json: "Name", js: "Name", typ: "" }, |
| 185 | 187 | { json: "Notes", js: "Notes", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/570ec.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: string[]; |
| @@ -41,15 +41,17 @@ export interface Text { | ||
| 41 | 41 | url: string; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | +export type TopLevel = TopLevelElement[]; | |
| 45 | + | |
| 44 | 46 | // Converts JSON strings to/from your types |
| 45 | 47 | // and asserts the results of JSON.parse at runtime |
| 46 | 48 | export class Convert { |
| 47 | - public static toTopLevel(json: string): TopLevel[] { | |
| 48 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 49 | + public static toTopLevel(json: string): TopLevel { | |
| 50 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 49 | 51 | } |
| 50 | 52 | |
| 51 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 52 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 53 | + public static topLevelToJson(value: TopLevel): string { | |
| 54 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 53 | 55 | } |
| 54 | 56 | } |
| 55 | 57 | |
| @@ -207,7 +209,7 @@ function r(name: string) { | ||
| 207 | 209 | } |
| 208 | 210 | |
| 209 | 211 | const typeMap: any = { |
| 210 | - "TopLevel": o([ | |
| 212 | + "TopLevelElement": o([ | |
| 211 | 213 | { json: "id", js: "id", typ: "" }, |
| 212 | 214 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 213 | 215 | { json: "keywords", js: "keywords", typ: a("") }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/5eae5.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,28 +1,30 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | Date: Date; |
| 12 | 12 | ID: number; |
| 13 | 13 | Sponsor: string; |
| 14 | 14 | Title: string; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | +export type TopLevel = TopLevelElement[]; | |
| 18 | + | |
| 17 | 19 | // Converts JSON strings to/from your types |
| 18 | 20 | // and asserts the results of JSON.parse at runtime |
| 19 | 21 | export class Convert { |
| 20 | - public static toTopLevel(json: string): TopLevel[] { | |
| 21 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 22 | + public static toTopLevel(json: string): TopLevel { | |
| 23 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 22 | 24 | } |
| 23 | 25 | |
| 24 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 25 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 26 | + public static topLevelToJson(value: TopLevel): string { | |
| 27 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 26 | 28 | } |
| 27 | 29 | } |
| 28 | 30 | |
| @@ -180,7 +182,7 @@ function r(name: string) { | ||
| 180 | 182 | } |
| 181 | 183 | |
| 182 | 184 | const typeMap: any = { |
| 183 | - "TopLevel": o([ | |
| 185 | + "TopLevelElement": o([ | |
| 184 | 186 | { json: "Date", js: "Date", typ: Date }, |
| 185 | 187 | { json: "ID", js: "ID", typ: 0 }, |
| 186 | 188 | { json: "Sponsor", js: "Sponsor", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/66121.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -44,15 +44,17 @@ export type MediaType = "text/html"; | ||
| 44 | 44 | |
| 45 | 45 | export type Title = "HTML"; |
| 46 | 46 | |
| 47 | +export type TopLevel = TopLevelElement[]; | |
| 48 | + | |
| 47 | 49 | // Converts JSON strings to/from your types |
| 48 | 50 | // and asserts the results of JSON.parse at runtime |
| 49 | 51 | export class Convert { |
| 50 | - public static toTopLevel(json: string): TopLevel[] { | |
| 51 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 52 | + public static toTopLevel(json: string): TopLevel { | |
| 53 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 55 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 56 | + public static topLevelToJson(value: TopLevel): string { | |
| 57 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 56 | 58 | } |
| 57 | 59 | } |
| 58 | 60 | |
| @@ -210,7 +212,7 @@ function r(name: string) { | ||
| 210 | 212 | } |
| 211 | 213 | |
| 212 | 214 | const typeMap: any = { |
| 213 | - "TopLevel": o([ | |
| 215 | + "TopLevelElement": o([ | |
| 214 | 216 | { json: "id", js: "id", typ: "" }, |
| 215 | 217 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 216 | 218 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/6eb00.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,27 +1,29 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | DirectorateID: number; |
| 12 | 12 | Id: number; |
| 13 | 13 | Name: string; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | +export type TopLevel = TopLevelElement[]; | |
| 17 | + | |
| 16 | 18 | // Converts JSON strings to/from your types |
| 17 | 19 | // and asserts the results of JSON.parse at runtime |
| 18 | 20 | export class Convert { |
| 19 | - public static toTopLevel(json: string): TopLevel[] { | |
| 20 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 21 | + public static toTopLevel(json: string): TopLevel { | |
| 22 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 24 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 25 | + public static topLevelToJson(value: TopLevel): string { | |
| 26 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 25 | 27 | } |
| 26 | 28 | } |
| 27 | 29 | |
| @@ -179,7 +181,7 @@ function r(name: string) { | ||
| 179 | 181 | } |
| 180 | 182 | |
| 181 | 183 | const typeMap: any = { |
| 182 | - "TopLevel": o([ | |
| 184 | + "TopLevelElement": o([ | |
| 183 | 185 | { json: "DirectorateID", js: "DirectorateID", typ: 0 }, |
| 184 | 186 | { json: "Id", js: "Id", typ: 0 }, |
| 185 | 187 | { json: "Name", js: "Name", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/77392.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | designation: string; |
| 12 | 12 | discovery_date: Date; |
| 13 | 13 | h_mag?: string; |
| @@ -24,15 +24,17 @@ export type OrbitClass = "Apollo" | "Amor" | "Aten" | "Comet" | "Jupiter-family | ||
| 24 | 24 | |
| 25 | 25 | export type Pha = "Y" | "N" | "n/a"; |
| 26 | 26 | |
| 27 | +export type TopLevel = TopLevelElement[]; | |
| 28 | + | |
| 27 | 29 | // Converts JSON strings to/from your types |
| 28 | 30 | // and asserts the results of JSON.parse at runtime |
| 29 | 31 | export class Convert { |
| 30 | - public static toTopLevel(json: string): TopLevel[] { | |
| 31 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 32 | + public static toTopLevel(json: string): TopLevel { | |
| 33 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 35 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 36 | + public static topLevelToJson(value: TopLevel): string { | |
| 37 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 36 | 38 | } |
| 37 | 39 | } |
| 38 | 40 | |
| @@ -190,7 +192,7 @@ function r(name: string) { | ||
| 190 | 192 | } |
| 191 | 193 | |
| 192 | 194 | const typeMap: any = { |
| 193 | - "TopLevel": o([ | |
| 195 | + "TopLevelElement": o([ | |
| 194 | 196 | { json: "designation", js: "designation", typ: "" }, |
| 195 | 197 | { json: "discovery_date", js: "discovery_date", typ: Date }, |
| 196 | 198 | { json: "h_mag", js: "h_mag", typ: u(undefined, "") }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/7f568.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | comments: number; |
| 12 | 12 | comments_url: string; |
| 13 | 13 | commits_url: string; |
| @@ -38,15 +38,17 @@ export type Language = "Markdown"; | ||
| 38 | 38 | |
| 39 | 39 | export type Type = "text/plain"; |
| 40 | 40 | |
| 41 | +export type TopLevel = TopLevelElement[]; | |
| 42 | + | |
| 41 | 43 | // Converts JSON strings to/from your types |
| 42 | 44 | // and asserts the results of JSON.parse at runtime |
| 43 | 45 | export class Convert { |
| 44 | - public static toTopLevel(json: string): TopLevel[] { | |
| 45 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 46 | + public static toTopLevel(json: string): TopLevel { | |
| 47 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 49 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 50 | + public static topLevelToJson(value: TopLevel): string { | |
| 51 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 50 | 52 | } |
| 51 | 53 | } |
| 52 | 54 | |
| @@ -204,7 +206,7 @@ function r(name: string) { | ||
| 204 | 206 | } |
| 205 | 207 | |
| 206 | 208 | const typeMap: any = { |
| 207 | - "TopLevel": o([ | |
| 209 | + "TopLevelElement": o([ | |
| 208 | 210 | { json: "comments", js: "comments", typ: 0 }, |
| 209 | 211 | { json: "comments_url", js: "comments_url", typ: "" }, |
| 210 | 212 | { json: "commits_url", js: "commits_url", typ: "" }, |
Test case
1 generated file · +12 −10test/inputs/json/misc/7fbfb.json
Mtypescriptdefault / TopLevel.ts+12 −10
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = TopLevelElement[] | PurpleTopLevel; | |
| 10 | +export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel; | |
| 11 | 11 | |
| 12 | -export interface TopLevelElement { | |
| 12 | +export interface PurpleTopLevel { | |
| 13 | 13 | country: Country; |
| 14 | 14 | date: string; |
| 15 | 15 | decimal: string; |
| @@ -26,22 +26,24 @@ export type ID = "CN" | "NY.GDP.MKTP.CD"; | ||
| 26 | 26 | |
| 27 | 27 | export type Value = "China" | "GDP (current US$)"; |
| 28 | 28 | |
| 29 | -export interface PurpleTopLevel { | |
| 29 | +export interface FluffyTopLevel { | |
| 30 | 30 | page: number; |
| 31 | 31 | pages: number; |
| 32 | 32 | per_page: string; |
| 33 | 33 | total: number; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | +export type TopLevel = TopLevelUnion[]; | |
| 37 | + | |
| 36 | 38 | // Converts JSON strings to/from your types |
| 37 | 39 | // and asserts the results of JSON.parse at runtime |
| 38 | 40 | export class Convert { |
| 39 | - public static toTopLevel(json: string): TopLevel[] { | |
| 40 | - return cast(JSON.parse(json), a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))); | |
| 41 | + public static toTopLevel(json: string): TopLevel { | |
| 42 | + return cast(JSON.parse(json), a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))); | |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 44 | - return JSON.stringify(uncast(value, a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))), null, 2); | |
| 45 | + public static topLevelToJson(value: TopLevel): string { | |
| 46 | + return JSON.stringify(uncast(value, a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))), null, 2); | |
| 45 | 47 | } |
| 46 | 48 | } |
| 47 | 49 | |
| @@ -199,7 +201,7 @@ function r(name: string) { | ||
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | const typeMap: any = { |
| 202 | - "TopLevelElement": o([ | |
| 204 | + "PurpleTopLevel": o([ | |
| 203 | 205 | { json: "country", js: "country", typ: r("Country") }, |
| 204 | 206 | { json: "date", js: "date", typ: "" }, |
| 205 | 207 | { json: "decimal", js: "decimal", typ: "" }, |
| @@ -210,7 +212,7 @@ const typeMap: any = { | ||
| 210 | 212 | { json: "id", js: "id", typ: r("ID") }, |
| 211 | 213 | { json: "value", js: "value", typ: r("Value") }, |
| 212 | 214 | ], false), |
| 213 | - "PurpleTopLevel": o([ | |
| 215 | + "FluffyTopLevel": o([ | |
| 214 | 216 | { json: "page", js: "page", typ: 0 }, |
| 215 | 217 | { json: "pages", js: "pages", typ: 0 }, |
| 216 | 218 | { json: "per_page", js: "per_page", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/9847b.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,26 +1,28 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | name: string; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | +export type TopLevel = TopLevelElement[]; | |
| 16 | + | |
| 15 | 17 | // Converts JSON strings to/from your types |
| 16 | 18 | // and asserts the results of JSON.parse at runtime |
| 17 | 19 | export class Convert { |
| 18 | - public static toTopLevel(json: string): TopLevel[] { | |
| 19 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 20 | + public static toTopLevel(json: string): TopLevel { | |
| 21 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 20 | 22 | } |
| 21 | 23 | |
| 22 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 23 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 24 | + public static topLevelToJson(value: TopLevel): string { | |
| 25 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 24 | 26 | } |
| 25 | 27 | } |
| 26 | 28 | |
| @@ -178,7 +180,7 @@ function r(name: string) { | ||
| 178 | 180 | } |
| 179 | 181 | |
| 180 | 182 | const typeMap: any = { |
| 181 | - "TopLevel": o([ | |
| 183 | + "TopLevelElement": o([ | |
| 182 | 184 | { json: "id", js: "id", typ: "" }, |
| 183 | 185 | { json: "name", js: "name", typ: "" }, |
| 184 | 186 | ], false), |
Test case
1 generated file · +9 −7test/inputs/json/misc/996bd.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -42,15 +42,17 @@ export type MediaType = "text/plain" | "text/html"; | ||
| 42 | 42 | |
| 43 | 43 | export type Title = "Plain Text" | "HTML"; |
| 44 | 44 | |
| 45 | +export type TopLevel = TopLevelElement[]; | |
| 46 | + | |
| 45 | 47 | // Converts JSON strings to/from your types |
| 46 | 48 | // and asserts the results of JSON.parse at runtime |
| 47 | 49 | export class Convert { |
| 48 | - public static toTopLevel(json: string): TopLevel[] { | |
| 49 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 50 | + public static toTopLevel(json: string): TopLevel { | |
| 51 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 53 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 54 | + public static topLevelToJson(value: TopLevel): string { | |
| 55 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 54 | 56 | } |
| 55 | 57 | } |
| 56 | 58 | |
| @@ -208,7 +210,7 @@ function r(name: string) { | ||
| 208 | 210 | } |
| 209 | 211 | |
| 210 | 212 | const typeMap: any = { |
| 211 | - "TopLevel": o([ | |
| 213 | + "TopLevelElement": o([ | |
| 212 | 214 | { json: "id", js: "id", typ: "" }, |
| 213 | 215 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 214 | 216 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/9a503.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -36,15 +36,17 @@ export interface Text { | ||
| 36 | 36 | url: string; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | +export type TopLevel = TopLevelElement[]; | |
| 40 | + | |
| 39 | 41 | // Converts JSON strings to/from your types |
| 40 | 42 | // and asserts the results of JSON.parse at runtime |
| 41 | 43 | export class Convert { |
| 42 | - public static toTopLevel(json: string): TopLevel[] { | |
| 43 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 44 | + public static toTopLevel(json: string): TopLevel { | |
| 45 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 47 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 48 | + public static topLevelToJson(value: TopLevel): string { | |
| 49 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 48 | 50 | } |
| 49 | 51 | } |
| 50 | 52 | |
| @@ -202,7 +204,7 @@ function r(name: string) { | ||
| 202 | 204 | } |
| 203 | 205 | |
| 204 | 206 | const typeMap: any = { |
| 205 | - "TopLevel": o([ | |
| 207 | + "TopLevelElement": o([ | |
| 206 | 208 | { json: "id", js: "id", typ: "" }, |
| 207 | 209 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 208 | 210 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/9eed5.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -36,15 +36,17 @@ export interface Text { | ||
| 36 | 36 | url: string; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | +export type TopLevel = TopLevelElement[]; | |
| 40 | + | |
| 39 | 41 | // Converts JSON strings to/from your types |
| 40 | 42 | // and asserts the results of JSON.parse at runtime |
| 41 | 43 | export class Convert { |
| 42 | - public static toTopLevel(json: string): TopLevel[] { | |
| 43 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 44 | + public static toTopLevel(json: string): TopLevel { | |
| 45 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 47 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 48 | + public static topLevelToJson(value: TopLevel): string { | |
| 49 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 48 | 50 | } |
| 49 | 51 | } |
| 50 | 52 | |
| @@ -202,7 +204,7 @@ function r(name: string) { | ||
| 202 | 204 | } |
| 203 | 205 | |
| 204 | 206 | const typeMap: any = { |
| 205 | - "TopLevel": o([ | |
| 207 | + "TopLevelElement": o([ | |
| 206 | 208 | { json: "id", js: "id", typ: "" }, |
| 207 | 209 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 208 | 210 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/a45b0.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | age: number; |
| 12 | 12 | country: Country; |
| 13 | 13 | females: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | |
| 19 | 19 | export type Country = "United States"; |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "age", js: "age", typ: 0 }, |
| 189 | 191 | { json: "country", js: "country", typ: r("Country") }, |
| 190 | 192 | { json: "females", js: "females", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/ab0d1.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | age: number; |
| 12 | 12 | country: Country; |
| 13 | 13 | females: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | |
| 19 | 19 | export type Country = "United States"; |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "age", js: "age", typ: 0 }, |
| 189 | 191 | { json: "country", js: "country", typ: r("Country") }, |
| 190 | 192 | { json: "females", js: "females", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/ad8be.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | identifiers: Identifier[]; |
| 13 | 13 | keywords: Keyword[]; |
| @@ -49,15 +49,17 @@ export type MediaType = "text/html" | "text/plain" | "application/pdf"; | ||
| 49 | 49 | |
| 50 | 50 | export type Title = "HTML" | "Plain Text" | "PDF"; |
| 51 | 51 | |
| 52 | +export type TopLevel = TopLevelElement[]; | |
| 53 | + | |
| 52 | 54 | // Converts JSON strings to/from your types |
| 53 | 55 | // and asserts the results of JSON.parse at runtime |
| 54 | 56 | export class Convert { |
| 55 | - public static toTopLevel(json: string): TopLevel[] { | |
| 56 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 57 | + public static toTopLevel(json: string): TopLevel { | |
| 58 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 57 | 59 | } |
| 58 | 60 | |
| 59 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 60 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 61 | + public static topLevelToJson(value: TopLevel): string { | |
| 62 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 61 | 63 | } |
| 62 | 64 | } |
| 63 | 65 | |
| @@ -215,7 +217,7 @@ function r(name: string) { | ||
| 215 | 217 | } |
| 216 | 218 | |
| 217 | 219 | const typeMap: any = { |
| 218 | - "TopLevel": o([ | |
| 220 | + "TopLevelElement": o([ | |
| 219 | 221 | { json: "id", js: "id", typ: "" }, |
| 220 | 222 | { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) }, |
| 221 | 223 | { json: "keywords", js: "keywords", typ: a(r("Keyword")) }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/b4865.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | ":@computed_region_cbhk_fwbd"?: string; |
| 12 | 12 | ":@computed_region_nnqa_25f4"?: string; |
| 13 | 13 | fall: Fall; |
| @@ -33,15 +33,17 @@ export type Type = "Point"; | ||
| 33 | 33 | |
| 34 | 34 | export type Nametype = "Valid"; |
| 35 | 35 | |
| 36 | +export type TopLevel = TopLevelElement[]; | |
| 37 | + | |
| 36 | 38 | // Converts JSON strings to/from your types |
| 37 | 39 | // and asserts the results of JSON.parse at runtime |
| 38 | 40 | export class Convert { |
| 39 | - public static toTopLevel(json: string): TopLevel[] { | |
| 40 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 41 | + public static toTopLevel(json: string): TopLevel { | |
| 42 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 44 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 45 | + public static topLevelToJson(value: TopLevel): string { | |
| 46 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 45 | 47 | } |
| 46 | 48 | } |
| 47 | 49 | |
| @@ -199,7 +201,7 @@ function r(name: string) { | ||
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | const typeMap: any = { |
| 202 | - "TopLevel": o([ | |
| 204 | + "TopLevelElement": o([ | |
| 203 | 205 | { json: ":@computed_region_cbhk_fwbd", js: ":@computed_region_cbhk_fwbd", typ: u(undefined, "") }, |
| 204 | 206 | { json: ":@computed_region_nnqa_25f4", js: ":@computed_region_nnqa_25f4", typ: u(undefined, "") }, |
| 205 | 207 | { json: "fall", js: "fall", typ: r("Fall") }, |
Test case
1 generated file · +12 −10test/inputs/json/misc/c8c7e.json
Mtypescriptdefault / TopLevel.ts+12 −10
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = TopLevelElement[] | PurpleTopLevel; | |
| 10 | +export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel; | |
| 11 | 11 | |
| 12 | -export interface TopLevelElement { | |
| 12 | +export interface PurpleTopLevel { | |
| 13 | 13 | country: Country; |
| 14 | 14 | date: string; |
| 15 | 15 | decimal: string; |
| @@ -26,22 +26,24 @@ export type ID = "IN" | "NY.GDP.MKTP.CD"; | ||
| 26 | 26 | |
| 27 | 27 | export type Value = "India" | "GDP (current US$)"; |
| 28 | 28 | |
| 29 | -export interface PurpleTopLevel { | |
| 29 | +export interface FluffyTopLevel { | |
| 30 | 30 | page: number; |
| 31 | 31 | pages: number; |
| 32 | 32 | per_page: string; |
| 33 | 33 | total: number; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | +export type TopLevel = TopLevelUnion[]; | |
| 37 | + | |
| 36 | 38 | // Converts JSON strings to/from your types |
| 37 | 39 | // and asserts the results of JSON.parse at runtime |
| 38 | 40 | export class Convert { |
| 39 | - public static toTopLevel(json: string): TopLevel[] { | |
| 40 | - return cast(JSON.parse(json), a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))); | |
| 41 | + public static toTopLevel(json: string): TopLevel { | |
| 42 | + return cast(JSON.parse(json), a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))); | |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 44 | - return JSON.stringify(uncast(value, a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))), null, 2); | |
| 45 | + public static topLevelToJson(value: TopLevel): string { | |
| 46 | + return JSON.stringify(uncast(value, a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))), null, 2); | |
| 45 | 47 | } |
| 46 | 48 | } |
| 47 | 49 | |
| @@ -199,7 +201,7 @@ function r(name: string) { | ||
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | const typeMap: any = { |
| 202 | - "TopLevelElement": o([ | |
| 204 | + "PurpleTopLevel": o([ | |
| 203 | 205 | { json: "country", js: "country", typ: r("Country") }, |
| 204 | 206 | { json: "date", js: "date", typ: "" }, |
| 205 | 207 | { json: "decimal", js: "decimal", typ: "" }, |
| @@ -210,7 +212,7 @@ const typeMap: any = { | ||
| 210 | 212 | { json: "id", js: "id", typ: r("ID") }, |
| 211 | 213 | { json: "value", js: "value", typ: r("Value") }, |
| 212 | 214 | ], false), |
| 213 | - "PurpleTopLevel": o([ | |
| 215 | + "FluffyTopLevel": o([ | |
| 214 | 216 | { json: "page", js: "page", typ: 0 }, |
| 215 | 217 | { json: "pages", js: "pages", typ: 0 }, |
| 216 | 218 | { json: "per_page", js: "per_page", typ: "" }, |
Test case
1 generated file · +12 −10test/inputs/json/misc/cda6c.json
Mtypescriptdefault / TopLevel.ts+12 −10
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = TopLevelElement[] | PurpleTopLevel; | |
| 10 | +export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel; | |
| 11 | 11 | |
| 12 | -export interface TopLevelElement { | |
| 12 | +export interface PurpleTopLevel { | |
| 13 | 13 | country: Country; |
| 14 | 14 | date: string; |
| 15 | 15 | decimal: string; |
| @@ -26,22 +26,24 @@ export type ID = "CN" | "SP.POP.TOTL"; | ||
| 26 | 26 | |
| 27 | 27 | export type Value = "China" | "Population, total"; |
| 28 | 28 | |
| 29 | -export interface PurpleTopLevel { | |
| 29 | +export interface FluffyTopLevel { | |
| 30 | 30 | page: number; |
| 31 | 31 | pages: number; |
| 32 | 32 | per_page: string; |
| 33 | 33 | total: number; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | +export type TopLevel = TopLevelUnion[]; | |
| 37 | + | |
| 36 | 38 | // Converts JSON strings to/from your types |
| 37 | 39 | // and asserts the results of JSON.parse at runtime |
| 38 | 40 | export class Convert { |
| 39 | - public static toTopLevel(json: string): TopLevel[] { | |
| 40 | - return cast(JSON.parse(json), a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))); | |
| 41 | + public static toTopLevel(json: string): TopLevel { | |
| 42 | + return cast(JSON.parse(json), a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))); | |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 44 | - return JSON.stringify(uncast(value, a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))), null, 2); | |
| 45 | + public static topLevelToJson(value: TopLevel): string { | |
| 46 | + return JSON.stringify(uncast(value, a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))), null, 2); | |
| 45 | 47 | } |
| 46 | 48 | } |
| 47 | 49 | |
| @@ -199,7 +201,7 @@ function r(name: string) { | ||
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | const typeMap: any = { |
| 202 | - "TopLevelElement": o([ | |
| 204 | + "PurpleTopLevel": o([ | |
| 203 | 205 | { json: "country", js: "country", typ: r("Country") }, |
| 204 | 206 | { json: "date", js: "date", typ: "" }, |
| 205 | 207 | { json: "decimal", js: "decimal", typ: "" }, |
| @@ -210,7 +212,7 @@ const typeMap: any = { | ||
| 210 | 212 | { json: "id", js: "id", typ: r("ID") }, |
| 211 | 213 | { json: "value", js: "value", typ: r("Value") }, |
| 212 | 214 | ], false), |
| 213 | - "PurpleTopLevel": o([ | |
| 215 | + "FluffyTopLevel": o([ | |
| 214 | 216 | { json: "page", js: "page", typ: 0 }, |
| 215 | 217 | { json: "pages", js: "pages", typ: 0 }, |
| 216 | 218 | { json: "per_page", js: "per_page", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/e2a58.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,26 +1,28 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | date: string; |
| 12 | 12 | "stop-and-search": string[]; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | +export type TopLevel = TopLevelElement[]; | |
| 16 | + | |
| 15 | 17 | // Converts JSON strings to/from your types |
| 16 | 18 | // and asserts the results of JSON.parse at runtime |
| 17 | 19 | export class Convert { |
| 18 | - public static toTopLevel(json: string): TopLevel[] { | |
| 19 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 20 | + public static toTopLevel(json: string): TopLevel { | |
| 21 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 20 | 22 | } |
| 21 | 23 | |
| 22 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 23 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 24 | + public static topLevelToJson(value: TopLevel): string { | |
| 25 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 24 | 26 | } |
| 25 | 27 | } |
| 26 | 28 | |
| @@ -178,7 +180,7 @@ function r(name: string) { | ||
| 178 | 180 | } |
| 179 | 181 | |
| 180 | 182 | const typeMap: any = { |
| 181 | - "TopLevel": o([ | |
| 183 | + "TopLevelElement": o([ | |
| 182 | 184 | { json: "date", js: "date", typ: "" }, |
| 183 | 185 | { json: "stop-and-search", js: "stop-and-search", typ: a("") }, |
| 184 | 186 | ], false), |
Test case
1 generated file · +12 −10test/inputs/json/misc/e53b5.json
Mtypescriptdefault / TopLevel.ts+12 −10
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = TopLevelElement[] | PurpleTopLevel; | |
| 10 | +export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel; | |
| 11 | 11 | |
| 12 | -export interface TopLevelElement { | |
| 12 | +export interface PurpleTopLevel { | |
| 13 | 13 | country: Country; |
| 14 | 14 | date: string; |
| 15 | 15 | decimal: string; |
| @@ -26,22 +26,24 @@ export type ID = "IN" | "SP.POP.TOTL"; | ||
| 26 | 26 | |
| 27 | 27 | export type Value = "India" | "Population, total"; |
| 28 | 28 | |
| 29 | -export interface PurpleTopLevel { | |
| 29 | +export interface FluffyTopLevel { | |
| 30 | 30 | page: number; |
| 31 | 31 | pages: number; |
| 32 | 32 | per_page: string; |
| 33 | 33 | total: number; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | +export type TopLevel = TopLevelUnion[]; | |
| 37 | + | |
| 36 | 38 | // Converts JSON strings to/from your types |
| 37 | 39 | // and asserts the results of JSON.parse at runtime |
| 38 | 40 | export class Convert { |
| 39 | - public static toTopLevel(json: string): TopLevel[] { | |
| 40 | - return cast(JSON.parse(json), a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))); | |
| 41 | + public static toTopLevel(json: string): TopLevel { | |
| 42 | + return cast(JSON.parse(json), a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))); | |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 44 | - return JSON.stringify(uncast(value, a(u(a(r("TopLevelElement")), r("PurpleTopLevel")))), null, 2); | |
| 45 | + public static topLevelToJson(value: TopLevel): string { | |
| 46 | + return JSON.stringify(uncast(value, a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel")))), null, 2); | |
| 45 | 47 | } |
| 46 | 48 | } |
| 47 | 49 | |
| @@ -199,7 +201,7 @@ function r(name: string) { | ||
| 199 | 201 | } |
| 200 | 202 | |
| 201 | 203 | const typeMap: any = { |
| 202 | - "TopLevelElement": o([ | |
| 204 | + "PurpleTopLevel": o([ | |
| 203 | 205 | { json: "country", js: "country", typ: r("Country") }, |
| 204 | 206 | { json: "date", js: "date", typ: "" }, |
| 205 | 207 | { json: "decimal", js: "decimal", typ: "" }, |
| @@ -210,7 +212,7 @@ const typeMap: any = { | ||
| 210 | 212 | { json: "id", js: "id", typ: r("ID") }, |
| 211 | 213 | { json: "value", js: "value", typ: r("Value") }, |
| 212 | 214 | ], false), |
| 213 | - "PurpleTopLevel": o([ | |
| 215 | + "FluffyTopLevel": o([ | |
| 214 | 216 | { json: "page", js: "page", typ: 0 }, |
| 215 | 217 | { json: "pages", js: "pages", typ: 0 }, |
| 216 | 218 | { json: "per_page", js: "per_page", typ: "" }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/e8a0b.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | age: number; |
| 12 | 12 | country: Country; |
| 13 | 13 | females: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | |
| 19 | 19 | export type Country = "United States"; |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "age", js: "age", typ: 0 }, |
| 189 | 191 | { json: "country", js: "country", typ: r("Country") }, |
| 190 | 192 | { json: "females", js: "females", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/e8b04.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | averageRating: number; |
| 12 | 12 | category?: string; |
| 13 | 13 | createdAt: number; |
| @@ -240,15 +240,17 @@ export interface TableAuthor { | ||
| 240 | 240 | |
| 241 | 241 | export type ViewType = "tabular"; |
| 242 | 242 | |
| 243 | +export type TopLevel = TopLevelElement[]; | |
| 244 | + | |
| 243 | 245 | // Converts JSON strings to/from your types |
| 244 | 246 | // and asserts the results of JSON.parse at runtime |
| 245 | 247 | export class Convert { |
| 246 | - public static toTopLevel(json: string): TopLevel[] { | |
| 247 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 248 | + public static toTopLevel(json: string): TopLevel { | |
| 249 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 248 | 250 | } |
| 249 | 251 | |
| 250 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 251 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 252 | + public static topLevelToJson(value: TopLevel): string { | |
| 253 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 252 | 254 | } |
| 253 | 255 | } |
| 254 | 256 | |
| @@ -406,7 +408,7 @@ function r(name: string) { | ||
| 406 | 408 | } |
| 407 | 409 | |
| 408 | 410 | const typeMap: any = { |
| 409 | - "TopLevel": o([ | |
| 411 | + "TopLevelElement": o([ | |
| 410 | 412 | { json: "averageRating", js: "averageRating", typ: 0 }, |
| 411 | 413 | { json: "category", js: "category", typ: u(undefined, "") }, |
| 412 | 414 | { json: "createdAt", js: "createdAt", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/f3139.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,26 +1,28 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | id: string; |
| 12 | 12 | name: string; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | +export type TopLevel = TopLevelElement[]; | |
| 16 | + | |
| 15 | 17 | // Converts JSON strings to/from your types |
| 16 | 18 | // and asserts the results of JSON.parse at runtime |
| 17 | 19 | export class Convert { |
| 18 | - public static toTopLevel(json: string): TopLevel[] { | |
| 19 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 20 | + public static toTopLevel(json: string): TopLevel { | |
| 21 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 20 | 22 | } |
| 21 | 23 | |
| 22 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 23 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 24 | + public static topLevelToJson(value: TopLevel): string { | |
| 25 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 24 | 26 | } |
| 25 | 27 | } |
| 26 | 28 | |
| @@ -178,7 +180,7 @@ function r(name: string) { | ||
| 178 | 180 | } |
| 179 | 181 | |
| 180 | 182 | const typeMap: any = { |
| 181 | - "TopLevel": o([ | |
| 183 | + "TopLevelElement": o([ | |
| 182 | 184 | { json: "id", js: "id", typ: "" }, |
| 183 | 185 | { json: "name", js: "name", typ: "" }, |
| 184 | 186 | ], false), |
Test case
1 generated file · +9 −7test/inputs/json/misc/f3edf.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | age: number; |
| 12 | 12 | country: Country; |
| 13 | 13 | females: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | |
| 19 | 19 | export type Country = "United States"; |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "age", js: "age", typ: 0 }, |
| 189 | 191 | { json: "country", js: "country", typ: r("Country") }, |
| 190 | 192 | { json: "females", js: "females", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/misc/f466a.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | age: number; |
| 12 | 12 | country: Country; |
| 13 | 13 | females: number; |
| @@ -18,15 +18,17 @@ export interface TopLevel { | ||
| 18 | 18 | |
| 19 | 19 | export type Country = "United States"; |
| 20 | 20 | |
| 21 | +export type TopLevel = TopLevelElement[]; | |
| 22 | + | |
| 21 | 23 | // Converts JSON strings to/from your types |
| 22 | 24 | // and asserts the results of JSON.parse at runtime |
| 23 | 25 | export class Convert { |
| 24 | - public static toTopLevel(json: string): TopLevel[] { | |
| 25 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 26 | + public static toTopLevel(json: string): TopLevel { | |
| 27 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 29 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 30 | + public static topLevelToJson(value: TopLevel): string { | |
| 31 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -184,7 +186,7 @@ function r(name: string) { | ||
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | const typeMap: any = { |
| 187 | - "TopLevel": o([ | |
| 189 | + "TopLevelElement": o([ | |
| 188 | 190 | { json: "age", js: "age", typ: 0 }, |
| 189 | 191 | { json: "country", js: "country", typ: r("Country") }, |
| 190 | 192 | { json: "females", js: "females", typ: 0 }, |
Test case
1 generated file · +12 −10test/inputs/json/priority/bug863.json
Mtypescriptdefault / TopLevel.ts+12 −10
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = TopLevelElement[] | PurpleTopLevel | string; | |
| 10 | +export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel | string; | |
| 11 | 11 | |
| 12 | -export interface TopLevelElement { | |
| 12 | +export interface PurpleTopLevel { | |
| 13 | 13 | ALIENLEVEL: number; |
| 14 | 14 | BREED: Breed; |
| 15 | 15 | CHAR_DIMENSION: number; |
| @@ -41,7 +41,7 @@ export type RankTitle = "President" | "Advisor" | "Veteran" | "Applicant"; | ||
| 41 | 41 | |
| 42 | 42 | export type Sex = "Neuter" | "Male" | "Female"; |
| 43 | 43 | |
| 44 | -export interface PurpleTopLevel { | |
| 44 | +export interface FluffyTopLevel { | |
| 45 | 45 | ADVENTURERCOUNT: number; |
| 46 | 46 | AGENTCOUNT: number; |
| 47 | 47 | ATROXCOUNT: number; |
| @@ -79,15 +79,17 @@ export interface PurpleTopLevel { | ||
| 79 | 79 | TRADERCOUNT: number; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | +export type TopLevel = TopLevelUnion[]; | |
| 83 | + | |
| 82 | 84 | // Converts JSON strings to/from your types |
| 83 | 85 | // and asserts the results of JSON.parse at runtime |
| 84 | 86 | export class Convert { |
| 85 | - public static toTopLevel(json: string): TopLevel[] { | |
| 86 | - return cast(JSON.parse(json), a(u(a(r("TopLevelElement")), r("PurpleTopLevel"), ""))); | |
| 87 | + public static toTopLevel(json: string): TopLevel { | |
| 88 | + return cast(JSON.parse(json), a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel"), ""))); | |
| 87 | 89 | } |
| 88 | 90 | |
| 89 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 90 | - return JSON.stringify(uncast(value, a(u(a(r("TopLevelElement")), r("PurpleTopLevel"), ""))), null, 2); | |
| 91 | + public static topLevelToJson(value: TopLevel): string { | |
| 92 | + return JSON.stringify(uncast(value, a(u(a(r("PurpleTopLevel")), r("FluffyTopLevel"), ""))), null, 2); | |
| 91 | 93 | } |
| 92 | 94 | } |
| 93 | 95 | |
| @@ -245,7 +247,7 @@ function r(name: string) { | ||
| 245 | 247 | } |
| 246 | 248 | |
| 247 | 249 | const typeMap: any = { |
| 248 | - "TopLevelElement": o([ | |
| 250 | + "PurpleTopLevel": o([ | |
| 249 | 251 | { json: "ALIENLEVEL", js: "ALIENLEVEL", typ: 0 }, |
| 250 | 252 | { json: "BREED", js: "BREED", typ: r("Breed") }, |
| 251 | 253 | { json: "CHAR_DIMENSION", js: "CHAR_DIMENSION", typ: 0 }, |
| @@ -264,7 +266,7 @@ const typeMap: any = { | ||
| 264 | 266 | { json: "RANK_TITLE", js: "RANK_TITLE", typ: r("RankTitle") }, |
| 265 | 267 | { json: "SEX", js: "SEX", typ: r("Sex") }, |
| 266 | 268 | ], false), |
| 267 | - "PurpleTopLevel": o([ | |
| 269 | + "FluffyTopLevel": o([ | |
| 268 | 270 | { json: "ADVENTURERCOUNT", js: "ADVENTURERCOUNT", typ: 0 }, |
| 269 | 271 | { json: "AGENTCOUNT", js: "AGENTCOUNT", typ: 0 }, |
| 270 | 272 | { json: "ATROXCOUNT", js: "ATROXCOUNT", typ: 0 }, |
Test case
1 generated file · +9 −7test/inputs/json/priority/issue2680-object-array.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,25 +1,27 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | key: string; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | +export type TopLevel = TopLevelElement[]; | |
| 15 | + | |
| 14 | 16 | // Converts JSON strings to/from your types |
| 15 | 17 | // and asserts the results of JSON.parse at runtime |
| 16 | 18 | export class Convert { |
| 17 | - public static toTopLevel(json: string): TopLevel[] { | |
| 18 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 19 | + public static toTopLevel(json: string): TopLevel { | |
| 20 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 19 | 21 | } |
| 20 | 22 | |
| 21 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 22 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 23 | + public static topLevelToJson(value: TopLevel): string { | |
| 24 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 23 | 25 | } |
| 24 | 26 | } |
| 25 | 27 | |
| @@ -177,7 +179,7 @@ function r(name: string) { | ||
| 177 | 179 | } |
| 178 | 180 | |
| 179 | 181 | const typeMap: any = { |
| 180 | - "TopLevel": o([ | |
| 182 | + "TopLevelElement": o([ | |
| 181 | 183 | { json: "key", js: "key", typ: "" }, |
| 182 | 184 | ], false), |
| 183 | 185 | }; |
Test case
1 generated file · +5 −3test/inputs/json/priority/issue2680-scalar-array.json
Mtypescriptdefault / TopLevel.ts+5 −3
| @@ -1,20 +1,22 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = number[]; | |
| 11 | + | |
| 10 | 12 | // Converts JSON strings to/from your types |
| 11 | 13 | // and asserts the results of JSON.parse at runtime |
| 12 | 14 | export class Convert { |
| 13 | - public static toTopLevel(json: string): number[] { | |
| 15 | + public static toTopLevel(json: string): TopLevel { | |
| 14 | 16 | return cast(JSON.parse(json), a(0)); |
| 15 | 17 | } |
| 16 | 18 | |
| 17 | - public static topLevelToJson(value: number[]): string { | |
| 19 | + public static topLevelToJson(value: TopLevel): string { | |
| 18 | 20 | return JSON.stringify(uncast(value, a(0)), null, 2); |
| 19 | 21 | } |
| 20 | 22 | } |
Test case
1 generated file · +9 −7test/inputs/json/priority/kotlin-enum-class-case-collision.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,27 +1,29 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | category: Category; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | export type Category = " " | ""; |
| 15 | 15 | |
| 16 | +export type TopLevel = TopLevelElement[]; | |
| 17 | + | |
| 16 | 18 | // Converts JSON strings to/from your types |
| 17 | 19 | // and asserts the results of JSON.parse at runtime |
| 18 | 20 | export class Convert { |
| 19 | - public static toTopLevel(json: string): TopLevel[] { | |
| 20 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 21 | + public static toTopLevel(json: string): TopLevel { | |
| 22 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 24 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 25 | + public static topLevelToJson(value: TopLevel): string { | |
| 26 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 25 | 27 | } |
| 26 | 28 | } |
| 27 | 29 | |
| @@ -179,7 +181,7 @@ function r(name: string) { | ||
| 179 | 181 | } |
| 180 | 182 | |
| 181 | 183 | const typeMap: any = { |
| 182 | - "TopLevel": o([ | |
| 184 | + "TopLevelElement": o([ | |
| 183 | 185 | { json: "category", js: "category", typ: r("Category") }, |
| 184 | 186 | ], false), |
| 185 | 187 | "Category": [ |
Test case
1 generated file · +9 −7test/inputs/json/priority/optional-union.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,27 +1,29 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | a?: A; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | export type A = number | string; |
| 15 | 15 | |
| 16 | +export type TopLevel = TopLevelElement[]; | |
| 17 | + | |
| 16 | 18 | // Converts JSON strings to/from your types |
| 17 | 19 | // and asserts the results of JSON.parse at runtime |
| 18 | 20 | export class Convert { |
| 19 | - public static toTopLevel(json: string): TopLevel[] { | |
| 20 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 21 | + public static toTopLevel(json: string): TopLevel { | |
| 22 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 24 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 25 | + public static topLevelToJson(value: TopLevel): string { | |
| 26 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 25 | 27 | } |
| 26 | 28 | } |
| 27 | 29 | |
| @@ -179,7 +181,7 @@ function r(name: string) { | ||
| 179 | 181 | } |
| 180 | 182 | |
| 181 | 183 | const typeMap: any = { |
| 182 | - "TopLevel": o([ | |
| 184 | + "TopLevelElement": o([ | |
| 183 | 185 | { json: "a", js: "a", typ: u(undefined, u(0, "")) }, |
| 184 | 186 | ], false), |
| 185 | 187 | }; |
Test case
1 generated file · +9 −7test/inputs/json/samples/github-events.json
Mtypescriptdefault / TopLevel.ts+9 −7
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | actor: Actor; |
| 12 | 12 | created_at: Date; |
| 13 | 13 | id: string; |
| @@ -295,15 +295,17 @@ export interface TopLevelRepo { | ||
| 295 | 295 | url: string; |
| 296 | 296 | } |
| 297 | 297 | |
| 298 | +export type TopLevel = TopLevelElement[]; | |
| 299 | + | |
| 298 | 300 | // Converts JSON strings to/from your types |
| 299 | 301 | // and asserts the results of JSON.parse at runtime |
| 300 | 302 | export class Convert { |
| 301 | - public static toTopLevel(json: string): TopLevel[] { | |
| 302 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 303 | + public static toTopLevel(json: string): TopLevel { | |
| 304 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 303 | 305 | } |
| 304 | 306 | |
| 305 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 306 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 307 | + public static topLevelToJson(value: TopLevel): string { | |
| 308 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 307 | 309 | } |
| 308 | 310 | } |
| 309 | 311 | |
| @@ -461,7 +463,7 @@ function r(name: string) { | ||
| 461 | 463 | } |
| 462 | 464 | |
| 463 | 465 | const typeMap: any = { |
| 464 | - "TopLevel": o([ | |
| 466 | + "TopLevelElement": o([ | |
| 465 | 467 | { json: "actor", js: "actor", typ: r("Actor") }, |
| 466 | 468 | { json: "created_at", js: "created_at", typ: Date }, |
| 467 | 469 | { json: "id", js: "id", typ: "" }, |
Test case
1 generated file · +5 −3test/inputs/schema/issue2680-top-level-array.schema
Mschema-typescriptdefault / TopLevel.ts+5 −3
| @@ -1,20 +1,22 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 type TopLevel = number[]; | |
| 11 | + | |
| 10 | 12 | // Converts JSON strings to/from your types |
| 11 | 13 | // and asserts the results of JSON.parse at runtime |
| 12 | 14 | export class Convert { |
| 13 | - public static toTopLevel(json: string): number[] { | |
| 15 | + public static toTopLevel(json: string): TopLevel { | |
| 14 | 16 | return cast(JSON.parse(json), a(0)); |
| 15 | 17 | } |
| 16 | 18 | |
| 17 | - public static topLevelToJson(value: number[]): string { | |
| 19 | + public static topLevelToJson(value: TopLevel): string { | |
| 18 | 20 | return JSON.stringify(uncast(value, a(0)), null, 2); |
| 19 | 21 | } |
| 20 | 22 | } |
Test case
25 generated files · +1,693 −0test/inputs/schema/root-array-ref.schema
Aschema-cplusplusdefault / quicktype.hpp+63 −0
| @@ -0,0 +1,63 @@ | ||
| 1 | +// To parse this JSON data, first install | |
| 2 | +// | |
| 3 | +// json.hpp https://github.com/nlohmann/json | |
| 4 | +// | |
| 5 | +// Then include this file, and then do | |
| 6 | +// | |
| 7 | +// TopLevel data = nlohmann::json::parse(jsonString); | |
| 8 | + | |
| 9 | +#pragma once | |
| 10 | + | |
| 11 | +#include "json.hpp" | |
| 12 | + | |
| 13 | +#include <optional> | |
| 14 | +#include <stdexcept> | |
| 15 | +#include <regex> | |
| 16 | + | |
| 17 | +namespace quicktype { | |
| 18 | + using nlohmann::json; | |
| 19 | + | |
| 20 | + #ifndef NLOHMANN_UNTYPED_quicktype_HELPER | |
| 21 | + #define NLOHMANN_UNTYPED_quicktype_HELPER | |
| 22 | + inline json get_untyped(const json & j, const char * property) { | |
| 23 | + if (j.find(property) != j.end()) { | |
| 24 | + return j.at(property).get<json>(); | |
| 25 | + } | |
| 26 | + return json(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + inline json get_untyped(const json & j, std::string property) { | |
| 30 | + return get_untyped(j, property.data()); | |
| 31 | + } | |
| 32 | + #endif | |
| 33 | + | |
| 34 | + class SomeObject { | |
| 35 | + public: | |
| 36 | + SomeObject() = default; | |
| 37 | + virtual ~SomeObject() = default; | |
| 38 | + | |
| 39 | + private: | |
| 40 | + std::string name; | |
| 41 | + | |
| 42 | + public: | |
| 43 | + const std::string & get_name() const { return name; } | |
| 44 | + std::string & get_mutable_name() { return name; } | |
| 45 | + void set_name(const std::string & value) { this->name = value; } | |
| 46 | + }; | |
| 47 | + | |
| 48 | + using TopLevel = std::vector<SomeObject>; | |
| 49 | +} | |
| 50 | + | |
| 51 | +namespace quicktype { | |
| 52 | + void from_json(const json & j, SomeObject & x); | |
| 53 | + void to_json(json & j, const SomeObject & x); | |
| 54 | + | |
| 55 | + inline void from_json(const json & j, SomeObject& x) { | |
| 56 | + x.set_name(j.at("name").get<std::string>()); | |
| 57 | + } | |
| 58 | + | |
| 59 | + inline void to_json(json & j, const SomeObject & x) { | |
| 60 | + j = json::object(); | |
| 61 | + j["name"] = x.get_name(); | |
| 62 | + } | |
| 63 | +} |
Aschema-csharp-recordsdefault / QuickType.cs+61 −0
| @@ -0,0 +1,61 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | +#pragma warning disable CS8604 | |
| 14 | +#pragma warning disable CS8625 | |
| 15 | +#pragma warning disable CS8765 | |
| 16 | + | |
| 17 | +namespace QuickType | |
| 18 | +{ | |
| 19 | + using System; | |
| 20 | + using System.Collections.Generic; | |
| 21 | + | |
| 22 | + using System.Globalization; | |
| 23 | + using Newtonsoft.Json; | |
| 24 | + using Newtonsoft.Json.Converters; | |
| 25 | + | |
| 26 | + public partial record TopLevel | |
| 27 | + { | |
| 28 | + [JsonProperty("name", Required = Required.Always)] | |
| 29 | + public string Name { get; set; } | |
| 30 | + } | |
| 31 | + | |
| 32 | + public partial record TopLevel | |
| 33 | + { | |
| 34 | + public static TopLevel[] FromJson(string json) => JsonConvert.DeserializeObject<TopLevel[]>(json, QuickType.Converter.Settings); | |
| 35 | + } | |
| 36 | + | |
| 37 | + public static partial class Serialize | |
| 38 | + { | |
| 39 | + public static string ToJson(this TopLevel[] self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 40 | + } | |
| 41 | + | |
| 42 | + internal static partial class Converter | |
| 43 | + { | |
| 44 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 45 | + { | |
| 46 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 47 | + DateParseHandling = DateParseHandling.None, | |
| 48 | + Converters = | |
| 49 | + { | |
| 50 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 51 | + }, | |
| 52 | + }; | |
| 53 | + } | |
| 54 | +} | |
| 55 | +#pragma warning restore CS8618 | |
| 56 | +#pragma warning restore CS8601 | |
| 57 | +#pragma warning restore CS8602 | |
| 58 | +#pragma warning restore CS8603 | |
| 59 | +#pragma warning restore CS8604 | |
| 60 | +#pragma warning restore CS8625 | |
| 61 | +#pragma warning restore CS8765 |
Aschema-csharp-SystemTextJsondefault / QuickType.cs+166 −0
| @@ -0,0 +1,166 @@ | ||
| 1 | +// <auto-generated /> | |
| 2 | +// | |
| 3 | +// To parse this JSON data, add NuGet 'System.Text.Json' then do: | |
| 4 | +// | |
| 5 | +// using QuickType; | |
| 6 | +// | |
| 7 | +// var topLevel = TopLevel.FromJson(jsonString); | |
| 8 | +#nullable enable | |
| 9 | +#pragma warning disable CS8618 | |
| 10 | +#pragma warning disable CS8601 | |
| 11 | +#pragma warning disable CS8602 | |
| 12 | +#pragma warning disable CS8603 | |
| 13 | + | |
| 14 | +namespace QuickType | |
| 15 | +{ | |
| 16 | + using System; | |
| 17 | + using System.Collections.Generic; | |
| 18 | + | |
| 19 | + using System.Text.Json; | |
| 20 | + using System.Text.Json.Serialization; | |
| 21 | + using System.Globalization; | |
| 22 | + | |
| 23 | + public partial class TopLevel | |
| 24 | + { | |
| 25 | + [JsonRequired] | |
| 26 | + [JsonPropertyName("name")] | |
| 27 | + public string Name { get; set; } | |
| 28 | + } | |
| 29 | + | |
| 30 | + public partial class TopLevel | |
| 31 | + { | |
| 32 | + public static TopLevel[] FromJson(string json) => JsonSerializer.Deserialize<TopLevel[]>(json, QuickType.Converter.Settings); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public static partial class Serialize | |
| 36 | + { | |
| 37 | + public static string ToJson(this TopLevel[] self) => JsonSerializer.Serialize(self, QuickType.Converter.Settings); | |
| 38 | + } | |
| 39 | + | |
| 40 | + internal static partial class Converter | |
| 41 | + { | |
| 42 | + public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General) | |
| 43 | + { | |
| 44 | + Converters = | |
| 45 | + { | |
| 46 | + new DateOnlyConverter(), | |
| 47 | + new TimeOnlyConverter(), | |
| 48 | + IsoDateTimeOffsetConverter.Singleton | |
| 49 | + }, | |
| 50 | + }; | |
| 51 | + } | |
| 52 | + | |
| 53 | + public class DateOnlyConverter : JsonConverter<DateOnly> | |
| 54 | + { | |
| 55 | + private readonly string serializationFormat; | |
| 56 | + public DateOnlyConverter() : this(null) { } | |
| 57 | + | |
| 58 | + public DateOnlyConverter(string? serializationFormat) | |
| 59 | + { | |
| 60 | + this.serializationFormat = serializationFormat ?? "yyyy-MM-dd"; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 64 | + { | |
| 65 | + var value = reader.GetString(); | |
| 66 | + return DateOnly.Parse(value!); | |
| 67 | + } | |
| 68 | + | |
| 69 | + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) | |
| 70 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 71 | + } | |
| 72 | + | |
| 73 | + public class TimeOnlyConverter : JsonConverter<TimeOnly> | |
| 74 | + { | |
| 75 | + private readonly string serializationFormat; | |
| 76 | + | |
| 77 | + public TimeOnlyConverter() : this(null) { } | |
| 78 | + | |
| 79 | + public TimeOnlyConverter(string? serializationFormat) | |
| 80 | + { | |
| 81 | + this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff"; | |
| 82 | + } | |
| 83 | + | |
| 84 | + public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 85 | + { | |
| 86 | + var value = reader.GetString(); | |
| 87 | + return TimeOnly.Parse(value!); | |
| 88 | + } | |
| 89 | + | |
| 90 | + public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) | |
| 91 | + => writer.WriteStringValue(value.ToString(serializationFormat)); | |
| 92 | + } | |
| 93 | + | |
| 94 | + internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset> | |
| 95 | + { | |
| 96 | + public override bool CanConvert(Type t) => t == typeof(DateTimeOffset); | |
| 97 | + | |
| 98 | + private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"; | |
| 99 | + | |
| 100 | + private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind; | |
| 101 | + private string? _dateTimeFormat; | |
| 102 | + private CultureInfo? _culture; | |
| 103 | + | |
| 104 | + public DateTimeStyles DateTimeStyles | |
| 105 | + { | |
| 106 | + get => _dateTimeStyles; | |
| 107 | + set => _dateTimeStyles = value; | |
| 108 | + } | |
| 109 | + | |
| 110 | + public string? DateTimeFormat | |
| 111 | + { | |
| 112 | + get => _dateTimeFormat ?? string.Empty; | |
| 113 | + set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value; | |
| 114 | + } | |
| 115 | + | |
| 116 | + public CultureInfo Culture | |
| 117 | + { | |
| 118 | + get => _culture ?? CultureInfo.CurrentCulture; | |
| 119 | + set => _culture = value; | |
| 120 | + } | |
| 121 | + | |
| 122 | + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) | |
| 123 | + { | |
| 124 | + string text; | |
| 125 | + | |
| 126 | + | |
| 127 | + if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal | |
| 128 | + || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal) | |
| 129 | + { | |
| 130 | + value = value.ToUniversalTime(); | |
| 131 | + } | |
| 132 | + | |
| 133 | + text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture); | |
| 134 | + | |
| 135 | + writer.WriteStringValue(text); | |
| 136 | + } | |
| 137 | + | |
| 138 | + public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | |
| 139 | + { | |
| 140 | + string? dateText = reader.GetString(); | |
| 141 | + | |
| 142 | + if (string.IsNullOrEmpty(dateText) == false) | |
| 143 | + { | |
| 144 | + if (!string.IsNullOrEmpty(_dateTimeFormat)) | |
| 145 | + { | |
| 146 | + return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles); | |
| 147 | + } | |
| 148 | + else | |
| 149 | + { | |
| 150 | + return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + else | |
| 154 | + { | |
| 155 | + return default(DateTimeOffset); | |
| 156 | + } | |
| 157 | + } | |
| 158 | + | |
| 159 | + | |
| 160 | + public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter(); | |
| 161 | + } | |
| 162 | +} | |
| 163 | +#pragma warning restore CS8618 | |
| 164 | +#pragma warning restore CS8601 | |
| 165 | +#pragma warning restore CS8602 | |
| 166 | +#pragma warning restore CS8603 |
Aschema-csharpdefault / QuickType.cs+61 −0
| @@ -0,0 +1,61 @@ | ||
| 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("name", Required = Required.Always)] | |
| 29 | + public string Name { get; set; } | |
| 30 | + } | |
| 31 | + | |
| 32 | + public partial class TopLevel | |
| 33 | + { | |
| 34 | + public static TopLevel[] FromJson(string json) => JsonConvert.DeserializeObject<TopLevel[]>(json, QuickType.Converter.Settings); | |
| 35 | + } | |
| 36 | + | |
| 37 | + public static partial class Serialize | |
| 38 | + { | |
| 39 | + public static string ToJson(this TopLevel[] self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); | |
| 40 | + } | |
| 41 | + | |
| 42 | + internal static partial class Converter | |
| 43 | + { | |
| 44 | + public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
| 45 | + { | |
| 46 | + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
| 47 | + DateParseHandling = DateParseHandling.None, | |
| 48 | + Converters = | |
| 49 | + { | |
| 50 | + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
| 51 | + }, | |
| 52 | + }; | |
| 53 | + } | |
| 54 | +} | |
| 55 | +#pragma warning restore CS8618 | |
| 56 | +#pragma warning restore CS8601 | |
| 57 | +#pragma warning restore CS8602 | |
| 58 | +#pragma warning restore CS8603 | |
| 59 | +#pragma warning restore CS8604 | |
| 60 | +#pragma warning restore CS8625 | |
| 61 | +#pragma warning restore CS8765 |
Aschema-dartdefault / TopLevel.dart+25 −0
| @@ -0,0 +1,25 @@ | ||
| 1 | +// To parse this JSON data, do | |
| 2 | +// | |
| 3 | +// final topLevel = topLevelFromJson(jsonString); | |
| 4 | + | |
| 5 | +import 'dart:convert'; | |
| 6 | + | |
| 7 | +List<TopLevel> topLevelFromJson(String str) => List<TopLevel>.from(json.decode(str).map((x) => TopLevel.fromJson(x))); | |
| 8 | + | |
| 9 | +String topLevelToJson(List<TopLevel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson()))); | |
| 10 | + | |
| 11 | +class TopLevel { | |
| 12 | + final String name; | |
| 13 | + | |
| 14 | + TopLevel({ | |
| 15 | + required this.name, | |
| 16 | + }); | |
| 17 | + | |
| 18 | + factory TopLevel.fromJson(Map<String, dynamic> json) => TopLevel( | |
| 19 | + name: json["name"], | |
| 20 | + ); | |
| 21 | + | |
| 22 | + Map<String, dynamic> toJson() => { | |
| 23 | + "name": name, | |
| 24 | + }; | |
| 25 | +} |
Aschema-elmdefault / QuickType.elm+57 −0
| @@ -0,0 +1,57 @@ | ||
| 1 | +-- To decode the JSON data, add this file to your project, run | |
| 2 | +-- | |
| 3 | +-- elm install NoRedInk/elm-json-decode-pipeline | |
| 4 | +-- | |
| 5 | +-- add these imports | |
| 6 | +-- | |
| 7 | +-- import Json.Decode exposing (decodeString) | |
| 8 | +-- import QuickType exposing (quickType) | |
| 9 | +-- | |
| 10 | +-- and you're off to the races with | |
| 11 | +-- | |
| 12 | +-- decodeString quickType myJsonString | |
| 13 | + | |
| 14 | +module QuickType exposing | |
| 15 | + ( QuickType | |
| 16 | + , quickTypeToString | |
| 17 | + , quickType | |
| 18 | + , SomeObject | |
| 19 | + ) | |
| 20 | + | |
| 21 | +import Json.Decode as Jdec | |
| 22 | +import Json.Decode.Pipeline as Jpipe | |
| 23 | +import Json.Encode as Jenc | |
| 24 | +import Dict exposing (Dict) | |
| 25 | + | |
| 26 | +type alias QuickType = List SomeObject | |
| 27 | + | |
| 28 | +type alias SomeObject = | |
| 29 | + { name : String | |
| 30 | + } | |
| 31 | + | |
| 32 | +-- decoders and encoders | |
| 33 | + | |
| 34 | +quickType : Jdec.Decoder QuickType | |
| 35 | +quickType = Jdec.list someObject | |
| 36 | + | |
| 37 | +quickTypeToString : QuickType -> String | |
| 38 | +quickTypeToString r = Jenc.encode 0 (Jenc.list encodeSomeObject r) | |
| 39 | + | |
| 40 | +someObject : Jdec.Decoder SomeObject | |
| 41 | +someObject = | |
| 42 | + Jdec.succeed SomeObject | |
| 43 | + |> Jpipe.required "name" Jdec.string | |
| 44 | + | |
| 45 | +encodeSomeObject : SomeObject -> Jenc.Value | |
| 46 | +encodeSomeObject x = | |
| 47 | + Jenc.object | |
| 48 | + [ ("name", Jenc.string x.name) | |
| 49 | + ] | |
| 50 | + | |
| 51 | +--- encoder helpers | |
| 52 | + | |
| 53 | +makeNullableEncoder : (a -> Jenc.Value) -> Maybe a -> Jenc.Value | |
| 54 | +makeNullableEncoder f m = | |
| 55 | + case m of | |
| 56 | + Just x -> f x | |
| 57 | + Nothing -> Jenc.null |
Aschema-flowdefault / TopLevel.js+188 −0
| @@ -0,0 +1,188 @@ | ||
| 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 | + name: string; | |
| 14 | +}; | |
| 15 | + | |
| 16 | +// Converts JSON strings to/from your types | |
| 17 | +// and asserts the results of JSON.parse at runtime | |
| 18 | +function toTopLevel(json: string): TopLevel[] { | |
| 19 | + return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 20 | +} | |
| 21 | + | |
| 22 | +function topLevelToJson(value: TopLevel[]): string { | |
| 23 | + return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 24 | +} | |
| 25 | + | |
| 26 | +function invalidValue(typ: any, val: any, key: any, parent: any = '') { | |
| 27 | + const prettyTyp = prettyTypeName(typ); | |
| 28 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 29 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 30 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 31 | +} | |
| 32 | + | |
| 33 | +function prettyTypeName(typ: any): string { | |
| 34 | + if (Array.isArray(typ)) { | |
| 35 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 36 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 37 | + } else { | |
| 38 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 39 | + } | |
| 40 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 41 | + return typ.literal; | |
| 42 | + } else { | |
| 43 | + return typeof typ; | |
| 44 | + } | |
| 45 | +} | |
| 46 | + | |
| 47 | +function jsonToJSProps(typ: any): any { | |
| 48 | + if (typ.jsonToJS === undefined) { | |
| 49 | + const map: any = {}; | |
| 50 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 51 | + typ.jsonToJS = map; | |
| 52 | + } | |
| 53 | + return typ.jsonToJS; | |
| 54 | +} | |
| 55 | + | |
| 56 | +function jsToJSONProps(typ: any): any { | |
| 57 | + if (typ.jsToJSON === undefined) { | |
| 58 | + const map: any = {}; | |
| 59 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 60 | + typ.jsToJSON = map; | |
| 61 | + } | |
| 62 | + return typ.jsToJSON; | |
| 63 | +} | |
| 64 | + | |
| 65 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 66 | + function transformPrimitive(typ: string, val: any): any { | |
| 67 | + if (typeof typ === typeof val) return val; | |
| 68 | + return invalidValue(typ, val, key, parent); | |
| 69 | + } | |
| 70 | + | |
| 71 | + function transformUnion(typs: any[], val: any): any { | |
| 72 | + // val must validate against one typ in typs | |
| 73 | + const l = typs.length; | |
| 74 | + for (let i = 0; i < l; i++) { | |
| 75 | + const typ = typs[i]; | |
| 76 | + try { | |
| 77 | + return transform(val, typ, getProps); | |
| 78 | + } catch (_) {} | |
| 79 | + } | |
| 80 | + return invalidValue(typs, val, key, parent); | |
| 81 | + } | |
| 82 | + | |
| 83 | + function transformEnum(cases: string[], val: any): any { | |
| 84 | + if (cases.indexOf(val) !== -1) return val; | |
| 85 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 86 | + } | |
| 87 | + | |
| 88 | + function transformArray(typ: any, val: any): any { | |
| 89 | + // val must be an array with no invalid elements | |
| 90 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 91 | + return val.map(el => transform(el, typ, getProps)); | |
| 92 | + } | |
| 93 | + | |
| 94 | + function transformDate(val: any): any { | |
| 95 | + if (val === null) { | |
| 96 | + return null; | |
| 97 | + } | |
| 98 | + const d = new Date(val); | |
| 99 | + if (isNaN(d.valueOf())) { | |
| 100 | + return invalidValue(l("Date"), val, key, parent); | |
| 101 | + } | |
| 102 | + return d; | |
| 103 | + } | |
| 104 | + | |
| 105 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 106 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 107 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 108 | + } | |
| 109 | + const result: any = {}; | |
| 110 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 111 | + const prop = props[key]; | |
| 112 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 113 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 114 | + }); | |
| 115 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 116 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 117 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 118 | + } | |
| 119 | + }); | |
| 120 | + return result; | |
| 121 | + } | |
| 122 | + | |
| 123 | + if (typ === "any") return val; | |
| 124 | + if (typ === null) { | |
| 125 | + if (val === null) return val; | |
| 126 | + return invalidValue(typ, val, key, parent); | |
| 127 | + } | |
| 128 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 129 | + let ref: any = undefined; | |
| 130 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 131 | + ref = typ.ref; | |
| 132 | + typ = typeMap[typ.ref]; | |
| 133 | + } | |
| 134 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 135 | + if (typeof typ === "object") { | |
| 136 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 137 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 138 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 139 | + : invalidValue(typ, val, key, parent); | |
| 140 | + } | |
| 141 | + // Numbers can be parsed by Date but shouldn't be. | |
| 142 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 143 | + return transformPrimitive(typ, val); | |
| 144 | +} | |
| 145 | + | |
| 146 | +function cast<T>(val: any, typ: any): T { | |
| 147 | + return transform(val, typ, jsonToJSProps); | |
| 148 | +} | |
| 149 | + | |
| 150 | +function uncast<T>(val: T, typ: any): any { | |
| 151 | + return transform(val, typ, jsToJSONProps); | |
| 152 | +} | |
| 153 | + | |
| 154 | +function l(typ: any) { | |
| 155 | + return { literal: typ }; | |
| 156 | +} | |
| 157 | + | |
| 158 | +function a(typ: any) { | |
| 159 | + return { arrayItems: typ }; | |
| 160 | +} | |
| 161 | + | |
| 162 | +function u(...typs: any[]) { | |
| 163 | + return { unionMembers: typs }; | |
| 164 | +} | |
| 165 | + | |
| 166 | +function o(props: any[], additional: any) { | |
| 167 | + return { props, additional }; | |
| 168 | +} | |
| 169 | + | |
| 170 | +function m(additional: any) { | |
| 171 | + const props: any[] = []; | |
| 172 | + return { props, additional }; | |
| 173 | +} | |
| 174 | + | |
| 175 | +function r(name: string) { | |
| 176 | + return { ref: name }; | |
| 177 | +} | |
| 178 | + | |
| 179 | +const typeMap: any = { | |
| 180 | + "TopLevel": o([ | |
| 181 | + { json: "name", js: "name", typ: "" }, | |
| 182 | + ], false), | |
| 183 | +}; | |
| 184 | + | |
| 185 | +module.exports = { | |
| 186 | + "topLevelToJson": topLevelToJson, | |
| 187 | + "toTopLevel": toTopLevel, | |
| 188 | +}; |
Aschema-golangdefault / quicktype.go+25 −0
| @@ -0,0 +1,25 @@ | ||
| 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 | +type TopLevel []SomeObject | |
| 12 | + | |
| 13 | +func UnmarshalTopLevel(data []byte) (TopLevel, error) { | |
| 14 | + var r TopLevel | |
| 15 | + err := json.Unmarshal(data, &r) | |
| 16 | + return r, err | |
| 17 | +} | |
| 18 | + | |
| 19 | +func (r *TopLevel) Marshal() ([]byte, error) { | |
| 20 | + return json.Marshal(r) | |
| 21 | +} | |
| 22 | + | |
| 23 | +type SomeObject struct { | |
| 24 | + Name string `json:"name"` | |
| 25 | +} |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / Converter.java+121 −0
| @@ -0,0 +1,121 @@ | ||
| 1 | +// To use this code, add the following Maven dependency to your project: | |
| 2 | +// | |
| 3 | +// | |
| 4 | +// com.fasterxml.jackson.core : jackson-databind : 2.9.0 | |
| 5 | +// | |
| 6 | +// | |
| 7 | +// Import this package: | |
| 8 | +// | |
| 9 | +// import io.quicktype.Converter; | |
| 10 | +// | |
| 11 | +// Then you can deserialize a JSON string with | |
| 12 | +// | |
| 13 | +// List<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 List<TopLevel> fromJsonString(String json) throws IOException { | |
| 70 | + return getObjectReader().readValue(json); | |
| 71 | + } | |
| 72 | + | |
| 73 | + public static String toJsonString(List<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(List.class); | |
| 109 | + writer = mapper.writerFor(List.class); | |
| 110 | + } | |
| 111 | + | |
| 112 | + private static ObjectReader getObjectReader() { | |
| 113 | + if (reader == null) instantiateMapper(); | |
| 114 | + return reader; | |
| 115 | + } | |
| 116 | + | |
| 117 | + private static ObjectWriter getObjectWriter() { | |
| 118 | + if (writer == null) instantiateMapper(); | |
| 119 | + return writer; | |
| 120 | + } | |
| 121 | +} |
Aschema-java-datetime-legacydefault / src / main / java / io / quicktype / TopLevel.java+12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class TopLevel { | |
| 6 | + private String name; | |
| 7 | + | |
| 8 | + @JsonProperty("name") | |
| 9 | + public String getName() { return name; } | |
| 10 | + @JsonProperty("name") | |
| 11 | + public void setName(String value) { this.name = value; } | |
| 12 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / Converter.java+101 −0
| @@ -0,0 +1,101 @@ | ||
| 1 | +// To use this code, add the following Maven dependency to your project: | |
| 2 | +// | |
| 3 | +// | |
| 4 | +// com.fasterxml.jackson.core : jackson-databind : 2.9.0 | |
| 5 | +// com.fasterxml.jackson.datatype : jackson-datatype-jsr310 : 2.9.0 | |
| 6 | +// | |
| 7 | +// Import this package: | |
| 8 | +// | |
| 9 | +// import io.quicktype.Converter; | |
| 10 | +// | |
| 11 | +// Then you can deserialize a JSON string with | |
| 12 | +// | |
| 13 | +// List<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 List<TopLevel> fromJsonString(String json) throws IOException { | |
| 64 | + return getObjectReader().readValue(json); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public static String toJsonString(List<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(List.class); | |
| 89 | + writer = mapper.writerFor(List.class); | |
| 90 | + } | |
| 91 | + | |
| 92 | + private static ObjectReader getObjectReader() { | |
| 93 | + if (reader == null) instantiateMapper(); | |
| 94 | + return reader; | |
| 95 | + } | |
| 96 | + | |
| 97 | + private static ObjectWriter getObjectWriter() { | |
| 98 | + if (writer == null) instantiateMapper(); | |
| 99 | + return writer; | |
| 100 | + } | |
| 101 | +} |
Aschema-java-lombokdefault / src / main / java / io / quicktype / TopLevel.java+12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class TopLevel { | |
| 6 | + private String name; | |
| 7 | + | |
| 8 | + @JsonProperty("name") | |
| 9 | + public String getName() { return name; } | |
| 10 | + @JsonProperty("name") | |
| 11 | + public void setName(String value) { this.name = value; } | |
| 12 | +} |
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 | +// List<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 List<TopLevel> fromJsonString(String json) throws IOException { | |
| 64 | + return getObjectReader().readValue(json); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public static String toJsonString(List<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(List.class); | |
| 89 | + writer = mapper.writerFor(List.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 / TopLevel.java+12 −0
| @@ -0,0 +1,12 @@ | ||
| 1 | +package io.quicktype; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.*; | |
| 4 | + | |
| 5 | +public class TopLevel { | |
| 6 | + private String name; | |
| 7 | + | |
| 8 | + @JsonProperty("name") | |
| 9 | + public String getName() { return name; } | |
| 10 | + @JsonProperty("name") | |
| 11 | + public void setName(String value) { this.name = value; } | |
| 12 | +} |
Aschema-javascriptdefault / TopLevel.js+182 −0
| @@ -0,0 +1,182 @@ | ||
| 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), a(r("TopLevel"))); | |
| 14 | +} | |
| 15 | + | |
| 16 | +function topLevelToJson(value) { | |
| 17 | + return JSON.stringify(uncast(value, a(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: "name", js: "name", typ: "" }, | |
| 176 | + ], false), | |
| 177 | +}; | |
| 178 | + | |
| 179 | +module.exports = { | |
| 180 | + "topLevelToJson": topLevelToJson, | |
| 181 | + "toTopLevel": toTopLevel, | |
| 182 | +}; |
Aschema-kotlin-jacksondefault / TopLevel.kt+32 −0
| @@ -0,0 +1,32 @@ | ||
| 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 | +class TopLevel(elements: Collection<SomeObject>) : ArrayList<SomeObject>(elements) { | |
| 22 | + fun toJson() = mapper.writeValueAsString(this) | |
| 23 | + | |
| 24 | + companion object { | |
| 25 | + fun fromJson(json: String) = mapper.readValue<TopLevel>(json) | |
| 26 | + } | |
| 27 | +} | |
| 28 | + | |
| 29 | +data class SomeObject ( | |
| 30 | + @get:JsonProperty(required=true)@field:JsonProperty(required=true) | |
| 31 | + val name: String | |
| 32 | +) |
Aschema-kotlindefault / TopLevel.kt+21 −0
| @@ -0,0 +1,21 @@ | ||
| 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 | +class TopLevel(elements: Collection<SomeObject>) : ArrayList<SomeObject>(elements) { | |
| 12 | + public fun toJson() = klaxon.toJsonString(this) | |
| 13 | + | |
| 14 | + companion object { | |
| 15 | + public fun fromJson(json: String) = TopLevel(klaxon.parseArray<SomeObject>(json)!!) | |
| 16 | + } | |
| 17 | +} | |
| 18 | + | |
| 19 | +data class SomeObject ( | |
| 20 | + val name: String | |
| 21 | +) |
Aschema-pikedefault / TopLevel.pmod+39 −0
| @@ -0,0 +1,39 @@ | ||
| 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 | +typedef array(SomeObject) TopLevel; | |
| 16 | + | |
| 17 | +TopLevel TopLevel_from_JSON(mixed json) { | |
| 18 | + return map(json, SomeObject_from_JSON); | |
| 19 | +} | |
| 20 | + | |
| 21 | +class SomeObject { | |
| 22 | + string name; // json: "name" | |
| 23 | + | |
| 24 | + string encode_json() { | |
| 25 | + mapping(string:mixed) json = ([ | |
| 26 | + "name" : name, | |
| 27 | + ]); | |
| 28 | + | |
| 29 | + return Standards.JSON.encode(json); | |
| 30 | + } | |
| 31 | +} | |
| 32 | + | |
| 33 | +SomeObject SomeObject_from_JSON(mixed json) { | |
| 34 | + SomeObject retval = SomeObject(); | |
| 35 | + | |
| 36 | + retval.name = json["name"]; | |
| 37 | + | |
| 38 | + return retval; | |
| 39 | +} |
Aschema-pythondefault / quicktype.py+44 −0
| @@ -0,0 +1,44 @@ | ||
| 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_list(f: Callable[[Any], T], x: Any) -> list[T]: | |
| 14 | + assert isinstance(x, list) | |
| 15 | + return [f(y) for y in x] | |
| 16 | + | |
| 17 | + | |
| 18 | +def to_class(c: Type[T], x: Any) -> dict: | |
| 19 | + assert isinstance(x, c) | |
| 20 | + return cast(Any, x).to_dict() | |
| 21 | + | |
| 22 | + | |
| 23 | +@dataclass | |
| 24 | +class SomeObject: | |
| 25 | + name: str | |
| 26 | + | |
| 27 | + @staticmethod | |
| 28 | + def from_dict(obj: Any) -> 'SomeObject': | |
| 29 | + assert isinstance(obj, dict) | |
| 30 | + name = from_str(obj.get("name")) | |
| 31 | + return SomeObject(name) | |
| 32 | + | |
| 33 | + def to_dict(self) -> dict: | |
| 34 | + result: dict = {} | |
| 35 | + result["name"] = from_str(self.name) | |
| 36 | + return result | |
| 37 | + | |
| 38 | + | |
| 39 | +def top_level_from_dict(s: Any) -> list[SomeObject]: | |
| 40 | + return from_list(SomeObject.from_dict, s) | |
| 41 | + | |
| 42 | + | |
| 43 | +def top_level_to_dict(x: list[SomeObject]) -> Any: | |
| 44 | + return from_list(lambda x: to_class(SomeObject, x), x) |
Aschema-rubydefault / TopLevel.rb+55 −0
| @@ -0,0 +1,55 @@ | ||
| 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.first.some_object_name | |
| 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 SomeObject < Dry::Struct | |
| 23 | + attribute :some_object_name, Types::String | |
| 24 | + | |
| 25 | + def self.from_dynamic!(d) | |
| 26 | + d = Types::Hash[d] | |
| 27 | + new( | |
| 28 | + some_object_name: d.fetch("name"), | |
| 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 | + "name" => some_object_name, | |
| 39 | + } | |
| 40 | + end | |
| 41 | + | |
| 42 | + def to_json(options = nil) | |
| 43 | + JSON.generate(to_dynamic, options) | |
| 44 | + end | |
| 45 | +end | |
| 46 | + | |
| 47 | +class TopLevel | |
| 48 | + def self.from_json!(json) | |
| 49 | + top_level = JSON.parse(json, quirks_mode: true).map { |x| SomeObject.from_dynamic!(x) } | |
| 50 | + top_level.define_singleton_method(:to_json) do | |
| 51 | + JSON.generate(self.map { |x| x.to_dynamic }) | |
| 52 | + end | |
| 53 | + top_level | |
| 54 | + end | |
| 55 | +end |
Aschema-rustdefault / module_under_test.rs+21 −0
| @@ -0,0 +1,21 @@ | ||
| 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 | +pub type TopLevel = Vec<SomeObject>; | |
| 17 | + | |
| 18 | +#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 19 | +pub struct SomeObject { | |
| 20 | + pub name: String, | |
| 21 | +} |
Aschema-scala3-upickledefault / TopLevel.scala+72 −0
| @@ -0,0 +1,72 @@ | ||
| 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 | +type TopLevel = List[SomeObject] | |
| 69 | + | |
| 70 | +case class SomeObject ( | |
| 71 | + val name : String | |
| 72 | +) derives OptionPickler.ReadWriter |
Aschema-scala3default / TopLevel.scala+15 −0
| @@ -0,0 +1,15 @@ | ||
| 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 | +type TopLevel = List[SomeObject] | |
| 11 | +given (using ev : SomeObject): Encoder[Seq[SomeObject]] = Encoder.encodeSeq[SomeObject] | |
| 12 | + | |
| 13 | +case class SomeObject ( | |
| 14 | + val name : String | |
| 15 | +) derives Encoder.AsObject, Decoder |
Aschema-schemadefault / TopLevel.schema+22 −0
| @@ -0,0 +1,22 @@ | ||
| 1 | +{ | |
| 2 | + "$schema": "http://json-schema.org/draft-06/schema#", | |
| 3 | + "type": "array", | |
| 4 | + "items": { | |
| 5 | + "$ref": "#/definitions/SomeObject" | |
| 6 | + }, | |
| 7 | + "definitions": { | |
| 8 | + "SomeObject": { | |
| 9 | + "type": "object", | |
| 10 | + "additionalProperties": false, | |
| 11 | + "properties": { | |
| 12 | + "name": { | |
| 13 | + "type": "string" | |
| 14 | + } | |
| 15 | + }, | |
| 16 | + "required": [ | |
| 17 | + "name" | |
| 18 | + ], | |
| 19 | + "title": "SomeObject" | |
| 20 | + } | |
| 21 | + } | |
| 22 | +} |
Aschema-typescriptdefault / TopLevel.ts+185 −0
| @@ -0,0 +1,185 @@ | ||
| 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 SomeObject { | |
| 11 | + name: string; | |
| 12 | +} | |
| 13 | + | |
| 14 | +export type TopLevel = SomeObject[]; | |
| 15 | + | |
| 16 | +// Converts JSON strings to/from your types | |
| 17 | +// and asserts the results of JSON.parse at runtime | |
| 18 | +export class Convert { | |
| 19 | + public static toTopLevel(json: string): TopLevel { | |
| 20 | + return cast(JSON.parse(json), a(r("SomeObject"))); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public static topLevelToJson(value: TopLevel): string { | |
| 24 | + return JSON.stringify(uncast(value, a(r("SomeObject"))), null, 2); | |
| 25 | + } | |
| 26 | +} | |
| 27 | + | |
| 28 | +function invalidValue(typ: any, val: any, key: any, parent: any = ''): never { | |
| 29 | + const prettyTyp = prettyTypeName(typ); | |
| 30 | + const parentText = parent ? ` on ${parent}` : ''; | |
| 31 | + const keyText = key ? ` for key "${key}"` : ''; | |
| 32 | + throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); | |
| 33 | +} | |
| 34 | + | |
| 35 | +function prettyTypeName(typ: any): string { | |
| 36 | + if (Array.isArray(typ)) { | |
| 37 | + if (typ.length === 2 && typ[0] === undefined) { | |
| 38 | + return `an optional ${prettyTypeName(typ[1])}`; | |
| 39 | + } else { | |
| 40 | + return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; | |
| 41 | + } | |
| 42 | + } else if (typeof typ === "object" && typ.literal !== undefined) { | |
| 43 | + return typ.literal; | |
| 44 | + } else { | |
| 45 | + return typeof typ; | |
| 46 | + } | |
| 47 | +} | |
| 48 | + | |
| 49 | +function jsonToJSProps(typ: any): any { | |
| 50 | + if (typ.jsonToJS === undefined) { | |
| 51 | + const map: any = {}; | |
| 52 | + typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); | |
| 53 | + typ.jsonToJS = map; | |
| 54 | + } | |
| 55 | + return typ.jsonToJS; | |
| 56 | +} | |
| 57 | + | |
| 58 | +function jsToJSONProps(typ: any): any { | |
| 59 | + if (typ.jsToJSON === undefined) { | |
| 60 | + const map: any = {}; | |
| 61 | + typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); | |
| 62 | + typ.jsToJSON = map; | |
| 63 | + } | |
| 64 | + return typ.jsToJSON; | |
| 65 | +} | |
| 66 | + | |
| 67 | +function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { | |
| 68 | + function transformPrimitive(typ: string, val: any): any { | |
| 69 | + if (typeof typ === typeof val) return val; | |
| 70 | + return invalidValue(typ, val, key, parent); | |
| 71 | + } | |
| 72 | + | |
| 73 | + function transformUnion(typs: any[], val: any): any { | |
| 74 | + // val must validate against one typ in typs | |
| 75 | + const l = typs.length; | |
| 76 | + for (let i = 0; i < l; i++) { | |
| 77 | + const typ = typs[i]; | |
| 78 | + try { | |
| 79 | + return transform(val, typ, getProps); | |
| 80 | + } catch (_) {} | |
| 81 | + } | |
| 82 | + return invalidValue(typs, val, key, parent); | |
| 83 | + } | |
| 84 | + | |
| 85 | + function transformEnum(cases: string[], val: any): any { | |
| 86 | + if (cases.indexOf(val) !== -1) return val; | |
| 87 | + return invalidValue(cases.map(a => { return l(a); }), val, key, parent); | |
| 88 | + } | |
| 89 | + | |
| 90 | + function transformArray(typ: any, val: any): any { | |
| 91 | + // val must be an array with no invalid elements | |
| 92 | + if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); | |
| 93 | + return val.map(el => transform(el, typ, getProps)); | |
| 94 | + } | |
| 95 | + | |
| 96 | + function transformDate(val: any): any { | |
| 97 | + if (val === null) { | |
| 98 | + return null; | |
| 99 | + } | |
| 100 | + const d = new Date(val); | |
| 101 | + if (isNaN(d.valueOf())) { | |
| 102 | + return invalidValue(l("Date"), val, key, parent); | |
| 103 | + } | |
| 104 | + return d; | |
| 105 | + } | |
| 106 | + | |
| 107 | + function transformObject(props: { [k: string]: any }, additional: any, val: any): any { | |
| 108 | + if (val === null || typeof val !== "object" || Array.isArray(val)) { | |
| 109 | + return invalidValue(l(ref || "object"), val, key, parent); | |
| 110 | + } | |
| 111 | + const result: any = {}; | |
| 112 | + Object.getOwnPropertyNames(props).forEach(key => { | |
| 113 | + const prop = props[key]; | |
| 114 | + const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; | |
| 115 | + result[prop.key] = transform(v, prop.typ, getProps, key, ref); | |
| 116 | + }); | |
| 117 | + Object.getOwnPropertyNames(val).forEach(key => { | |
| 118 | + if (!Object.prototype.hasOwnProperty.call(props, key)) { | |
| 119 | + result[key] = transform(val[key], additional, getProps, key, ref); | |
| 120 | + } | |
| 121 | + }); | |
| 122 | + return result; | |
| 123 | + } | |
| 124 | + | |
| 125 | + if (typ === "any") return val; | |
| 126 | + if (typ === null) { | |
| 127 | + if (val === null) return val; | |
| 128 | + return invalidValue(typ, val, key, parent); | |
| 129 | + } | |
| 130 | + if (typ === false) return invalidValue(typ, val, key, parent); | |
| 131 | + let ref: any = undefined; | |
| 132 | + while (typeof typ === "object" && typ.ref !== undefined) { | |
| 133 | + ref = typ.ref; | |
| 134 | + typ = typeMap[typ.ref]; | |
| 135 | + } | |
| 136 | + if (Array.isArray(typ)) return transformEnum(typ, val); | |
| 137 | + if (typeof typ === "object") { | |
| 138 | + return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) | |
| 139 | + : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) | |
| 140 | + : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) | |
| 141 | + : invalidValue(typ, val, key, parent); | |
| 142 | + } | |
| 143 | + // Numbers can be parsed by Date but shouldn't be. | |
| 144 | + if (typ === Date && typeof val !== "number") return transformDate(val); | |
| 145 | + return transformPrimitive(typ, val); | |
| 146 | +} | |
| 147 | + | |
| 148 | +function cast<T>(val: any, typ: any): T { | |
| 149 | + return transform(val, typ, jsonToJSProps); | |
| 150 | +} | |
| 151 | + | |
| 152 | +function uncast<T>(val: T, typ: any): any { | |
| 153 | + return transform(val, typ, jsToJSONProps); | |
| 154 | +} | |
| 155 | + | |
| 156 | +function l(typ: any) { | |
| 157 | + return { literal: typ }; | |
| 158 | +} | |
| 159 | + | |
| 160 | +function a(typ: any) { | |
| 161 | + return { arrayItems: typ }; | |
| 162 | +} | |
| 163 | + | |
| 164 | +function u(...typs: any[]) { | |
| 165 | + return { unionMembers: typs }; | |
| 166 | +} | |
| 167 | + | |
| 168 | +function o(props: any[], additional: any) { | |
| 169 | + return { props, additional }; | |
| 170 | +} | |
| 171 | + | |
| 172 | +function m(additional: any) { | |
| 173 | + const props: any[] = []; | |
| 174 | + return { props, additional }; | |
| 175 | +} | |
| 176 | + | |
| 177 | +function r(name: string) { | |
| 178 | + return { ref: name }; | |
| 179 | +} | |
| 180 | + | |
| 181 | +const typeMap: any = { | |
| 182 | + "SomeObject": o([ | |
| 183 | + { json: "name", js: "name", typ: "" }, | |
| 184 | + ], false), | |
| 185 | +}; |
Test case
1 generated file · +9 −7test/inputs/schema/union.schema
Mschema-typescriptdefault / TopLevel.ts+9 −7
| @@ -1,28 +1,30 @@ | ||
| 1 | 1 | // To parse this data: |
| 2 | 2 | // |
| 3 | -// import { Convert } from "./TopLevel"; | |
| 3 | +// import { Convert, TopLevel } from "./TopLevel"; | |
| 4 | 4 | // |
| 5 | 5 | // const topLevel = Convert.toTopLevel(json); |
| 6 | 6 | // |
| 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 { | |
| 10 | +export interface TopLevelElement { | |
| 11 | 11 | one?: number; |
| 12 | 12 | two: boolean; |
| 13 | 13 | three?: number; |
| 14 | 14 | [property: string]: unknown; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | +export type TopLevel = TopLevelElement[]; | |
| 18 | + | |
| 17 | 19 | // Converts JSON strings to/from your types |
| 18 | 20 | // and asserts the results of JSON.parse at runtime |
| 19 | 21 | export class Convert { |
| 20 | - public static toTopLevel(json: string): TopLevel[] { | |
| 21 | - return cast(JSON.parse(json), a(r("TopLevel"))); | |
| 22 | + public static toTopLevel(json: string): TopLevel { | |
| 23 | + return cast(JSON.parse(json), a(r("TopLevelElement"))); | |
| 22 | 24 | } |
| 23 | 25 | |
| 24 | - public static topLevelToJson(value: TopLevel[]): string { | |
| 25 | - return JSON.stringify(uncast(value, a(r("TopLevel"))), null, 2); | |
| 26 | + public static topLevelToJson(value: TopLevel): string { | |
| 27 | + return JSON.stringify(uncast(value, a(r("TopLevelElement"))), null, 2); | |
| 26 | 28 | } |
| 27 | 29 | } |
| 28 | 30 | |
| @@ -180,7 +182,7 @@ function r(name: string) { | ||
| 180 | 182 | } |
| 181 | 183 | |
| 182 | 184 | const typeMap: any = { |
| 183 | - "TopLevel": o([ | |
| 185 | + "TopLevelElement": o([ | |
| 184 | 186 | { json: "one", js: "one", typ: u(undefined, 0) }, |
| 185 | 187 | { json: "two", js: "two", typ: true }, |
| 186 | 188 | { json: "three", js: "three", typ: u(undefined, 3.14) }, |
No generated files match these filters.