java - Is there any javascript library to represent json-result of @JsonIdentityInfo? -
i have 2 bi-directional models , use jsonidentityinfo
prevention infinite recursion in json result. here 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 part of json result 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 repetitive in json result, jackson put it's identifier of it. want know there javascript/jquery library represent json-result of @jsonidentityinfo? example, when javascript/jquery library arrive @ 1005 identifier , automaticaly load group object id = 1005.
it's unlikely such targeted library exists. write function though deconstructs data array of users , groups using typeof
checking, , using recursion when new object encountered in groups
or users
attributes.
just careful when printing out results don't create circular reference. see this question 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 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));
Comments
Post a Comment