All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.timefold.solver.jackson.impl.domain.solution.JacksonSolutionFileIO Maven / Gradle / Ivy

Go to download

Timefold solves planning problems. This lightweight, embeddable planning engine implements powerful and scalable algorithms to optimize business resource scheduling and planning. This module contains the Jackson integration.

The newest version!
package ai.timefold.solver.jackson.impl.domain.solution;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import ai.timefold.solver.core.api.domain.solution.PlanningSolution;
import ai.timefold.solver.persistence.common.api.domain.solution.SolutionFileIO;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *
 * @param  the solution type, the class with the {@link PlanningSolution} annotation
 */
public class JacksonSolutionFileIO implements SolutionFileIO {

    private final Class clazz;
    private final ObjectMapper mapper;

    public JacksonSolutionFileIO(Class clazz) {
        this(clazz, new ObjectMapper());
    }

    public JacksonSolutionFileIO(Class clazz, ObjectMapper mapper) {
        this.clazz = clazz;
        this.mapper = mapper;
        // Loads TimefoldJacksonModule via ServiceLoader, as well as any other Jackson modules on the classpath.
        mapper.findAndRegisterModules();
    }

    @Override
    public String getInputFileExtension() {
        return "json";
    }

    @Override
    public String getOutputFileExtension() {
        return "json";
    }

    @Override
    public Solution_ read(File inputSolutionFile) {
        try {
            return mapper.readValue(inputSolutionFile, clazz);
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed reading inputSolutionFile (" + inputSolutionFile + ").", e);
        }
    }

    public Solution_ read(InputStream inputSolutionStream) {
        try {
            return mapper.readValue(inputSolutionStream, clazz);
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed reading inputSolutionStream.", e);
        }
    }

    @Override
    public void write(Solution_ solution, File file) {
        try {
            mapper.writerWithDefaultPrettyPrinter().writeValue(file, solution);
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed write", e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy