com.powsybl.contingency.json.ContingencyDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powsybl-contingency-api Show documentation
Show all versions of powsybl-contingency-api Show documentation
An API to describe and trigger contingencies
/**
* Copyright (c) 2018, 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.contingency.json;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.google.common.base.Suppliers;
import com.powsybl.commons.extensions.Extension;
import com.powsybl.commons.extensions.ExtensionJsonSerializer;
import com.powsybl.commons.extensions.ExtensionProviders;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.ContingencyElement;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
/**
*
* @author Massimo Ferraro {@literal }
* @author Teofil Calin BANC {@literal }
*/
public class ContingencyDeserializer extends StdDeserializer {
private static final Supplier> SUPPLIER =
Suppliers.memoize(() -> ExtensionProviders.createProvider(ExtensionJsonSerializer.class, "security-analysis"));
public ContingencyDeserializer() {
super(Contingency.class);
}
@Override
public Contingency deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException {
String id = null;
String name = null;
List elements = Collections.emptyList();
List> extensions = Collections.emptyList();
while (parser.nextToken() != JsonToken.END_OBJECT) {
switch (parser.getCurrentName()) {
case "id" -> id = parser.nextTextValue();
case "name" -> name = parser.nextTextValue();
case "elements" -> {
parser.nextToken();
elements = JsonUtil.readList(deserializationContext, parser, ContingencyElement.class);
}
case "extensions" -> {
parser.nextToken();
extensions = JsonUtil.readExtensions(parser, deserializationContext, SUPPLIER.get());
}
default -> throw new IllegalStateException("Unexpected field: " + parser.getCurrentName());
}
}
Contingency contingency = new Contingency(id, name, elements);
SUPPLIER.get().addExtensions(contingency, extensions);
return contingency;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy