0

I have 2 bi-directional models and I use JsonIdentityInfo to prevention from infinite Recursion in json result. Here is my viewmodels:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class GroupsViewModel extends BaseEntityViewModel<Long> {
     private String               title;
     private Set<UserViewModel>   users;
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class UserViewModel extends BaseEntityViewModel<Long> {
     private String                 username;
     private String                 password;
     private Set<GroupsViewModel>   groups;
}

and a part of my json result is like below:

[{
      "id" : 1,
      "title" : "adminGroup",
      "users" : [{
            "id" : 1,
            "username" : "admin",
            "password" : "...",
            "groups" : [1]
         }, {
            "id" : 31,
            "username" : "user78",
            "password" : "...",
            "groups" : [1]
         }, {
            "id" : 3,
            "username" : "ali",
            "password" : "...",
            "groups" : [{
                  "id" : 2,
                  "title" : "newsWriterGroup",
                  "users" : [{
                        "id" : 14,
                        "username" : "staff1",
                        "password" : "...",
                        "groups" : [{
                              "id" : 1005,
                              "title" : "FileManagerAccessGroup",
                              "users" : [{
                                    "id" : 25,
                                    "username" : "test1",
                                    "password" : "...",
                                    "groups" : [1005, {
                                          "id" : 1006,
                                          "title" : "noAccessGroup",
                                          "users" : [25, {
                                                "id" : 26,
                                                "username" : "test5",
                                                "password" : "...",
                                                "groups" : [1006]
                                             }
                                          ]
                                       }
                                    ]
                                 }, 14]
                           }, 2]
                     }, ...

As shown in above, if object is repetitive in json result, Jackson put only it's identifier of it. Now I want to know Is there any javascript/jquery library to represent json-result of @JsonIdentityInfo? for example, when javascript/jquery library arrive at 1005 identifier , it automaticaly load group object with id = 1005.

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
  • JSON can be parsed to native JavaScript arrays and objects using `JSON.parse`. There's no need to import a library. – 4castle Aug 29 '16 at 04:13

1 Answers1

0

It's very unlikely that such a targeted library exists. You could easily write a function though that deconstructs the data back into an array of users and groups by using typeof checking, and then using recursion when a new object is encountered in the groups or users attributes.

Just be careful when printing out the results that you don't create a circular reference. See this question for help on that.

function Group(group) {
  this.id = group.id;
  this.title = group.title;
  this.users = [];
  Group.cache[this.id] = this;
  group.users.forEach(this.addUser, this);
}
Group.cache = {};
Group.prototype.addUser = function(user) {
  this.users.push(
    typeof user === 'number'
      ? User.cache[user]
      : new User(user)
  );
};

function User(user) {
  this.id = user.id;
  this.username = user.username;
  this.password = user.password;
  this.groups = [];
  User.cache[this.id] = this;
  user.groups.forEach(this.addGroup, this);
}
User.cache = {};
User.prototype.addGroup = function(group) {
  this.groups.push(
    typeof group === 'number'
      ? Group.cache[group]
      : new Group(group)
  );
};

// begins the recursion
JSON.parse(
    '[{"id":1,"title":"adminGroup","users":[{"id":1,"username":"admin","password":"...","groups":[1]},{"id":31,"username":"user78","password":"...","groups":[1]},{"id":3,"username":"ali","password":"...","groups":[{"id":2,"title":"newsWriterGroup","users":[{"id":14,"username":"staff1","password":"...","groups":[{"id":1005,"title":"FileManagerAccessGroup","users":[{"id":25,"username":"test1","password":"...","groups":[1005]},14]},2]},3]},1]}]}]'
).forEach(function(group) { new Group(group) });

function stopCircularWithId(key, value) {
  return key === 'users' || key === 'groups'
    ? value.map(function(u) { return u.id })
    : value;
}
console.log('groups:', JSON.stringify(Group.cache, stopCircularWithId, 4));
console.log('users:', JSON.stringify(User.cache, stopCircularWithId, 4));

JSFiddle

Community
  • 1
  • 1
4castle
  • 32,613
  • 11
  • 69
  • 106