Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone JsonObject (shadow and deep) following JSR-353 (JSON-P) spec

I convert the JsonObject to String, then parsed them back.

public static JsonObject clone(JsonObject o) {
  if (o == null)
    return null;

  StringWriter buffer = new StringWriter();
  JsonWriter writer = Json.createWriter(buffer);
  writer.write(o);
  writer.close();

  return Json.createReader(new StringReader(buffer.toString())).readObject();
}

I look for more elegant method.

like image 236
sca. Avatar asked Feb 15 '14 04:02

sca.


1 Answers

There is no point cloning JsonObject. The javadoc states

JsonObject class represents an immutable JSON object value (an unordered collection of zero or more name/value pairs).

Emphasis mine. Just re-use the object. No one will be able to change it.

like image 87
Sotirios Delimanolis Avatar answered Oct 05 '22 02:10

Sotirios Delimanolis