Skip to content

Commit

Permalink
Fix/null serializing (#8)
Browse files Browse the repository at this point in the history
* add yarn.

* yarn file

* adding null as a type

* adding error for undefined input, adding null checking

* adding null value transformation

* changelog
  • Loading branch information
buehler authored Apr 12, 2017
1 parent d80d297 commit 6508c85
Show file tree
Hide file tree
Showing 10 changed files with 1,757 additions and 1,585 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ node_js:

before_install:
- export TZ=Europe/Zurich
- curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
- sudo apt-key adv --keyserver pgp.mit.edu --recv D101F7899D41F3C3
- echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
- sudo apt-get update -qq
- sudo apt-get install -y -qq yarn

script:
- npm test
- npm install coveralls@^2.11.9 && cat ./coverage/lcov-mapped.info | coveralls
- npm run build
- yarn test
- yarn add --no-lockfile coveralls@^2.11.9 && cat ./coverage/lcov-mapped.info | coveralls
- yarn run build

deploy:
provider: npm
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
#### Added
- Possibility to serialize / deserialize `null`.

## [1.1.0]
#### Added
Expand Down
28 changes: 22 additions & 6 deletions TsSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReferenceObjectNotFoundError, TypeNotRegisteredError } from './errors';
import { ReferenceObjectNotFoundError, TypeNotRegisteredError, UndefinedInputError } from './errors';
import { Resolver } from './Resolver';
import { TransportObject } from './TransportObject';

Expand Down Expand Up @@ -70,10 +70,13 @@ export class TsSerializer {
* @memberOf TsSerializer
*/
public serialize(objectOrArray: any): string {
if (objectOrArray === undefined) {
throw new UndefinedInputError('serialize');
}
this.references = {};
let serialized: any;
if (objectOrArray.constructor === Array) {
serialized = objectOrArray.map(o => this.serializeObject(o));
if (objectOrArray !== null && objectOrArray.constructor === Array) {
serialized = objectOrArray.filter(o => o !== undefined).map(o => this.serializeObject(o));
} else {
serialized = this.serializeObject(objectOrArray);
}
Expand All @@ -91,10 +94,13 @@ export class TsSerializer {
* @memberOf TsSerializer
*/
public deserialize<T>(json: string): T {
if (json === undefined) {
throw new UndefinedInputError('deserialize');
}
this.references = {};
const parsed = JSON.parse(json);
let deserialized: any;
if (parsed.constructor === Array) {
if (parsed !== null && parsed.constructor === Array) {
deserialized = parsed.map(o => this.deserializeObject(o));
} else {
deserialized = this.deserializeObject(parsed);
Expand All @@ -114,12 +120,17 @@ export class TsSerializer {
* @memberOf TsSerializer
*/
private serializeObject(obj: any): TransportObject {
if (obj.constructor === Date) {
if (obj === null) {
return {
__type: 'null',
__value: null
};
} else if (obj.constructor === Date) {
return {
__type: 'Date',
__value: obj
};
} else if (obj.constructor === Array) {
} else if (obj.constructor === Array) {
return {
__type: 'Array',
__value: obj.map(o => this.serializeObject(o))
Expand Down Expand Up @@ -176,6 +187,8 @@ export class TsSerializer {
*/
private deserializeObject(obj: TransportObject): any {
switch (obj.__type) {
case 'null':
return null;
case 'Date':
return new Date(obj.__value);
case 'Number':
Expand Down Expand Up @@ -224,6 +237,9 @@ export class TsSerializer {
* @memberOf TsSerializer
*/
private resolveReferences(obj: any): void {
if (obj === null) {
return;
}
for (let property of Object.keys(obj)) {
const prop = obj[property];
if (prop instanceof ReferencedObject) {
Expand Down
15 changes: 15 additions & 0 deletions errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ export class ReferenceObjectNotFoundError {
this.message = 'The reference object was not found in the previous deserialized objects';
}
}

/**
* Error that is thrown when an input to deserialize or serialize is undefined.
*
* @export
* @class UndefinedInputError
*/
export class UndefinedInputError {
public message: string;

constructor(functionname: string) {
this.message = `The input of your '${functionname}' call was undefined.
Undefined can't be serlialized or deserialized.`;
}
}
Loading

0 comments on commit 6508c85

Please sign in to comment.