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;
}
}
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)));
}
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)));
}
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))));
}
}
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"));
}
}
Aggregations