Skip to content

Commit

Permalink
Merge pull request #20 from SyslogicNL/develop
Browse files Browse the repository at this point in the history
 0.1.2
  • Loading branch information
Henk van de Berg authored Apr 25, 2018
2 parents 1023529 + 4182395 commit 654245e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
2 changes: 2 additions & 0 deletions dist/graph-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
* @returns {any}
*/
function deserialize(type, src) {
if (src === null)
return null;
var ret = new type();
var isDerivedClass = Object.getPrototypeOf(type) instanceof Function;
if (isDerivedClass) {
Expand Down
2 changes: 1 addition & 1 deletion dist/graph-serializer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
"scripts": {
"test": "node ./node_modules/mocha/bin/_mocha --require ts-node/register ./test/**/*.ts"
},
"version": "0.1.1",
"version": "0.1.2",
"typings": "dist/graph-serializer.d.ts"
}
5 changes: 4 additions & 1 deletion src/graph-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,15 @@ const store = new Store();
*/
export function deserialize(type: any, src: any): any {

if(src === null)
return null;

let ret = new type();

let isDerivedClass = Object.getPrototypeOf(type) instanceof Function;
if(isDerivedClass) {
let extendedType = Object.getPrototypeOf(Object.getPrototypeOf(new type())).constructor;
Object.assign(ret,deserialize(extendedType,src));
Object.assign(ret, deserialize(extendedType,src));
}

store.get(type).properties.forEach((property: PropertyDescription, propertyName: string) => {
Expand Down
78 changes: 78 additions & 0 deletions test/nullobject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {expect} from 'chai';
import {array, custom, date, deserialize, object, serializable, serialize} from "../src/graph-serializer";

/** @todo find a strategy to handle any of the standard objects, such as defined in
* https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects
* and
* https://developer.mozilla.org/en-US/docs/Web/API
*/

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

describe('null value in nested class', () => {

class Foo {
@serializable() name:string;
}
class Bar {
@serializable() name:string;
@serializable() nest:Foo;
}

it('deserialize', () => {

let output = deserialize(Bar,{
name: "bar",
nest: {
name: "foo"
}
});
expect(output).to.be.instanceOf(Bar);
expect(output.nest.name).to.equal('foo');

output = deserialize(Bar,{
name: "bar",
nest: null
});
expect(output.nest).to.equal(null);

});
});

describe('null object value class', () => {
// Test for use case. If decorated object property is null, it should be deserialized as such
class Foo {
@serializable() fooName: string;
}
class Bar {
@serializable({scheme: object(Foo)}) foo: Foo;
}
it('deserialize', () => {
let output = deserialize(Bar,{
name: 'bar',
foo: null,
})
expect(output.foo).to.equal(null);
});
});

describe('null object value in decorator-less class', () => {

class Foo {
foo: string;
}
class Bar {
@serializable({scheme: object(Foo)}) foo: Foo;
}
it('deserialize', () => {
let output = deserialize(Bar,{
name: 'bar',
foo: null,
});
expect(output.foo).to.equal(null);
});
});


});

0 comments on commit 654245e

Please sign in to comment.