Loading ...
Sorry, an error occurred while loading the content.

Serialize JSON to Java

Expand Messages
  • haritha.juturu
    I have been looking for some slightly complicated samples for json to java objects conversion using JSON-LIB but in vain. So i thought i will post my sample
    Message 1 of 1 , Dec 5, 2007
    • 0 Attachment
      I have been looking for some slightly complicated samples for json to
      java objects conversion using JSON-LIB but in vain. So i thought i
      will post my sample here to know if i am doing the right thing or
      theres a simpler way to serialize json to java. I have given both the
      json string and java code snippet. Please have a look and send in your
      comments.

      This is my JSON string

      {
      "name":"Nameless One",
      "pages":[
      {"name":"Page1",
      "canvas":"{"objectType":"EditorCanvas",
      "id":"editorCanvas",
      "name":"",
      "sizeWidth":940,
      "cssClass":"",
      "style":"width:940px;",
      "children":[
      {"objectType":"TextObject",
      "id":"Text_1196907790312",
      "name":"",
      "cssClass":"",
      "style":"left:40px;top:60px;width:620px;height:100px;",
      "text":""[Winning the Nobel Peace Prize] brings the floodlight for
      attention from the whole world right to one person, so the whole
      world's watching you, what you're doing, what you're saying. So in a
      way it's exciting, very exciting thing, but at the same time it's an
      opportunity, opportunity to bring out the issues that I have been
      raising for years. Sometimes felt that nobody was paying any
      attention, like I've been screaming and nobody's hearing me. Now
      suddenly this prestigious prize comes, and you get a feeling that you
      can whisper, the whole world listens. This is your time to say what
      you wanted to say." ¬óMohammad Yunus"},

      {"objectType":"ImageObject",
      "id":"Image_1196907800312",
      "name":"",
      "position":({left:40, top:220}),
      "size":({width:300, height:300}),
      "cssClass":"",
      "style":"left:40px;top:220px;width:300px;height:300px;",
      }
      ]
      }"
      }
      ]
      }

      JSON json = JSONSerializer.toJSON(jsonStr);
      //where jsonStris the one above
      MorpherRegistry morpherRegistry = new MorpherRegistry();
      morpherRegistry.registerMorpher( new BeanMorpher( BaseObject.class,
      morpherRegistry));
      morpherRegistry.registerMorpher( new BeanMorpher(
      SplashNoteObject.class, morpherRegistry));
      morpherRegistry.registerMorpher( new BeanMorpher( PageObject.class,
      morpherRegistry));
      morpherRegistry.registerMorpher( new BeanMorpher(
      EditorCanvas.class, morpherRegistry));

      SplashNoteObject snObject = (SplashNoteObject) JSONObject.toBean(
      (JSONObject) json, SplashNoteObject.class );
      PageObject page =
      (PageObject)morpherRegistry.morph(PageObject.class,snObject.getPages().get(0));

      EditorCanvas bean = (EditorCanvas)page.getCanvas();
      //i havent included the code for any further levels like TextObject
      and ImageObject

      Is this the only was i can serialize json to java objects. Do i need
      to morph the objects at every level to the bean i need.

      Cheers
      Haritha
    Your message has been successfully submitted and would be delivered to recipients shortly.