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.
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:
context
option (similar packages are: class-transformer and TypedJSON); instead, with JSON.parse
you will get just a simple plain (literal) JavaScript object (just Object
type);Map
, Set
, BigInt
, Typed Arrays (such as Int8Array
);This library can be useful in more complex cases, for example when you want to:
@JsonTypeInfo
, @JsonSubTypes
, and @JsonTypeName
. A similar package is TypedJSON);Most of the use cases of the Java FasterXML/jackson annotations are similar or equal.
npm install --save jackson-js
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
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:
stringify(obj: T, context?: JsonStringifierContext): string
: a method for serializing a JavaScript object or a value to a JSON string with decorators applied;parse(text: string, context?: JsonParserContext): T
: a method for deserializing a JSON string into a JavaScript object/value (of type T
, based on the context given) with decorators applied.JsonParser
provides functionality for writing JSON and applies jackson-js
decorators. The main methods are:
parse(text: string, context?: JsonParserContext): T
: a method for deserializing a JSON string into a JavaScript object/value (of type T
, based on the context given) with decorators applied;transform(value: any, context?: JsonParserContext): any
: a method for applying jackson-js
decorators to a JavaScript object/value parsed. It returns a JavaScript object/value with decorators applied.JsonStringifier
provides functionality for reading JSON and applies jackson-js
decorators. The main methods are:
stringify(obj: T, context?: JsonStringifierContext): string
: a method for serializing a JavaScript object or a value to a JSON string with decorators applied;transform(value: any, context?: JsonStringifierContext): any
: a method for applying jackson-js
decorators to a JavaScript object/value. It returns a JavaScript object/value with decorators applied and ready to be JSON serialized.Decorators available:
The most important decorators are:
@JsonProperty()
: each class property (or its getter/setter) must be decorated with this decorator, otherwise deserialization and serialization will not work properly! That's because, for example, given a JavaScript class, there isn't any way or API (such as Reflection API for Java) to get for sure all the class properties; also because, sometimes, compilers such as TypeScript and Babel, can strip class properties after compilation from the class properties declaration;@JsonClassType()
: this decorator, instead, is used to define the type of a class property or method parameter. This information is used during serialization and, more important, during deserialization to know about the type of a property/parameter. This is necessary because JavaScript isn't a strongly-typed programming language, so, for example, during deserialization, without the usage of this decorator, there isn't any way to know the specific type of a class property, such as a Date
or a custom Class type.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[] = [];
}
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.