use of java.io.Writer in project camel by apache.
the class ApprovalRequestsTest method shouldSerializeAsXml.
@Test
public void shouldSerializeAsXml() {
final String xml = //
"<ProcessApprovalRequest>" + //
"<requests>" + //
"<actionType>Submit</actionType>" + //
"<contextActorId>005D00000015rZy</contextActorId>" + //
"<contextId>001D000000I8mIm</contextId>" + //
"<comments>this is a test 1</comments>" + //
"<nextApproverIds>005D00000015rY9</nextApproverIds>" + //
"<processDefinitionNameOrId>PTO_Request_Process</processDefinitionNameOrId>" + //
"<skipEntryCriteria>true</skipEntryCriteria>" + //
"</requests>" + //
"<requests>" + //
"<actionType>Submit</actionType>" + //
"<contextActorId>005D00000015rZy</contextActorId>" + //
"<contextId>001D000000I8dIm</contextId>" + //
"<comments>this is a test 2</comments>" + //
"<nextApproverIds>005D00000015xY9</nextApproverIds>" + //
"<processDefinitionNameOrId>PTO_Request_Process</processDefinitionNameOrId>" + //
"<skipEntryCriteria>true</skipEntryCriteria>" + //
"</requests>" + "</ProcessApprovalRequest>";
final XStream xStream = new XStream(new XppDriver(new NoNameCoder()) {
@Override
public HierarchicalStreamWriter createWriter(final Writer out) {
return new CompactWriter(out, getNameCoder());
}
});
xStream.ignoreUnknownElements();
XStreamUtils.addDefaultPermissions(xStream);
xStream.registerConverter(new DateTimeConverter());
xStream.processAnnotations(ApprovalRequests.class);
final String serialized = xStream.toXML(requests);
assertEquals("Approval requests should serialize as XML", xml, serialized);
}
use of java.io.Writer in project storm-json by rapportive-oss.
the class SimpleJSONSerializer method serialize.
/**
* Serialise a JSON object or array to the stream.
*
* @throws IllegalArgumentException if <tt>object</tt> is not a JSON
* object or array
* @throws IOException if there is an error writing to the stream.
*/
@Override
public void serialize(Object object, DataOutputStream stream) throws IOException {
final Writer writer = new OutputStreamWriter(stream, ENCODING);
if (object instanceof JSONObject) {
((JSONObject) object).writeJSONString(writer);
} else if (object instanceof JSONArray) {
((JSONArray) object).writeJSONString(writer);
} else {
throw new IllegalArgumentException("Unexpected class " + object.getClass().getCanonicalName());
}
writer.flush();
}
use of java.io.Writer in project retrofit by square.
the class GsonRequestBodyConverter method convert.
@Override
public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
adapter.write(jsonWriter, value);
jsonWriter.close();
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
use of java.io.Writer in project libgdx by libgdx.
the class FileDescriptor method writeString.
/** Writes the specified string to the file as UTF-8. Parent directories will be created if necessary.
* @param append If false, this file will be overwritten if it exists, otherwise it will be appended.
* @param charset May be null to use the default charset.
* @throw RuntimeException if this file handle represents a directory, if it is a {@link FileType#Classpath} or
* FileType#Internal file, or if it could not be written. */
public void writeString(String string, boolean append, String charset) {
Writer writer = null;
try {
writer = writer(append, charset);
writer.write(string);
} catch (Exception ex) {
throw new RuntimeException("Error writing file: " + file + " (" + type + ")", ex);
} finally {
try {
if (writer != null)
writer.close();
} catch (Exception ignored) {
}
}
}
use of java.io.Writer in project libgdx by libgdx.
the class FlameMain method saveEffect.
public void saveEffect(File file) {
Writer fileWriter = null;
try {
ParticleEffectLoader loader = (ParticleEffectLoader) assetManager.getLoader(ParticleEffect.class);
loader.save(effect, new ParticleEffectSaveParameter(new FileHandle(file.getAbsolutePath()), assetManager, particleSystem.getBatches()));
} catch (Exception ex) {
System.out.println("Error saving effect: " + file.getAbsolutePath());
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error saving effect.");
} finally {
StreamUtils.closeQuietly(fileWriter);
}
}
Aggregations