package me.chyxion.tigon.json;

import java.io.IOException;
import java.lang.reflect.Type;
import me.chyxion.tigon.model.ViewModelable;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Mar 19, 2017 11:55:49
 */
public class ViewModelableSerializer implements ObjectSerializer {

    /**
     * {@inheritDoc}
     */
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if (object != null) {
            serializer.write(((ViewModelable<?>) object).toMap());
        }
        else {
            serializer.writeNull();
        }
    }
}
