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

org.onosproject.codec.CodecContext Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
/*
 * Copyright 2014-present Open Networking Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.codec;

import static com.google.common.base.Preconditions.checkArgument;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * Context for codecs to use while encoding/decoding.
 */
public interface CodecContext {

    /**
     * Returns the JSON object mapper.
     *
     * @return object mapper
     */
    ObjectMapper mapper();

    /**
     * Returns the JSON codec for the specified entity class.
     *
     * @param entityClass entity class
     * @param          entity type
     * @return JSON codec; null if no codec available for the class
     */
     JsonCodec codec(Class entityClass);

    /**
     * Returns reference to the specified service implementation.
     *
     * @param serviceClass service class
     * @param           service type
     * @return service implementation; null if no implementation available for the class
     */
     T getService(Class serviceClass);

    /**
     * Decodes the specified entity from JSON using codec
     * registered to this context.
     *
     * @param json    JSON to decode
     * @param entityClass entity class
     * @param  entity type
     * @return decoded entity
     */
    default  T decode(JsonNode json, Class entityClass) {
        checkArgument(json.isObject());
        return codec(entityClass).decode((ObjectNode) json, this);
    }

    /**
     * Encodes the specified entity into JSON using codec
     * registered to this context.
     *
     * @param entity  entity to encode
     * @param entityClass entity class
     * @param  entity type
     * @return JSON node
     */
    default  ObjectNode encode(T entity, Class entityClass) {
        return codec(entityClass).encode(entity, this);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy