Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Problems deserializing JSON
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gustav  
View profile  
 More options Sep 12 2012, 12:54 pm
From: Gustav <development...@gmail.com>
Date: Wed, 12 Sep 2012 09:54:54 -0700 (PDT)
Local: Wed, Sep 12 2012 12:54 pm
Subject: Problems deserializing JSON

Hello Laurent,

first of all thanks for developing this great library... it really makes
life easier.

I have made everything work great and I have been able to receive a JSON
response from
the REST service I am hitting... the problem is that I get a nullpointer
exception because
the Json does not get deserialized OR maybe because it does not get mapped
to the
Model.

Here is some code:

I am trying to deserialize this:

{
    "contracts": [
        {
            "id": 0,
            "name": "whatever",
            "epochs": [
                {
                    "start": "2011-01-01T00:00:00Z",
                    "end": "2011-12-31T00:00:00Z",
                    "tariffs": [
                        {
                            "id": 0,
                            "name": "t1",
                            "cost": 20.0
                        },
                        {
                            "id": 1,
                            "name": "t2",
                            "cost": 15.0
                        }
                    ]
                }
            ]
        }
    ]}

As you see there are many arrays so what I did was this:
I created 3 different classes...
Contracts
Epochs
Tariffs

public class Contracts {

    @JsonProperty("id")
    public Long id;    
    @JsonProperty("name")
    public String name;    
    @JsonProperty("epochs")
    public List<Epochs> epochs;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Epochs> getEpochs() {
        return epochs;
    }
    public void setEpochs(List<Epochs> epochs) {
        this.epochs = epochs;
    }

*************
public class Epochs {

    @JsonProperty("start")
    public String startDate;
    @JsonProperty("end")
    public String endDate;    
    @JsonProperty("tariffs")
    public List<Tariffs> tariffs;

    public String getStartDate() {
        return startDate;
    }
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }
    public String getEndDate() {
        return endDate;
    }
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
    public List<Tariffs> getTariffs() {
        return tariffs;
    }
    public void setTariffs(List<Tariffs> tariffs) {
        this.tariffs = tariffs;
    }

*************

public class Tariffs {

    @JsonProperty("id")
    public Long id;
    @JsonProperty("name")
    public String name;
    @JsonProperty("cost")
    public Float cost;

    public Tariffs()
    {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getCost() {
        return cost;
    }

    public void setCost(Float cost) {
        this.cost = cost;
    }

}

and then I have this on my interface

@EndPoint("http://server.com")
@Path("v2/{the.utility}")
@HeaderParam(value = "Accept-Encoding", defaultValue = "gzip,deflate")
@Consumes("application/json")
@SocketTimeout(2000)
@ConnectionTimeout(2000)
@ResponseHandler(CustomResponse.class)
public interface TheService{

    @Path("contract")
    public Contracts[] getContract();

************

I call this like this:

        CRest crest = CRest.basicAuth(username, password).
                            placeholder("the.utility","util").
                            build();

        TheService api2 = crest.build(TheService.class);

        Contracts[] contract = api2.getContract();

After doing this ... no matter if I use [] or List...
contract = null

Can you please help me..
Thank you very much
Gustav


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Laurent Gilles  
View profile  
 More options Sep 13 2012, 4:20 am
From: Laurent Gilles <laurent.gil...@codegist.org>
Date: Thu, 13 Sep 2012 01:20:58 -0700 (PDT)
Local: Thurs, Sep 13 2012 4:20 am
Subject: Re: Problems deserializing JSON

Hi Gustav,

Thanks for the support!
Re your issue, I think the problem comes from your Jackson mappings.
You see, you need to tell jackson how to extract that array of contracts
out of:

{
    "contracts":[/*...*/]

}

So you need an additional class:

public class Contracts {

    @JsonProperty("contracts")
    public List<Contract> contracts;

     /* ...getter and setters... */

}

And change your interface to be as

/* ...Annotations... */
public interface TheService{

    @Path("contract")
    public Contracts getContract();

}

That way Jacksons will be able to deserialize your JSON.

Now if you want the service to return a list of contracts, you have
different options:

1. Change your web service response to be:

     [
        {
            "id": 0,
            "name": "whatever",
            "epochs": [
                {
                    "start": "2011-01-01T00:00:00Z",
                    "end": "2011-12-31T00:00:00Z",
                    "tariffs": [
                        {
                            "id": 0,
                            "name": "t1",
                            "cost": 20.0
                        },
                        {
                            "id": 1,
                            "name": "t2",
                            "cost": 15.0
                        }
                    ]
                }
            ]
        }
    ]
ie: without the main "contracts:[]" wrapper object.
2. Without changing the response format, write a custom response handler that will deserialize it into that object I've mentioned above and extract the list out of it and return it.
   Please see the Google sample (http://crest.codegist.org/sample-google.html). That sample interacts with a google webservice using a custom response handler that does what options 2. is about. Google response format is done in such a way it makes it really easy and enable uses of Generics.
   With your current response format ({contract:[]}), you cannot use generics as there is no simple way I know of with Jackson do dynamically change the JSON property to read to inject into an object.

Hope that helped!
Cheers
Laurent


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gustav  
View profile  
 More options Sep 13 2012, 11:01 am
From: Gustav <development...@gmail.com>
Date: Thu, 13 Sep 2012 08:01:16 -0700 (PDT)
Local: Thurs, Sep 13 2012 11:01 am
Subject: Re: Problems deserializing JSON

Hi Laurent,

that helped very much... that was exactly the problem!
it obviously made sense I just oversaw it!

Thanks a lot for your help!
G


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »