All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jetro.object.serializer.addons.ToStringSerializer Maven / Gradle / Ivy

The newest version!
package net.sf.jetro.object.serializer.addons;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

import net.sf.jetro.object.serializer.TypeSerializer;
import net.sf.jetro.visitor.JsonVisitor;

public class ToStringSerializer implements TypeSerializer {
	private Set> applicableClasses = new LinkedHashSet<>();
	
	public ToStringSerializer(Class... applicableClasses) {
		this.applicableClasses.addAll(Arrays.asList(applicableClasses));
	}
	
	public ToStringSerializer(Set> applicableClasses) {
		this.applicableClasses.addAll(applicableClasses);
	}
	
	@Override
	public boolean canSerialize(Object toSerialize) {
		return applicableClasses.parallelStream()
				.filter(clazz -> clazz.equals(toSerialize.getClass())).findFirst().isPresent();
	}

	@Override
	public void serialize(Object toSerialize, JsonVisitor recipient) {
		recipient.visitValue(toSerialize.toString());
	}
}