Java Resource Path Get deserialize(Class parentOfResource, String resourcePath, Class targetClass)

Here you can find the source of deserialize(Class parentOfResource, String resourcePath, Class targetClass)

Description

deserialize

License

Open Source License

Declaration

public static <T> T deserialize(Class<?> parentOfResource, String resourcePath, Class<T> targetClass) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URL;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class Main {
    public static final ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T deserialize(Class<?> parentOfResource, String resourcePath, Class<T> targetClass) {
        URL resourceUrl = parentOfResource.getResource(resourcePath);
        requireNonNull(resourceUrl,/* ww w.  j  av  a  2 s. c  om*/
                format("resource '%s' of class '%s' not found", resourcePath, parentOfResource.getCanonicalName()));
        try {
            return objectMapper.readValue(resourceUrl, targetClass);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. classToResource(String className)
  2. existsResource(String pathName)
  3. getAbsolutePathFromResource(Class reference, String resource)
  4. getAbsoluteResource(String path)
  5. getAlternateResourceFile(final String resourcePath)