title: GSON Object (RESTful api)
body: <p>I have a client (java) that send some json data to my server (c++). After this my server respond with some informations depending the operation made my java client. For now its works.</p>
  
  <pre><code>Example body request:
  {
     "userEmail": "email@email.com",
     "userPassword": "12345678"
  }
  </code></pre>
  
  <p>And the server, receive the email and password and do the operations and send back a response.</p>
  
  <p>But now i need to change my java client to send the request like this:</p>
  
  <pre><code>{
    "userInformation":{
          "userEmail": "email@email.com",
          "userPassword": "12345678"
    }
  
  }
  </code></pre>
  
  <p>This request can be use to do the login. This is a very complex architecture so i cannot copy all code, but in login class i used the gson (Note i only work in c++ server, i dont work in client and its impossible to contact the guy that made the java client to ask him about this doubts)</p>
  
  <p>Java client - login class</p>
  
  <pre><code>public final String userEmail;
  public final String userPassword;
  
  
  public LoginRequestArgs( String userEmail, String userPassword)
  {
      this.userEmail = userEmail;
      this.userPassword = userPassword;       
  }
  
  public static LoginRequestArgs fromStringJson(String data)
  {
      try
      {
          Gson gson = new Gson();
          return gson.fromJson(data, LoginRequestArgs.class);
      }
      catch(Exception e)
      {
          return null;
      }
  }
  
  public static LoginRequestArgs fromBytesJson(byte[] data)
  {
      if (data == null) return null;
      try 
      {
          String str = new String(data, "utf-8");
          return fromStringJson(str); 
      }
      catch (Exception e) 
      {
          return null;
      }
  }
  
  
  @Override
  public String toJsonString()
  {
      try
      {
          Gson gson = new Gson();
          return gson.toJson(this);
      }
      catch(Exception e)
      {
          return null;
      }
  }
  
  @Override
  public byte[] toJsonBytes()
  {
      try 
      {
          return this.toJsonString().getBytes("utf-8");
      }
      catch (Exception e)
      {
          return null;
      }
  }
  </code></pre>
  
  <p>I have other class that connect to my server and send the request. This class call the login function (the request is a SerializableJSON variable) and  place this request to my server <code>request = LoginRequestArgs.fromStringJson(args.httpEntity);</code></p>
  
  <p>(for tests i use one rest client chrome extension) </p>
  
  <p>I know that can be some hard to understand but its not easy to explain all things. I try to explain the essencial. </p>
  
  <p>real problem: i cannot adapt the java client to send the email and password that are inside "userInformation". Someone can help me please? Thanks</p>
  
  <p>EDIT (OTHER BODY REQUEST EXAMPLE):</p>
  
  <pre><code>{
      "userInformation": {
          "otherSection": {
              "key1": "value1",
              "key2": "value2"
          }
      }
  }
  </code></pre>
  
  <p>EDIT 2 (authentication method):</p>
  
  <pre><code>{
      "authenticationMethod": {
          "sessionId": {
              "email": "email@email.com",
              "password": "pass123"
          }
      },
      "userInformation": {
          "userId": "user",
          "userPassword": "user123"
      }
  }
  </code></pre>
  
  <p>EDIT 3 (TOKEN):</p>
  
  <pre><code>{
      "authenticationMethod": {
          "token": {
              "token": "HDI393UDDNDMAY4758SAD"
          }
      },
      "userInformation": {
          "userId": "user",
          "userPassword": "user123"
      }
  }
  </code></pre>
  
  <p>EDIT 4:</p>
  
  <pre><code>{
    "sessionId":{
      "email":"email@email.com",
      "password":"dasdas"
  
    },
    "userInformation":{
          "userId": "userId1",
          "userPassword": "12345678"
    }
  
  }
  </code></pre>
  
  <p>I already send this json to my c++ server and its work and my decode session id working too, its not problem for me work on c++. But i need to send sessionId (or token) but inside "AuthenticationMethod", its only the thing i need to implement now. Note the "userInformation" its like and example it can be for example "bookInformation", "carInformation" , depending the request type, i send different data with different keys/values inside.. but the authentication method (session id or token) it is always mandatory to use in all request. </p>
  
  <p>To work like i show to you i implement this:</p>
  
  <pre><code>public class SessionId {
  
      public String email;
      public String password;
  
      public SessionId(String email, String password)
      {
          this.email = email;
          this.password = password;
      }
  
  }
  </code></pre>
  
  <p>And inside the construct of my class (can be for example one class like LoginRequestArgs, the login cass) i call the super:</p>
  
  <pre><code>public UserInfo userInformation;
  public SessionId sessionId;
  
  public LoginRequestArgs(String email, String password,String userEmail, String userPassword)
      {   
          super(email,password);
          userInformation = new UserInfo(userEmail, userPassword);
      }
  
  
  static class UserInfo {
          public String userId;
          public String userPassword;
  
          public UserInfo(String userEmail, String userPassword) {
              this.userId = userEmail;
              this.userPassword = userPassword;
          }
      }
  </code></pre>
  
  <p>So for now, i only need to add "authenticationMethod" before session id or token (i believe the way to do this its the same for both)</p>
  
  <p>**** EDIT 5 ********</p>
  
  <p>login.java</p>
  
  <pre><code>public class LoginRequestArgs implements SerializableJSON  {
  
      public UserInfo userInformation;
      public AuthenticationMethod authenticationMethod;
  
      public LoginRequestArgs(String email, String password,String userId, String userPassword)
      {   
          AuthenticationMethod auth = new SessionId(email, password);
          setAuth(auth);
          userInformation = new UserInfo(userId, userPassword);
      }
  
      public void setAuth(AuthenticationMethod authenticationMethod){
              this.authenticationMethod = authenticationMethod;
      }
  
      static class UserInfo {
          public String userId;
          public String userPassword;
  
          public UserInfo(String userEmail, String userPassword) {
              this.userId = userEmail;
              this.userPassword = userPassword;
          }
      }
  </code></pre>
  
  <p>SessionId.java</p>
  
  <pre><code>public class SessionId extends AuthenticationMethod {
  
      Session sessionId;
  
       public SessionId(String email, String password)
       {
           this.sessionId = new Session(email,password);
  
       }
  
       static class Session{
           String email;
           String password;
  
           public Session(String email, String password)
           {
               this.email = email;
               this.password = password;
           }
  
       }
  }
  </code></pre>
  
  <p>AuthenticationMethod.java</p>
  
  <pre><code>public class AuthenticationMethod {
  
  
  }
  </code></pre>