use of com.cedarsoftware.util.io.JsonWriter in project daikon by Talend.
the class SerializerDeserializer method toSerialized.
/**
* Returns a serialized version of the specified object.
*
* @return the serialized {@code String}, use {@link #fromSerialized(String, Class, PostDeserializeSetup, boolean)}
* to materialize the object.
*/
public static String toSerialized(Object object, boolean persistent, Map<String, Object> jsonIoOptions) {
JsonWriter.JsonClassWriterEx writer = new JsonWriter.JsonClassWriterEx() {
@Override
public void write(Object o, boolean showType, Writer output, Map<String, Object> args) throws IOException {
JsonWriter writer = JsonWriter.JsonClassWriterEx.Support.getWriter(args);
int version = ((SerializeSetVersion) o).getVersionNumber();
if (version > 0) {
// We don't have to add a ',' to the json object when our object is referenced (have an @id)
// The json-io lib do add one in this case just before writing the object o below
boolean isReferenced = writer.getObjectsReferenced().containsKey(o);
output.write("\"" + VERSION_FIELD + "\":" + version + (isReferenced ? "" : ","));
}
writer.writeObject(o, false, true);
}
};
Map<Class, JsonWriter.JsonClassWriterEx> writerMap = new HashMap<>();
writerMap.put(SerializeSetVersion.class, writer);
final Map<String, Object> args = new HashMap<>();
args.put(JsonWriter.CUSTOM_WRITER_MAP, writerMap);
if (jsonIoOptions != null) {
Map<String, Object> additionalOption = new HashMap<>(jsonIoOptions);
// check for CUSTOM_WRITER_MAP
if (additionalOption.containsKey(JsonWriter.CUSTOM_WRITER_MAP)) {
Map<Class, JsonWriter.JsonClassWriterEx> additionalWriterMap = (Map<Class, JsonClassWriterEx>) additionalOption.get(JsonWriter.CUSTOM_WRITER_MAP);
// remove the object from the additionalOption
additionalOption.remove(additionalWriterMap);
// add the map elements to our own map
writerMap.putAll(additionalWriterMap);
}
args.putAll(jsonIoOptions);
}
return JsonWriter.objectToJson(object, args);
}
Aggregations