Deserializing into a Null Object

  • Anonymous - 2012-11-14


    I am new to JSON so any help will be very much appreciated.

    I have a JSON string that I want to deserialize which corresponds to 3 classes:


    with the following class structure:

    The variable "publication" in D is of type List<Publication>
    The variable "result" in Publication is of type List<Result>

    Each of the class has getter and setter methods for all the attributes.

    I am deserializing as follows:

    JSONDeserializer<D> jsond = new JSONDeserializer<D>()

                D dtmp = jsond.deserialize(json);

    Finally, I try to print one of the fields of D:

    System.out.println("This is Version:" + dtmp.getVersion());

    What I get is

    This is Version:null

    I am not sure why the deserialization does not happen correctly. Am I missing some thing in the process.


  • Charlie Hubbard

    Charlie Hubbard - 2012-11-14

    It's hard to say from just the code you posted.  It mostly looks correct from how your JSONDeserializer is defined.  Can you post the JSON you want to deserialize?  Are other fields populated or null too?

    On a side note, you can shorten your code up by removing the specified ArrayList.  By default any List interface encountered during deserialization will be assumed to be an ArrayList by default so there isn't a need to specify those when doing deserialization.  That could reduce your code to this:

    D dtmp = new JSONDeserializer<D>().deserialize( json, D.class )

    And if you use generics in your instance variables for those two lists you don't have to specify Publication and Result either:

    D dtmp = new JSONDeserializer<D>().deserialize( json, D.class );

    Notice the target class is D, and I can just specify that in the deserialize as opposed to having a .use( null, D.class ).  The only times you have to specify the objects types is for polymorphism that isn't a known Collection (known collections are List, Map, Set, etc).  Or when you don't use generic types for container type classes like (List,Map,Set,etc).



Log in to post a comment.