0

I am trying use jsonSchema2pojo POJO generating library and i am successful in doing so as well. But issue i am facing is deserialisation Exception when i try to generate map by typeReferencing generated rootClass i.e EsRootDoc(in below code).

Here , is the code that i am testing:

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();
    }

Exception :

  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"])

Now, If i am generating classes here and then typeReferencing same generated rootClass , how do i get to dissimilar json to Pojo issue ??

Editted(added 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"
}
Kimchy
  • 501
  • 8
  • 24
  • How does your json look like? Please add your json string to the question – Davis Molinari Jun 20 '17 at 08:52
  • @davis molinari, please check i have editted my question and input json added at the end of my query – Kimchy Jun 24 '17 at 17:23
  • Hi All, i just back to this issue after some , anyone would like to comment on above issue, i also tried removing all pojo files on every start and no older files exists and whole new process is being done to generate new Pojo and typeReference to new generated EsRootDoc. any suggestion would be appreciated as i dont have further idea? Feel like putting query on jsonSchema2Pojo github issue page now @joelittlejohn – Kimchy Jul 21 '17 at 06:47
  • Haven't used `jsonSchema2pojo` before, but your `classLocation` looks suspicious; looks like it's a *sources* folder (`System.getProperty("user.dir")+ "/src/main/java"`) whereas any classes/bytecode generated at runtime should probably be placed in a *classes* folder (e.g. `target/classes`) so that it is visible to the classloaders (i.e. included in the runtime classpath). (But the exception you're getting seems more like an unexpected JSON syntax, rather than a 'missing class' issue.) – Janaka Bandara Nov 25 '17 at 11:43
  • yeah, thanks Janaka, you are right, its not basically class missing issue as class is loaded but just once but as json input gets changed at runtime and new class is made to generate using jsonSchema2Pojo library but already loaded class is to be replaced for newly generated class. I did generate .class files for newly generated .java files and replaced old .class files in target folder as well. In src/main/java only .java files are generated which are being compiled to target location as well. – Kimchy Nov 28 '17 at 07:46

0 Answers0