Search in sources :

Example 11 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class ResourceConfigSupplier method loadInternal.

private static <Message extends PMessage<Message, Field>, Field extends PField> Message loadInternal(String resourceName, PMessageDescriptor<Message, Field> descriptor) throws ProvidenceConfigException {
    int lastDot = resourceName.lastIndexOf(".");
    if (lastDot < 1) {
        throw new ProvidenceConfigException("No file ending, or no resource file name: " + resourceName);
    }
    int lastSlash = resourceName.lastIndexOf("/");
    String fileName = resourceName;
    if (lastSlash >= 0) {
        fileName = resourceName.substring(lastSlash + 1);
    }
    String suffix = resourceName.substring(lastDot).toLowerCase(Locale.US);
    Serializer serializer;
    switch(suffix) {
        case ".jsn":
        case ".json":
            serializer = new JsonSerializer();
            break;
        case ".cfg":
        case ".cnf":
        case ".config":
        case ".pvd":
        case ".providence":
            serializer = new PrettySerializer().config();
            break;
        // TODO: Add YAML serializer to the file options. Could be a wrapper around SnakeYAML.
        default:
            throw new ProvidenceConfigException(String.format(Locale.US, "Unrecognized resource config type: %s (%s)", suffix, resourceName));
    }
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    InputStream in = classLoader.getResourceAsStream(resourceName);
    if (in == null) {
        in = ResourceConfigSupplier.class.getResourceAsStream(resourceName);
        if (in == null) {
            throw new ProvidenceConfigException("No such config resource: " + resourceName);
        }
    }
    try {
        try (InputStream bin = new BufferedInputStream(in)) {
            return serializer.deserialize(bin, descriptor);
        } catch (TokenizerException te) {
            throw new ProvidenceConfigException(te);
        } catch (JsonSerializerException se) {
            throw new ProvidenceConfigException(se);
        } catch (IOException e) {
            throw new ProvidenceConfigException(e, "Unknown serializer exception: " + e.getMessage());
        }
    } catch (ProvidenceConfigException pce) {
        pce.setFile(fileName);
        throw pce;
    }
}
Also used : JsonSerializerException(net.morimekta.providence.serializer.JsonSerializerException) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer)

Example 12 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class MessageStreamsTest method testToStream.

@Test
public void testToStream() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int size = list.stream().collect(MessageCollectors.toStream(baos, new JsonSerializer()));
    assertThat(baos.size(), is(size));
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    List<CompactFields> out = MessageStreams.stream(bais, new JsonSerializer(), CompactFields.kDescriptor).collect(Collectors.toList());
    assertThat(out, is(equalTo(list)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) Test(org.junit.Test)

Example 13 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class FileMessageRWTest method testService.

@Test
public void testService() throws IOException {
    String content = "[\"calculate\",\"call\",44,{\"op\":{\"operator\":\"ADD\",\"operands\":[]}}]";
    File test = writeContentTo(content, tmp.newFile());
    PServiceCall<Operation, Operation._Field> call = null;
    try (FileMessageReader reader = new FileMessageReader(test, new JsonSerializer())) {
        call = reader.read(Calculator.kDescriptor);
    }
    File result = tmp.newFile();
    try (FileMessageWriter writer = new FileMessageWriter(result, new JsonSerializer().named())) {
        writer.write(call);
    }
    assertThat(new String(Files.readAllBytes(result.toPath()), StandardCharsets.UTF_8), is(equalTo(content)));
}
Also used : Operation(net.morimekta.test.providence.core.calculator.Operation) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) File(java.io.File) Test(org.junit.Test)

Example 14 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class FileMessageRWTest method testReadable.

@Test
public void testReadable() throws IOException {
    File test = tmp.newFile();
    try (FileMessageWriter writer = new FileMessageWriter(test, new JsonSerializer())) {
        writer.write(m1);
        writer.separator();
        writer.write(m2);
    }
    try (FileMessageReader reader = new FileMessageReader(test, new JsonSerializer())) {
        assertThat(m1, is(equalTo(reader.read(CompactFields.kDescriptor))));
        assertThat(m2, is(equalTo(reader.read(OptionalFields.kDescriptor))));
    }
}
Also used : JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) File(java.io.File) Test(org.junit.Test)

Example 15 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class MessageProgramParserTest method testFail.

@Test
public void testFail() throws IOException {
    MessageProgramParser parser = new MessageProgramParser(new JsonSerializer());
    ByteArrayInputStream in = new ByteArrayInputStream(("{\n" + "  \"namespaces\": {\n" + "    \"java\": \"net.morimekta.providence\"\n" + "  },\n" + "}").getBytes(StandardCharsets.UTF_8));
    try {
        parser.parse(in, new File("test.json"), ImmutableSet.of());
        fail("no exception");
    } catch (ParseException e) {
        assertThat(e.getMessage(), is("Failed to deserialize definition file test.json"));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) File(java.io.File) Test(org.junit.Test)

Aggregations

JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)16 Test (org.junit.Test)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 File (java.io.File)4 IOException (java.io.IOException)3 SizeBasedRollingPolicy (net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy)2 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)2 CompactFields (net.morimekta.test.providence.core.CompactFields)2 Operation (net.morimekta.test.providence.core.calculator.Operation)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Fairy (io.codearte.jfairy.Fairy)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Random (java.util.Random)1