serializer.ts
Advanced tools
Comparing version 0.0.1 to 0.0.2
/** | ||
* Specifies a type of the property. Property needs to known about its type | ||
*/ | ||
export declare function Type(typeFunction?: () => Function): Function; | ||
export declare function Type(typeFunction?: (type?: any) => Function): (target: any, key: string) => void; | ||
/** | ||
@@ -6,0 +6,0 @@ * Marks property as skipped on the process of serialization and deserialization. By default it skips the property |
{ | ||
"name": "serializer.ts", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Proper serialization and deserialization raw json objects to classes in Typescript", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -91,3 +91,3 @@ # Serializer.ts | ||
```javascript | ||
import {serialize} from "serializer.ts"; | ||
import {serialize} from "serializer.ts/Serializer"; | ||
@@ -103,3 +103,3 @@ let photo = serialize(photo); | ||
```javascript | ||
import {deserialize} from "serializer.ts"; | ||
import {deserialize} from "serializer.ts/Serializer"; | ||
@@ -164,2 +164,28 @@ let users = deserialize<User[]>(User, usersJson); | ||
## Converting date strings into Date objects | ||
Sometimes you have dates in your plain old javascript objects received in a string format. And you want to create a | ||
real javascript Date objects from them. To make deserializer to automatically make your date strings a Date objects | ||
simply pass Date object to the `@Type` decorator: | ||
```javascript | ||
import {Skip, Type} from "serializer.ts/Decorators"; | ||
export class User { | ||
id: number; | ||
email: string; | ||
@Skip() | ||
password: string; | ||
@Type(() => Date) | ||
registrationDate: Date; | ||
} | ||
``` | ||
Same technique can be used with `Number`, `String`, `Boolean` primitive types when you want to convert your values | ||
into these types. | ||
## Example with Angular2 | ||
@@ -166,0 +192,0 @@ |
@@ -43,6 +43,20 @@ var MetadataStorage_1 = require("./metadata/MetadataStorage"); | ||
} | ||
else if (object[key] instanceof Object) { | ||
else if (object[key] instanceof Object || type) { | ||
if (!type) | ||
throw new TypeMissingError_1.TypeMissingError(cls, key); | ||
newObject[key] = _this.convert(type, object[key], operationType); | ||
if (type === Date) { | ||
newObject[key] = new Date(object[key]); | ||
} | ||
else if (type === String) { | ||
newObject[key] = String(object[key]); | ||
} | ||
else if (type === Number) { | ||
newObject[key] = Number(object[key]); | ||
} | ||
else if (type === Boolean) { | ||
newObject[key] = Boolean(object[key]); | ||
} | ||
else { | ||
newObject[key] = _this.convert(type, object[key], operationType); | ||
} | ||
} | ||
@@ -49,0 +63,0 @@ else { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32323
375
211