title: Deserialization to a Java object using JSON?
body: <p>I must receive the following JSON:</p>
  
  <pre><code>{
     "method":"methodName1",
     "args":{
        "arg1Name":"arg1Value",
        "arg2Name":"arg2Value"
     }
  
  }
  </code></pre>
  
  <p>Here's another example:</p>
  
  <pre><code>{
     "method":"methodName2",
     "args":{
        "arg1Name":"arg1Value",
        "arg2Name":"arg2Value"
        "arg3Name":"arg3Value"
        "arg4Name":"arg4Value"
     }
  
  }
  </code></pre>
  
  <p>I need to parse those JSONs in order to call the specified method with the specified "args" as parameters.</p>
  
  <p>The easy way (for me) would be to use a <code>JsonParser</code> to get a <code>JsonElement</code>, then a <code>JsonObject</code> and then extract each value using <code>.get()</code>... something like this:</p>
  
  <pre><code>JsonParser jsonParser = new JsonParser();
  JsonElement jsonElement = jsonParser.parse(jsonString);
  if (jsonElement.isJsonObject()) {
       JsonObject jsonObject = jsonElement.getAsJsonObject();
       String method = jsonObject.get("method").getAsString();
       JsonObject jsonArgs = jsonObject.getAsJsonObject("args");
       String arg1 = jsonArgs.get("arg1Name").getAsString();
       String arg2 = jsonArgs.get("arg2Name").getAsString();
  
  }
  </code></pre>
  
  <p>And then I could call the specified method with the appropriate arguments (using a <code>switch</code>).</p>
  
  <p>I was just wondering if there would be an easier (or prettier) way to achieve this.</p>
  
  <p>I could use <code>.fromJson()</code> to retrieve an object instead, but I don't know how I should create my class to do so (there's something like an array of args and not all methods have the same number of args).</p>
  
  <p>I'm new to JSON (and to Java as well).</p>
  
  <p>I've managed to do this:</p>
  
  <pre><code>import lotus.domino.*;
  import com.google.gson.*;
  import java.io.*;
  import java.lang.reflect.Method;
  
  public class JavaAgent extends AgentBase {
      public void NotesMain() {
          try {
              String jsonReceived = "{\"method\":\"methodName\",\"args\":{\"arg1Name\":\"arg1Value\",\"arg2Name\":\"arg2Value\"}}";
              Gson gson = new Gson();
              MethodCall mc = gson.fromJson(jsonReceived, MethodCall.class);
  
              JavaAgent ja = new JavaAgent();
              Method method = getClass().getMethod(mc.getMethod(), MethodCall.class);
              String s = (String)method.invoke(ja, mc);
  
              PrintWriter pw = getAgentOutput();
              pw.println(s);
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      public String methodName(MethodCall mc) {
          String s = mc.getArgs().get("arg1Name") + " " + mc.getArgs().get("arg2Name");
          return s;
      }
  }
  
  import java.util.*;
  public class MethodCall {
      private String method;
      private Map&lt;String, String&gt; args;
  
      public String getMethod() {
          return method;
      }
  
      public Map&lt;String, String&gt; getArgs() {
          return args;
      }
  }
  </code></pre>
  
  <p>It seems to work... but since I'm new to Java, I'm not sure that's the proper way to do it. What do you think?</p>