Generated-output differences

quicktype output changed between the PR base and tested PR merge revisions.
← Back to the pull request
46test cases
70files differ
45modified
25new
0deleted
2,439changed lines
+2,111 −328insertions / deletions
Base a49b94e8f6f1b319a4da9fb15d57ef13ba316ddc · PR merge b07290bfdcaa174b58d78cf8cabfd7b630a356cf · Head b15821ad80eda380675fc61c87fc4728fe69e931 · raw patch
Test case

test/inputs/json/misc/00c36.json

1 generated file · +12 −10
Mtypescriptdefault / TopLevel.ts+12 −10
@@ -1,15 +1,15 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export type TopLevel = TopLevelElement[] | PurpleTopLevel;
10+export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel;
1111
12-export interface TopLevelElement {
12+export interface PurpleTopLevel {
1313 country: Country;
1414 date: string;
1515 decimal: string;
@@ -26,22 +26,24 @@ export type ID = "US" | "NY.GDP.MKTP.CD";
2626
2727 export type Value = "United States" | "GDP (current US$)";
2828
29-export interface PurpleTopLevel {
29+export interface FluffyTopLevel {
3030 page: number;
3131 pages: number;
3232 per_page: string;
3333 total: number;
3434 }
3535
36+export type TopLevel = TopLevelUnion[];
37+
3638 // Converts JSON strings to/from your types
3739 // and asserts the results of JSON.parse at runtime
3840 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"))));
4143 }
4244
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);
4547 }
4648 }
4749
@@ -199,7 +201,7 @@ function r(name: string) {
199201 }
200202
201203 const typeMap: any = {
202- "TopLevelElement": o([
204+ "PurpleTopLevel": o([
203205 { json: "country", js: "country", typ: r("Country") },
204206 { json: "date", js: "date", typ: "" },
205207 { json: "decimal", js: "decimal", typ: "" },
@@ -210,7 +212,7 @@ const typeMap: any = {
210212 { json: "id", js: "id", typ: r("ID") },
211213 { json: "value", js: "value", typ: r("Value") },
212214 ], false),
213- "PurpleTopLevel": o([
215+ "FluffyTopLevel": o([
214216 { json: "page", js: "page", typ: 0 },
215217 { json: "pages", js: "pages", typ: 0 },
216218 { json: "per_page", js: "per_page", typ: "" },
Test case

test/inputs/json/misc/010b1.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 age: number;
1212 country: Country;
1313 females: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818
1919 export type Country = "United States";
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "age", js: "age", typ: 0 },
189191 { json: "country", js: "country", typ: r("Country") },
190192 { json: "females", js: "females", typ: 0 },
Test case

test/inputs/json/misc/050b0.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -49,15 +49,17 @@ export type MediaType = "text/html" | "text/plain";
4949
5050 export type Title = "HTML" | "Plain Text";
5151
52+export type TopLevel = TopLevelElement[];
53+
5254 // Converts JSON strings to/from your types
5355 // and asserts the results of JSON.parse at runtime
5456 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")));
5759 }
5860
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);
6163 }
6264 }
6365
@@ -215,7 +217,7 @@ function r(name: string) {
215217 }
216218
217219 const typeMap: any = {
218- "TopLevel": o([
220+ "TopLevelElement": o([
219221 { json: "id", js: "id", typ: "" },
220222 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
221223 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/06bee.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -44,15 +44,17 @@ export type MediaType = "text/html";
4444
4545 export type Title = "HTML";
4646
47+export type TopLevel = TopLevelElement[];
48+
4749 // Converts JSON strings to/from your types
4850 // and asserts the results of JSON.parse at runtime
4951 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")));
5254 }
5355
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);
5658 }
5759 }
5860
@@ -210,7 +212,7 @@ function r(name: string) {
210212 }
211213
212214 const typeMap: any = {
213- "TopLevel": o([
215+ "TopLevelElement": o([
214216 { json: "id", js: "id", typ: "" },
215217 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
216218 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/07c75.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: string[];
@@ -34,15 +34,17 @@ export interface Text {
3434 url: string;
3535 }
3636
37+export type TopLevel = TopLevelElement[];
38+
3739 // Converts JSON strings to/from your types
3840 // and asserts the results of JSON.parse at runtime
3941 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")));
4244 }
4345
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);
4648 }
4749 }
4850
@@ -200,7 +202,7 @@ function r(name: string) {
200202 }
201203
202204 const typeMap: any = {
203- "TopLevel": o([
205+ "TopLevelElement": o([
204206 { json: "id", js: "id", typ: "" },
205207 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
206208 { json: "keywords", js: "keywords", typ: a("") },
Test case

test/inputs/json/misc/0a91a.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 actor: Actor;
1212 created_at: Date;
1313 id: string;
@@ -224,15 +224,17 @@ export interface TopLevelRepo {
224224
225225 export type Type = "PushEvent" | "CreateEvent" | "WatchEvent" | "PullRequestEvent" | "DeleteEvent";
226226
227+export type TopLevel = TopLevelElement[];
228+
227229 // Converts JSON strings to/from your types
228230 // and asserts the results of JSON.parse at runtime
229231 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")));
232234 }
233235
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);
236238 }
237239 }
238240
@@ -390,7 +392,7 @@ function r(name: string) {
390392 }
391393
392394 const typeMap: any = {
393- "TopLevel": o([
395+ "TopLevelElement": o([
394396 { json: "actor", js: "actor", typ: r("Actor") },
395397 { json: "created_at", js: "created_at", typ: Date },
396398 { json: "id", js: "id", typ: "" },
Test case

test/inputs/json/misc/10be4.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -49,15 +49,17 @@ export type MediaType = "text/html" | "text/plain" | "application/pdf";
4949
5050 export type Title = "HTML" | "Plain Text" | "PDF";
5151
52+export type TopLevel = TopLevelElement[];
53+
5254 // Converts JSON strings to/from your types
5355 // and asserts the results of JSON.parse at runtime
5456 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")));
5759 }
5860
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);
6163 }
6264 }
6365
@@ -215,7 +217,7 @@ function r(name: string) {
215217 }
216218
217219 const typeMap: any = {
218- "TopLevel": o([
220+ "TopLevelElement": o([
219221 { json: "id", js: "id", typ: "" },
220222 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
221223 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/13d8d.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 category: string;
1212 context: string;
1313 id: number;
@@ -35,15 +35,17 @@ export interface OutcomeStatus {
3535 date: string;
3636 }
3737
38+export type TopLevel = TopLevelElement[];
39+
3840 // Converts JSON strings to/from your types
3941 // and asserts the results of JSON.parse at runtime
4042 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")));
4345 }
4446
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);
4749 }
4850 }
4951
@@ -201,7 +203,7 @@ function r(name: string) {
201203 }
202204
203205 const typeMap: any = {
204- "TopLevel": o([
206+ "TopLevelElement": o([
205207 { json: "category", js: "category", typ: "" },
206208 { json: "context", js: "context", typ: "" },
207209 { json: "id", js: "id", typ: 0 },
Test case

test/inputs/json/misc/1a7f5.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 age: number;
1212 country: Country;
1313 females: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818
1919 export type Country = "United States";
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "age", js: "age", typ: 0 },
189191 { json: "country", js: "country", typ: r("Country") },
190192 { json: "females", js: "females", typ: 0 },
Test case

test/inputs/json/misc/2df80.json

1 generated file · +12 −10
Mtypescriptdefault / TopLevel.ts+12 −10
@@ -1,15 +1,15 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export type TopLevel = TopLevelElement[] | PurpleTopLevel;
10+export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel;
1111
12-export interface TopLevelElement {
12+export interface PurpleTopLevel {
1313 adminregion: Adminregion;
1414 capitalCity: string;
1515 id: string;
@@ -31,22 +31,24 @@ export type ID = "" | "SAS" | "SSA" | "ECA" | "LAC" | "EAP" | "MNA" | "HIC" | "L
3131
3232 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";
3333
34-export interface PurpleTopLevel {
34+export interface FluffyTopLevel {
3535 page: number;
3636 pages: number;
3737 per_page: string;
3838 total: number;
3939 }
4040
41+export type TopLevel = TopLevelUnion[];
42+
4143 // Converts JSON strings to/from your types
4244 // and asserts the results of JSON.parse at runtime
4345 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"))));
4648 }
4749
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);
5052 }
5153 }
5254
@@ -204,7 +206,7 @@ function r(name: string) {
204206 }
205207
206208 const typeMap: any = {
207- "TopLevelElement": o([
209+ "PurpleTopLevel": o([
208210 { json: "adminregion", js: "adminregion", typ: r("Adminregion") },
209211 { json: "capitalCity", js: "capitalCity", typ: "" },
210212 { json: "id", js: "id", typ: "" },
@@ -220,7 +222,7 @@ const typeMap: any = {
220222 { json: "id", js: "id", typ: r("ID") },
221223 { json: "value", js: "value", typ: r("Value") },
222224 ], false),
223- "PurpleTopLevel": o([
225+ "FluffyTopLevel": o([
224226 { json: "page", js: "page", typ: 0 },
225227 { json: "pages", js: "pages", typ: 0 },
226228 { json: "per_page", js: "per_page", typ: "" },
Test case

test/inputs/json/misc/32d5c.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 BirthDate: Date | null;
1212 BirthDateIsProtected: boolean;
1313 GenderTypeID: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818 PreferredName: string;
1919 }
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "BirthDate", js: "BirthDate", typ: u(Date, null) },
189191 { json: "BirthDateIsProtected", js: "BirthDateIsProtected", typ: true },
190192 { json: "GenderTypeID", js: "GenderTypeID", typ: 0 },
Test case

test/inputs/json/misc/3536b.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -38,15 +38,17 @@ export interface Text {
3838 url: string;
3939 }
4040
41+export type TopLevel = TopLevelElement[];
42+
4143 // Converts JSON strings to/from your types
4244 // and asserts the results of JSON.parse at runtime
4345 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")));
4648 }
4749
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);
5052 }
5153 }
5254
@@ -204,7 +206,7 @@ function r(name: string) {
204206 }
205207
206208 const typeMap: any = {
207- "TopLevel": o([
209+ "TopLevelElement": o([
208210 { json: "id", js: "id", typ: "" },
209211 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
210212 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/43970.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,27 +1,29 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 ID: number;
1212 Name: string;
1313 Notes: string;
1414 }
1515
16+export type TopLevel = TopLevelElement[];
17+
1618 // Converts JSON strings to/from your types
1719 // and asserts the results of JSON.parse at runtime
1820 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")));
2123 }
2224
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);
2527 }
2628 }
2729
@@ -179,7 +181,7 @@ function r(name: string) {
179181 }
180182
181183 const typeMap: any = {
182- "TopLevel": o([
184+ "TopLevelElement": o([
183185 { json: "ID", js: "ID", typ: 0 },
184186 { json: "Name", js: "Name", typ: "" },
185187 { json: "Notes", js: "Notes", typ: "" },
Test case

test/inputs/json/misc/570ec.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: string[];
@@ -41,15 +41,17 @@ export interface Text {
4141 url: string;
4242 }
4343
44+export type TopLevel = TopLevelElement[];
45+
4446 // Converts JSON strings to/from your types
4547 // and asserts the results of JSON.parse at runtime
4648 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")));
4951 }
5052
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);
5355 }
5456 }
5557
@@ -207,7 +209,7 @@ function r(name: string) {
207209 }
208210
209211 const typeMap: any = {
210- "TopLevel": o([
212+ "TopLevelElement": o([
211213 { json: "id", js: "id", typ: "" },
212214 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
213215 { json: "keywords", js: "keywords", typ: a("") },
Test case

test/inputs/json/misc/5eae5.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,28 +1,30 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 Date: Date;
1212 ID: number;
1313 Sponsor: string;
1414 Title: string;
1515 }
1616
17+export type TopLevel = TopLevelElement[];
18+
1719 // Converts JSON strings to/from your types
1820 // and asserts the results of JSON.parse at runtime
1921 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")));
2224 }
2325
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);
2628 }
2729 }
2830
@@ -180,7 +182,7 @@ function r(name: string) {
180182 }
181183
182184 const typeMap: any = {
183- "TopLevel": o([
185+ "TopLevelElement": o([
184186 { json: "Date", js: "Date", typ: Date },
185187 { json: "ID", js: "ID", typ: 0 },
186188 { json: "Sponsor", js: "Sponsor", typ: "" },
Test case

test/inputs/json/misc/66121.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -44,15 +44,17 @@ export type MediaType = "text/html";
4444
4545 export type Title = "HTML";
4646
47+export type TopLevel = TopLevelElement[];
48+
4749 // Converts JSON strings to/from your types
4850 // and asserts the results of JSON.parse at runtime
4951 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")));
5254 }
5355
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);
5658 }
5759 }
5860
@@ -210,7 +212,7 @@ function r(name: string) {
210212 }
211213
212214 const typeMap: any = {
213- "TopLevel": o([
215+ "TopLevelElement": o([
214216 { json: "id", js: "id", typ: "" },
215217 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
216218 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/6eb00.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,27 +1,29 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 DirectorateID: number;
1212 Id: number;
1313 Name: string;
1414 }
1515
16+export type TopLevel = TopLevelElement[];
17+
1618 // Converts JSON strings to/from your types
1719 // and asserts the results of JSON.parse at runtime
1820 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")));
2123 }
2224
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);
2527 }
2628 }
2729
@@ -179,7 +181,7 @@ function r(name: string) {
179181 }
180182
181183 const typeMap: any = {
182- "TopLevel": o([
184+ "TopLevelElement": o([
183185 { json: "DirectorateID", js: "DirectorateID", typ: 0 },
184186 { json: "Id", js: "Id", typ: 0 },
185187 { json: "Name", js: "Name", typ: "" },
Test case

test/inputs/json/misc/77392.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 designation: string;
1212 discovery_date: Date;
1313 h_mag?: string;
@@ -24,15 +24,17 @@ export type OrbitClass = "Apollo" | "Amor" | "Aten" | "Comet" | "Jupiter-family
2424
2525 export type Pha = "Y" | "N" | "n/a";
2626
27+export type TopLevel = TopLevelElement[];
28+
2729 // Converts JSON strings to/from your types
2830 // and asserts the results of JSON.parse at runtime
2931 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")));
3234 }
3335
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);
3638 }
3739 }
3840
@@ -190,7 +192,7 @@ function r(name: string) {
190192 }
191193
192194 const typeMap: any = {
193- "TopLevel": o([
195+ "TopLevelElement": o([
194196 { json: "designation", js: "designation", typ: "" },
195197 { json: "discovery_date", js: "discovery_date", typ: Date },
196198 { json: "h_mag", js: "h_mag", typ: u(undefined, "") },
Test case

test/inputs/json/misc/7f568.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 comments: number;
1212 comments_url: string;
1313 commits_url: string;
@@ -38,15 +38,17 @@ export type Language = "Markdown";
3838
3939 export type Type = "text/plain";
4040
41+export type TopLevel = TopLevelElement[];
42+
4143 // Converts JSON strings to/from your types
4244 // and asserts the results of JSON.parse at runtime
4345 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")));
4648 }
4749
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);
5052 }
5153 }
5254
@@ -204,7 +206,7 @@ function r(name: string) {
204206 }
205207
206208 const typeMap: any = {
207- "TopLevel": o([
209+ "TopLevelElement": o([
208210 { json: "comments", js: "comments", typ: 0 },
209211 { json: "comments_url", js: "comments_url", typ: "" },
210212 { json: "commits_url", js: "commits_url", typ: "" },
Test case

test/inputs/json/misc/7fbfb.json

1 generated file · +12 −10
Mtypescriptdefault / TopLevel.ts+12 −10
@@ -1,15 +1,15 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export type TopLevel = TopLevelElement[] | PurpleTopLevel;
10+export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel;
1111
12-export interface TopLevelElement {
12+export interface PurpleTopLevel {
1313 country: Country;
1414 date: string;
1515 decimal: string;
@@ -26,22 +26,24 @@ export type ID = "CN" | "NY.GDP.MKTP.CD";
2626
2727 export type Value = "China" | "GDP (current US$)";
2828
29-export interface PurpleTopLevel {
29+export interface FluffyTopLevel {
3030 page: number;
3131 pages: number;
3232 per_page: string;
3333 total: number;
3434 }
3535
36+export type TopLevel = TopLevelUnion[];
37+
3638 // Converts JSON strings to/from your types
3739 // and asserts the results of JSON.parse at runtime
3840 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"))));
4143 }
4244
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);
4547 }
4648 }
4749
@@ -199,7 +201,7 @@ function r(name: string) {
199201 }
200202
201203 const typeMap: any = {
202- "TopLevelElement": o([
204+ "PurpleTopLevel": o([
203205 { json: "country", js: "country", typ: r("Country") },
204206 { json: "date", js: "date", typ: "" },
205207 { json: "decimal", js: "decimal", typ: "" },
@@ -210,7 +212,7 @@ const typeMap: any = {
210212 { json: "id", js: "id", typ: r("ID") },
211213 { json: "value", js: "value", typ: r("Value") },
212214 ], false),
213- "PurpleTopLevel": o([
215+ "FluffyTopLevel": o([
214216 { json: "page", js: "page", typ: 0 },
215217 { json: "pages", js: "pages", typ: 0 },
216218 { json: "per_page", js: "per_page", typ: "" },
Test case

test/inputs/json/misc/9847b.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,26 +1,28 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 name: string;
1313 }
1414
15+export type TopLevel = TopLevelElement[];
16+
1517 // Converts JSON strings to/from your types
1618 // and asserts the results of JSON.parse at runtime
1719 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")));
2022 }
2123
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);
2426 }
2527 }
2628
@@ -178,7 +180,7 @@ function r(name: string) {
178180 }
179181
180182 const typeMap: any = {
181- "TopLevel": o([
183+ "TopLevelElement": o([
182184 { json: "id", js: "id", typ: "" },
183185 { json: "name", js: "name", typ: "" },
184186 ], false),
Test case

test/inputs/json/misc/996bd.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -42,15 +42,17 @@ export type MediaType = "text/plain" | "text/html";
4242
4343 export type Title = "Plain Text" | "HTML";
4444
45+export type TopLevel = TopLevelElement[];
46+
4547 // Converts JSON strings to/from your types
4648 // and asserts the results of JSON.parse at runtime
4749 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")));
5052 }
5153
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);
5456 }
5557 }
5658
@@ -208,7 +210,7 @@ function r(name: string) {
208210 }
209211
210212 const typeMap: any = {
211- "TopLevel": o([
213+ "TopLevelElement": o([
212214 { json: "id", js: "id", typ: "" },
213215 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
214216 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/9a503.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -36,15 +36,17 @@ export interface Text {
3636 url: string;
3737 }
3838
39+export type TopLevel = TopLevelElement[];
40+
3941 // Converts JSON strings to/from your types
4042 // and asserts the results of JSON.parse at runtime
4143 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")));
4446 }
4547
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);
4850 }
4951 }
5052
@@ -202,7 +204,7 @@ function r(name: string) {
202204 }
203205
204206 const typeMap: any = {
205- "TopLevel": o([
207+ "TopLevelElement": o([
206208 { json: "id", js: "id", typ: "" },
207209 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
208210 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/9eed5.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -36,15 +36,17 @@ export interface Text {
3636 url: string;
3737 }
3838
39+export type TopLevel = TopLevelElement[];
40+
3941 // Converts JSON strings to/from your types
4042 // and asserts the results of JSON.parse at runtime
4143 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")));
4446 }
4547
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);
4850 }
4951 }
5052
@@ -202,7 +204,7 @@ function r(name: string) {
202204 }
203205
204206 const typeMap: any = {
205- "TopLevel": o([
207+ "TopLevelElement": o([
206208 { json: "id", js: "id", typ: "" },
207209 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
208210 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/a45b0.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 age: number;
1212 country: Country;
1313 females: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818
1919 export type Country = "United States";
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "age", js: "age", typ: 0 },
189191 { json: "country", js: "country", typ: r("Country") },
190192 { json: "females", js: "females", typ: 0 },
Test case

test/inputs/json/misc/ab0d1.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 age: number;
1212 country: Country;
1313 females: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818
1919 export type Country = "United States";
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "age", js: "age", typ: 0 },
189191 { json: "country", js: "country", typ: r("Country") },
190192 { json: "females", js: "females", typ: 0 },
Test case

test/inputs/json/misc/ad8be.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 identifiers: Identifier[];
1313 keywords: Keyword[];
@@ -49,15 +49,17 @@ export type MediaType = "text/html" | "text/plain" | "application/pdf";
4949
5050 export type Title = "HTML" | "Plain Text" | "PDF";
5151
52+export type TopLevel = TopLevelElement[];
53+
5254 // Converts JSON strings to/from your types
5355 // and asserts the results of JSON.parse at runtime
5456 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")));
5759 }
5860
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);
6163 }
6264 }
6365
@@ -215,7 +217,7 @@ function r(name: string) {
215217 }
216218
217219 const typeMap: any = {
218- "TopLevel": o([
220+ "TopLevelElement": o([
219221 { json: "id", js: "id", typ: "" },
220222 { json: "identifiers", js: "identifiers", typ: a(r("Identifier")) },
221223 { json: "keywords", js: "keywords", typ: a(r("Keyword")) },
Test case

test/inputs/json/misc/b4865.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 ":@computed_region_cbhk_fwbd"?: string;
1212 ":@computed_region_nnqa_25f4"?: string;
1313 fall: Fall;
@@ -33,15 +33,17 @@ export type Type = "Point";
3333
3434 export type Nametype = "Valid";
3535
36+export type TopLevel = TopLevelElement[];
37+
3638 // Converts JSON strings to/from your types
3739 // and asserts the results of JSON.parse at runtime
3840 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")));
4143 }
4244
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);
4547 }
4648 }
4749
@@ -199,7 +201,7 @@ function r(name: string) {
199201 }
200202
201203 const typeMap: any = {
202- "TopLevel": o([
204+ "TopLevelElement": o([
203205 { json: ":@computed_region_cbhk_fwbd", js: ":@computed_region_cbhk_fwbd", typ: u(undefined, "") },
204206 { json: ":@computed_region_nnqa_25f4", js: ":@computed_region_nnqa_25f4", typ: u(undefined, "") },
205207 { json: "fall", js: "fall", typ: r("Fall") },
Test case

test/inputs/json/misc/c8c7e.json

1 generated file · +12 −10
Mtypescriptdefault / TopLevel.ts+12 −10
@@ -1,15 +1,15 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export type TopLevel = TopLevelElement[] | PurpleTopLevel;
10+export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel;
1111
12-export interface TopLevelElement {
12+export interface PurpleTopLevel {
1313 country: Country;
1414 date: string;
1515 decimal: string;
@@ -26,22 +26,24 @@ export type ID = "IN" | "NY.GDP.MKTP.CD";
2626
2727 export type Value = "India" | "GDP (current US$)";
2828
29-export interface PurpleTopLevel {
29+export interface FluffyTopLevel {
3030 page: number;
3131 pages: number;
3232 per_page: string;
3333 total: number;
3434 }
3535
36+export type TopLevel = TopLevelUnion[];
37+
3638 // Converts JSON strings to/from your types
3739 // and asserts the results of JSON.parse at runtime
3840 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"))));
4143 }
4244
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);
4547 }
4648 }
4749
@@ -199,7 +201,7 @@ function r(name: string) {
199201 }
200202
201203 const typeMap: any = {
202- "TopLevelElement": o([
204+ "PurpleTopLevel": o([
203205 { json: "country", js: "country", typ: r("Country") },
204206 { json: "date", js: "date", typ: "" },
205207 { json: "decimal", js: "decimal", typ: "" },
@@ -210,7 +212,7 @@ const typeMap: any = {
210212 { json: "id", js: "id", typ: r("ID") },
211213 { json: "value", js: "value", typ: r("Value") },
212214 ], false),
213- "PurpleTopLevel": o([
215+ "FluffyTopLevel": o([
214216 { json: "page", js: "page", typ: 0 },
215217 { json: "pages", js: "pages", typ: 0 },
216218 { json: "per_page", js: "per_page", typ: "" },
Test case

test/inputs/json/misc/cda6c.json

1 generated file · +12 −10
Mtypescriptdefault / TopLevel.ts+12 −10
@@ -1,15 +1,15 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export type TopLevel = TopLevelElement[] | PurpleTopLevel;
10+export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel;
1111
12-export interface TopLevelElement {
12+export interface PurpleTopLevel {
1313 country: Country;
1414 date: string;
1515 decimal: string;
@@ -26,22 +26,24 @@ export type ID = "CN" | "SP.POP.TOTL";
2626
2727 export type Value = "China" | "Population, total";
2828
29-export interface PurpleTopLevel {
29+export interface FluffyTopLevel {
3030 page: number;
3131 pages: number;
3232 per_page: string;
3333 total: number;
3434 }
3535
36+export type TopLevel = TopLevelUnion[];
37+
3638 // Converts JSON strings to/from your types
3739 // and asserts the results of JSON.parse at runtime
3840 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"))));
4143 }
4244
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);
4547 }
4648 }
4749
@@ -199,7 +201,7 @@ function r(name: string) {
199201 }
200202
201203 const typeMap: any = {
202- "TopLevelElement": o([
204+ "PurpleTopLevel": o([
203205 { json: "country", js: "country", typ: r("Country") },
204206 { json: "date", js: "date", typ: "" },
205207 { json: "decimal", js: "decimal", typ: "" },
@@ -210,7 +212,7 @@ const typeMap: any = {
210212 { json: "id", js: "id", typ: r("ID") },
211213 { json: "value", js: "value", typ: r("Value") },
212214 ], false),
213- "PurpleTopLevel": o([
215+ "FluffyTopLevel": o([
214216 { json: "page", js: "page", typ: 0 },
215217 { json: "pages", js: "pages", typ: 0 },
216218 { json: "per_page", js: "per_page", typ: "" },
Test case

test/inputs/json/misc/e2a58.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,26 +1,28 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 date: string;
1212 "stop-and-search": string[];
1313 }
1414
15+export type TopLevel = TopLevelElement[];
16+
1517 // Converts JSON strings to/from your types
1618 // and asserts the results of JSON.parse at runtime
1719 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")));
2022 }
2123
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);
2426 }
2527 }
2628
@@ -178,7 +180,7 @@ function r(name: string) {
178180 }
179181
180182 const typeMap: any = {
181- "TopLevel": o([
183+ "TopLevelElement": o([
182184 { json: "date", js: "date", typ: "" },
183185 { json: "stop-and-search", js: "stop-and-search", typ: a("") },
184186 ], false),
Test case

test/inputs/json/misc/e53b5.json

1 generated file · +12 −10
Mtypescriptdefault / TopLevel.ts+12 −10
@@ -1,15 +1,15 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export type TopLevel = TopLevelElement[] | PurpleTopLevel;
10+export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel;
1111
12-export interface TopLevelElement {
12+export interface PurpleTopLevel {
1313 country: Country;
1414 date: string;
1515 decimal: string;
@@ -26,22 +26,24 @@ export type ID = "IN" | "SP.POP.TOTL";
2626
2727 export type Value = "India" | "Population, total";
2828
29-export interface PurpleTopLevel {
29+export interface FluffyTopLevel {
3030 page: number;
3131 pages: number;
3232 per_page: string;
3333 total: number;
3434 }
3535
36+export type TopLevel = TopLevelUnion[];
37+
3638 // Converts JSON strings to/from your types
3739 // and asserts the results of JSON.parse at runtime
3840 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"))));
4143 }
4244
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);
4547 }
4648 }
4749
@@ -199,7 +201,7 @@ function r(name: string) {
199201 }
200202
201203 const typeMap: any = {
202- "TopLevelElement": o([
204+ "PurpleTopLevel": o([
203205 { json: "country", js: "country", typ: r("Country") },
204206 { json: "date", js: "date", typ: "" },
205207 { json: "decimal", js: "decimal", typ: "" },
@@ -210,7 +212,7 @@ const typeMap: any = {
210212 { json: "id", js: "id", typ: r("ID") },
211213 { json: "value", js: "value", typ: r("Value") },
212214 ], false),
213- "PurpleTopLevel": o([
215+ "FluffyTopLevel": o([
214216 { json: "page", js: "page", typ: 0 },
215217 { json: "pages", js: "pages", typ: 0 },
216218 { json: "per_page", js: "per_page", typ: "" },
Test case

test/inputs/json/misc/e8a0b.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 age: number;
1212 country: Country;
1313 females: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818
1919 export type Country = "United States";
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "age", js: "age", typ: 0 },
189191 { json: "country", js: "country", typ: r("Country") },
190192 { json: "females", js: "females", typ: 0 },
Test case

test/inputs/json/misc/e8b04.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 averageRating: number;
1212 category?: string;
1313 createdAt: number;
@@ -240,15 +240,17 @@ export interface TableAuthor {
240240
241241 export type ViewType = "tabular";
242242
243+export type TopLevel = TopLevelElement[];
244+
243245 // Converts JSON strings to/from your types
244246 // and asserts the results of JSON.parse at runtime
245247 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")));
248250 }
249251
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);
252254 }
253255 }
254256
@@ -406,7 +408,7 @@ function r(name: string) {
406408 }
407409
408410 const typeMap: any = {
409- "TopLevel": o([
411+ "TopLevelElement": o([
410412 { json: "averageRating", js: "averageRating", typ: 0 },
411413 { json: "category", js: "category", typ: u(undefined, "") },
412414 { json: "createdAt", js: "createdAt", typ: 0 },
Test case

test/inputs/json/misc/f3139.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,26 +1,28 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 id: string;
1212 name: string;
1313 }
1414
15+export type TopLevel = TopLevelElement[];
16+
1517 // Converts JSON strings to/from your types
1618 // and asserts the results of JSON.parse at runtime
1719 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")));
2022 }
2123
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);
2426 }
2527 }
2628
@@ -178,7 +180,7 @@ function r(name: string) {
178180 }
179181
180182 const typeMap: any = {
181- "TopLevel": o([
183+ "TopLevelElement": o([
182184 { json: "id", js: "id", typ: "" },
183185 { json: "name", js: "name", typ: "" },
184186 ], false),
Test case

test/inputs/json/misc/f3edf.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 age: number;
1212 country: Country;
1313 females: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818
1919 export type Country = "United States";
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "age", js: "age", typ: 0 },
189191 { json: "country", js: "country", typ: r("Country") },
190192 { json: "females", js: "females", typ: 0 },
Test case

test/inputs/json/misc/f466a.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 age: number;
1212 country: Country;
1313 females: number;
@@ -18,15 +18,17 @@ export interface TopLevel {
1818
1919 export type Country = "United States";
2020
21+export type TopLevel = TopLevelElement[];
22+
2123 // Converts JSON strings to/from your types
2224 // and asserts the results of JSON.parse at runtime
2325 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")));
2628 }
2729
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);
3032 }
3133 }
3234
@@ -184,7 +186,7 @@ function r(name: string) {
184186 }
185187
186188 const typeMap: any = {
187- "TopLevel": o([
189+ "TopLevelElement": o([
188190 { json: "age", js: "age", typ: 0 },
189191 { json: "country", js: "country", typ: r("Country") },
190192 { json: "females", js: "females", typ: 0 },
Test case

test/inputs/json/priority/bug863.json

1 generated file · +12 −10
Mtypescriptdefault / TopLevel.ts+12 −10
@@ -1,15 +1,15 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export type TopLevel = TopLevelElement[] | PurpleTopLevel | string;
10+export type TopLevelUnion = PurpleTopLevel[] | FluffyTopLevel | string;
1111
12-export interface TopLevelElement {
12+export interface PurpleTopLevel {
1313 ALIENLEVEL: number;
1414 BREED: Breed;
1515 CHAR_DIMENSION: number;
@@ -41,7 +41,7 @@ export type RankTitle = "President" | "Advisor" | "Veteran" | "Applicant";
4141
4242 export type Sex = "Neuter" | "Male" | "Female";
4343
44-export interface PurpleTopLevel {
44+export interface FluffyTopLevel {
4545 ADVENTURERCOUNT: number;
4646 AGENTCOUNT: number;
4747 ATROXCOUNT: number;
@@ -79,15 +79,17 @@ export interface PurpleTopLevel {
7979 TRADERCOUNT: number;
8080 }
8181
82+export type TopLevel = TopLevelUnion[];
83+
8284 // Converts JSON strings to/from your types
8385 // and asserts the results of JSON.parse at runtime
8486 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"), "")));
8789 }
8890
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);
9193 }
9294 }
9395
@@ -245,7 +247,7 @@ function r(name: string) {
245247 }
246248
247249 const typeMap: any = {
248- "TopLevelElement": o([
250+ "PurpleTopLevel": o([
249251 { json: "ALIENLEVEL", js: "ALIENLEVEL", typ: 0 },
250252 { json: "BREED", js: "BREED", typ: r("Breed") },
251253 { json: "CHAR_DIMENSION", js: "CHAR_DIMENSION", typ: 0 },
@@ -264,7 +266,7 @@ const typeMap: any = {
264266 { json: "RANK_TITLE", js: "RANK_TITLE", typ: r("RankTitle") },
265267 { json: "SEX", js: "SEX", typ: r("Sex") },
266268 ], false),
267- "PurpleTopLevel": o([
269+ "FluffyTopLevel": o([
268270 { json: "ADVENTURERCOUNT", js: "ADVENTURERCOUNT", typ: 0 },
269271 { json: "AGENTCOUNT", js: "AGENTCOUNT", typ: 0 },
270272 { json: "ATROXCOUNT", js: "ATROXCOUNT", typ: 0 },
Test case

test/inputs/json/priority/issue2680-object-array.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,25 +1,27 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 key: string;
1212 }
1313
14+export type TopLevel = TopLevelElement[];
15+
1416 // Converts JSON strings to/from your types
1517 // and asserts the results of JSON.parse at runtime
1618 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")));
1921 }
2022
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);
2325 }
2426 }
2527
@@ -177,7 +179,7 @@ function r(name: string) {
177179 }
178180
179181 const typeMap: any = {
180- "TopLevel": o([
182+ "TopLevelElement": o([
181183 { json: "key", js: "key", typ: "" },
182184 ], false),
183185 };
Test case

test/inputs/json/priority/issue2680-scalar-array.json

1 generated file · +5 −3
Mtypescriptdefault / TopLevel.ts+5 −3
@@ -1,20 +1,22 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10+export type TopLevel = number[];
11+
1012 // Converts JSON strings to/from your types
1113 // and asserts the results of JSON.parse at runtime
1214 export class Convert {
13- public static toTopLevel(json: string): number[] {
15+ public static toTopLevel(json: string): TopLevel {
1416 return cast(JSON.parse(json), a(0));
1517 }
1618
17- public static topLevelToJson(value: number[]): string {
19+ public static topLevelToJson(value: TopLevel): string {
1820 return JSON.stringify(uncast(value, a(0)), null, 2);
1921 }
2022 }
Test case

test/inputs/json/priority/kotlin-enum-class-case-collision.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,27 +1,29 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 category: Category;
1212 }
1313
1414 export type Category = " " | "";
1515
16+export type TopLevel = TopLevelElement[];
17+
1618 // Converts JSON strings to/from your types
1719 // and asserts the results of JSON.parse at runtime
1820 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")));
2123 }
2224
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);
2527 }
2628 }
2729
@@ -179,7 +181,7 @@ function r(name: string) {
179181 }
180182
181183 const typeMap: any = {
182- "TopLevel": o([
184+ "TopLevelElement": o([
183185 { json: "category", js: "category", typ: r("Category") },
184186 ], false),
185187 "Category": [
Test case

test/inputs/json/priority/optional-union.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,27 +1,29 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 a?: A;
1212 }
1313
1414 export type A = number | string;
1515
16+export type TopLevel = TopLevelElement[];
17+
1618 // Converts JSON strings to/from your types
1719 // and asserts the results of JSON.parse at runtime
1820 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")));
2123 }
2224
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);
2527 }
2628 }
2729
@@ -179,7 +181,7 @@ function r(name: string) {
179181 }
180182
181183 const typeMap: any = {
182- "TopLevel": o([
184+ "TopLevelElement": o([
183185 { json: "a", js: "a", typ: u(undefined, u(0, "")) },
184186 ], false),
185187 };
Test case

test/inputs/json/samples/github-events.json

1 generated file · +9 −7
Mtypescriptdefault / TopLevel.ts+9 −7
@@ -1,13 +1,13 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 actor: Actor;
1212 created_at: Date;
1313 id: string;
@@ -295,15 +295,17 @@ export interface TopLevelRepo {
295295 url: string;
296296 }
297297
298+export type TopLevel = TopLevelElement[];
299+
298300 // Converts JSON strings to/from your types
299301 // and asserts the results of JSON.parse at runtime
300302 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")));
303305 }
304306
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);
307309 }
308310 }
309311
@@ -461,7 +463,7 @@ function r(name: string) {
461463 }
462464
463465 const typeMap: any = {
464- "TopLevel": o([
466+ "TopLevelElement": o([
465467 { json: "actor", js: "actor", typ: r("Actor") },
466468 { json: "created_at", js: "created_at", typ: Date },
467469 { json: "id", js: "id", typ: "" },
Test case

test/inputs/schema/issue2680-top-level-array.schema

1 generated file · +5 −3
Mschema-typescriptdefault / TopLevel.ts+5 −3
@@ -1,20 +1,22 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10+export type TopLevel = number[];
11+
1012 // Converts JSON strings to/from your types
1113 // and asserts the results of JSON.parse at runtime
1214 export class Convert {
13- public static toTopLevel(json: string): number[] {
15+ public static toTopLevel(json: string): TopLevel {
1416 return cast(JSON.parse(json), a(0));
1517 }
1618
17- public static topLevelToJson(value: number[]): string {
19+ public static topLevelToJson(value: TopLevel): string {
1820 return JSON.stringify(uncast(value, a(0)), null, 2);
1921 }
2022 }
Test case

test/inputs/schema/root-array-ref.schema

25 generated files · +1,693 −0
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

test/inputs/schema/union.schema

1 generated file · +9 −7
Mschema-typescriptdefault / TopLevel.ts+9 −7
@@ -1,28 +1,30 @@
11 // To parse this data:
22 //
3-// import { Convert } from "./TopLevel";
3+// import { Convert, TopLevel } from "./TopLevel";
44 //
55 // const topLevel = Convert.toTopLevel(json);
66 //
77 // These functions will throw an error if the JSON doesn't
88 // match the expected interface, even if the JSON is valid.
99
10-export interface TopLevel {
10+export interface TopLevelElement {
1111 one?: number;
1212 two: boolean;
1313 three?: number;
1414 [property: string]: unknown;
1515 }
1616
17+export type TopLevel = TopLevelElement[];
18+
1719 // Converts JSON strings to/from your types
1820 // and asserts the results of JSON.parse at runtime
1921 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")));
2224 }
2325
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);
2628 }
2729 }
2830
@@ -180,7 +182,7 @@ function r(name: string) {
180182 }
181183
182184 const typeMap: any = {
183- "TopLevel": o([
185+ "TopLevelElement": o([
184186 { json: "one", js: "one", typ: u(undefined, 0) },
185187 { json: "two", js: "two", typ: true },
186188 { json: "three", js: "three", typ: u(undefined, 3.14) },
No generated files match these filters.