• <small id='YMeYp'></small><noframes id='YMeYp'>

  • <legend id='YMeYp'><style id='YMeYp'><dir id='YMeYp'><q id='YMeYp'></q></dir></style></legend>

    <tfoot id='YMeYp'></tfoot>
  • <i id='YMeYp'><tr id='YMeYp'><dt id='YMeYp'><q id='YMeYp'><span id='YMeYp'><b id='YMeYp'><form id='YMeYp'><ins id='YMeYp'></ins><ul id='YMeYp'></ul><sub id='YMeYp'></sub></form><legend id='YMeYp'></legend><bdo id='YMeYp'><pre id='YMeYp'><center id='YMeYp'></center></pre></bdo></b><th id='YMeYp'></th></span></q></dt></tr></i><div id='YMeYp'><tfoot id='YMeYp'></tfoot><dl id='YMeYp'><fieldset id='YMeYp'></fieldset></dl></div>

      • <bdo id='YMeYp'></bdo><ul id='YMeYp'></ul>

      1. 使用snakeYaml解析根有地图的YAML文档

        时间:2023-09-26

        <small id='GWsHi'></small><noframes id='GWsHi'>

        <legend id='GWsHi'><style id='GWsHi'><dir id='GWsHi'><q id='GWsHi'></q></dir></style></legend>

          <bdo id='GWsHi'></bdo><ul id='GWsHi'></ul>

            <tfoot id='GWsHi'></tfoot>
              <tbody id='GWsHi'></tbody>
            • <i id='GWsHi'><tr id='GWsHi'><dt id='GWsHi'><q id='GWsHi'><span id='GWsHi'><b id='GWsHi'><form id='GWsHi'><ins id='GWsHi'></ins><ul id='GWsHi'></ul><sub id='GWsHi'></sub></form><legend id='GWsHi'></legend><bdo id='GWsHi'><pre id='GWsHi'><center id='GWsHi'></center></pre></bdo></b><th id='GWsHi'></th></span></q></dt></tr></i><div id='GWsHi'><tfoot id='GWsHi'></tfoot><dl id='GWsHi'><fieldset id='GWsHi'></fieldset></dl></div>
                • 本文介绍了使用snakeYaml解析根有地图的YAML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想将 YAML 文档读取到自定义对象的映射(而不是映射,snakeYaml 默认会这样做).所以这个:

                  I want to read a YAML document to a map of custom objects (instead of maps, which snakeYaml does by default). So this:

                  19:
                    typeID: 2
                    limit: 300
                  20:
                    typeID: 8
                    limit: 100
                  

                  将加载到如下所示的地图:

                  Would be loaded to a map which looks like this:

                  Map<Integer, Item>
                  

                  项目在哪里:

                  class Item {
                      private Integer typeId;
                      private Integer limit;
                  }
                  

                  我找不到使用snakeYaml 的方法,也找不到更好的库来完成这项任务.

                  I could not find a way to do this with snakeYaml, and I couldn't find a better library for the task either.

                  文档仅包含嵌套在其他对象中的地图/集合的示例,因此您可以执行以下操作:

                  The documentation only has examples with maps/collections nested inside other objects, so that you can do the following:

                      TypeDescription typeDescription = new TypeDescription(ClassContainingAMap.class);
                      typeDescription.putMapPropertyType("propertyNameOfNestedMap", Integer.class, Item.class);
                      Constructor constructor = new Constructor(typeDescription);
                      Yaml yaml = new Yaml(constructor);
                      /* creating an input stream (is) */
                      ClassContainingAMap obj = (ClassContainingAMap) yaml.load(is);
                  

                  但是当 Map 格式位于文档的根目录时,我该如何定义它呢?

                  But how do I go about defining the Map format when it is at the root of the document?

                  推荐答案

                  你需要添加一个自定义构造函数.但是,在您的情况下,您不想注册项目"或项目列表"标签.

                  You need to add a custom Constructor. However, in your case you don't want register an "item" or "item-list" tag.

                  实际上,您希望将 Duck Typing 应用于您的 Yaml.这不是超级高效,但有一种相对简单的方法可以做到这一点.

                  In effect, you want to apply Duck Typing to your Yaml. It's not super efficient, but there is a relatively easy way to do this.

                  class YamlConstructor extends Constructor {
                    @Override
                    protected Object constructObject(Node node) {
                  
                      if (node.getTag() == Tag.MAP) {
                          LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) super
                                  .constructObject(node);
                          // If the map has the typeId and limit attributes
                          // return a new Item object using the values from the map
                          ...
                      }
                       // In all other cases, use the default constructObject.
                      return super.constructObject(node);
                  

                  这篇关于使用snakeYaml解析根有地图的YAML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从 Yaml 文件为 Request 和 Response 对象生成 Java 类 下一篇:您如何在 Java 中实际解析 YAML 中的值?

                  相关文章

                  最新文章

                    <bdo id='HVTdL'></bdo><ul id='HVTdL'></ul>
                  <legend id='HVTdL'><style id='HVTdL'><dir id='HVTdL'><q id='HVTdL'></q></dir></style></legend>

                      <small id='HVTdL'></small><noframes id='HVTdL'>

                      <i id='HVTdL'><tr id='HVTdL'><dt id='HVTdL'><q id='HVTdL'><span id='HVTdL'><b id='HVTdL'><form id='HVTdL'><ins id='HVTdL'></ins><ul id='HVTdL'></ul><sub id='HVTdL'></sub></form><legend id='HVTdL'></legend><bdo id='HVTdL'><pre id='HVTdL'><center id='HVTdL'></center></pre></bdo></b><th id='HVTdL'></th></span></q></dt></tr></i><div id='HVTdL'><tfoot id='HVTdL'></tfoot><dl id='HVTdL'><fieldset id='HVTdL'></fieldset></dl></div>
                    1. <tfoot id='HVTdL'></tfoot>