MCPcopy Create free account
hub / github.com/CoderLine/alphaTab / generateToJsonBody

Function generateToJsonBody

packages/alphatab/scripts/Serializer.toJson.ts:6–314  ·  view source on GitHub ↗
(serializable: TypeSchema, importer: (name: string, module: string) => void)

Source from the content-addressed store, hash-verified

4import type { TypeSchema } from './TypeSchema';
5
6function generateToJsonBody(serializable: TypeSchema, importer: (name: string, module: string) => void) {
7 const statements: ts.Statement[] = [];
8
9 statements.push(
10 createNodeFromSource<ts.IfStatement>(
11 `
12 if(!obj) {
13 return null;
14 }
15 `,
16 ts.SyntaxKind.IfStatement
17 )
18 );
19
20 statements.push(
21 createNodeFromSource<ts.VariableStatement>(
22 `
23 const o = new Map<string, unknown>();
24 `,
25 ts.SyntaxKind.VariableStatement
26 )
27 );
28
29 for (const prop of serializable.properties) {
30 const fieldName = prop.name;
31 const jsonName = prop.jsonNames.filter(n => n !== '')[0];
32
33 if (!jsonName || prop.isJsonReadOnly) {
34 continue;
35 }
36
37 let propertyStatements: ts.Statement[] = [];
38
39 if (prop.asRaw || prop.type.isPrimitiveType) {
40 propertyStatements.push(
41 createNodeFromSource<ts.ExpressionStatement>(
42 `
43 o.set(${JSON.stringify(jsonName)}, obj.${fieldName});
44 `,
45 ts.SyntaxKind.ExpressionStatement
46 )
47 );
48 } else if (prop.type.isEnumType) {
49 if (prop.type.isNullable) {
50 propertyStatements.push(
51 createNodeFromSource<ts.ExpressionStatement>(
52 `
53 o.set(${JSON.stringify(jsonName)}, obj.${fieldName} as number|null);
54 `,
55 ts.SyntaxKind.ExpressionStatement
56 )
57 );
58 } else if (prop.type.isOptional) {
59 propertyStatements.push(
60 createNodeFromSource<ts.ExpressionStatement>(
61 `
62 o.set(${JSON.stringify(jsonName)}, obj.${fieldName} as number|undefined);
63 `,

Callers 1

createToJsonMethodFunction · 0.85

Calls 7

createNodeFromSourceFunction · 0.90
findSerializerModuleFunction · 0.90
filterMethod · 0.80
stringifyMethod · 0.80
importerFunction · 0.70
pushMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected