【问题标题】:Snakeyaml : properties with generics extended mapsSnakeyaml : 具有泛型扩展映射的属性
【发布时间】:2023-04-10 04:33:02
【问题描述】:

关于snakeyaml 1.11 和yaml。

当我转储具有扩展通用映射属性的 bean 时,snakeyaml 崩溃。

这是一个扩展的通用地图:

  public static class MyStupidHash<T> extends java.util.HashMap<String, T>
  {    
  }

这是一个具有snakeyam 无法转储的属性的bean:

  public class FactoryOfStupid
  {
    public MyStupidHash<Integer> getStupid()
    {
      return new MyStupidHash<Integer>();
    }
  }

当我使用snakeyaml 转储FactoryOfStupid 对象时,我得到:

java.lang.ArrayIndexOutOfBoundsException: 1
    at org.yaml.snakeyaml.representer.Representer.checkGlobalTag(Representer.java:204)
    at org.yaml.snakeyaml.representer.Representer.representJavaBeanProperty(Representer.java:144)
    at org.yaml.snakeyaml.representer.Representer.representJavaBean(Representer.java:83)
    at org.yaml.snakeyaml.representer.Representer$RepresentJavaBean.representData(Representer.java:49)
    at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:109)
    at org.yaml.snakeyaml.representer.BaseRepresenter.represent(BaseRepresenter.java:65)
    at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:270)
    at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:261)
    at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:233)
    at org.yaml.snakeyaml.Yaml.dump(Yaml.java:209)

这里是所有的junit测试:

/*
 */
package unitaire;

import java.util.HashMap;
import org.junit.*;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

/**
 *
 * @author herve
 */
public class YamlRepresenterUnitTest
{  
  public YamlRepresenterUnitTest()
  {
  }

  @BeforeClass
  public static void setUpClass() throws Exception
  {
  }

  @AfterClass
  public static void tearDownClass() throws Exception
  {
  }

  @Before
  public void setUp()
  {
  }

  @After
  public void tearDown()
  {
  }

  @Test
  public void onMyStupidHash() throws Exception
  {
    MyStupidHash<Integer> shash;
    Yaml yaml;
    String txt;
    DumperOptions options;
    FactoryOfStupid fact;

    fact = new FactoryOfStupid();
    shash = fact.getStupid();
    shash.put("toto", new Integer(10));
    options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    yaml = new Yaml(options);
    txt = yaml.dump(fact);
System.out.println("txt="+txt);
  }

  public static class MyStupidHash<T> extends java.util.HashMap<String, T>
  {    
  }

  public class FactoryOfStupid
  {
    public MyStupidHash<Integer> getStupid()
    {
      return new MyStupidHash<Integer>();
    }
  }
}

snakeyaml 中是否有一个神奇的选项来转储这类东西?

谢谢。

【问题讨论】:

    标签: java yaml snakeyaml


    【解决方案1】:

    您可以关注进度in the SnakeYAML project

    【讨论】:

      猜你喜欢
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多