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

com.firefly.utils.json.serializer.ObjectSerializer Maven / Gradle / Ivy

There is a newer version: 4.0.3.2
Show newest version
package com.firefly.utils.json.serializer;

import static com.firefly.utils.json.JsonStringSymbol.OBJ_PRE;
import static com.firefly.utils.json.JsonStringSymbol.OBJ_SUF;

import java.io.IOException;

import com.firefly.utils.json.JsonWriter;
import com.firefly.utils.json.Serializer;
import com.firefly.utils.json.compiler.EncodeCompiler;
import com.firefly.utils.json.support.SerializerMetaInfo;

public class ObjectSerializer implements Serializer {
	
	private SerializerMetaInfo[] serializerMetaInfos;
	
	public void init(Class clazz) {
		serializerMetaInfos = EncodeCompiler.compile(clazz);
	}

	@Override
	public void convertTo(JsonWriter writer, Object obj) throws IOException {		
		if (writer.existRef(obj)) { // prevent circular reference
			writer.writeNull();
			return;
		}
		
		writer.pushRef(obj);
		writer.append(OBJ_PRE);
		for(SerializerMetaInfo metaInfo : serializerMetaInfos){
			writer.write(metaInfo.getPropertyName());
			metaInfo.toJson(obj, writer);
		}
		writer.append(OBJ_SUF);
		writer.popRef();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy