下面列出了com.alibaba.fastjson.parser.ParserConfig#putDeserializer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 自定义管道的处理方法
*
* @param resultItems 自定义Processor处理完后的所有参数
* @param virtualArticles 爬虫文章集合
*/
final void process(ResultItems resultItems, List<VirtualArticle> virtualArticles, Hunter spider) {
if (null == spider) {
return;
}
Map<String, Object> map = resultItems.getAll();
if (CollectionUtil.isEmpty(map)) {
return;
}
String title = String.valueOf(map.get("title"));
ParserConfig jcParserConfig = new ParserConfig();
jcParserConfig.putDeserializer(Date.class, HunterDateDeserializer.instance);
VirtualArticle virtualArticle = JSON.parseObject(JSON.toJSONString(map), VirtualArticle.class, jcParserConfig, JSON.DEFAULT_PARSER_FEATURE);
virtualArticle.setDescription(CommonUtil.getRealDescription(virtualArticle.getDescription(), virtualArticle.getContent()))
.setKeywords(CommonUtil.getRealKeywords(virtualArticle.getKeywords()));
if (this.config.isConvertImg()) {
virtualArticle.setContent(CommonUtil.formatHtml(virtualArticle.getContent()));
virtualArticle.setImageLinks(CommonUtil.getAllImageLink(virtualArticle.getContent()));
}
if (CollectionUtils.isEmpty(virtualArticle.getTags())) {
virtualArticle.setTags(Collections.singletonList("其他"));
}
virtualArticles.add(virtualArticle);
writer.print(String.format("<a href=\"%s\" target=\"_blank\">%s</a> -- %s -- %s", virtualArticle.getSource(), title, virtualArticle.getAuthor(), virtualArticle.getReleaseDate()));
}
private void handleForDeserializer(final ObjectDeserializer deserializer, Class targetType) {
ClassNode node = repo.node(targetType.getName());
if (null == node) {
warn("Unknown target type: " + targetType.getName());
return;
}
final ParserConfig config = ParserConfig.getGlobalInstance();
node.visitSubTree(new Lang.Visitor<ClassNode>() {
@Override
public void visit(ClassNode classNode) throws Lang.Break {
Class type = app.classForName(classNode.name());
config.putDeserializer(type, deserializer);
}
});
config.putDeserializer(targetType, deserializer);
}
@BeforeClass
public static void prepare() {
SerializeConfig config = SerializeConfig.getGlobalInstance();
config.put(KV.class, FastJsonKvCodec.INSTANCE);
config.put(KVStore.class, FastJsonKvCodec.INSTANCE);
ParserConfig parserConfig = ParserConfig.getGlobalInstance();
parserConfig.putDeserializer(KV.class, FastJsonKvCodec.INSTANCE);
parserConfig.putDeserializer(KVStore.class, FastJsonKvCodec.INSTANCE);
JSON.DEFAULT_PARSER_FEATURE = Feature.config(JSON.DEFAULT_PARSER_FEATURE, Feature.UseBigDecimal, false);
}
public static void configure(final App app) {
SerializeConfig config = SerializeConfig.getGlobalInstance();
// patch https://github.com/alibaba/fastjson/issues/478
config.put(FastJsonIterable.class, FastJsonIterableSerializer.instance);
FastJsonJodaDateCodec jodaDateCodec = new FastJsonJodaDateCodec(app);
app.registerSingleton(FastJsonJodaDateCodec.class, jodaDateCodec);
FastJsonValueObjectSerializer valueObjectSerializer = new FastJsonValueObjectSerializer();
app.registerSingleton(FastJsonValueObjectSerializer.class, valueObjectSerializer);
FastJsonKeywordCodec keywordCodec = new FastJsonKeywordCodec();
FastJsonSObjectCodec sObjectCodec = new FastJsonSObjectCodec();
config.put(DateTime.class, jodaDateCodec);
config.put(LocalDate.class, jodaDateCodec);
config.put(LocalTime.class, jodaDateCodec);
config.put(LocalDateTime.class, jodaDateCodec);
config.put(ValueObject.class, valueObjectSerializer);
config.put(Keyword.class, keywordCodec);
config.put(KV.class, FastJsonKvCodec.INSTANCE);
config.put(KVStore.class, FastJsonKvCodec.INSTANCE);
final ParserConfig parserConfig = ParserConfig.getGlobalInstance();
parserConfig.putDeserializer(DateTime.class, jodaDateCodec);
parserConfig.putDeserializer(LocalDate.class, jodaDateCodec);
parserConfig.putDeserializer(LocalTime.class, jodaDateCodec);
parserConfig.putDeserializer(LocalDateTime.class, jodaDateCodec);
parserConfig.putDeserializer(Keyword.class, keywordCodec);
parserConfig.putDeserializer(KV.class, FastJsonKvCodec.INSTANCE);
parserConfig.putDeserializer(KVStore.class, FastJsonKvCodec.INSTANCE);
parserConfig.putDeserializer(ISObject.class, sObjectCodec);
parserConfig.putDeserializer(SObject.class, sObjectCodec);
MvcConfig.jsonSerializer(new $.Func2<Writer, Object, Void>() {
@Override
public Void apply(Writer writer, Object v) throws NotAppliedException, $.Break {
ActContext ctx = ActContext.Base.currentContext();
new JsonWriter(v, null, false, ctx).apply(writer);
return null;
}
});
app.eventBus().bind(CLASS_LOADER_INITIALIZED, new SysEventListenerBase<AppClassLoaderInitialized>() {
@Override
public void on(AppClassLoaderInitialized event) {
parserConfig.setDefaultClassLoader(app.classLoader());
TypeUtils.clearClassMapping();
}
});
}