Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pichillilorenzo/f295a76702c6535d7b7b4ae93abf3125 to your computer and use it in GitHub Desktop.
Save pichillilorenzo/f295a76702c6535d7b7b4ae93abf3125 to your computer and use it in GitHub Desktop.
@JsonManagedReference() and @JsonBackReference() example
import { JsonProperty, JsonClassType, ObjectMapper, JsonManagedReference, JsonBackReference } from 'jackson-js';
class User {
@JsonProperty() @JsonClassType({type: () => [Number]})
id: number;
@JsonProperty() @JsonClassType({type: () => [String]})
email: string;
@JsonProperty() @JsonClassType({type: () => [String]})
firstname: string;
@JsonProperty() @JsonClassType({type: () => [String]})
lastname: string;
@JsonProperty() @JsonClassType({type: () => [Array, [Item]]})
@JsonManagedReference()
items: Item[] = [];
constructor(id: number, email: string, firstname: string, lastname: string) {
this.id = id;
this.email = email;
this.firstname = firstname;
this.lastname = lastname;
}
}
class Item {
@JsonProperty() @JsonClassType({type: () => [Number]})
id: number;
@JsonProperty() @JsonClassType({type: () => [String]})
name: string;
@JsonProperty() @JsonClassType({type: () => [User]})
@JsonBackReference()
owner: User;
constructor(id: number, name: string, @JsonClassType({type: () => [User]}) owner: User) {
this.id = id;
this.name = name;
this.owner = owner;
}
}
const user = new User(1, 'john.alfa@gmail.com', 'John', 'Alfa');
const item1 = new Item(1, 'Book', user);
const item2 = new Item(2, 'Computer', user);
user.items.push(...[item1, item2]);
const objectMapper = new ObjectMapper();
const jsonData = objectMapper.stringify<User>(user);
console.log(jsonData);
// {"items":[{"id":1,"name":"Book"},{"id":2,"name":"Computer"}],"id":1,"email":"john.alfa@gmail.com","firstname":"John","lastname":"Alfa"}
const userParsed = objectMapper.parse<User>(jsonData, {mainCreator: () => [User]});
console.log(userParsed);
/*
<ref *1> User {
items: [
Item { id: 1, name: 'Book', owner: [Circular *1] },
Item { id: 2, name: 'Computer', owner: [Circular *1] }
],
id: 1,
email: 'john.alfa@gmail.com',
firstname: 'John',
lastname: 'Alfa'
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment