jsonSchema2pojo杰克逊反序列化问题

时间:2017-06-20 08:45:51

标签: json jackson jsonschema2pojo

我正在尝试使用jsonSchema2pojo POJO生成库,我也成功地这样做了。但我面临的问题是反序列化异常,当我尝试通过typeReferencing生成的map生成rootClass,即EsRootDoc(在下面的代码中)。

这是我正在测试的代码:

JCodeModel codeModel = new JCodeModel();
String classLocation = System.getProperty("user.dir")+ "/src/main/java";
GenerationConfig config = new DefaultGenerationConfig() {
                @Override
                public boolean isGenerateBuilders() { // set config option by overriding method
                    return true;
                }

                @Override
                public SourceType getSourceType() {
                    return SourceType.JSON;
                }
final RuleFactory ruleFactory = new RuleFactory(config,
            new Jackson2Annotator(config), new SchemaStore());

            final SchemaMapper mapperSchema = new SchemaMapper(ruleFactory,new SchemaGenerator());

    mapperSchema.generate(codeModel, "EsRootDoc",           "com.tech.EnrichmentService.EnrichmentController.tempJsonToClass", json);

codeModel.build(new File(classLocation));

ObjectMapper mapper = new ObjectMapper();
try{

     p = mapper.readValue(json,new TypeReference<com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.EsRootDoc>() {});

    }
    catch(Exception e){
    e.printStackTrace();
    }

例外:

  com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
 at [Source: java.io.StringReader@72b070e8; line: 1, column: 15974] (through reference chain: com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.EsRootDoc["svo"]->java.util.ArrayList[1]->com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.Svo["svoMetadata"]->com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.SvoMetadata["event"])

现在,如果我在这里生成类,然后键入引用相同的生成的rootClass,我如何得到不同的json到Pojo问题?

编辑(添加了json):

{
  "coref": [
    {
      "endOffset": 260,
      "coreference": "Hind Times",
      "entity": "them",
      "beginOffset": 256
    }
  ],

  "upCategory": "News",
  "dateFacet": "2016-06-14T04:14:06Z",
  "svo": [
    {
      "clause": "kids go missing from ? forest",
      "svoMetadata": {
        "sentenceID": 1,
        "clauseId": 2,
        "svoId": 0
      },
      "objectPOS": "IN POS NN NN",
      "isRelatedKeySVO": false,
      "subjectPOS": " NNS",
      "subject": "kids",
      "confidence": 0.786255231995661,
      "verb": "missing",
      "verbPOS": "VBG",
      "source": "OpenIE4",
      "isKeySVO": true,
      "object": "from ' forest toilet"
    },
    {
      "clause": "lack of toilets forced them to defacate in the open",
      "svoMetadata": {
        "sentenceID": 1,
        "clauseId": 0,
        "event": [
          "forced"
        ],
        "svoId": 0,
        "verbType": "event,"
      }
    }
  ],
    "projectName": "guest",
  "uniqueId": "575f848ee4b0011ac0e1ef091"
}

0 个答案:

没有答案