Java Code Examples for com.alibaba.fastjson.parser.ParserConfig#putDeserializer()

The following examples show how to use com.alibaba.fastjson.parser.ParserConfig#putDeserializer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HunterProcessor.java    From blog-hunter with MIT License 6 votes vote down vote up
/**
 * 自定义管道的处理方法
 *
 * @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()));
}
 
Example 2
Source File: FastJson.java    From actframework with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: FastJsonKvCodecTest.java    From actframework with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: JsonUtilConfig.java    From actframework with Apache License 2.0 4 votes vote down vote up
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();
        }
    });
}