protected Node representSequence(Tag tag, Iterable<?> sequence, Boolean flowStyle) { int size = 10;// default for ArrayList if (sequence instanceof List<?>) { size = ((List<?>) sequence).size(); } List<Node> value = new ArrayList<Node>(size); SequenceNode node = new SequenceNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Object item : sequence) { Node nodeItem = representData(item); if (!(nodeItem instanceof ScalarNode && ((ScalarNode) nodeItem).getStyle() == null)) { bestStyle = false; } value.add(nodeItem); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
protected Node representMapping(Tag tag, Map<?, ?> mapping, Boolean flowStyle) { List<NodeTuple> value = new ArrayList<NodeTuple>(mapping.size()); MappingNode node = new MappingNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Map.Entry<?, ?> entry : mapping.entrySet()) { Node nodeKey = representData(entry.getKey()); Node nodeValue = representData(entry.getValue()); if (!(nodeKey instanceof ScalarNode && ((ScalarNode) nodeKey).getStyle() == null)) { bestStyle = false; } if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) { bestStyle = false; } value.add(new NodeTuple(nodeKey, nodeValue)); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
public YamlSnakeYaml() { // Representer ExtensibleRepresenter representer = new ExtensibleRepresenter(); // Install Java / Apache Cassandra serializers addDefaultSerializers(representer); // Install MongoDB / BSON serializers tryToAddSerializers("io.datatree.dom.adapters.YamlSnakeYamlBsonSerializers", representer); // Create flow-style YAML mapper DumperOptions optionsNormal = new DumperOptions(); optionsNormal.setDefaultFlowStyle(FlowStyle.FLOW); mapper = new Yaml(representer, optionsNormal); // Create "pretty" YAML mapper DumperOptions optionsPretty = new DumperOptions(); optionsPretty.setDefaultFlowStyle(FlowStyle.BLOCK); prettyMapper = new Yaml(representer, optionsPretty); }
public static Yaml newYaml() { PropertyUtils propertyUtils = new AdvancedPropertyUtils(); propertyUtils.setSkipMissingProperties(true); Constructor constructor = new Constructor(Federations.class); TypeDescription federationDescription = new TypeDescription(Federations.class); federationDescription.putListPropertyType("federatedMetaStores", FederatedMetaStore.class); constructor.addTypeDescription(federationDescription); constructor.setPropertyUtils(propertyUtils); Representer representer = new AdvancedRepresenter(); representer.setPropertyUtils(new FieldOrderPropertyUtils()); representer.addClassTag(Federations.class, Tag.MAP); representer.addClassTag(AbstractMetaStore.class, Tag.MAP); representer.addClassTag(WaggleDanceConfiguration.class, Tag.MAP); representer.addClassTag(YamlStorageConfiguration.class, Tag.MAP); representer.addClassTag(GraphiteConfiguration.class, Tag.MAP); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setIndent(2); dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK); return new Yaml(constructor, representer, dumperOptions); }
/** * !!timestamp must be used, without it the implicit tag will be ignored * because 'date' is the JavaBean property. * * Since the timestamp contains ':' character it cannot use plain scalar * style in the FLOW mapping style. Emitter suggests single quoted scalar * style and that is why the explicit '!!timestamp' is present in the YAML * document. * * @see <a href="http://code.google.com/p/snakeyaml/issues/detail?id=128"></a> * */ public void testLoadBeanWithAutoFlow() { MyBean bean = new MyBean(); bean.setId("id123"); DateTime etalon = new DateTime(timestamp, DateTimeZone.UTC); bean.setDate(etalon); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.AUTO); Yaml dumper = new Yaml(new JodaTimeRepresenter(), options); String doc = dumper.dump(bean); // System.out.println(doc); assertEquals( "!!examples.jodatime.MyBean {date: !!timestamp '2001-09-09T01:46:40Z', id: id123}\n", doc); Yaml loader = new Yaml(new JodaTimeImplicitContructor()); MyBean parsed = (MyBean) loader.load(doc); assertEquals(etalon, parsed.getDate()); }
public void testLoadDefaultJavaSqlTimestamp() { JavaBeanWithSqlTimestamp javaBeanToDump = new JavaBeanWithSqlTimestamp(); Timestamp stamp = new Timestamp(1000000000000L); javaBeanToDump.setTimestamp(stamp); Date date = new Date(1001376000000L); javaBeanToDump.setDate(date); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.BLOCK); Yaml yaml = new Yaml(options); String dumpStr = yaml.dump(javaBeanToDump); assertEquals( "!!org.yaml.snakeyaml.JavaBeanWithSqlTimestamp\ndate: 2001-09-25T00:00:00Z\ntimestamp: 2001-09-09T01:46:40Z\n", dumpStr); Yaml loader = new Yaml(); JavaBeanWithSqlTimestamp javaBeanToLoad = loader.loadAs(dumpStr, JavaBeanWithSqlTimestamp.class); assertEquals(stamp, javaBeanToLoad.getTimestamp()); assertEquals(date, javaBeanToLoad.getDate()); }
private void checkQuotes(boolean isBlock, String expectation) { DumperOptions options = new DumperOptions(); options.setIndent(4); if (isBlock) { options.setDefaultFlowStyle(FlowStyle.BLOCK); } Representer representer = new Representer(); Yaml yaml = new Yaml(new SafeConstructor(), representer, options); LinkedHashMap<String, Object> lvl1 = new LinkedHashMap<String, Object>(); lvl1.put("steak:cow", "11"); LinkedHashMap<String, Object> root = new LinkedHashMap<String, Object>(); root.put("cows", lvl1); String output = yaml.dump(root); assertEquals(expectation + "\n", output); // parse the value back @SuppressWarnings("unchecked") Map<String, Object> cows = (Map<String, Object>) yaml.load(output); @SuppressWarnings("unchecked") Map<String, String> cow = (Map<String, String>) cows.get("cows"); assertEquals("11", cow.get("steak:cow")); }
public void testNoRootTag() { CarWithWheel car1 = new CarWithWheel(); car1.setPlate("12-XP-F4"); Wheel wheel = new Wheel(); wheel.setId(2); car1.setWheel(wheel); Map<String, Integer> map = new HashMap<String, Integer>(); map.put("id", 3); car1.setMap(map); car1.setYear("2008"); String carYaml1 = new Yaml().dumpAs(car1, Tag.MAP, FlowStyle.AUTO); assertEquals(Util.getLocalResource("constructor/car-without-root-tag.yaml"), carYaml1); // Constructor contructor = new Constructor(CarWithWheel.class); CarWithWheel car2 = (CarWithWheel) new Yaml(contructor).load(carYaml1); String carYaml2 = new Yaml().dumpAs(car2, Tag.MAP, FlowStyle.AUTO); assertEquals(carYaml1, carYaml2); }
public void testSplitLineExpectFirstFlowSequenceItem() { DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); options.setWidth(8); Yaml yaml; String output; Map<String, Object> map = new TreeMap<String, Object>(); map.put("12345", Arrays.asList("1111111111")); // Split lines enabled (default) yaml = new Yaml(options); output = yaml.dump(map); assertEquals("{\"12345\": [\n \"1111111111\"]}\n", output); // Split lines disabled options.setSplitLines(false); assertFalse(options.getSplitLines()); yaml = new Yaml(options); output = yaml.dump(map); assertEquals("{\"12345\": [\"1111111111\"]}\n", output); }
public void testSplitLineExpectFlowSequenceItem() { DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); options.setWidth(8); Yaml yaml; String output; // Split lines enabled (default) yaml = new Yaml(options); output = yaml.dump(Arrays.asList("1111111111", "2222222222")); assertEquals("[\"1111111111\",\n \"2222222222\"]\n", output); output = yaml.dump(Arrays.asList("1", "2")); assertEquals("[\"1\", \"2\"]\n", output); // Split lines disabled options.setSplitLines(false); assertFalse(options.getSplitLines()); yaml = new Yaml(options); output = yaml.dump(Arrays.asList("1111111111", "2222222222")); assertEquals("[\"1111111111\", \"2222222222\"]\n", output); output = yaml.dump(Arrays.asList("1", "2")); assertEquals("[\"1\", \"2\"]\n", output); }
public void testWriteMultiLineList() { String one = "first\nsecond\nthird"; String two = "one\ntwo\nthree\n"; byte[] binary = { 8, 14, 15, 10, 126, 32, 65, 65, 65 }; List<Object> list = new ArrayList<Object>(2); list.add(one); list.add(two); list.add(binary); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.BLOCK); Yaml yaml = new Yaml(options); String output = yaml.dump(list); // System.out.println(output); String etalon = "- |-\n first\n second\n third\n- |\n one\n two\n three\n- !!binary |-\n CA4PCn4gQUFB\n"; assertEquals(etalon, output); @SuppressWarnings("unchecked") List<Object> parsed = (List<Object>) yaml.load(etalon); assertEquals(3, parsed.size()); assertEquals(one, parsed.get(0)); assertEquals(two, parsed.get(1)); assertEquals(new String(binary), new String((byte[]) parsed.get(2))); }
protected final String dumpMap(LinkedHashMap<Object, Object> toStore, String header) { DumperOptions dumperOptions = new DumperOptions(); toStore = cleanMap(toStore); dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK); dumperOptions.setAllowUnicode(true); Yaml rl = new Yaml(dumperOptions); StringWriter stringWriter = new StringWriter(); BufferedWriter ws = new BufferedWriter(stringWriter); try { ws.write(header); ws.newLine(); rl.dump(toStore, ws); return stringWriter.toString().replaceAll(NOVALUE, ""); } catch (Exception e) { throw new RuntimeException(e); } }
/** * !!timestamp must be used, without it the implicit tag will be ignored * because 'date' is the JavaBean property. * * Since the timestamp contains ':' character it cannot use plain scalar * style in the FLOW mapping style. Emitter suggests single quoted scalar * style and that is why the explicit '!!timestamp' is present in the YAML * document. * * @see http://code.google.com/p/snakeyaml/issues/detail?id=128 * */ public void testLoadBeanWithAutoFlow() { MyBean bean = new MyBean(); bean.setId("id123"); DateTime etalon = new DateTime(timestamp, DateTimeZone.UTC); bean.setDate(etalon); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.AUTO); Yaml dumper = new Yaml(new JodaTimeRepresenter(), options); String doc = dumper.dump(bean); // System.out.println(doc); assertEquals( "!!examples.jodatime.MyBean {date: !!timestamp '2001-09-09T01:46:40Z', id: id123}\n", doc); Yaml loader = new Yaml(new JodaTimeImplicitContructor()); MyBean parsed = (MyBean) loader.load(doc); assertEquals(etalon, parsed.getDate()); }
protected Node representSequence(Tag tag, Iterable<? extends Object> sequence, Boolean flowStyle) { int size = 10;// default for ArrayList if (sequence instanceof List<?>) { size = ((List<?>) sequence).size(); } List<Node> value = new ArrayList<Node>(size); SequenceNode node = new SequenceNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Object item : sequence) { Node nodeItem = representData(item); if (!((nodeItem instanceof ScalarNode && ((ScalarNode) nodeItem).getStyle() == null))) { bestStyle = false; } value.add(nodeItem); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
protected Node representMapping(Tag tag, Map<? extends Object, Object> mapping, Boolean flowStyle) { List<NodeTuple> value = new ArrayList<NodeTuple>(mapping.size()); MappingNode node = new MappingNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Map.Entry<? extends Object, Object> entry : mapping.entrySet()) { Node nodeKey = representData(entry.getKey()); Node nodeValue = representData(entry.getValue()); if (!((nodeKey instanceof ScalarNode && ((ScalarNode) nodeKey).getStyle() == null))) { bestStyle = false; } if (!((nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null))) { bestStyle = false; } value.add(new NodeTuple(nodeKey, nodeValue)); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
protected Node representSequence(final Tag tag, final Iterable<?> sequence, final Boolean flowStyle) { int size = 10;// default for ArrayList if (sequence instanceof List<?>) { size = ((List<?>) sequence).size(); } List<Node> value = new ArrayList<Node>(size); SequenceNode node = new SequenceNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Object item : sequence) { Node nodeItem = representData(item); if (!((nodeItem instanceof ScalarNode && ((ScalarNode) nodeItem).getStyle() == null))) { bestStyle = false; } value.add(nodeItem); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
protected Node representMapping(final Tag tag, final Map<?, ?> mapping, final Boolean flowStyle) { List<NodeTuple> value = new ArrayList<NodeTuple>(mapping.size()); MappingNode node = new MappingNode(tag, value, flowStyle); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Map.Entry<?, ?> entry : mapping.entrySet()) { Node nodeKey = representData(entry.getKey()); Node nodeValue = representData(entry.getValue()); if (!((nodeKey instanceof ScalarNode && ((ScalarNode) nodeKey).getStyle() == null))) { bestStyle = false; } if (!((nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null))) { bestStyle = false; } value.add(new NodeTuple(nodeKey, nodeValue)); } if (flowStyle == null) { if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } } return node; }
/** * Create a {@link MappingNode} from the specified entries and tag. * * @param tag the tag * @param mapping the entries * * @return the mapping node */ private MappingNode delegate(Tag tag, Iterable<Entry<YamlNode, YamlNode>> mapping) { List<NodeTuple> value = new LinkedList<>(); MappingNode node = new MappingNode(tag, value, null); representedObjects.put(objectToRepresent, node); boolean bestStyle = true; for (Map.Entry<?, ?> entry : mapping) { Node nodeKey = representData(entry.getKey()); Node nodeValue = representData(entry.getValue()); bestStyle = bestStyle(nodeKey, bestStyle); bestStyle = bestStyle(nodeValue, bestStyle); value.add(new NodeTuple(nodeKey, nodeValue)); } if (getDefaultFlowStyle() != FlowStyle.AUTO) { node.setFlowStyle(getDefaultFlowStyle().getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } return node; }
/** * !!timestamp must be used, without it the implicit tag will be ignored * because 'date' is the JavaBean property. * * Since the timestamp contains ':' character it cannot use plain scalar * style in the FLOW mapping style. Emitter suggests single quoted scalar * style and that is why the explicit '!!timestamp' is present in the YAML * document. * * @see http://code.google.com/p/snakeyaml/issues/detail?id=128 * */ public void testLoadBeanWithAutoFlow() { MyBean bean = new MyBean(); bean.setId("id123"); DateTime etalon = new DateTime(timestamp, DateTimeZone.UTC); bean.setDate(etalon); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.AUTO); Yaml dumper = new Yaml(new JodaTimeRepresenter(), options); String doc = dumper.dump(bean); // System.out.println(doc); assertEquals("!!examples.jodatime.MyBean {date: !!timestamp '2001-09-09T01:46:40Z', id: id123}\n", doc); Yaml loader = new Yaml(new JodaTimeImplicitContructor()); MyBean parsed = (MyBean) loader.load(doc); assertEquals(etalon, parsed.getDate()); }
public void save() throws IOException { FileWriter writer = new FileWriter(getConfigFile()); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.AUTO); options.setIndent(4); Representer representer = new Representer(); representer.getPropertyUtils().setBeanAccess(BeanAccess.DEFAULT); new Yaml(options).dump(this, writer); }
/** * Tag logic:<br/> * - explicit root tag is set in serializer <br/> * - if there is a predefined class tag it is used<br/> * - a global tag with class name is always used as tag. The JavaBean parent * of the specified JavaBean may set another tag (tag:yaml.org,2002:map) * when the property class is the same as runtime class * * @param properties * JavaBean getters * @param javaBean * instance for Node * @return Node to get serialized */ protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) { List<NodeTuple> value = new ArrayList<NodeTuple>(properties.size()); Tag tag; Tag customTag = classTags.get(javaBean.getClass()); tag = customTag != null ? customTag : new Tag(javaBean.getClass()); // flow style will be chosen by BaseRepresenter MappingNode node = new MappingNode(tag, value, null); representedObjects.put(javaBean, node); boolean bestStyle = true; for (Property property : properties) { Object memberValue = property.get(javaBean); Tag customPropertyTag = memberValue == null ? null : classTags.get(memberValue .getClass()); NodeTuple tuple = representJavaBeanProperty(javaBean, property, memberValue, customPropertyTag); if (tuple == null) { continue; } if (((ScalarNode) tuple.getKeyNode()).getStyle() != null) { bestStyle = false; } Node nodeValue = tuple.getValueNode(); if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) { bestStyle = false; } value.add(tuple); } if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } return node; }
/** * Tag logic: * - explicit root tag is set in serializer * - if there is a predefined class tag it is used * - a global tag with class name is always used as tag. The JavaBean parent * of the specified JavaBean may set another tag (tag:yaml.org,2002:map) * when the property class is the same as runtime class * * @param properties * JavaBean getters * @param javaBean * instance for Node * @return Node to get serialized */ protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) { List<NodeTuple> value = new ArrayList<NodeTuple>(properties.size()); Tag tag; Tag customTag = classTags.get(javaBean.getClass()); tag = customTag != null ? customTag : new Tag(javaBean.getClass()); // flow style will be chosen by BaseRepresenter MappingNode node = new MappingNode(tag, value, null); representedObjects.put(javaBean, node); boolean bestStyle = true; for (Property property : properties) { Object memberValue = property.get(javaBean); Tag customPropertyTag = memberValue == null ? null : classTags.get(memberValue .getClass()); NodeTuple tuple = representJavaBeanProperty(javaBean, property, memberValue, customPropertyTag); if (tuple == null) { continue; } if (((ScalarNode) tuple.getKeyNode()).getStyle() != null) { bestStyle = false; } Node nodeValue = tuple.getValueNode(); if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) { bestStyle = false; } value.add(tuple); } if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } return node; }
@Override protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) { List<NodeTuple> value = new ArrayList<>(properties.size()); Tag tag; Tag customTag = classTags.get(javaBean.getClass()); tag = customTag != null ? customTag : new Tag(javaBean.getClass()); MappingNode node = new MappingNode(tag, value, null); representedObjects.put(javaBean, node); boolean bestStyle = true; List<Property> orderProperties = new ArrayList<>(properties); orderProperties.sort(sorter); for (Property property : orderProperties) { Object memberValue = property.get(javaBean); Tag customPropertyTag = memberValue == null ? null : classTags.get(memberValue.getClass()); NodeTuple tuple = representJavaBeanProperty(javaBean, property, memberValue, customPropertyTag); if (tuple == null) { continue; } if (((ScalarNode) tuple.getKeyNode()).getStyle() != null) { bestStyle = false; } org.yaml.snakeyaml.nodes.Node nodeValue = tuple.getValueNode(); if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) { bestStyle = false; } value.add(tuple); } if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } return node; }
/** * Tag logic:<br> * - explicit root tag is set in serializer <br> * - if there is a predefined class tag it is used<br> * - a global tag with class name is always used as tag. The JavaBean parent * of the specified JavaBean may set another tag (tag:yaml.org,2002:map) * when the property class is the same as runtime class * * @param properties * JavaBean getters * @param javaBean * instance for Node * @return Node to get serialized */ protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) { List<NodeTuple> value = new ArrayList<NodeTuple>(properties.size()); Tag tag; Tag customTag = classTags.get(javaBean.getClass()); tag = customTag != null ? customTag : new Tag(javaBean.getClass()); // flow style will be chosen by BaseRepresenter MappingNode node = new MappingNode(tag, value, null); representedObjects.put(javaBean, node); boolean bestStyle = true; for (Property property : properties) { Object memberValue = property.get(javaBean); Tag customPropertyTag = memberValue == null ? null : classTags.get(memberValue .getClass()); NodeTuple tuple = representJavaBeanProperty(javaBean, property, memberValue, customPropertyTag); if (tuple == null) { continue; } if (((ScalarNode) tuple.getKeyNode()).getStyle() != null) { bestStyle = false; } Node nodeValue = tuple.getValueNode(); if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).getStyle() == null)) { bestStyle = false; } value.add(tuple); } if (defaultFlowStyle != FlowStyle.AUTO) { node.setFlowStyle(defaultFlowStyle.getStyleBoolean()); } else { node.setFlowStyle(bestStyle); } return node; }
public void testNoChildrenPretty() { Human father = new Human(); father.setName("Father"); father.setBirthday(new Date(1000000000)); father.setBirthPlace("Leningrad"); father.setBankAccountOwner(father); Human mother = new Human(); mother.setName("Mother"); mother.setBirthday(new Date(100000000000L)); mother.setBirthPlace("Saint-Petersburg"); father.setPartner(mother); mother.setPartner(father); mother.setBankAccountOwner(father); DumperOptions options = new DumperOptions(); options.setPrettyFlow(true); options.setDefaultFlowStyle(FlowStyle.FLOW); Yaml yaml = new Yaml(options); String output = yaml.dump(father); String etalon = Util.getLocalResource("recursive/no-children-1-pretty.yaml"); assertEquals(etalon, output); // Human father2 = (Human) yaml.load(output); assertNotNull(father2); assertEquals("Father", father2.getName()); assertEquals("Mother", father2.getPartner().getName()); assertEquals("Father", father2.getBankAccountOwner().getName()); assertSame(father2, father2.getBankAccountOwner()); }
public void testDumpBlock() { DumperOptions options = new DumperOptions(); options.setAllowReadOnlyProperties(true); options.setDefaultFlowStyle(FlowStyle.BLOCK); Yaml yaml = new Yaml(new SetRepresenter(), options); String output = yaml.dump(createBlog()); // System.out.println(output); assertEquals(Util.getLocalResource("issues/issue73-dump8.txt"), output); // check(output); }
public void testNoTags() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); B data = createB(); String dump = yaml2dump.dumpAs(data, Tag.MAP, FlowStyle.AUTO); // System.out.println(dump); assertEquals("meta:\n- [whatever]\n- [something, something else]\n", dump); // Constructor constr = new Constructor(B.class); Yaml yaml2load = new Yaml(constr); yaml2load.setBeanAccess(BeanAccess.FIELD); B loaded = (B) yaml2load.load(dump); Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); }
public void testDumperNullStyle2() { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(FlowStyle.BLOCK); Yaml yaml = new Yaml(options); Bean124 bean = new Bean124(); String output1 = yaml.dumpAs(bean, new Tag("!!foo2.bar2"), null); assertEquals("!!foo2.bar2\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1); }