-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
198 additions
and
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,22 @@ | ||
/** | ||
* Options for the @toJson decorator. | ||
*/ | ||
export interface IToJsonOptions { | ||
/** | ||
* When set to true it will overwrite any toJSON already existing on the prototype. | ||
*/ | ||
overwrite?: boolean; | ||
} | ||
/** | ||
* Decorator that will generate toJSON function on the class prototype that allows | ||
* JSON.stringify to be used instead of TypedJSON.stringify. Under the hood it will | ||
* simply delegate to TypedJSON. | ||
* By default it will throw if the prototype already has a toJSON function defined. | ||
* @param target the class which prototype should be modified. | ||
*/ | ||
export declare function toJson<T extends Object>(target: Function): void; | ||
/** | ||
* Decorator factory that accepts the options interface. | ||
* @param options for configuring the toJSON creation. | ||
*/ | ||
export declare function toJson<T extends Object>(options: IToJsonOptions): ((target: Function) => void); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import {jsonObject, jsonMember, toJson} from "../js/typedjson"; | ||
|
||
describe('toJson decorator', function () { | ||
it('should work with JSON.stringify', function () { | ||
@toJson | ||
@jsonObject | ||
class Person { | ||
firstName?: string; | ||
|
||
@jsonMember({name: 'surname'}) | ||
lastName?: string; | ||
|
||
public getFullName() { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
} | ||
|
||
const person = new Person; | ||
person.firstName = 'John'; | ||
person.lastName = 'Doe'; | ||
expect(JSON.stringify(person)).toBe('{"surname":"Doe"}'); | ||
}); | ||
|
||
it('should work on the abstract class', function () { | ||
@toJson | ||
abstract class Base { | ||
@jsonMember({name: 'renamed'}) | ||
prop?: string; | ||
} | ||
|
||
@jsonObject | ||
class Sub extends Base { | ||
@jsonMember({name: 'numeric'}) | ||
num?: number; | ||
} | ||
|
||
@jsonObject | ||
class OtherSub extends Base { | ||
@jsonMember | ||
decimal?: number; | ||
ignored?: string; | ||
} | ||
|
||
|
||
const sub = new Sub; | ||
sub.prop = 'value'; | ||
sub.num = 20; | ||
expect(JSON.stringify(sub)).toBe('{"renamed":"value","numeric":20}'); | ||
|
||
const otherSub = new OtherSub; | ||
otherSub.prop = 'value'; | ||
otherSub.decimal = 123; | ||
otherSub.ignored = 'assigned'; | ||
expect(JSON.stringify(otherSub)).toBe('{"renamed":"value","decimal":123}'); | ||
}); | ||
|
||
it("should throw an error when toJSON already exists", function () { | ||
try { | ||
@toJson | ||
@jsonObject | ||
class Some { | ||
@jsonMember | ||
prop?: string; | ||
|
||
toJSON() { | ||
return {}; | ||
} | ||
} | ||
|
||
const some = new Some; | ||
some.prop = 'value'; | ||
expect(JSON.stringify(some)).toBe('{}'); | ||
|
||
fail('Should not succeed'); | ||
} catch (error) { | ||
// ok | ||
} | ||
}); | ||
|
||
|
||
it("should overwrite toJSON when overwrite is true", function () { | ||
@toJson({overwrite: true}) | ||
@jsonObject | ||
class Some { | ||
@jsonMember | ||
prop?: string; | ||
|
||
toJSON() { | ||
return {}; | ||
} | ||
} | ||
|
||
const some = new Some; | ||
some.prop = 'value'; | ||
expect(JSON.stringify(some)).toBe('{"prop":"value"}'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { TypedJSON } from "../parser"; | ||
|
||
/** | ||
* Options for the @toJson decorator. | ||
*/ | ||
export interface IToJsonOptions { | ||
/** | ||
* When set to true it will overwrite any toJSON already existing on the prototype. | ||
*/ | ||
overwrite?: boolean; | ||
} | ||
|
||
/** | ||
* Decorator that will generate toJSON function on the class prototype that allows | ||
* JSON.stringify to be used instead of TypedJSON.stringify. Under the hood it will | ||
* simply delegate to TypedJSON. | ||
* By default it will throw if the prototype already has a toJSON function defined. | ||
* @param target the class which prototype should be modified. | ||
*/ | ||
export function toJson<T extends Object>(target: Function): void; | ||
/** | ||
* Decorator factory that accepts the options interface. | ||
* @param options for configuring the toJSON creation. | ||
*/ | ||
export function toJson<T extends Object>(options: IToJsonOptions): ((target: Function) => void); | ||
export function toJson<T extends Object>(optionsOrTarget: IToJsonOptions | Function | ||
): ((target: Function) => void) | void { | ||
if (typeof optionsOrTarget === 'function') { | ||
// used directly | ||
toJsonDecorator(optionsOrTarget, {}); | ||
return; | ||
} | ||
// used as a factory | ||
return (target: Function) => { | ||
toJsonDecorator(target, optionsOrTarget); | ||
} | ||
} | ||
|
||
function toJsonDecorator<T extends Object>(target: Function, options: IToJsonOptions): void { | ||
if (!options.overwrite && target.prototype.toJSON) { | ||
throw new Error(`${target.name} already has toJSON defined!`); | ||
} | ||
target.prototype.toJSON = function () { | ||
return TypedJSON.toPlainJson(this, Object.getPrototypeOf(this).constructor); | ||
} | ||
} |