Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将groovy表示转储到YAML并避免使用未标记的节点?_Groovy_Yaml_Snakeyaml - Fatal编程技术网

如何将groovy表示转储到YAML并避免使用未标记的节点?

如何将groovy表示转储到YAML并避免使用未标记的节点?,groovy,yaml,snakeyaml,Groovy,Yaml,Snakeyaml,我想将以下结构转储到YAML文件: public class TestSuite { String name List testCases = [] } 其中测试用例列表为此类: class TestCase { String name String id } 我希望它看起来像这样: name: Carrier Handling and Traffic testCases: - name: Call setup by UE id: DCM00000001

我想将以下结构转储到YAML文件:

public class TestSuite {
    String name
    List testCases = []
}
其中测试用例列表为此类:

class TestCase {
    String name
    String id
}
我希望它看起来像这样:

name: Carrier Handling and Traffic
testCases:
- name: Call setup by UE
  id: DCM00000001
但结果是这样的:

name: Carrier Handling and Traffic
testCases:
- !!com.package.path.TestCase
  name: Call setup by UE
  id: DCM00000001

我想这与列表不是标记的数据结构这一事实有关,但我不知道如何获得测试用例的名称来表示对象。提示?

是否将
TestSuite
定义为:

public class TestSuite {
    String name
    List<TestCase> testCases = []
}

嗯,我真的不明白这里发生了什么。您是否重写默认的Representer以永远不输出元类的名称?难道没有更简单的方法吗?不是我能找到的。。。你能发布你是如何输出你在问题中显示的yaml的吗?正如我所说,我并没有真正使用SnakeyAml,我只是直接将其转储,而不使用Representer。我认为我误解了YAML本身的某些内容,但我似乎无法充分理解为什么它一开始看起来就是这样。@Fylke看起来像snakeyaml放入了一个,以便在反序列化YAML文件时可以将其转换回其原始类型。。。我必须使用定制的Representer,因为所有Groovy对象都有一个元类,在我的测试中,这也被放入YAML中(所以我的非MetaClassRepresenter过滤掉了它)啊,现在我开始理解了。这就是为什么当我只使用常规字符串列表而不是自定义Groovy类时,它会“起作用”。那么你的解决方案听起来是唯一明智的选择,谢谢!
@Grab( 'org.yaml:snakeyaml:1.10' )
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.representer.Representer
import java.beans.IntrospectionException
import org.yaml.snakeyaml.introspector.Property

public class TestSuite {
    String name
    List<TestCase> testCases = []
}

class TestCase {
    String name
    String id
}

class NonMetaClassRepresenter extends Representer {
  protected Set<Property> getProperties( Class<? extends Object> type ) throws IntrospectionException {
    super.getProperties( type ).findAll { it.name != 'metaClass' }
  }
}

TestSuite suite = new TestSuite( name:'Carrier Handling and Traffic' )
suite.testCases << new TestCase( name:'Call setup by UE', id:'DCM00000001' ) 

println new Yaml( new NonMetaClassRepresenter() ).dumpAsMap( suite )
name: Carrier Handling and Traffic
testCases:
- id: DCM00000001
  name: Call setup by UE