Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
ts-json-serializer
Advanced tools
Readme
This typescript serializer project should bring ease when using objects on both ends of a
json based communication. Most of the time, interfaces
are sufficent since there are no methods
on the communication objects. But what happens when "calculated fields" are used (like a read only
property). This library does register the used type as classes in a type system and uses a
special notation to transfer the objects over the wire. After that, the deserialized objects are
real instances (so instanceof
does work), even with references to other types (i.e. in circular
references).
To install this package, simply run
To use the ts-json-serializer
instantiate an instance of it and throw your decorated models in it.
models.ts
:
import { Serializable } from 'ts-json-serializer';
@Serializable()
export class User {
public name: string;
public surname: string;
public address: Address;
}
@Serializable()
export class Address {
public street: string;
public city: string;
public zip: number;
}
Now when you wan't to send your models to another client, just use the TsSerializer
:
import { User, Address } from './models';
import { TsSerializer } from 'ts-json-serializer';
const address = new Address();
address.street = 'Foobar';
address.city = 'SomewhereThere';
address.zip = 1337
const user1 = new User();
user1.name = 'Sally';
user1.surname = `O'Brien`;
user1.address = address;
const user2 = new User();
user2.name = 'Jake';
user2.surname = `O'Brien`;
user2.address = address;
const serializer = new TsSerializer();
serializer.serialize([user1, user2]); // <- This returns a transmittable string
To deserialize, just use the json string that is returned by serialize
and reverse the process.
const deserializer = new TsSerializer();
const deserialized = deserializer.deserialize<User[]>(jsonString);
const user1 = deserialized[0];
const user2 = deserialized[1];
user1.address === user2.address; // --> TRUE.
Actually, you can serialize normal objects, or array of objects. The library does recognize the array
and does parse the objects one by one. The generic type T
is not mandatory, but strongly recommended.
This is especally useful, if you transmit multiple objects that can be identified by an interface or an
abstract class. The types are instantiated with the concrete class, but you could use the interface
as type generic information and just parse them all:
interface Foobar {
sayHello(): void;
}
class FooOne implements Foobar {
public sayHello(): void {
console.log('hello from FooOne');
}
}
class FooTwo implements Foobar {
public sayHello(): void {
console.log('hello from FooTwo');
}
}
const serializer = new TsSerializer();
const foobarThings = serializer.deserializer<Foobar[]>(string);
for(let foo of foobarThings){
foo.sayHello();
}
The changelog is based on keep a changelog and is located here:
This software is licenced under the MIT licence.
FAQs
Object serialization made easy with decorators.
The npm package ts-json-serializer receives a total of 87 weekly downloads. As such, ts-json-serializer popularity was classified as not popular.
We found that ts-json-serializer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.