Search in sources :

Example 1 with JsonSerializerException

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

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)1 JsonSerializerException (net.morimekta.providence.serializer.JsonSerializerException)1 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)1 Serializer (net.morimekta.providence.serializer.Serializer)1 TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)1