public static String toJson(String name, Object object) {
    final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.STATIC)
        // Tells Gson how to serialize fields with type of Class.
        .registerTypeHierarchyAdapter(Class.class, new TypeAdapter<Class>() {
          @Override
          public void write(JsonWriter out, Class value) throws IOException {
            if (value == null) {
              out.nullValue();
            } else {
              out.value(value.getName());
            }
          }

          @Override
          public Class read(JsonReader in) {
            throw new SamzaException("Deserialization from json is not supported.");
          }
        }).create();
    try {
      return gson.toJson(object);
    } catch (Exception e) {
      throw new SamzaException(String.format("Failed to serialize %s to json", name), e);
    }
  }
Helper method to serialize Java objects as json strings
@param name name of object used for logging
@param object object to be serialized
@return Json representation of the object
samza-api/src/main/java/org/apache/samza/table/utils/SerdeUtils.java