/** * Convert JSON To bytes * @param instance instance to convert * @return a byte array * @throws IOException */ public byte[] toBytes(T instance) throws IOException { String json = toJson(instance); return json.getBytes(UTF_8); }
/** * Convert an instance to a string form for output. This is a robust * operation which will convert any JSON-generating exceptions into * error text. * @param instance non-null instance * @return a JSON string */ public String toString(T instance) { Preconditions.checkArgument(instance != null, "Null instance argument"); try { return toJson(instance); } catch (IOException e) { return "Failed to convert to a string: " + e; } } }
/** * clone by converting to JSON and back again. * This is much less efficient than any Java clone process. * @param instance instance to duplicate * @return a new instance * @throws IOException problems. */ public T fromInstance(T instance) throws IOException { return fromJson(toJson(instance)); }