Java源码示例:org.yaml.snakeyaml.serializer.Serializer
示例1
public String toYaml() {
StringWriter writer = new StringWriter();
DumperOptions dumperOptions = new DumperOptions();
Serializer serializer = new Serializer(new Emitter(writer, dumperOptions),
new Resolver(), dumperOptions, Tag.MAP);
try {
serializer.open();
serializer.serialize(this);
serializer.close();
return writer.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例2
@Override
public SoyValue applyForJava(SoyValue value, List<SoyValue> args) {
Preconditions.checkArgument(
value instanceof PrimitiveData || value instanceof SanitizedContent,
"|yamlprimitive directive only supports primitive types");
Node node;
if (value instanceof BooleanData) {
node = representer.represent(value.booleanValue());
} else if (value instanceof FloatData) {
node = representer.represent(value.floatValue());
} else if (value instanceof IntegerData) {
node = representer.represent(value.integerValue());
} else {
node = representer.represent(value.coerceToString());
}
int indent = args.get(0).integerValue();
StringWriter writer = new StringWriter();
Serializer serializer = new Serializer(
new Emitter(writer, dumperOptions), resolver, dumperOptions, null);
try {
serializer.open();
serializer.serialize(node);
serializer.close();
return StringData.forValue(indentLines(writer.toString().trim(), indent));
} catch (IOException e) {
// Should not happen.
throw new RuntimeException(e);
}
}
示例3
@VisibleForTesting
@Restricted(NoExternalUse.class)
public static void serializeYamlNode(Node root, Writer writer) throws IOException {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(BLOCK);
options.setDefaultScalarStyle(PLAIN);
options.setSplitLines(true);
options.setPrettyFlow(true);
Serializer serializer = new Serializer(new Emitter(writer, options), new Resolver(),
options, null);
serializer.open();
serializer.serialize(root);
serializer.close();
}
示例4
private void dumpAll(Iterator<? extends Object> data, Writer output, Tag rootTag) {
Serializer serializer = new Serializer(new Emitter(output, dumperOptions), resolver,
dumperOptions, rootTag);
try {
serializer.open();
while (data.hasNext()) {
Node node = representer.represent(data.next());
serializer.serialize(node);
}
serializer.close();
} catch (java.io.IOException e) {
throw new YAMLException(e);
}
}
示例5
/**
* Serialize the representation tree into Events.
*
* @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
* @param data
* representation tree
* @return Event list
*/
public List<Event> serialize(Node data) {
SilentEmitter emitter = new SilentEmitter();
@SuppressWarnings("deprecation")
Serializer serializer = new Serializer(emitter, resolver, dumperOptions,
dumperOptions.getExplicitRoot());
try {
serializer.open();
serializer.serialize(data);
serializer.close();
} catch (java.io.IOException e) {
throw new YAMLException(e);
}
return emitter.getEvents();
}
示例6
private void dumpAll(Iterator<? extends Object> data, Writer output, Tag rootTag) {
Serializer serializer = new Serializer(new Emitter(output, dumperOptions), resolver,
dumperOptions, rootTag);
try {
serializer.open();
while (data.hasNext()) {
Node node = representer.represent(data.next());
serializer.serialize(node);
}
serializer.close();
} catch (IOException e) {
throw new YAMLException(e);
}
}
示例7
/**
* Serialize the representation tree into Events.
*
* @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
* @param data
* representation tree
* @return Event list
*/
public List<Event> serialize(Node data) {
SilentEmitter emitter = new SilentEmitter();
Serializer serializer = new Serializer(emitter, resolver, dumperOptions, null);
try {
serializer.open();
serializer.serialize(data);
serializer.close();
} catch (IOException e) {
throw new YAMLException(e);
}
return emitter.getEvents();
}
示例8
private void dumpAll(Iterator<? extends Object> data, Writer output, Tag rootTag) {
Serializer serializer = new Serializer(new Emitter(output, dumperOptions), resolver,
dumperOptions, rootTag);
try {
serializer.open();
while (data.hasNext()) {
Node node = representer.represent(data.next());
serializer.serialize(node);
}
serializer.close();
} catch (IOException e) {
throw new YAMLException(e);
}
}
示例9
/**
* Serialize the representation tree into Events.
*
* @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
* @param data
* representation tree
* @return Event list
*/
public List<Event> serialize(Node data) {
SilentEmitter emitter = new SilentEmitter();
Serializer serializer = new Serializer(emitter, resolver, dumperOptions, null);
try {
serializer.open();
serializer.serialize(data);
serializer.close();
} catch (IOException e) {
throw new YAMLException(e);
}
return emitter.getEvents();
}