如何将jgrapht简单图形序列化为json?

时间:2016-09-11 17:48:37

标签: java json serialization jgrapht

我有一个来自jgrapht的简单有向图,我正在尝试使用jackson将其序列化为JSON文件,如下所示:

ObjectMapper mapper = new ObjectMapper();
File output = new File("P:\\tree.json");
ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter();
objectWriter.writeValue(output,simpleDirectedGraph);

但是我收到了这个错误:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.jgrapht.graph.AbstractBaseGraph$ArrayListFactory and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.jgrapht.graph.SimpleDirectedGraph["edgeSetFactory"])
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1387)
    at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1088)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:909)
    at ms.fragment.JSONTreeGenerator.main(JSONTreeGenerator.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我已经看到有一个GmlExporter,但我对json感兴趣...我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式禁用该异常:

    "labels": ["05-Jul-07", "06-Dec-07", "07-Feb-08", "10-Apr-08", "08-Oct-08", "06-Nov-08", "04-Dec-08", "08-Jan-09", "05-Feb-09", "05-Mar-09", "04-Aug-16", "02-Nov-17", "02-Aug-18"],
    "datasets": [{
        "data": [
    { x: "05-Jul-07", y: 0.0575 },
    { x: "06-Dec-07", y: 0.055 },
    { x: "07-Feb-08", y: 0.0525 },
    { x: "10-Apr-08", y: 0.05 },
    { x: "08-Oct-08", y: 0.045 },
    { x: "06-Nov-08", y: 0.03 },
    { x: "04-Dec-08", y: 0.02 },
    { x: "08-Jan-09", y: 0.015 },
    { x: "05-Feb-09", y: 0.01 },
    { x: "05-Mar-09", y: 0.005 },
    { x: "04-Aug-16", y: 0.0025 },
    { x: "02-Nov-17", y: 0.005 },
    { x: "02-Aug-18", y: 0.0075 }
  ],

答案 1 :(得分:0)

你从杰克逊那里得到的例外:

<块引用>

JsonMappingException:找不到类 org.jgrapht.graph.Abs​​tractBaseGraph$ArrayListFactory 的序列化程序 并且没有发现创建 BeanSerializer 的属性 (为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过参考链: org.jgrapht.graph.SimpleDirectedGraph["edgeSetFactory"]) 在 com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69)

给出了如何解决的线索:

  • 未发现创建 BeanSerializer
  • 的属性
  • 属性 SimpleDirectedGraph["edgeSetFactory"] 似乎

从序列化中排除属性 edgeSetFactory 的类型:

AbstractBaseGraph$ArrayListFactory

编写自定义序列化程序

通常 Jackson 会使用 StdBeanSerializer 将任何非原始类写入 JSON。 不幸的是,这不适用于抽象类。

因此您可以编写自己的 JsonSerializer 来处理特殊字段。