Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions tsp-typescript-client/src/utils/jsonbig-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// tslint:disable: no-unused-expression

import { Experiment } from '../models/experiment';
import { Identifier } from '../models/identifier';
import { Query } from '../models/query/query';
import { HttpRequest, HttpResponse, RestClient } from '../protocol/rest-client';
import { FixtureSet } from '../protocol/test-utils';
import { TspClient } from '../protocol/tsp-client';
import { JSONBigUtils } from './jsonbig-utils';

describe('JSONBigUtils tests', () => {

const client = new TspClient('not-relevant');
const httpRequestMock = jest.fn<Promise<HttpResponse>, [req: HttpRequest]>();

let fixtures: FixtureSet;

beforeAll(async () => {
fixtures = await FixtureSet.fromFolder(__dirname, '../../fixtures/tsp-client');
RestClient['httpRequest'] = httpRequestMock;
});

beforeEach(() => {
httpRequestMock.mockReset();
httpRequestMock.mockResolvedValue({ text: '', status: 404, statusText: 'Not found' });
});

it('testJSONBigUtils-with-normalizer', async () => {
httpRequestMock.mockReturnValueOnce(fixtures.asResponse('create-experiment-0.json'));
const response = await client.createExperiment(new Query({}));
const experiment = response.getModel()!;

const input = JSONBigUtils.stringify(experiment);
const experiment2: Experiment = JSONBigUtils.parse(input, Experiment);

expect(experiment).toEqual(experiment2);
});

it('testJSONBigUtils-no-normalizer', async () => {
const inputObj = 'hallo';
const input = JSONBigUtils.stringify(inputObj);
const output = JSONBigUtils.parse<string>(input);
expect(typeof output).toEqual('string');

let bigIntObj = BigInt("1234567890123456789");
const bigIntInput = JSONBigUtils.stringify(bigIntObj);
const bigIntOutput = JSONBigUtils.parse<bigint>(bigIntInput);
expect(typeof bigIntOutput).toEqual('bigint');

const idObj: Identifier = {
version: "version",
buildTime: "buildTime",
os: "os",
osArch: "osArch",
osVersion: "osVersion",
cpuCount: "cpuCount",
maxMemory: "maxMemory",
launcherName: "launcherName",
productId: "productId"
};
const idInput = JSONBigUtils.stringify(idObj);
const idOutput: Identifier = JSONBigUtils.parse<Identifier>(idInput);
expect(typeof idOutput).toEqual('object');
expect(idOutput).toEqual(idObj);
});
});
48 changes: 48 additions & 0 deletions tsp-typescript-client/src/utils/jsonbig-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Normalizer } from '../protocol/serialization';
import JSONBigConfig = require('json-bigint');

const JSONBig = JSONBigConfig({
useNativeBigInt: true,
});

/**
* Rest client helper to make request.
* The request response status code indicates if the request is successful.
* The json object in the response may be undefined when an error occurs.
*/
export class JSONBigUtils {

static parse<T>(url: string): T;
static parse<T>(input: string, normalizer?: Normalizer<T>): T;
/**
* Parse JSON-encoded data using a normalizer. It will create `BigInt'
* values instead of `number` as defined by normalizer.
*
* @template T is the expected type of the json object returned
* @param input Input JSON string to deserialize
* @param normalizer Normalizer to create type T
*/
public static parse<T>(input: string, normalizer?: Normalizer<T>) {
try {
const parsed = JSONBig.parse(input);
try {
if (normalizer) {
return normalizer(parsed) as T;
}
return parsed as T;
} catch (err) {
console.log('Error normalizing parsed input string: ' + err.toString());
}
} catch (err) {
console.log('Error parsing input string: ' + JSON.stringify(err));
}
return null;
}

/**
* Stringify JS objects. Can stringify `BigInt` values.
*/
public static stringify(data: any): string {
return JSONBig.stringify(data);
}
}