Search in sources :

Example 1 with SerializeSetVersion

use of org.talend.daikon.serialize.migration.SerializeSetVersion 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);
}
Also used : HashMap(java.util.HashMap) JsonClassWriterEx(com.cedarsoftware.util.io.JsonWriter.JsonClassWriterEx) SerializeSetVersion(org.talend.daikon.serialize.migration.SerializeSetVersion) JsonClassWriterEx(com.cedarsoftware.util.io.JsonWriter.JsonClassWriterEx) JsonWriter(com.cedarsoftware.util.io.JsonWriter) JsonObject(com.cedarsoftware.util.io.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) JsonWriter(com.cedarsoftware.util.io.JsonWriter)

Aggregations

JsonObject (com.cedarsoftware.util.io.JsonObject)1 JsonWriter (com.cedarsoftware.util.io.JsonWriter)1 JsonClassWriterEx (com.cedarsoftware.util.io.JsonWriter.JsonClassWriterEx)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SerializeSetVersion (org.talend.daikon.serialize.migration.SerializeSetVersion)1