Files
redkale/com/wentch/redkale/convert/AnyEncoder.java
地平线 ac6a2e57c6
2015-09-22 17:34:27 +08:00

41 lines
1.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.wentch.redkale.convert;
import java.lang.reflect.Type;
/**
* 对不明类型的对象进行序列化; BSON序列化时将对象的类名写入WriterJSON则不写入。
*
* @author zhangjx
* @param <T>
*/
public final class AnyEncoder<T> implements Encodeable<Writer, T> {
final Factory factory;
AnyEncoder(Factory factory) {
this.factory = factory;
}
@Override
@SuppressWarnings("unchecked")
public void convertTo(final Writer out, final T value) {
if (value == null) {
out.writeNull();
} else {
out.wirteClassName(factory.getEntity(value.getClass()));
factory.loadEncoder(value.getClass()).convertTo(out, value);
}
}
@Override
public Type getType() {
return Object.class;
}
}