Search in sources :

Example 1 with JsonGenerator

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);
    }
}
Also used : JsonGenerator(net.viperfish.framework.serialization.JsonGenerator) TextIOStreamHandler(net.viperfish.framework.file.TextIOStreamHandler) IOException(java.io.IOException) IOFile(net.viperfish.framework.file.IOFile) File(java.io.File) IOFile(net.viperfish.framework.file.IOFile)

Example 2 with JsonGenerator

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();
}
Also used : JsonGenerator(net.viperfish.framework.serialization.JsonGenerator) TextIOStreamHandler(net.viperfish.framework.file.TextIOStreamHandler) Journal(net.viperfish.journal.framework.Journal) IOException(java.io.IOException) IOFile(net.viperfish.framework.file.IOFile) File(java.io.File) IOFile(net.viperfish.framework.file.IOFile) Test(org.junit.Test)

Example 3 with JsonGenerator

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);
    }
}
Also used : FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) JsonGenerator(net.viperfish.framework.serialization.JsonGenerator) IOException(java.io.IOException)

Example 4 with JsonGenerator

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);
    }
}
Also used : FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) JsonGenerator(net.viperfish.framework.serialization.JsonGenerator) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 JsonGenerator (net.viperfish.framework.serialization.JsonGenerator)4 File (java.io.File)2 IOFile (net.viperfish.framework.file.IOFile)2 TextIOStreamHandler (net.viperfish.framework.file.TextIOStreamHandler)2 FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)2 Journal (net.viperfish.journal.framework.Journal)1 Test (org.junit.Test)1