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

io.micronaut.json.convert.JsonNodeConvertibleValues Maven / Gradle / Ivy

There is a newer version: 4.7.5
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * 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
 *
 * https://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 io.micronaut.json.convert;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.value.ConvertibleValues;
import io.micronaut.json.tree.JsonNode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * Simple facade over a {@link JsonNode} to make it a {@link ConvertibleValues}.
 *
 * @param  The generic type for values
 * @author Graeme Rocher
 * @since 1.0
 */
@Internal
public class JsonNodeConvertibleValues implements ConvertibleValues {

    private final JsonNode objectNode;
    private final ConversionService conversionService;

    /**
     * @param objectNode The node that maps to JSON object structure
     * @param conversionService To convert the JSON node into given type
     */
    public JsonNodeConvertibleValues(JsonNode objectNode, ConversionService conversionService) {
        if (!objectNode.isObject()) {
            throw new IllegalArgumentException("Expected object node");
        }
        this.objectNode = objectNode;
        this.conversionService = conversionService;
    }

    @Override
    public Set names() {
        var set = new LinkedHashSet();
        for (Map.Entry entry : objectNode.entries()) {
            set.add(entry.getKey());
        }
        return Collections.unmodifiableSet(set);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Collection values() {
        var values = new ArrayList();
        objectNode.values().forEach(v -> values.add((V) v));
        return Collections.unmodifiableCollection(values);
    }

    @Override
    public  Optional get(CharSequence name, ArgumentConversionContext conversionContext) {
        String fieldName = name.toString();
        JsonNode jsonNode = objectNode.get(fieldName);
        if (jsonNode == null) {
            return Optional.empty();
        } else {
            return conversionService.convert(jsonNode, conversionContext);
        }
    }

    @Override
    public @NonNull ConversionService getConversionService() {
        return conversionService;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy