net.lecousin.framework.serialization.rules.SerializationRule Maven / Gradle / Ivy
The newest version!
package net.lecousin.framework.serialization.rules;
import java.util.List;
import net.lecousin.framework.serialization.SerializationClass;
import net.lecousin.framework.serialization.SerializationContext;
import net.lecousin.framework.serialization.SerializationException;
import net.lecousin.framework.serialization.TypeDefinition;
/** Interface for a serialization rule. */
@SuppressWarnings("unused")
public interface SerializationRule {
/** Apply the rule to the given type, knowing the given context.
* If true is returned no other rule will be applied on the type.
*/
boolean apply(SerializationClass type, SerializationContext context, List rules, boolean serializing)
throws SerializationException;
/** Check if this rule is equivalent to the given rule. */
boolean isEquivalent(SerializationRule rule);
/** Before to serialize a value, this method is called in order to give the opportunity to convert to another value. */
default Object convertSerializationValue(Object value, TypeDefinition type, SerializationContext context) throws SerializationException {
return value;
}
/** Can be used to change a type during deserialization, in a similar way as the method
* convertSerializationValue during serialization. This is also used for writing
* serialization specification.
*/
default TypeDefinition getDeserializationType(TypeDefinition type, SerializationContext context) {
return type;
}
/** Can be used to change the deserialization, in a similar way as the method
* convertSerializationValue during serialization.
*/
default Object getDeserializationValue(Object value, TypeDefinition type, SerializationContext context) throws SerializationException {
return value;
}
/** During deserialization, when a type needs to be instantiated, this method is called to know
* if this rule is providing a custom way to instantiate the given type.
*/
default boolean canInstantiate(TypeDefinition type, SerializationContext context) {
return false;
}
/** Called if the method canInstantiate previously returned true during deserialization. */
default Object instantiate(TypeDefinition type, SerializationContext context) throws SerializationException {
return null;
}
/** Called each time a type is instantiated during deserialization. */
default void onInstantiation(TypeDefinition type, Object instance, SerializationContext context) throws SerializationException {
}
/** Add the given rule to the newRules if no equivalent rule is found neither in rules or newRules. */
static void addRuleIfNoEquivalent(SerializationRule rule, List newRules, List rules) {
if (rule == null)
return;
boolean found = false;
for (SerializationRule r : rules)
if (r.isEquivalent(rule)) {
found = true;
break;
}
if (!found)
for (SerializationRule r : newRules)
if (r.isEquivalent(rule)) {
found = true;
break;
}
if (!found)
newRules.add(rule);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy