Home

Awesome

jackson-js

npm downloads jackson-js version Travis Coverage Status license Donate to this project using Paypal Donate to this project using Patreon

As the name implies, jackson-js is heavily inspired by the famous Java FasterXML/jackson library.

It can be used on both client (browser) and server (Node.js) side.

Why this library? What's the difference between using this library instead of JSON.parse and JSON.stringify?

For simple cases, you don't need this library of course, you can just use JSON.parse and JSON.stringify to serialize/deserialize JSON.

With jackson-js , you can easily manipulate your JavaScript objects/values serialization/deserialization using decorators such as @JsonProperty(), @JsonFormat(), @JsonIgnore(), and more. However, this library uses JSON.parse and JSON.stringify under the hood.

Furthermore: 

This library can be useful in more complex cases, for example when you want to:

Most of the use cases of the Java FasterXML/jackson annotations are similar or equal.

Installation

npm install --save jackson-js

API

API docs can be found here.

The main classes that jackson-js offers to serialize and deserialize JavaScript objects are: ObjectMapper, JsonStringifier and JsonParser.

ObjectMapper

ObjectMapper provides functionality for both reading and writing JSON and applies jackson-js decorators. It will use instances of JsonParser and JsonStringifier for implementing actual reading/writing of JSON. It has two methods:

JsonParser

JsonParser provides functionality for writing JSON and applies jackson-js decorators. The main methods are:

JsonStringifier

JsonStringifier provides functionality for reading JSON and applies jackson-js decorators. The main methods are:

Decorators

Decorators available:

Important note

The most important decorators are:

Here is a quick example about this two decorators:

class Book {
  @JsonProperty() @JsonClassType({type: () => [String]})
  name: string;

  @JsonProperty() @JsonClassType({type: () => [String]})
  category: string;
}

class Writer {
  @JsonProperty() @JsonClassType({type: () => [Number]})
  id: number;
  @JsonProperty() @JsonClassType({type: () => [String]})
  name: string;

  @JsonProperty() @JsonClassType({type: () => [Array, [Book]]})
  books: Book[] = [];
}

Tutorials

Examples

Code examples can be found inside the tests folder and in this example repository. The example repository gives a simple example using the jackson-js library with Angular 9 for the client side and two examples for the server side: one using Node.js + Express + SQLite3 (with Sequelize 5) and another one using Node.js + LoopBack 4.