MCPcopy Index your code
hub / github.com/fastify/fast-json-stringify / buildObject

Function buildObject

index.js:553–601  ·  view source on GitHub ↗
(context, location, input)

Source from the content-addressed store, hash-verified

551}
552
553function buildObject (context, location, input) {
554 const schema = location.schema
555
556 if (context.functionsNamesBySchema.has(schema)) {
557 const funcName = context.functionsNamesBySchema.get(schema)
558 return `json += ${funcName}(${input})`
559 }
560
561 const nullable = schema.nullable === true
562
563 const schemaId = location.schemaId || ''
564 const jsonPointer = location.jsonPointer || ''
565 const fullPath = `${schemaId}#${jsonPointer}`
566
567 if (context.recursivePaths.has(fullPath) || context.buildingSet.has(schema) || schemaId !== '') {
568 const functionName = generateFuncName(context)
569 context.functionsNamesBySchema.set(schema, functionName)
570
571 const schemaRef = getSafeSchemaRef(context, location)
572
573 const functionCode = `
574 // ${schemaRef}
575 function ${functionName} (input) {
576 const obj = ${toJSON('input')}
577 if (obj === null) return ${nullable ? 'JSON_STR_NULL' : 'JSON_STR_EMPTY_OBJECT'}
578 let json = ''
579
580 ${buildInnerObject(context, location, 'obj')}
581 return json
582 }
583 `
584
585 context.functions.push(functionCode)
586 return `json += ${functionName}(${input})`
587 }
588
589 context.buildingSet.add(schema)
590 const objVar = `obj_${context.uid++}`
591 const code = `
592 const ${objVar} = ${toJSON(input)}
593 if (${objVar} === null) {
594 json += ${nullable ? 'JSON_STR_NULL' : 'JSON_STR_EMPTY_OBJECT'}
595 } else {
596 ${buildInnerObject(context, location, objVar)}
597 }
598 `
599 context.buildingSet.delete(schema)
600 return code
601}
602
603function buildArray (context, location, input) {
604 const schema = location.schema

Callers 1

Calls 4

generateFuncNameFunction · 0.85
getSafeSchemaRefFunction · 0.85
toJSONFunction · 0.85
buildInnerObjectFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…