com.fitbur.jackson.databind.ser.impl.WritableObjectId Maven / Gradle / Ivy
package com.fitbur.jackson.databind.ser.impl;
import java.io.IOException;
import com.fitbur.jackson.annotation.ObjectIdGenerator;
import com.fitbur.jackson.core.JsonGenerator;
import com.fitbur.jackson.core.SerializableString;
import com.fitbur.jackson.databind.SerializerProvider;
/**
* Simple value container used to keep track of Object Ids during
* serialization.
*/
public final class WritableObjectId
{
public final ObjectIdGenerator> generator;
public Object id;
protected boolean idWritten = false;
public WritableObjectId(ObjectIdGenerator> generator) {
this.generator = generator;
}
public boolean writeAsId(JsonGenerator gen, SerializerProvider provider, ObjectIdWriter w) throws IOException
{
if (id != null && (idWritten || w.alwaysAsId)) {
// 03-Aug-2013, tatu: Prefer Native Object Ids if available
if (gen.canWriteObjectId()) {
gen.writeObjectRef(String.valueOf(id));
} else {
w.serializer.serialize(id, gen, provider);
}
return true;
}
return false;
}
public Object generateId(Object forPojo) {
return (id = generator.generateId(forPojo));
}
/**
* Method called to output Object Id as specified.
*/
public void writeAsField(JsonGenerator gen, SerializerProvider provider,
ObjectIdWriter w) throws IOException
{
idWritten = true;
// 03-Aug-2013, tatu: Prefer Native Object Ids if available
if (gen.canWriteObjectId()) {
// Need to assume String(ified) ids, for now... could add 'long' variant?
gen.writeObjectId(String.valueOf(id));
return;
}
SerializableString name = w.propertyName;
if (name != null) {
gen.writeFieldName(name);
w.serializer.serialize(id, gen, provider);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy