2016-08-29 53 views
0

我有2個雙向模型,我使用JsonIdentityInfo預防json結果中的無限遞歸。這是我的ViewModels:是否有任何JavaScript庫來表示@JsonIdentityInfo的json結果?

@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; 
} 

和我的JSON結果的一部分是象下面這樣:

[{ 
     "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] 
        }, ... 

如上文所示,如果對象是在JSON結果重複的,傑克遜只把它是它標識。現在我想知道是否有任何JavaScript/jQuery庫來表示@JsonIdentityInfo的json結果?例如,當使用Javascript/jQuery庫在1005標識到達,它自動加載組對象使用id = 1005

+0

JSON可以被解析以使用'JSON.parse'本地JavaScript數組和對象。沒有必要導入一個庫。 – 4castle

回答

0

這是非常不可能的,這樣的目標庫中存在。您可以輕鬆編寫函數,但通過使用typeof檢查將數據解構成用戶和組的數組,然後在groupsusers屬性中遇到新對象時使用遞歸。

打印出不要創建循環引用的結果時要小心。請參閱this question尋求幫助。

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

相關問題