Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

json - deserialize Jackson object in JavaScript containing JsonIdentityInfo

hello (sorry for my english)

I'm working on angularjs front end website consuming web service producing json with SPRING MVC. The spring mvc use JsonIdentityInfo option for seralization so each object are writed only one time in the json and each other time a reference is used, example her there is 2 "computer" using the same object "component", so spring put an id to the first component ("@componentID": 2) and for the second component juste the id ( 2 ) :

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

what i want :

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

I make many search for a code who do this but i didn't find anythink.

I can't edit the web service for removing this behavor. Can i edit the json on client side with javascript or jquery (or another librairie) to replace references with the real referenced object ? ( the data are in fact more complex and deeper, i have 3 level of sub object in object).

thanks a lot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I recently came across an exact scenario that OP has described here. Below was my solution. Use JSOG (Javascript Object Graph) format to solve this.

Server Side Use Jackson-Jsog plugin https://github.com/jsog/jsog-jackson and annotate each class using below annotation.

@JsonIdentityInfo(generator=JSOGGenerator.class)

instead of the

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

this would generate in JSOG format. (@id and @ref)

On the Client Side, use the jsog.js

convert the JSOG structure to the cyclic one using the below call

cyclicGraph = JSOG.decode(jsogStructure);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...