Reading YAML with Jackson ignores keys without values












2















I have the following YAML file:



---
-
id: 001
start: 21.11.2018
additional:
dependency:
result: 2


which I like to read in with jackson as a simple List<Map<String, Object>>.



For this I use the following code



 private List<Map<String, Object>> readDefaultInputAsMap() {
var objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
try {
return objectMapper.readValue(inputResource, new TypeReference<List<Map<String, Object>>>() {
});
} catch (IOException e) {
e.printStackTrace();
return new ArrayList<>();
}
}


Unfortunately, this returns a map with only id, start and result, so the two others are ignored.



How can I get Jackson to parse the file and create the full map and with e.g. null as values for the empy keys ? (Or any other default value)?










share|improve this question



























    2















    I have the following YAML file:



    ---
    -
    id: 001
    start: 21.11.2018
    additional:
    dependency:
    result: 2


    which I like to read in with jackson as a simple List<Map<String, Object>>.



    For this I use the following code



     private List<Map<String, Object>> readDefaultInputAsMap() {
    var objectMapper = new ObjectMapper(new YAMLFactory());
    objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
    try {
    return objectMapper.readValue(inputResource, new TypeReference<List<Map<String, Object>>>() {
    });
    } catch (IOException e) {
    e.printStackTrace();
    return new ArrayList<>();
    }
    }


    Unfortunately, this returns a map with only id, start and result, so the two others are ignored.



    How can I get Jackson to parse the file and create the full map and with e.g. null as values for the empy keys ? (Or any other default value)?










    share|improve this question

























      2












      2








      2








      I have the following YAML file:



      ---
      -
      id: 001
      start: 21.11.2018
      additional:
      dependency:
      result: 2


      which I like to read in with jackson as a simple List<Map<String, Object>>.



      For this I use the following code



       private List<Map<String, Object>> readDefaultInputAsMap() {
      var objectMapper = new ObjectMapper(new YAMLFactory());
      objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
      try {
      return objectMapper.readValue(inputResource, new TypeReference<List<Map<String, Object>>>() {
      });
      } catch (IOException e) {
      e.printStackTrace();
      return new ArrayList<>();
      }
      }


      Unfortunately, this returns a map with only id, start and result, so the two others are ignored.



      How can I get Jackson to parse the file and create the full map and with e.g. null as values for the empy keys ? (Or any other default value)?










      share|improve this question














      I have the following YAML file:



      ---
      -
      id: 001
      start: 21.11.2018
      additional:
      dependency:
      result: 2


      which I like to read in with jackson as a simple List<Map<String, Object>>.



      For this I use the following code



       private List<Map<String, Object>> readDefaultInputAsMap() {
      var objectMapper = new ObjectMapper(new YAMLFactory());
      objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
      try {
      return objectMapper.readValue(inputResource, new TypeReference<List<Map<String, Object>>>() {
      });
      } catch (IOException e) {
      e.printStackTrace();
      return new ArrayList<>();
      }
      }


      Unfortunately, this returns a map with only id, start and result, so the two others are ignored.



      How can I get Jackson to parse the file and create the full map and with e.g. null as values for the empy keys ? (Or any other default value)?







      java jackson yaml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 22:47









      Emerson CodEmerson Cod

      1,1371722




      1,1371722
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Actually, it does that by default. Double-check the format of your YML (because your example seems to be invalid). Here is what worked for me:



          test.yml:



          ---
          - id: 001
          start: 21.11.2018
          additional:
          dependency:
          result: 2


          Test.java:



          public class App {
          public static void main(String args) {
          ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
          objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
          try {
          final Object value = objectMapper.readValue(App.class.getResource("test.yml"), new TypeReference<List<Map<String, Object>>>() {
          });

          System.out.println(value);
          } catch (IOException e) {
          e.printStackTrace();
          }
          }
          }


          And I got the result:



          [{id=1, start=21.11.2018, additional=null, dependency=null, result=2}]


          As you see, additional and dependency are both null.






          share|improve this answer
























          • acutally, the format of the yaml file seems not to be a problem. But I have found my error, which is false check at another part of the code, so you are absolutely right about the default behavior

            – Emerson Cod
            Nov 25 '18 at 1:38



















          1














          You should create your own Object (new class) with all possible attributes you want, for instance:



           class MyCompleteInfo {
          String id;
          String start;
          String additional;
          String dependency;
          String result;
          }


          And use a List of it (as method return, and in your reader):



          List<MyCompleteInfo >


          Edit: You may have used @JsonInclude(Include.NON_EMPTY) somewhere?
          You must not.



          See documentation.






          share|improve this answer


























          • i do not know beforehand the attributes to build a class

            – Emerson Cod
            Nov 25 '18 at 1:30











          • You may have used @JsonInclude(Include.NON_EMPTY) somewhere? You shouldn't.

            – Bsquare
            Nov 25 '18 at 1:51











          • On Stackoverflow you could give up-vote to people's helpful answers to thank them and select any one of the answer as correct answer too out of all.

            – Bsquare
            Dec 30 '18 at 14:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463018%2freading-yaml-with-jackson-ignores-keys-without-values%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Actually, it does that by default. Double-check the format of your YML (because your example seems to be invalid). Here is what worked for me:



          test.yml:



          ---
          - id: 001
          start: 21.11.2018
          additional:
          dependency:
          result: 2


          Test.java:



          public class App {
          public static void main(String args) {
          ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
          objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
          try {
          final Object value = objectMapper.readValue(App.class.getResource("test.yml"), new TypeReference<List<Map<String, Object>>>() {
          });

          System.out.println(value);
          } catch (IOException e) {
          e.printStackTrace();
          }
          }
          }


          And I got the result:



          [{id=1, start=21.11.2018, additional=null, dependency=null, result=2}]


          As you see, additional and dependency are both null.






          share|improve this answer
























          • acutally, the format of the yaml file seems not to be a problem. But I have found my error, which is false check at another part of the code, so you are absolutely right about the default behavior

            – Emerson Cod
            Nov 25 '18 at 1:38
















          1














          Actually, it does that by default. Double-check the format of your YML (because your example seems to be invalid). Here is what worked for me:



          test.yml:



          ---
          - id: 001
          start: 21.11.2018
          additional:
          dependency:
          result: 2


          Test.java:



          public class App {
          public static void main(String args) {
          ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
          objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
          try {
          final Object value = objectMapper.readValue(App.class.getResource("test.yml"), new TypeReference<List<Map<String, Object>>>() {
          });

          System.out.println(value);
          } catch (IOException e) {
          e.printStackTrace();
          }
          }
          }


          And I got the result:



          [{id=1, start=21.11.2018, additional=null, dependency=null, result=2}]


          As you see, additional and dependency are both null.






          share|improve this answer
























          • acutally, the format of the yaml file seems not to be a problem. But I have found my error, which is false check at another part of the code, so you are absolutely right about the default behavior

            – Emerson Cod
            Nov 25 '18 at 1:38














          1












          1








          1







          Actually, it does that by default. Double-check the format of your YML (because your example seems to be invalid). Here is what worked for me:



          test.yml:



          ---
          - id: 001
          start: 21.11.2018
          additional:
          dependency:
          result: 2


          Test.java:



          public class App {
          public static void main(String args) {
          ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
          objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
          try {
          final Object value = objectMapper.readValue(App.class.getResource("test.yml"), new TypeReference<List<Map<String, Object>>>() {
          });

          System.out.println(value);
          } catch (IOException e) {
          e.printStackTrace();
          }
          }
          }


          And I got the result:



          [{id=1, start=21.11.2018, additional=null, dependency=null, result=2}]


          As you see, additional and dependency are both null.






          share|improve this answer













          Actually, it does that by default. Double-check the format of your YML (because your example seems to be invalid). Here is what worked for me:



          test.yml:



          ---
          - id: 001
          start: 21.11.2018
          additional:
          dependency:
          result: 2


          Test.java:



          public class App {
          public static void main(String args) {
          ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
          objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
          try {
          final Object value = objectMapper.readValue(App.class.getResource("test.yml"), new TypeReference<List<Map<String, Object>>>() {
          });

          System.out.println(value);
          } catch (IOException e) {
          e.printStackTrace();
          }
          }
          }


          And I got the result:



          [{id=1, start=21.11.2018, additional=null, dependency=null, result=2}]


          As you see, additional and dependency are both null.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 0:16









          madheadmadhead

          14.4k1383123




          14.4k1383123













          • acutally, the format of the yaml file seems not to be a problem. But I have found my error, which is false check at another part of the code, so you are absolutely right about the default behavior

            – Emerson Cod
            Nov 25 '18 at 1:38



















          • acutally, the format of the yaml file seems not to be a problem. But I have found my error, which is false check at another part of the code, so you are absolutely right about the default behavior

            – Emerson Cod
            Nov 25 '18 at 1:38

















          acutally, the format of the yaml file seems not to be a problem. But I have found my error, which is false check at another part of the code, so you are absolutely right about the default behavior

          – Emerson Cod
          Nov 25 '18 at 1:38





          acutally, the format of the yaml file seems not to be a problem. But I have found my error, which is false check at another part of the code, so you are absolutely right about the default behavior

          – Emerson Cod
          Nov 25 '18 at 1:38













          1














          You should create your own Object (new class) with all possible attributes you want, for instance:



           class MyCompleteInfo {
          String id;
          String start;
          String additional;
          String dependency;
          String result;
          }


          And use a List of it (as method return, and in your reader):



          List<MyCompleteInfo >


          Edit: You may have used @JsonInclude(Include.NON_EMPTY) somewhere?
          You must not.



          See documentation.






          share|improve this answer


























          • i do not know beforehand the attributes to build a class

            – Emerson Cod
            Nov 25 '18 at 1:30











          • You may have used @JsonInclude(Include.NON_EMPTY) somewhere? You shouldn't.

            – Bsquare
            Nov 25 '18 at 1:51











          • On Stackoverflow you could give up-vote to people's helpful answers to thank them and select any one of the answer as correct answer too out of all.

            – Bsquare
            Dec 30 '18 at 14:49
















          1














          You should create your own Object (new class) with all possible attributes you want, for instance:



           class MyCompleteInfo {
          String id;
          String start;
          String additional;
          String dependency;
          String result;
          }


          And use a List of it (as method return, and in your reader):



          List<MyCompleteInfo >


          Edit: You may have used @JsonInclude(Include.NON_EMPTY) somewhere?
          You must not.



          See documentation.






          share|improve this answer


























          • i do not know beforehand the attributes to build a class

            – Emerson Cod
            Nov 25 '18 at 1:30











          • You may have used @JsonInclude(Include.NON_EMPTY) somewhere? You shouldn't.

            – Bsquare
            Nov 25 '18 at 1:51











          • On Stackoverflow you could give up-vote to people's helpful answers to thank them and select any one of the answer as correct answer too out of all.

            – Bsquare
            Dec 30 '18 at 14:49














          1












          1








          1







          You should create your own Object (new class) with all possible attributes you want, for instance:



           class MyCompleteInfo {
          String id;
          String start;
          String additional;
          String dependency;
          String result;
          }


          And use a List of it (as method return, and in your reader):



          List<MyCompleteInfo >


          Edit: You may have used @JsonInclude(Include.NON_EMPTY) somewhere?
          You must not.



          See documentation.






          share|improve this answer















          You should create your own Object (new class) with all possible attributes you want, for instance:



           class MyCompleteInfo {
          String id;
          String start;
          String additional;
          String dependency;
          String result;
          }


          And use a List of it (as method return, and in your reader):



          List<MyCompleteInfo >


          Edit: You may have used @JsonInclude(Include.NON_EMPTY) somewhere?
          You must not.



          See documentation.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 1:52

























          answered Nov 25 '18 at 0:14









          BsquareBsquare

          3,31131034




          3,31131034













          • i do not know beforehand the attributes to build a class

            – Emerson Cod
            Nov 25 '18 at 1:30











          • You may have used @JsonInclude(Include.NON_EMPTY) somewhere? You shouldn't.

            – Bsquare
            Nov 25 '18 at 1:51











          • On Stackoverflow you could give up-vote to people's helpful answers to thank them and select any one of the answer as correct answer too out of all.

            – Bsquare
            Dec 30 '18 at 14:49



















          • i do not know beforehand the attributes to build a class

            – Emerson Cod
            Nov 25 '18 at 1:30











          • You may have used @JsonInclude(Include.NON_EMPTY) somewhere? You shouldn't.

            – Bsquare
            Nov 25 '18 at 1:51











          • On Stackoverflow you could give up-vote to people's helpful answers to thank them and select any one of the answer as correct answer too out of all.

            – Bsquare
            Dec 30 '18 at 14:49

















          i do not know beforehand the attributes to build a class

          – Emerson Cod
          Nov 25 '18 at 1:30





          i do not know beforehand the attributes to build a class

          – Emerson Cod
          Nov 25 '18 at 1:30













          You may have used @JsonInclude(Include.NON_EMPTY) somewhere? You shouldn't.

          – Bsquare
          Nov 25 '18 at 1:51





          You may have used @JsonInclude(Include.NON_EMPTY) somewhere? You shouldn't.

          – Bsquare
          Nov 25 '18 at 1:51













          On Stackoverflow you could give up-vote to people's helpful answers to thank them and select any one of the answer as correct answer too out of all.

          – Bsquare
          Dec 30 '18 at 14:49





          On Stackoverflow you could give up-vote to people's helpful answers to thank them and select any one of the answer as correct answer too out of all.

          – Bsquare
          Dec 30 '18 at 14:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463018%2freading-yaml-with-jackson-ignores-keys-without-values%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)