1 | // Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
|
2 | // Node module: @loopback/testlab
|
3 | // This file is licensed under the MIT License.
|
4 | // License text available at https://opensource.org/licenses/MIT
|
5 |
|
6 | /* eslint-disable @typescript-eslint/unified-signatures */
|
7 |
|
8 | // Important! Date.prototype.toJSON() returns a string.
|
9 | export function toJSON(value: Date): string;
|
10 |
|
11 | // Important! Functions cannot be encoded in JSON.
|
12 | export function toJSON(value: Function): undefined;
|
13 |
|
14 | // Distinguish arrays from objects (an array is an object too)
|
15 | export function toJSON(value: unknown[]): unknown[];
|
16 |
|
17 | /**
|
18 | * JSON encoding does not preserve properties that are undefined
|
19 | * As a result, deepEqual checks fail because the expected model
|
20 | * value contains these undefined property values, while the actual
|
21 | * result returned by REST API does not.
|
22 | * Use this function to convert a model instance into a data object
|
23 | * as returned by REST API
|
24 | */
|
25 | export function toJSON(value: object): object;
|
26 |
|
27 | // The following overloads are provided for convenience.
|
28 | // In practice, they should not be necessary, as they simply return the input
|
29 | // value without any modifications.
|
30 |
|
31 | export function toJSON(value: undefined): undefined;
|
32 | export function toJSON(value: null): null;
|
33 | export function toJSON(value: number): number;
|
34 | export function toJSON(value: boolean): boolean;
|
35 | export function toJSON(value: string): string;
|
36 |
|
37 | // The following overloads are required to allow TypesScript handle
|
38 | // commonly used union types.
|
39 |
|
40 | export function toJSON(value: unknown[] | null): unknown[] | null;
|
41 | export function toJSON(value: unknown[] | undefined): unknown[] | undefined;
|
42 | export function toJSON(
|
43 | value: unknown[] | null | undefined,
|
44 | ): unknown[] | null | undefined;
|
45 |
|
46 | export function toJSON(value: object | null): object | null;
|
47 | export function toJSON(value: object | undefined): object | undefined;
|
48 | export function toJSON(
|
49 | value: object | null | undefined,
|
50 | ): object | null | undefined;
|
51 |
|
52 | export function toJSON<T>(value: T) {
|
53 | return JSON.parse(JSON.stringify({value})).value;
|
54 | }
|