Quantico
Quantico

Reputation: 2436

GSON with several known classes

I have the following json

{ "file": {"file": "foo.c", "owner": "user123"}
  "methods": [{"name": "proc1", "value":"val"}, {"name":"proc2","value":"val2"}]
  etc...
}

I know that I can do something like

class file{
      public String file
      public String owner
    }
class methods{
      public String name
      public String value
    }

and I can either call

File file= gson.fromJson(jsonInString, File.class);  
methods[] array = gson.fromJson(jsonInString, methods[].class);

but what do I do if I need to handle a complex json that contains many objects all togther I cannot specify gson.fromJson(jsonInString, ListOfClasses)

Upvotes: 3

Views: 228

Answers (1)

Mrunal Pagnis
Mrunal Pagnis

Reputation: 809

I normally follow this approach to get any complex classes converted from json to object. This approach works for almost everything like list, map etc. The idea is simple create holders for the complex classes and then create the classes. Give as much depth as much required. The trick is to match name in Json and your holders (and subclasses).

File Config:

class FileConfig{
  public String file;
  public String owner;

  //define toString, getters and setters 
}

Method Class:

class Method{
  public String name;
  public String value;

  //define toString, getters and setters   
}

Method Config:

class MethodConfig{
  List<Method> methods = null;

  //define toString, getters and setters 
}

Holding the Config:

public class HolderConfig {

  private FileConfig file = null;
  private MethodConfig methods = null;

  public FileConfig getFile() {
    return file;
  }

  public void setFile(FileConfig file) {
    this.file = file;
  }
  public MethodConfig getMethods() {
    return file;
  }

  public void setMethods(MethodConfig methods) {
    this.methods = methods;
  } 
}

Building the config:

public class HolderConfigBuilder {

public static HolderConfig build(JsonObject holderConfigJson) {

    HolderConfig configHolderInstance = null;         

    Gson gsonInstance = null;

    gsonInstance = new GsonBuilder().create();

    configHolderInstance = gsonInstance.fromJson(holderConfigJson,HolderConfig.class);

    return configHolderInstance;
  }
}

Demo class:

public class App 
{
      public static void main( String[] args )
      {

           HolderConfig configHolderInstance = null;
           FileConfig file = null;
           configHolderInstance = HolderConfigBuilder.build(<Input Json>);
           file = configHolderInstance.getFile();
           System.out.println("The fileConfig is : "+file.toString());
      }
 }

Input Json:

{ "file": {"file": "foo.c", "owner": "user123"}
  "methods": [
               {"name": "proc1", "value":"val"},
               {"name":"proc2","value":"val2"}
             ]
}

Note: Write the code to get Input JSON in your test code.

In this way whenever you add more elements to your JSON you have to create a separate class for that element and just add the element name same as in your json into the HolderConfig. You need not change rest of the code.

Hope it helps.

Upvotes: 1

Related Questions