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

gu.sql2java.json.FastjsonDeserializer Maven / Gradle / Ivy

There is a newer version: 5.3.2
Show newest version
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 converter; public FastjsonDeserializer(ParserConfig config, Class beanClass) { super(config, checkNotNull(beanClass,"beanClass is null")); converter = new MapConverter<>(beanClass, JSONObject.class); } public FastjsonDeserializer(Class 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