gu.sql2java.json.FastjsonDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-base Show documentation
Show all versions of sql2java-base Show documentation
sql2java common class package
package gu.sql2java.json;
import java.lang.reflect.Type;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer;
import gu.sql2java.BaseBean;
import gu.sql2java.MapConverter;
import gu.sql2java.RowMetaData;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* implementation of BaseBean class for
* FastJson {@code com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer},
* MAVEN dependencies:
*
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>fastjson</artifactId>
* <version>${fastjson.version}</version>
* <scope>provided</scope>
* </dependency>
*
*
* @author guyadong
*
*/
public class FastjsonDeserializer extends JavaBeanDeserializer {
private final MapConverter extends BaseBean, JSONObject> converter;
public FastjsonDeserializer(ParserConfig config, Class extends BaseBean> beanClass) {
super(config, checkNotNull(beanClass,"beanClass is null"));
converter = new MapConverter<>(beanClass, JSONObject.class);
}
public FastjsonDeserializer(Class extends BaseBean> beanClass) {
this(ParserConfig.global, beanClass);
}
@SuppressWarnings("unchecked")
@Override
public T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
/** deserialze to JSONObject */
JSONObject json = (JSONObject)super.deserialze(parser, JSONObject.class, fieldName);
/** convert to BaseBean */
return (T) converter.fromRight(json);
}
@Override
public int getFastMatchToken() {
return JSONToken.LBRACE;
}
/**
* define FASTJSON deserializer for all class derived from BaseBean
*/
public static void install() {
for(RowMetaData metaData:RowMetaData.allMetaDataList()){
ParserConfig.global.putDeserializer(
metaData.beanType,
new FastjsonDeserializer(metaData.beanType));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy