use of net.morimekta.providence.serializer.JsonSerializerException 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;
}
}
Aggregations