MCPcopy Create free account
hub / github.com/loopbackio/loopback-next / toJSON

Method toJSON

packages/repository/src/model.ts:341–370  ·  view source on GitHub ↗

* Serialize into a plain JSON object

()

Source from the content-addressed store, hash-verified

339 * Serialize into a plain JSON object
340 */
341 toJSON(): Object {
342 const def = (this.constructor as typeof Model).definition;
343 if (def == null || def.settings.strict === false) {
344 return this.toObject({ignoreUnknownProperties: false});
345 }
346
347 const copyPropertyAsJson = (key: string) => {
348 const val = asJSON((this as AnyObject)[key]);
349 if (val !== undefined) {
350 json[key] = val;
351 }
352 };
353
354 const json: AnyObject = {};
355 const hiddenProperties: string[] = def.settings.hiddenProperties || [];
356 for (const p in def.properties) {
357 if (p in this && !hiddenProperties.includes(p)) {
358 copyPropertyAsJson(p);
359 }
360 }
361
362 for (const r in def.relations) {
363 const relName = def.relations[r].name;
364 if (relName in this) {
365 copyPropertyAsJson(relName);
366 }
367 }
368
369 return json;
370 }
371
372 /**
373 * Convert to a plain object as DTO

Callers 15

asJSONFunction · 0.45
serializeMethod · 0.45
serializeMethod · 0.45
serializeMethod · 0.45
serializeMethod · 0.45
coerceMethod · 0.45
model.unit.tsFile · 0.45
type.unit.tsFile · 0.45

Calls 1

toObjectMethod · 0.95

Tested by

no test coverage detected