2
\$\begingroup\$

Today I was trying GSON after a while and went to the YQL console to run this query:

select * from yahoo.finance.xchange where pair in ("EURUSD","GBPUSD","INRUSD","JPYUSD")

and get back this JSON:

{
 "query": {
  "count": 4,
  "created": "2014-12-11T18:41:23Z",
  "lang": "en-US",
  "results": {
   "rate": [
    {
     "id": "EURUSD",
     "Name": "EUR to USD",
     "Rate": "1.238",
     "Date": "12/11/2014",
     "Time": "1:41pm",
     "Ask": "1.2382",
     "Bid": "1.2378"
    },
    {
     "id": "GBPUSD",
     "Name": "GBP to USD",
     "Rate": "1.5705",
     "Date": "12/11/2014",
     "Time": "1:41pm",
     "Ask": "1.5708",
     "Bid": "1.5703"
    },
    {
     "id": "INRUSD",
     "Name": "INR to USD",
     "Rate": "0.016",
     "Date": "12/11/2014",
     "Time": "1:40pm",
     "Ask": "0.016",
     "Bid": "0.016"
    },
    {
     "id": "JPYUSD",
     "Name": "JPY to USD",
     "Rate": "0.0084",
     "Date": "12/11/2014",
     "Time": "1:41pm",
     "Ask": "0.0084",
     "Bid": "0.0084"
    }
   ]
  }
 }
}

To parse it I wrote this code which works fine:

Wrapper.java

public class Wrapper {
    private Query query;

    public Query getQuery() { return query; }
}

Query.java

public class Query {
    private int count;
    private Date created;
    private String lang;
    private Result results;

    public int getCount() { return count; }
    public Date getCreated() { return created; }
    public String getLang() { return lang; }
    public Result getResults() { return results; }

}

Result.java

public class Result {
    private Rate[] rate;

    public Rate[] getRate() { return rate; }
}

Rate.java

public class Rate {
    private String id;
    private String Name;
    private Double Rate;
    private String Date;
    private String Time;
    private Double Ask;
    private Double Bid;

    public String getId() { return id; }
    public String getName() { return Name; }
    public Double getRate() { return Rate; }
    public String getDate() { return Date; }
    public String getTime() { return Time; }
    public Double getAsk() { return Ask; }
    public Double getBid() { return Bid; }
}

I created separate classes and then in a main class with a main method created the Gson object and did this:

Gson gson = new GsonBuilder().create();
Wrapper wrapper = gson.fromJson(jsonString, Wrapper.class);

But what differences would there be if I had the above classes as nested classes in the Wrapper.java class? Or is creating them separately and then putting them in a package more preferable?

What are the differences in terms of speed, efficiency, design, or anything else anyone could advise on?

\$\endgroup\$
3
\$\begingroup\$

But what differences would there be if I had the above classes as nested classes in the Wrapper.java class? Or is creating them separately and then putting them in a package more preferable?

It depends on your information hiding principles. In general, it's good to hide as much information as possible. The fewer classes you expose, the fewer classes you have to support, and therefore the more freedom you have to make changes later.

So, if these classes don't need be visible by users of your library, then they can be nested inside Wrapper. Wrapper too, looks like something that shouldn't be exposed.

On the other hand: (from Effective Java by Joshua Bloch)

A nested class should exist only to serve its enclosing class. If a nested class would be useful in some other context, then it should be a top-level class.


What are the differences in terms of speed, efficiency, design, or anything else anyone could advise on?

Speed, nothing. Efficiency, nothing. The difference is in API design. Everything that's public is a liability for you. Once other developers start using it, you'll have to pay attention to the changes you make, in case you might break compatibility.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.