
Product
Rust Support in Socket Is Now Generally Available
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.
@simonbackx/simple-encoding
Advanced tools
This small library has no dependencies and adds interfaces and classes that you can use to encode and decode your own classes to a JSON compatible structure. Can get used for storage, APIs,
This small library has no dependencies and adds interfaces and classes that you can use to encode and decode your own classes to a JSON compatible structure. Can get used for storage, APIs,
Encoding and decoding can get customized, and that's the whole point of this library. You'll need to write some minor boilerplate code, but that will allow you to customize and work fast.
import { Encodeable, Data, ObjectData } from "@simonbackx/simple-encoding";
class MyClass implements Encodeable {
id: number;
name: string;
other?: MyClass;
constructor(data: { id: number; name: string; other?: MyClass }) {
this.id = data.id;
this.name = data.name;
this.other = data?.other;
}
writeName() {
console.log(this.name);
}
/**
* Note that the static MyClass implements Decoder.
* -> static inheritance is not supported in TypeScript
*/
static decode(data: Data): MyClass {
return new MyClass({
// 'walk' the data you receive
// data.field('fieldname') returns a new Data and throws if the field does not exist
// The returned Data object can get decoded with a Decoder using .decode(Decoder)
// or with a convenience getter: number, string
id: data.field("id").number,
name: data.field("name").string,
other: data.optionalField("other")?.decode(MyClass),
});
}
encode(context) {
return {
id: this.id,
name: this.name,
other: this.other?.encode(context),
};
}
}
const test = new MyClass({ id: 123, name: "Hello world" });
const test2 = new MyClass({ id: 123, name: "Hello world", other: test });
// Simulate a network or file storage by converting to JSON
// You can also convert it to a different storage type
const json = JSON.stringify(test2.encode());
// ... Store or send the JSON somewhere
// Decode from JSON
const plainObject = JSON.parse(json);
const data = new ObjectData(plainObject);
const instance = MyClass.decode(data);
// Decode throws if fields are missing or types are not okay.
// You can use the errors to provide good errors in you API's that help developers to
// point out the missing fields or types.
// Now all methods are available again
instance.writeName();
FAQs
This small library has no dependencies and adds interfaces and classes that you can use to encode and decode your own classes to a JSON compatible structure. This enables you to build powerful API's and storage that can automigrate to newer versions on th
The npm package @simonbackx/simple-encoding receives a total of 45 weekly downloads. As such, @simonbackx/simple-encoding popularity was classified as not popular.
We found that @simonbackx/simple-encoding demonstrated a healthy version release cadence and project activity because the last version was released less than 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.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.

Research
Five coordinated Chrome extensions enable session hijacking and block security controls across enterprise HR and ERP platforms.