Socket
Socket
Sign inDemoInstall

serializer.ts

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serializer.ts - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

2

Decorators.d.ts
/**
* 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc