代码之家  ›  专栏  ›  技术社区  ›  The-Big-K

SnakeYAML:如何使用TypeDescription加载带有Set的yaml?

  •  0
  • The-Big-K  · 技术社区  · 7 年前

    我在YAML中使用的集合与此类似:

    # Explicitly typed set.
    baseball players: !!set
      ? Mark McGwire
      ? Sammy Sosa
      ? Ken Griffey
    # Flow style
    baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees }
    

    注册构造函数时如何显式设置要设置的类型?

    http://javadox.com/org.yaml/snakeyaml/1.13/org/yaml/snakeyaml/TypeDescription.html

     void   putListPropertyType(String property, Class<? extends Object> type)
              Specify that the property is a type-safe List.
     void   putMapPropertyType(String property, Class<? extends Object> key, Class<? extends Object> value)
              Specify that the property is a type-safe Map.
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   The-Big-K    7 年前

    我终于明白了。YAML集是具有空值的映射。因此,我最终将数据模型定义为具有集合成员,然后使用putMapPropertyType API注册绑定。

    Class Config {
       Set<String> players;
    }
    
    
    // in the YAML parsing code block :
    typeDef.putMapPropertyType("players", String.class, Object.class /* Value type doesnt matter */ );