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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# typescript-json-serializer

[![Build Status](https://travis-ci.org/GillianPerard/typescript-json-serializer.svg?branch=master)](https://travis-ci.org/GillianPerard/typescript-json-serializer)
[![Coverage Status](https://coveralls.io/repos/github/GillianPerard/typescript-json-serializer/badge.svg)](https://coveralls.io/github/GillianPerard/typescript-json-serializer)
[![Known Vulnerabilities](https://snyk.io/test/github/gillianperard/typescript-json-serializer/badge.svg?targetFile=package.json)](https://snyk.io/test/github/gillianperard/typescript-json-serializer?targetFile=package.json)
Expand Down
81 changes: 71 additions & 10 deletions dist/spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = require("chai");
var index_1 = require("../src/index");
var panther_1 = require("../examples/models/panther");
var snake_1 = require("../examples/models/snake");
var zoo_1 = require("../examples/models/zoo");
var panther_1 = require("../examples/models/panther");
var data_1 = require("../examples/json/data");
require("reflect-metadata");
var _ = require("lodash");
var rewire = require('rewire');
var tjs = rewire('../src/index');
describe('Serializable', function () {
var zoo = new zoo_1.Zoo();
var panther = new panther_1.Panther();
var snake = new snake_1.Snake();
it('should return false', function () {
var hasMetadata = Reflect.hasOwnMetadata('api:map:serializable', snake_1.Snake);
chai_1.expect(hasMetadata).to.equal(false);
Expand All @@ -31,9 +30,7 @@ describe('Serializable', function () {
});
describe('serialize', function () {
it('should return true', function () {
var result = index_1.serialize(data_1.deserializedData);
var isEqual = _.isEqual(result, data_1.data);
chai_1.expect(isEqual).to.equal(true);
chai_1.expect(index_1.serialize(data_1.deserializedData)).to.deep.equal(data_1.data);
});
it('should return 3', function () {
var result = index_1.serialize(data_1.deserializedData, false);
Expand All @@ -48,8 +45,72 @@ describe('serialize', function () {
});
describe('deserialize', function () {
it('should return true', function () {
var result = index_1.deserialize(data_1.data, zoo_1.Zoo);
var isEqual = _.isEqual(result, data_1.deserializedData);
chai_1.expect(isEqual).to.equal(true);
chai_1.expect(index_1.deserialize(data_1.data, zoo_1.Zoo)).to.deep.equal(data_1.deserializedData);
});
});
describe('castSimpleData', function () {
var castSimpleData = tjs.__get__('castSimpleData');
it('should return hello', function () {
chai_1.expect(castSimpleData('string', 'hello')).to.equal('hello');
});
it('should return 4', function () {
chai_1.expect(castSimpleData('number', 4)).to.equal(4);
});
it('should return false', function () {
chai_1.expect(castSimpleData('boolean', false)).to.equal(false);
});
it('should return 4 as string', function () {
chai_1.expect(castSimpleData('string', 4)).to.equal('4');
});
it('should return true as string', function () {
chai_1.expect(castSimpleData('string', true)).to.equal('true');
});
it('should return 4', function () {
chai_1.expect(castSimpleData('number', '4')).to.equal(4);
});
it('should return undefined', function () {
chai_1.expect(castSimpleData('number', 'hello')).to.equal(undefined);
});
it('should return undefined', function () {
chai_1.expect(castSimpleData('boolean', 'hello')).to.equal(undefined);
});
it('should return undefined', function () {
chai_1.expect(castSimpleData('boolean', 4)).to.equal(undefined);
});
it('should return undefined', function () {
chai_1.expect(castSimpleData('date', 'hello')).to.equal(undefined);
});
it('should return undefined', function () {
chai_1.expect(castSimpleData('date', true)).to.equal(undefined);
});
it('should return a date', function () {
chai_1.expect(castSimpleData('date', 4)).to.deep.equal(new Date(4));
});
it('should return 2018-05-01T12:50:59.534Z', function () {
chai_1.expect(castSimpleData('date', '2018-05-01T12:50:59.534Z')).to.deep.equal(new Date('2018-05-01T12:50:59.534Z'));
});
});
describe('isSerializable', function () {
var isSerializable = tjs.__get__('isSerializable');
it('should return true', function () {
chai_1.expect(isSerializable(zoo_1.Zoo)).to.equal(true);
});
it('should return false', function () {
chai_1.expect(isSerializable(snake_1.Snake)).to.equal(false);
});
});
describe('getJsonPropertyValue', function () {
var getJsonPropertyValue = tjs.__get__('getJsonPropertyValue');
it('should return name equals to key and type equals undefined', function () {
chai_1.expect(getJsonPropertyValue('hello', undefined)).to.deep.equal({ name: 'hello', type: undefined });
});
it('should return name equals to args and type equals undefined', function () {
chai_1.expect(getJsonPropertyValue('hello', 'Hello')).to.deep.equal({ name: 'Hello', type: undefined });
});
it('should return name equals to key and type equals args["type"]', function () {
chai_1.expect(getJsonPropertyValue('zoo', { type: zoo_1.Zoo })).to.deep.equal({ name: 'zoo', type: zoo_1.Zoo });
});
it('should return name equals to args["name"] and type equals args["type"]', function () {
chai_1.expect(getJsonPropertyValue('zoo', { name: 'myZoo', type: zoo_1.Zoo })).to.deep.equal({ name: 'myZoo', type: zoo_1.Zoo });
});
});
13 changes: 8 additions & 5 deletions dist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function convertDataToProperty(instance, key, value, data) {
* Function to test if a class has the serializable decorator (metadata)
*/
function isSerializable(type) {
return Reflect.hasMetadata('api:map:serializable', type);
return Reflect.hasOwnMetadata('api:map:serializable', type);
}
/**
* Function to transform the JsonProperty value into an object like {name: string, type: Function}
Expand All @@ -151,7 +151,7 @@ function getJsonPropertyValue(key, args) {
*/
function castSimpleData(type, data) {
type = type.toLowerCase();
if ((typeof data).toLowerCase() === type.toLowerCase()) {
if ((typeof data).toLowerCase() === type) {
return data;
}
else {
Expand All @@ -161,19 +161,22 @@ function castSimpleData(type, data) {
else if (type === 'number') {
var n = +data;
if (isNaN(n)) {
throw new Error(data + ": Type " + typeof data + " is not assignable to type " + type + ".");
console.error(data + ": Type " + typeof data + " is not assignable to type " + type + ".");
return undefined;
}
else {
return n;
}
}
else if (type === 'boolean') {
throw new Error(data + ": Type " + typeof data + " is not assignable to type " + type + ".");
console.error(data + ": Type " + typeof data + " is not assignable to type " + type + ".");
return undefined;
}
else if (type === 'date') {
var n = Date.parse(data);
if (isNaN(n)) {
throw new Error(data + ": Type " + typeof data + " is not assignable to type " + type + ".");
console.error(data + ": Type " + typeof data + " is not assignable to type " + type + ".");
return undefined;
}
else {
return new Date(data);
Expand Down
Loading