* Serialize into a plain JSON object
()
| 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 |
no test coverage detected