com.regnosys.rosetta.common.serialisation.JsonDataLoaderUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rosetta-common Show documentation
Show all versions of rosetta-common Show documentation
Rune Common is a java library that is utilised by Rosetta Code Generators and models expressed in the Rosetta DSL.
package com.regnosys.rosetta.common.serialisation;
/*-
* ==============
* Rune Common
* ==============
* Copyright (C) 2018 - 2024 REGnosys
* ==============
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ==============
*/
import com.fasterxml.jackson.databind.ObjectMapper;
import com.regnosys.rosetta.common.util.UrlUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.List;
import java.util.Optional;
public class JsonDataLoaderUtil {
public static U readType(Class type, ObjectMapper rosettaObjectMapper, URL url) {
try {
return rosettaObjectMapper.readValue(UrlUtils.openURL(url), type);
} catch (IOException e) {
throw new RuntimeException(url + " cannot be serialised to " + type, e);
}
}
public static U readType(Class type, ObjectMapper rosettaObjectMapper, String json) {
try {
return rosettaObjectMapper.readValue(json, type);
} catch (IOException e) {
throw new RuntimeException("JSON cannot be serialised to " + type + "[" + json + "]", e);
}
}
public static List readTypeList(Class type, ObjectMapper rosettaObjectMapper, URL url) {
try {
return rosettaObjectMapper.readValue(UrlUtils.openURL(url), rosettaObjectMapper.getTypeFactory().constructCollectionType(List.class, type));
} catch (IOException e) {
throw new RuntimeException(url + " cannot be serialised to list of " + type, e);
}
}
public static List readTypeList(Class type, ObjectMapper rosettaObjectMapper, Reader input) {
try {
return rosettaObjectMapper.readValue(input, rosettaObjectMapper.getTypeFactory().constructCollectionType(List.class, type));
} catch (IOException e) {
throw new RuntimeException(input + " cannot be serialised to list of " + type, e);
}
}
public static U fromObject(Object obj, Class type, ObjectMapper rosettaObjectMapper) {
try {
return readType(type, rosettaObjectMapper, rosettaObjectMapper.writeValueAsString(obj));
} catch (IOException e) {
throw new RuntimeException(obj.getClass() + " cannot be serialised to " + type + "[" + obj.toString() + "]", e);
}
}
public static Optional openURL(URL descriptorUrl) {
try {
return Optional.of(UrlUtils.openURL(descriptorUrl));
} catch (FileNotFoundException e) {
return Optional.empty();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static Class> loadClass(String type, ClassLoader classLoader) {
try {
return classLoader.loadClass(type);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Could not load class for type " + type);
}
}
}