com.github.TKnudsen.ComplexDataObject.model.io.json.JSONLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of complex-data-object Show documentation
Show all versions of complex-data-object Show documentation
A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.
The newest version!
package com.github.TKnudsen.ComplexDataObject.model.io.json;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.github.TKnudsen.ComplexDataObject.data.DataSchema;
import com.github.TKnudsen.ComplexDataObject.data.complexDataObject.ComplexDataObject;
import com.github.TKnudsen.ComplexDataObject.data.features.numericalData.NumericalFeatureVector;
import com.github.TKnudsen.ComplexDataObject.model.scoring.functions.AttributeScoringFunction;
/**
*
* Loads ComplexDataObjects from JSON. Brings its own ObjectMapper for speedup
* purposes, but can also be run with an external ObjectMapper parameter.
*
*
*
* Copyright: Copyright (c) 2017-2020
*
*
* @author Juergen Bernard
* @version 1.03
*/
public class JSONLoader {
private static ObjectMapper mapper = ObjectMapperFactory.getComplexDataObjectObjectMapper();
public static ComplexDataObject loadFromString(String json) {
ComplexDataObject complexDataObject;
try {
complexDataObject = mapper.readValue(json, ComplexDataObject.class);
return complexDataObject;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static ComplexDataObject loadFromFile(String file) {
ComplexDataObject complexDataObject;
try {
complexDataObject = mapper.readValue(new File(file), ComplexDataObject.class);
return complexDataObject;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static ComplexDataObject loadFromFile(String file, ObjectMapper mapper) {
ComplexDataObject complexDataObject;
try {
complexDataObject = mapper.readValue(new File(file), ComplexDataObject.class);
return complexDataObject;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static DataSchema loadDataSchemaFromString(String json) {
DataSchema dataSchema;
try {
dataSchema = mapper.readValue(json, DataSchema.class);
return dataSchema;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static DataSchema loadDataSchemaFromFile(String file) {
DataSchema dataSchema;
try {
dataSchema = mapper.readValue(new File(file), DataSchema.class);
return dataSchema;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static NumericalFeatureVector loadNumericalFeatureVectorFromString(String json) {
NumericalFeatureVector numericalFeatureVector;
try {
numericalFeatureVector = mapper.readValue(json, NumericalFeatureVector.class);
return numericalFeatureVector;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static NumericalFeatureVector loadNumericalFeatureVectorFromFile(String file) {
NumericalFeatureVector numericalFeatureVector;
try {
numericalFeatureVector = mapper.readValue(new File(file), NumericalFeatureVector.class);
return numericalFeatureVector;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static AttributeScoringFunction> loadAttributeScoringFunctionFromFile(String file) {
AttributeScoringFunction> function;
try {
function = mapper.readValue(new File(file), AttributeScoringFunction.class);
return function;
} catch (UnrecognizedPropertyException ue) {
ObjectMapper objectMapper = ObjectMapperFactory.getComplexDataObjectObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
function = objectMapper.readValue(new File(file), AttributeScoringFunction.class);
return function;
} catch (IOException e_) {
e_.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}