io.vertx.core.json.jackson.DatabindCodec Java Examples

The following examples show how to use io.vertx.core.json.jackson.DatabindCodec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VertxJobsService.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@PostConstruct
void initialize() {
    DatabindCodec.mapper().disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
    DatabindCodec.mapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);             
    DatabindCodec.mapper().registerModule(new JavaTimeModule());
    DatabindCodec.mapper().findAndRegisterModules();
    
    DatabindCodec.prettyMapper().registerModule(new JavaTimeModule());
    DatabindCodec.prettyMapper().findAndRegisterModules();
    DatabindCodec.prettyMapper().disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
    DatabindCodec.prettyMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    if (providedWebClient.isResolvable()) {
        this.client = providedWebClient.get();
        LOGGER.debug("Using provided web client instance");
    } else {
        final URI jobServiceURL = getJobsServiceUri();
        this.client = WebClient.create(vertx,
                                       new WebClientOptions()
                                               .setDefaultHost(jobServiceURL.getHost())
                                               .setDefaultPort(jobServiceURL.getPort()));
        LOGGER.debug("Creating new instance of web client for host {} and port {}", jobServiceURL.getHost(), jobServiceURL.getPort());
    }
}
 
Example #2
Source File: DynamicObjectTest.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
void should_support_json_encode_decode() {
  Map<String, Object> map = new HashMap<>();
  map.put("k", "v");

  DynamicObject dynamicObject = DatabindCodec.mapper().convertValue(map, DynamicObject.class);

  assertThat(dynamicObject.getDynamic()).isNotSameAs(map);
  assertThat(dynamicObject.getDynamic()).isEqualTo(map);
  assertThat(Json.encode(dynamicObject)).isEqualTo("{\"k\":\"v\"}");
}
 
Example #3
Source File: JacksonPropertyNodeNameProvider.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private String getJavaBeanPropertyName(JavaBeanProperty property) {
  ObjectMapper objectMapper = DatabindCodec.mapper();
  JavaType type = objectMapper.constructType(property.getDeclaringClass());
  BeanDescription desc = objectMapper.getSerializationConfig().introspect(type);

  return desc.findProperties()
      .stream()
      .filter(prop -> prop.getInternalName().equals(property.getName()))
      .map(BeanPropertyDefinition::getName)
      .findFirst()
      .orElse(property.getName());
}
 
Example #4
Source File: JsonResponseWriter.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Override
public void write(T result, HttpServerRequest request, HttpServerResponse response) {

	if (result != null) {
		response.end(JsonUtils.toJson(result, DatabindCodec.mapper()));
	}
	else {
		response.end();
	}
}
 
Example #5
Source File: JsonValueReader.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
@Override
public T read(String value, Class<T> type) {

	if (StringUtils.isNullOrEmptyTrimmed(value)) {
		return null;
	}

	return JsonUtils.fromJson(value, type, DatabindCodec.mapper());
}
 
Example #6
Source File: PgClientTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalDateTimeWithJackson(TestContext ctx) {
  DatabindCodec.mapper().registerModule(new JavaTimeModule());
  SqlTemplate<Map<String, Object>, RowSet<LocalDateTimePojo>> template = SqlTemplate
    .forQuery(connection, "SELECT #{value} :: TIMESTAMP WITHOUT TIME ZONE \"localDateTime\"")
    .mapTo(LocalDateTimePojo.class);
  LocalDateTime ldt = LocalDateTime.parse("2017-05-14T19:35:58.237666");
  template.execute(Collections.singletonMap("value", ldt), ctx.asyncAssertSuccess(result -> {
    ctx.assertEquals(1, result.size());
    ctx.assertEquals(ldt, result.iterator().next().localDateTime);
  }));
}
 
Example #7
Source File: JsonTypeValidator.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public RequestParameter isValid(String value) throws ValidationException {
  try {
    JsonNode node;
    if (value == null)
      throw ValidationException.ValidationExceptionFactory.generateNotParsableJsonBodyException("Json should not be null");
    else if (value.length() == 0)
      node = JsonNodeFactory.instance.textNode("");
    else
      node = DatabindCodec.mapper().readTree(value);

    Set<ValidationMessage> errors = schema.validate(node);
    if (errors.size() == 0) {
      if (node.isArray())
        return RequestParameter.create(new JsonArray(value));
      else if (node.isObject())
        return RequestParameter.create(new JsonObject(value));
      else
        return RequestParameter.create(value);
    } else {
      ValidationMessage firstError = errors.iterator().next();
      throw ValidationException.ValidationExceptionFactory.generateInvalidJsonBodyException(firstError.getPath(), value, firstError.getMessage());
    }
  } catch (IOException e) {
    throw ValidationException.ValidationExceptionFactory.generateNotParsableJsonBodyException(e.getMessage());
  }
}
 
Example #8
Source File: JsonTypeValidator.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public static JsonTypeValidator createJsonTypeValidator(String schema) {
  if (schema.length() != 0) {
    try {
      return createJsonTypeValidator(DatabindCodec.mapper().readTree(schema));
    } catch (IOException e) {
      throw new IllegalArgumentException("schema provided is invalid: " + e);
    }
  }
  else return null;
}
 
Example #9
Source File: Util.java    From vertx-starter with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static JsonObject loadStarterData() throws IOException {
  try (InputStream is = WebVerticle.class.getClassLoader().getResourceAsStream("starter.json")) {
    return new JsonObject(DatabindCodec.mapper().readValue(is, Map.class));
  }
}