-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility to support json serializtion/deserialization with JSONBigInt
fixes #57 Signed-off-by: Bernd Hufmann <[email protected]>
- Loading branch information
Showing
17 changed files
with
126 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tsp-typescript-client/src/utils/serialization-utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// tslint:disable: no-unused-expression | ||
|
||
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 { SerializationUtil } from './serialization-utils'; | ||
import { Experiment } from '../models/experiment'; | ||
|
||
describe('SerializationUtils 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('testSerializationUtil', async () => { | ||
httpRequestMock.mockReturnValueOnce(fixtures.asResponse('create-experiment-0.json')); | ||
const response = await client.createExperiment(new Query({})); | ||
const experiment = response.getModel()!; | ||
|
||
const input = SerializationUtil.serialize(experiment); | ||
const experiment2 = SerializationUtil.deserialize(input, Experiment); | ||
|
||
expect(experiment).toEqual(experiment2); | ||
}); | ||
|
||
it('testSerializationUtil-no-normalizer', async () => { | ||
const inputObj = 'hallo'; | ||
const input = SerializationUtil.serialize(inputObj); | ||
const output = SerializationUtil.deserialize<string>(input); | ||
expect(typeof output).toEqual('string'); | ||
|
||
let bigIntObj = BigInt("1234567890123456789"); | ||
const bigIntInput = SerializationUtil.serialize(bigIntObj); | ||
const bigIntOutput = SerializationUtil.deserialize<bigint>(bigIntInput); | ||
expect(typeof bigIntOutput).toEqual('bigint'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Deserialized, Normalizer } from './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 SerializationUtil { | ||
|
||
static deserialize<T>(url: string): Deserialized<T>; | ||
static deserialize<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 | ||
*/ | ||
static deserialize<T>(input: string, normalizer?: Normalizer<T>) { | ||
try { | ||
const parsed = this.jsonParse(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. | ||
*/ | ||
static serialize(object: any): string { | ||
return this.jsonStringify(object); | ||
} | ||
|
||
/** | ||
* Stringify JS objects. Can stringify `BigInt` values. | ||
*/ | ||
protected static jsonStringify(data: any): string { | ||
return JSONBig.stringify(data); | ||
} | ||
|
||
/** | ||
* Parse JSON-encoded data. If a number is too large to fit into a regular | ||
* `number` then it will be deserialized as `BigInt`. | ||
*/ | ||
protected static jsonParse(text: string): any { | ||
return JSONBig.parse(text); | ||
} | ||
} |
File renamed without changes.