利用多态性从JSON生成对象

huangapple 未分类评论51阅读模式
英文:

Generate objects from JSON with Polymorphism

问题

I have this JSON and I want to generate objects with it.

  1. {
  2. "responseId": "response-id",
  3. "session": "projects/project-id/agent/sessions/session-id",
  4. "queryResult": {
  5. "queryText": "End-user expression",
  6. "parameters": {
  7. "orderNumber": "PQL7648194AAAAA",
  8. "lastName": "RIVERO"
  9. },
  10. "action": "order",
  11. "allRequiredParamsPresent": true,
  12. "intent": {
  13. "name": "projects/project-id/agent/intents/intent-id",
  14. "displayName": "[InfoVuelo] Dia-hora de salida - datos de orden"
  15. },
  16. "languageCode": "es"
  17. },
  18. "originalDetectIntentRequest": {}
  19. }

The fields of "parameters" are variable. For example for one situation parameters could be

  1. {
  2. "parameters": {
  3. "email": "example@example.com"
  4. }
  5. }

Also, I could have

  1. {
  2. "parameters": {
  3. "orderNumber": "PQL7648194AAAAA",
  4. "lastName": "RIVERO"
  5. }
  6. }

I made a class Parameters and 2 classes (OrderNumberAndLastNameParams and EmailParams) that extends Parameters

  1. public class EmailParams implements Parameters {
  2. private String email;
  3. }
  4. public class OrderNumberLastNameParams implements Parameters {
  5. private String orderNumber;
  6. private String lastName;
  7. }

My method to create objects with a given JSON file

  1. public static <T> T buildBodyFromJson(String filePath, Class<T> clazz) {
  2. ObjectMapper myMapper = new ObjectMapper();
  3. T jsonMapper = null;
  4. try {
  5. jsonMapper = myMapper.readValue(new ClassPathResource(filePath).getInputStream(), clazz);
  6. } catch (IOException e) {
  7. fail(e.getLocalizedMessage());
  8. }
  9. return jsonMapper;
  10. }

But when I try to build the object, It tries to instantiate Parameter object instead of children objects getting "Unrecognized field ... not marked as ignorable".

英文:

I have this JSON and I want to generate objects with it.

  1. {
  2. &quot;responseId&quot;: &quot;response-id&quot;,
  3. &quot;session&quot;: &quot;projects/project-id/agent/sessions/session-id&quot;,
  4. &quot;queryResult&quot;: {
  5. &quot;queryText&quot;: &quot;End-user expression&quot;,
  6. &quot;parameters&quot;: {
  7. &quot;orderNumber&quot;: &quot;PQL7648194AAAAA&quot;,
  8. &quot;lastName&quot;: &quot;RIVERO&quot;
  9. },
  10. &quot;action&quot;: &quot;order&quot;,
  11. &quot;allRequiredParamsPresent&quot;: true,
  12. &quot;intent&quot;: {
  13. &quot;name&quot;: &quot;projects/project-id/agent/intents/intent-id&quot;,
  14. &quot;displayName&quot;: &quot;[InfoVuelo] Dia-hora de salida - datos de orden&quot;
  15. },
  16. &quot;languageCode&quot;: &quot;es&quot;
  17. },
  18. &quot;originalDetectIntentRequest&quot;: {}
  19. }

The fields of "parameters" are variable. For example for one situation parameters could be

  1. {
  2. &quot;parameters&quot;: {
  3. &quot;email&quot;:&quot;example@example.com&quot;
  4. }
  5. }

Also, I could have

  1. {
  2. &quot;parameters&quot;: {
  3. &quot;orderNumber&quot;: &quot;PQL7648194AAAAA&quot;,
  4. &quot;lastName&quot;: &quot;RIVERO&quot;
  5. }
  6. }

I made a class Parameters and 2 classes(OrderNumberAndLastNameParams and EmailParams) that extends Parameters

  1. public class EmailParams implements Parameters {
  2. private String email;
  3. }
  4. public class OrderNumberLastNameParams implements Parameters {
  5. private String orderNumber;
  6. private String lastName;
  7. }

My method to create objects with a given JSON file

  1. public static &lt;T&gt; T buildBodyFromJson(String filePath, Class&lt;T&gt; clazz) {
  2. ObjectMapper myMapper = new ObjectMapper();
  3. T jsonMapper = null;
  4. try {
  5. jsonMapper = myMapper.readValue(new ClassPathResource(filePath).getInputStream(), clazz);
  6. } catch (IOException e) {
  7. fail(e.getLocalizedMessage());
  8. }
  9. return jsonMapper;
  10. }

But when I try to build the object, It tries to instantiate Parameter object instead of children objects getting &quot;Unrecognized field ... not marked as ignorable &quot;.

答案1

得分: 0

我理解您并不真正需要两个分开的类,问题是您需要生成一个不包含空/空字段的请求正文。好的,有一个注释可能会对您有所帮助。

创建一个参数的单一类,包含所有的3个属性,并使用 @JsonInclude(JsonInclude.Include.NON_EMPTY) 进行注释。

  1. @JsonInclude(JsonInclude.Include.NON_EMPTY)
  2. public class Parameters {
  3. private String email;
  4. private String orderNumber;
  5. private String lastName;
  6. }

这个注释会使空的或者为null的字段不会出现在您正在创建的请求正文中。

英文:

I'm understanding that you don't really need two separated classes, the problem is that you need to generate a requestbody with this object that doesn't have null / empty fields.
Well, there is an annotation that may help you.

Create a single class for paramters with all the 3 properties and annotate with @JsonInclude(JsonInclude.Include.NON_EMPTY)

  1. @JsonInclude(JsonInclude.Include.NON_EMPTY)
  2. public class Parameters {
  3. private String email;
  4. private String orderNumber;
  5. private String lastName;
  6. }

This annotation makes empty or null fields to not appears on the requestbody that you're creating.

huangapple
  • 本文由 发表于 2020年4月4日 06:38:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/61021451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定