JsonMarginCalculationParameters.java
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawo.margincalculation.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.dynawo.margincalculation.MarginCalculationParameters;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
/**
* Provides methods to read and write MarginCalculationParameters from and to JSON.
*
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public final class JsonMarginCalculationParameters {
private JsonMarginCalculationParameters() {
}
/**
* Reads parameters from a JSON file (will NOT rely on platform config).
*/
public static MarginCalculationParameters read(Path jsonFile) {
Objects.requireNonNull(jsonFile);
try (InputStream is = Files.newInputStream(jsonFile)) {
return read(is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Reads parameters from a JSON file (will NOT rely on platform config).
*/
public static MarginCalculationParameters read(InputStream jsonStream) {
try {
ObjectMapper objectMapper = createObjectMapper();
return objectMapper.readerFor(MarginCalculationParameters.class).readValue(jsonStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Writes parameters as JSON to a file.
*/
public static void write(MarginCalculationParameters parameters, Path jsonFile) {
Objects.requireNonNull(jsonFile);
try (OutputStream outputStream = Files.newOutputStream(jsonFile)) {
write(parameters, outputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Writes parameters as JSON to an output stream.
*/
public static void write(MarginCalculationParameters parameters, OutputStream outputStream) {
try {
ObjectMapper objectMapper = createObjectMapper();
ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
writer.writeValue(outputStream, parameters);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static ObjectMapper createObjectMapper() {
return JsonUtil.createObjectMapper()
.registerModule(new MarginCalculationParametersJsonModule());
}
}