title: Using Gson convert Java object into JsonObject
body: <p>I have a Java object, and I want to output it as a JSON string. BUT I want to avoid printing out a property in the Java object. I know that I could do this using GsonBuilder's excludeFieldsWithoutExposeAnnotation() method. However, I thought I'd try the alternate approach of removing the property from the JsonObject before printing it out. The following code works:</p>
  
  <pre><code>Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd' 'HH:mm:ss").create();
  String javaObjectString = gson.toJson(javaObject);
  //javaObjectString currently include "property":"value"
  JsonElement jsonElement = gson.toJsonTree(javaObjectString, javaObject.getClass());
  JsonObject jsonObject = (JsonObject) jsonElement;
  jsonObject.remove("property");
  javaObjectString = gson.toJson(jsonObject);
  //javaObjectString currently no longer includes "property":"value"
  </code></pre>
  
  <p>However, it feels a but hacky because I have to output the Java object to a String, and then create a JsonElement from the String, and then cast the JsonElement to a JsonObject.</p>
  
  <p>Is there a more direct way to go from a Java object to a JsonObject?</p>