在包含JsonIdentityInfo的JavaScript中反序列化Jackson对象

时间:2014-06-13 14:48:57

标签: javascript json spring angularjs jsonidentityinfo

你好(对不起我的英文)

我正在使用angularjs前端网站消费使用SPRING MVC生成json的Web服务。 spring mvc使用JsonIdentityInfo选项进行seralization,因此每个对象在json中只被写入一次并且每次使用一个引用,例如她有2个“计算机”使用相同的对象“component”,所以spring将id设置为第一个组件(“@componentID”:2)和第二个组件juste id(2):

[
  {
    "@computerID": 1,
    "component": {
      "@componentID": 2,
      "processor": 2,
      "ram": "8g",
      "harddrive": "wd"
    }
  },
  {
    "@computerID": 3,
    "component": 2
  }
]

我想要的是什么:

[
  {
    "@computerID": 1,
    "owner" : "Mister B",
    "component": {
      "@componentID": 2,
      "processor": 2,
      "ram": "8g",
      "harddrive": "wd"
    }
  },
  {
    "@computerID": 3,
    "owner" : "Mister A",
    "component": {
      "@componentID": 2,
      "processor": 2,
      "ram": "8g",
      "harddrive": "wd"
    }
  }
]

我做了很多搜索代码的人,但我没有发现任何想法。

我无法编辑用于删除此行为的Web服务。我可以使用javascript或jquery(或其他librairie)编辑客户端的json,以用真实引用的对象替换引用吗? (数据实际上更复杂,更深,我在对象中有3级子对象)。

非常感谢。

2 个答案:

答案 0 :(得分:27)

我最近遇到了OP在此描述的确切场景。以下是我的解决方案。 使用JSOG(Javascript Object Graph)格式来解决这个问题。

服务器端 使用Jackson-Jsog插件https://github.com/jsog/jsog-jackson 并使用下面的注释注释每个类。

@JsonIdentityInfo(generator=JSOGGenerator.class)

而不是

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")

这将以JSOG格式生成。 (@id@ref

在客户端,使用jsog.js

使用以下调用

将JSOG结构转换为循环结构

cyclicGraph = JSOG.decode(jsogStructure);

答案 1 :(得分:2)

将所有数组成员拆分为新数组:具有完整component属性(不仅仅是数字)的数组和没有数组成员的数组。循环遍历剩余的原始成员,这些成员应该只具有数字component属性,然后从" good"中查找相应的@componentID。数组,并做一些复制和移动。

// initialize some vars
var final = [], temp = [], bad = [],
    c = {},
    computers = [
      {
        "@computerID": 1,
        "component": {
          "@componentID": 2,
          "processor": 2,
          "ram": "8g",
          "harddrive": "wd"
        }
      },
      {
        "@computerID": 3,
        "component": 2
      }
    ];

// split original array into 3: final, bad, & temp
while(computers.length > 0) {
    c = computers.pop();
    if (c.hasOwnProperty("component")) {
        if (typeof c.component === "number") {
            temp.push(c);
        } else {
            final.push(c);
        }
    } else {
        bad.push(c);
    }
}

// loop through temp & look up @componentID within final
while (temp.length > 0) {
    c = temp.pop();
    // should @componentID be 1-of-a-kind?
    var found = getObjects(final, "@componentID", c.component);
    if (found.length) {
        c.component = found[0];
        final.push(c);
    } else {
        bad.push(c);
    }
}


// SOURCE: http://stackoverflow.com/a/4992429/1072176
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}

// should result in just one or two populated arrays: final and/or bad
alert(JSON.stringify(final));

你注意到我实际上制作了三个数组,但只有两个最终填充:final有你的好新对象,而另一个(bad)是一个全能的没有组件属性的对象,或者组件编号无法找到相应的@componentID。