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());
}
}
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();
}
};
}
}
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;
}
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;
}
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);
}
}
Aggregations