use of net.viperfish.framework.serialization.JsonGenerator in project vsDiaryWriter by shilongdai.
the class OperationTest method exportJournals.
private void exportJournals(List<Journal> src) {
try {
String exported = new JsonGenerator().toJson(src);
IOFile exportFile = new IOFile(new File("test.txt"), new TextIOStreamHandler());
exportFile.write(exported, StandardCharsets.UTF_16);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of net.viperfish.framework.serialization.JsonGenerator in project vsDiaryWriter by shilongdai.
the class OperationTest method testExportOperation.
@Test
public void testExportOperation() throws FailToSyncEntryException {
cleanUp();
addEntries(20, "toExport");
List<Journal> all = db.getAll();
for (Journal i : all) {
i.setId(null);
}
try {
String expectedOutput = new JsonGenerator().toJson(all.toArray(new Journal[1]));
new ExportJournalOperation("test.txt").execute();
IOFile exportFile = new IOFile(new File("test.txt"), new TextIOStreamHandler());
String actualOutput = exportFile.read(StandardCharsets.UTF_16);
Assert.assertEquals(expectedOutput, actualOutput);
} catch (IOException e) {
throw new RuntimeException(e);
}
cleanUp();
}
use of net.viperfish.framework.serialization.JsonGenerator in project vsDiaryWriter by shilongdai.
the class FileEntryDatabase method load.
/**
* load the data from the file in JSON format
*/
public synchronized void load() {
try {
String buf = file.read(StandardCharsets.UTF_16);
if (buf.length() == 0) {
return;
}
JsonGenerator generator = new JsonGenerator();
struct = generator.fromJson(FileMemoryStructure.class, buf);
} catch (IOException e) {
FailToSyncEntryException f = new FailToSyncEntryException("Cannot load entries from file:" + file.getFile() + " message:" + e.getMessage());
f.initCause(e);
throw new RuntimeException(f);
}
}
use of net.viperfish.framework.serialization.JsonGenerator in project vsDiaryWriter by shilongdai.
the class FileEntryDatabase method flush.
/**
* flush the data into the file in JSON format
*/
public synchronized void flush() {
JsonGenerator generator = new JsonGenerator();
try {
String toWrite = generator.toJson(struct);
file.write(toWrite, StandardCharsets.UTF_16);
} catch (IOException e) {
FailToSyncEntryException e1 = new FailToSyncEntryException("Cannot flush entries to file at:" + file.getFile() + " message:" + e.getMessage());
e1.initCause(e);
throw new RuntimeException(e1);
}
}
Aggregations