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

com.powsybl.contingency.json.ContingencyDeserializer Maven / Gradle / Ivy

There is a newer version: 6.6.0
Show newest version
/**
 * 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.currentName()) {
                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.currentName());
            }
        }
        Contingency contingency = new Contingency(id, name, elements);
        SUPPLIER.get().addExtensions(contingency, extensions);

        return contingency;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy