All Projects → pichillilorenzo → jackson-js

pichillilorenzo / jackson-js

Licence: MIT license
JavaScript object serialization and deserialization library using decorators. It supports also advanced Object concepts such as polymorphism, Object identity and cyclic objects.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to jackson-js

kson
A Java serialization/deserialization library to convert Java Objects into json and back, faster and powerful then Gson.
Stars: ✭ 25 (-70.93%)
Mutual labels:  json-serialization, json-parser, json-deserialization
Fastjson
A fast JSON parser/generator for Java.
Stars: ✭ 23,997 (+27803.49%)
Mutual labels:  json-serialization, json-parser
Python Rapidjson
Python wrapper around rapidjson
Stars: ✭ 417 (+384.88%)
Mutual labels:  json-serialization, json-parser
domino-jackson
Jackson with Annotation processing
Stars: ✭ 46 (-46.51%)
Mutual labels:  json-serialization, json-parser
JsonFormatter
Easy, Fast and Lightweight Json Formatter. (Serializer and Deserializer)
Stars: ✭ 26 (-69.77%)
Mutual labels:  json-serialization, json-parser
Easyjson
Fast JSON serializer for golang.
Stars: ✭ 3,512 (+3983.72%)
Mutual labels:  json-serialization, json-parser
Spray Json
A lightweight, clean and simple JSON implementation in Scala
Stars: ✭ 917 (+966.28%)
Mutual labels:  json-serialization, json-parser
nosj
a json parser written in pure bash
Stars: ✭ 33 (-61.63%)
Mutual labels:  json-parser, json-parse
Mir Ion
WIP, use libmir/asdf package for now
Stars: ✭ 78 (-9.3%)
Mutual labels:  json-serialization, json-parser
Waargonaut
JSON decoding/encoding/manipulation library.
Stars: ✭ 82 (-4.65%)
Mutual labels:  json-serialization, json-parser
Spotify Json
Fast and nice to use C++ JSON library.
Stars: ✭ 145 (+68.6%)
Mutual labels:  json-serialization, json-parser
libstud-json
JSON pull-parser/push-serializer library for C++
Stars: ✭ 20 (-76.74%)
Mutual labels:  json-serialization, json-parser
Json Dry
🌞 JSON-dry allows you to serialize & revive objects containing circular references, dates, regexes, class instances,...
Stars: ✭ 214 (+148.84%)
Mutual labels:  json-serialization, json-parser
Jsoncons
A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
Stars: ✭ 400 (+365.12%)
Mutual labels:  json-serialization, json-parser
representable
Maps representation documents from and to Ruby objects. Includes JSON, XML and YAML support, plain properties and compositions.
Stars: ✭ 689 (+701.16%)
Mutual labels:  json-serialization, json-parser
Json
JSON for Modern C++
Stars: ✭ 27,824 (+32253.49%)
Mutual labels:  json-serialization, json-parser
node-comment-json
Parse and stringify JSON with comments. It will retain comments even when after saved!
Stars: ✭ 106 (+23.26%)
Mutual labels:  json-stringify, json-parse
Jsondoc
JSON object for Delphi based on IUnknown and Variant
Stars: ✭ 20 (-76.74%)
Mutual labels:  json-serialization, json-parser
Bfj
MOVED TO GITLAB
Stars: ✭ 164 (+90.7%)
Mutual labels:  json-serialization, json-parser
Thorsserializer
C++ Serialization library for JSON
Stars: ✭ 241 (+180.23%)
Mutual labels:  json-serialization, json-parser

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: 

  • it not only deserialize JSON text into a JavaScript object, it also converts it into an instance of the class specified in the 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);
  • it supports more advanced Object concepts such as polymorphism and Object identity;
  • it supports cyclic object serialization/deserialization;
  • it supports serialization/deserialization of other native JavaScript types: Map, Set, BigInt, Typed Arrays (such as Int8Array);

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

  • manipulate JSON in depth;
  • restore a JavaScript type (a similar package is class-transformer);
  • preserve type information (using polymorphic type handling decorators: @JsonTypeInfo, @JsonSubTypes, and @JsonTypeName. A similar package is TypedJSON);
  • hide some properties for certain HTTP endpoints or some other external service;
  • have different JSON response for some external application or manage different JSON data coming from other application (for example you need to communicate with a Spring Boot application that uses different JSON Schema for the same model or with other applications made with Python, PHP, etc...);
  • manage cyclic references;
  • manage other JavaScript native types such as Maps and Sets;
  • etc.

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:

  • 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

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

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

Decorators available:

Important note

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[] = [];
}

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].