本文整理汇总了Java中org.yaml.snakeyaml.representer.Representer类的典型用法代码示例。如果您正苦于以下问题:Java Representer类的具体用法?Java Representer怎么用?Java Representer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Representer类属于org.yaml.snakeyaml.representer包,在下文中一共展示了Representer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadPackageMetadata
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
private PackageMetadata loadPackageMetadata(File file) {
// The Representer will not try to set the value in the YAML on the
// Java object if it isn't present on the object
Representer representer = new Representer();
representer.getPropertyUtils().setSkipMissingProperties(true);
Yaml yaml = new Yaml(new Constructor(PackageMetadata.class), representer);
String fileContents = null;
try {
fileContents = FileUtils.readFileToString(file);
}
catch (IOException e) {
e.printStackTrace();
}
PackageMetadata pkgMetadata = (PackageMetadata) yaml.load(fileContents);
return pkgMetadata;
}
示例2: initialValue
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
@Override
protected Yaml initialValue()
{
Representer representer = new Representer() {
{
representers.put(Configuration.class, new Represent() {
@Override
public Node representData(Object data)
{
return represent(((Configuration) data).self);
}
});
}
};
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
return new Yaml(new Constructor(), representer, options);
}
示例3: Yaml
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
/**
* Create Yaml instance. It is safe to create a few instances and use them
* in different Threads.
*
* @param constructor
* BaseConstructor to construct incoming documents
* @param representer
* Representer to emit outgoing objects
* @param dumperOptions
* DumperOptions to configure outgoing objects
* @param resolver
* Resolver to detect implicit type
*/
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
Resolver resolver) {
if (!constructor.isExplicitPropertyUtils()) {
constructor.setPropertyUtils(representer.getPropertyUtils());
} else if (!representer.isExplicitPropertyUtils()) {
representer.setPropertyUtils(constructor.getPropertyUtils());
}
this.constructor = constructor;
representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
representer.getPropertyUtils().setAllowReadOnlyProperties(
dumperOptions.isAllowReadOnlyProperties());
representer.setTimeZone(dumperOptions.getTimeZone());
this.representer = representer;
this.dumperOptions = dumperOptions;
this.resolver = resolver;
this.name = "Yaml:" + System.identityHashCode(this);
}
示例4: Yaml
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
/**
* Create Yaml instance. It is safe to create a few instances and use them
* in different Threads.
*
* @param constructor
* BaseConstructor to construct incoming documents
* @param representer
* Representer to emit outgoing objects
* @param dumperOptions
* DumperOptions to configure outgoing objects
* @param loadingConfig
* LoadingConfig to control load behavior
* @param resolver
* Resolver to detect implicit type
*/
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
LoaderOptions loadingConfig, Resolver resolver) {
if (!constructor.isExplicitPropertyUtils()) {
constructor.setPropertyUtils(representer.getPropertyUtils());
} else if (!representer.isExplicitPropertyUtils()) {
representer.setPropertyUtils(constructor.getPropertyUtils());
}
this.constructor = constructor;
this.constructor.setAllowDuplicateKeys(loadingConfig.isAllowDuplicateKeys());
if (dumperOptions.getIndent() <= dumperOptions.getIndicatorIndent()) {
throw new YAMLException("Indicator indent must be smaller then indent.");
}
representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
representer.getPropertyUtils()
.setAllowReadOnlyProperties(dumperOptions.isAllowReadOnlyProperties());
representer.setTimeZone(dumperOptions.getTimeZone());
this.representer = representer;
this.dumperOptions = dumperOptions;
this.loadingConfig = loadingConfig;
this.resolver = resolver;
this.name = "Yaml:" + System.identityHashCode(this);
}
示例5: newYaml
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
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);
}
示例6: testLocalTag
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
/**
* use wrapper with local tag
*/
public void testLocalTag() {
JavaBeanWithStaticState bean = new JavaBeanWithStaticState();
bean.setName("Bahrack");
bean.setAge(-47);
JavaBeanWithStaticState.setType("Type3");
JavaBeanWithStaticState.color = "Violet";
Representer repr = new Representer();
repr.addClassTag(Wrapper.class, new Tag("!mybean"));
Yaml yaml = new Yaml(repr);
String output = yaml.dump(new Wrapper(bean));
// System.out.println(output);
assertEquals("!mybean {age: -47, color: Violet, name: Bahrack, type: Type3}\n", output);
// parse back to instance
Constructor constr = new Constructor();
TypeDescription description = new TypeDescription(Wrapper.class, new Tag("!mybean"));
constr.addTypeDescription(description);
yaml = new Yaml(constr);
Wrapper wrapper = (Wrapper) yaml.load(output);
JavaBeanWithStaticState bean2 = wrapper.createBean();
assertEquals(-47, bean2.getAge());
assertEquals("Bahrack", bean2.getName());
}
示例7: testEmitWithTags
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
public void testEmitWithTags() {
TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
DumperOptions options = new DumperOptions();
options.setExplicitStart(true);
Representer repr = new Representer();
repr.addClassTag(TestObject.class, new Tag("!ruby/object:Test::Module::Object"));
repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1"));
repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2"));
Yaml yaml2 = new Yaml(repr, options);
String output = yaml2.dump(result);
// System.out.println(output);
assertTrue("Tags must be present.",
output.startsWith("--- !ruby/object:Test::Module::Object"));
assertTrue("Tags must be present: " + output,
output.contains("!ruby/object:Test::Module::Sub1"));
assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2"));
// parse back.
TestObject result2 = parseObject(output);
assertEquals(0, result2.getSub1().getAtt2());
assertEquals("MyString", result2.getSub2().getAtt1());
assertEquals(1, result2.getSub2().getAtt2().size());
assertEquals(12345, result2.getSub2().getAtt3());
}
示例8: testEmitWithTags2WithoutTagForParentJavabean
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
public void testEmitWithTags2WithoutTagForParentJavabean() {
TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
DumperOptions options = new DumperOptions();
options.setExplicitStart(true);
Representer repr = new Representer();
repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1"));
repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2"));
Yaml yaml2 = new Yaml(repr, options);
String output = yaml2.dump(result);
// System.out.println(output);
assertTrue("Tags must be present.",
output.startsWith("--- !!org.yaml.snakeyaml.ruby.TestObject"));
assertTrue("Tags must be present: " + output,
output.contains("!ruby/object:Test::Module::Sub1"));
assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2"));
// parse back.
TestObject result2 = parseObject(output);
assertEquals(0, result2.getSub1().getAtt2());
assertEquals("MyString", result2.getSub2().getAtt1());
assertEquals(1, result2.getSub2().getAtt2().size());
assertEquals(12345, result2.getSub2().getAtt3());
}
示例9: testABProperty
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
public void testABProperty() {
SuperSaverAccount supersaver = new SuperSaverAccount();
GeneralAccount generalAccount = new GeneralAccount();
CustomerAB_Property customerAB_property = new CustomerAB_Property();
ArrayList<Account> all = new ArrayList<Account>();
all.add(supersaver);
all.add(generalAccount);
ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
general.add(generalAccount);
general.add(supersaver);
customerAB_property.acc = generalAccount;
customerAB_property.bGeneral = general;
Constructor constructor = new Constructor();
Representer representer = new Representer();
Yaml yaml = new Yaml(constructor, representer);
String dump = yaml.dump(customerAB_property);
// System.out.println(dump);
CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump);
assertNotNull(parsed);
}
开发者ID:bmoliveira,项目名称:snake-yaml,代码行数:25,代码来源:PropOrderInfluenceWhenAliasedInGenericCollectionTest.java
示例10: checkQuotes
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
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"));
}
示例11: testErrorMessage
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
public void testErrorMessage() throws Exception {
BeanA1 b = new BeanA1();
b.setId(2l);
b.setName("name1");
Constructor c = new Constructor();
Representer r = new Representer();
PropertyUtils pu = new PropertyUtils();
c.setPropertyUtils(pu);
r.setPropertyUtils(pu);
pu.getProperties(BeanA1.class, BeanAccess.FIELD);
Yaml yaml = new Yaml(c, r);
// yaml.setBeanAccess(BeanAccess.FIELD);
String dump = yaml.dump(b);
BeanA1 b2 = (BeanA1) yaml.load(dump);
assertEquals(b.getId(), b2.getId());
assertEquals(b.getName(), b2.getName());
}
示例12: testDumpClassTag
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
public void testDumpClassTag() {
Car car = new Car();
car.setPlate("12-XP-F4");
List<Wheel> wheels = new ArrayList<Wheel>();
for (int i = 1; i < 6; i++) {
Wheel wheel = new Wheel();
wheel.setId(i);
wheels.add(wheel);
}
car.setWheels(wheels);
Representer representer = new Representer();
representer.addClassTag(Car.class, new Tag("!car"));
representer.addClassTag(Wheel.class, Tag.MAP);
Yaml yaml = new Yaml(representer);
String output = yaml.dump(car);
assertEquals(Util.getLocalResource("constructor/car-without-tags.yaml"), output);
}
示例13: testLoadClassTag
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
public void testLoadClassTag() {
Constructor constructor = new Constructor();
constructor.addTypeDescription(new TypeDescription(Car.class, "!car"));
Yaml yaml = new Yaml(constructor);
Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-without-tags.yaml"));
assertEquals("12-XP-F4", car.getPlate());
List<Wheel> wheels = car.getWheels();
assertNotNull(wheels);
assertEquals(5, wheels.size());
Wheel w1 = wheels.get(0);
assertEquals(1, w1.getId());
//
String carYaml1 = new Yaml().dump(car);
assertTrue(carYaml1.startsWith("!!org.yaml.snakeyaml.constructor.Car"));
//
Representer representer = new Representer();
representer.addClassTag(Car.class, new Tag("!car"));
yaml = new Yaml(representer);
String carYaml2 = yaml.dump(car);
assertEquals(Util.getLocalResource("constructor/car-without-tags.yaml"), carYaml2);
}
示例14: configYamlFormat
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
private static void configYamlFormat(final ToolBelt belt, final AppConfig config) {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(
"BLOCK".equalsIgnoreCase(config.getString("RD_YAML_FLOW", "BLOCK")) ?
DumperOptions.FlowStyle.BLOCK :
DumperOptions.FlowStyle.FLOW
);
dumperOptions.setPrettyFlow(config.getBool("RD_YAML_PRETTY", true));
Representer representer = new Representer();
representer.addClassTag(JobItem.class, Tag.MAP);
representer.addClassTag(ScheduledJobItem.class, Tag.MAP);
representer.addClassTag(DateInfo.class, Tag.MAP);
representer.addClassTag(Execution.class, Tag.MAP);
belt.formatter(new YamlFormatter(representer, dumperOptions));
belt.channels().infoEnabled(false);
belt.channels().warningEnabled(false);
belt.channels().errorEnabled(false);
}
示例15: initialValue
import org.yaml.snakeyaml.representer.Representer; //导入依赖的package包/类
@Override
protected Yaml initialValue()
{
Representer representer = new Representer()
{
{
representers.put( Configuration.class, new Represent()
{
@Override
public Node representData(Object data)
{
return represent( ( (Configuration) data ).self );
}
} );
}
};
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK );
return new Yaml( new Constructor(), representer, options );
}