-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicktypeDemo.js
61 lines (50 loc) · 1.61 KB
/
quicktypeDemo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const {
quicktype,
InputData,
jsonInputForTargetLanguage,
JSONSchemaInput,
JSONSchemaStore
} = require("quicktype-core");
async function quicktypeJSON(targetLanguage, typeName, jsonString) {
const jsonInput = jsonInputForTargetLanguage(targetLanguage);
// We could add multiple samples for the same desired
// type, or many sources for other types. Here we're
// just making one type from one piece of sample JSON.
await jsonInput.addSource({
name: typeName,
samples: [jsonString]
});
const inputData = new InputData();
inputData.addInput(jsonInput);
return await quicktype({
inputData,
lang: targetLanguage
});
}
async function quicktypeJSONSchema(targetLanguage, typeName, jsonSchemaString) {
const schemaInput = new JSONSchemaInput(new JSONSchemaStore());
// We could add multiple schemas for multiple types,
// but here we're just making one type from JSON schema.
await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });
const inputData = new InputData();
inputData.addInput(schemaInput);
return await quicktype({
inputData,
lang: targetLanguage
});
}
async function main(jsonString) {
const { lines: swiftPerson } = await quicktypeJSON(
"typescript",
"Person",
jsonString
);
console.log(swiftPerson.join("\n"));
// const { lines: pythonPerson } = await quicktypeJSONSchema(
// "python",
// "Person",
// jsonSchemaString
// );
// console.log(pythonPerson.join("\n"));
}
main('{ "name": "David" }');