Search in sources :

Example 1 with JsonNode

use of io.micronaut.json.tree.JsonNode in project micronaut-core by micronaut-projects.

the class JsonNodeTraversingParser method getNumberType.

@Override
public NumberType getNumberType() throws IOException {
    JsonNode currentNode = currentNodeOrNull();
    if (currentNode == null || !currentNode.isNumber()) {
        return null;
    }
    Number value = currentNode.getNumberValue();
    if (value instanceof BigDecimal) {
        return JsonParser.NumberType.BIG_DECIMAL;
    } else if (value instanceof Double) {
        return JsonParser.NumberType.DOUBLE;
    } else if (value instanceof Float) {
        return JsonParser.NumberType.FLOAT;
    } else if (value instanceof Byte || value instanceof Short || value instanceof Integer) {
        return JsonParser.NumberType.INT;
    } else if (value instanceof Long) {
        return JsonParser.NumberType.LONG;
    } else if (value instanceof BigInteger) {
        return JsonParser.NumberType.BIG_INTEGER;
    } else {
        throw new IllegalStateException("Unknown number type " + value.getClass().getName());
    }
}
Also used : BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) JsonNode(io.micronaut.json.tree.JsonNode) BigDecimal(java.math.BigDecimal)

Example 2 with JsonNode

use of io.micronaut.json.tree.JsonNode in project micronaut-core by micronaut-projects.

the class JsonBeanPropertyBinder method bind.

@Override
public BindingResult<Object> bind(ArgumentConversionContext<Object> context, Map<CharSequence, ? super Object> source) {
    try {
        JsonNode objectNode = buildSourceObjectNode(source.entrySet());
        Object result = jsonMapper.readValueFromTree(objectNode, context.getArgument());
        return () -> Optional.of(result);
    } catch (Exception e) {
        context.reject(e);
        return new BindingResult<Object>() {

            @Override
            public List<ConversionError> getConversionErrors() {
                return CollectionUtils.iterableToList(context);
            }

            @Override
            public boolean isSatisfied() {
                return false;
            }

            @Override
            public Optional<Object> getValue() {
                return Optional.empty();
            }
        };
    }
}
Also used : Optional(java.util.Optional) JsonNode(io.micronaut.json.tree.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) IOException(java.io.IOException)

Example 3 with JsonNode

use of io.micronaut.json.tree.JsonNode in project micronaut-core by micronaut-projects.

the class JsonBeanPropertyBinder method bind.

@Override
public <T2> T2 bind(T2 object, ArgumentConversionContext<T2> context, Set<? extends Map.Entry<? extends CharSequence, Object>> source) {
    try {
        JsonNode objectNode = buildSourceObjectNode(source);
        jsonMapper.updateValueFromTree(object, objectNode);
    } catch (Exception e) {
        context.reject(e);
    }
    return object;
}
Also used : JsonNode(io.micronaut.json.tree.JsonNode) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) IOException(java.io.IOException)

Example 4 with JsonNode

use of io.micronaut.json.tree.JsonNode in project micronaut-core by micronaut-projects.

the class JsonBeanPropertyBinder method bind.

@Override
public <T2> T2 bind(T2 object, Set<? extends Map.Entry<? extends CharSequence, Object>> source) throws ConversionErrorException {
    try {
        JsonNode objectNode = buildSourceObjectNode(source);
        jsonMapper.updateValueFromTree(object, objectNode);
    } catch (Exception e) {
        throw newConversionError(object, e);
    }
    return object;
}
Also used : JsonNode(io.micronaut.json.tree.JsonNode) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) IOException(java.io.IOException)

Example 5 with JsonNode

use of io.micronaut.json.tree.JsonNode in project micronaut-core by micronaut-projects.

the class ComputeInstanceMetadataResolverUtils method populateMetadata.

/**
 * Populates the instance instance metadata's {@link AbstractComputeInstanceMetadata#setMetadata(Map)} property.
 *
 * @param instanceMetadata The instance metadata
 * @param metadata         A json object of metadata
 */
@Experimental
public static void populateMetadata(AbstractComputeInstanceMetadata instanceMetadata, JsonNode metadata) {
    if (metadata != null) {
        Map<String, String> finalMetadata = new HashMap<>(metadata.size());
        for (Map.Entry<String, JsonNode> entry : metadata.entries()) {
            JsonNode value = entry.getValue();
            if (value.isString()) {
                finalMetadata.put(entry.getKey(), value.getStringValue());
            }
        }
        instanceMetadata.setMetadata(finalMetadata);
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(io.micronaut.json.tree.JsonNode) HashMap(java.util.HashMap) Map(java.util.Map) Experimental(io.micronaut.core.annotation.Experimental)

Aggregations

JsonNode (io.micronaut.json.tree.JsonNode)10 IOException (java.io.IOException)4 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)3 Internal (io.micronaut.core.annotation.Internal)2 Publishers (io.micronaut.core.async.publisher.Publishers)2 Argument (io.micronaut.core.type.Argument)2 NetworkInterface (io.micronaut.discovery.cloud.NetworkInterface)2 MediaType (io.micronaut.http.MediaType)2 JsonMapper (io.micronaut.json.JsonMapper)2 ByteBuf (io.netty.buffer.ByteBuf)2 ByteBufUtil (io.netty.buffer.ByteBufUtil)2 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)2 ArrayList (java.util.ArrayList)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Processor (org.reactivestreams.Processor)2 Subscriber (org.reactivestreams.Subscriber)2 Subscription (org.reactivestreams.Subscription)2 NettyByteBufferFactory (io.micronaut.buffer.netty.NettyByteBufferFactory)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationMetadataResolver (io.micronaut.core.annotation.AnnotationMetadataResolver)1