io.vertx.core.json.Json.decodeValue()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(6.0k)|赞(0)|评价(0)|浏览(215)

本文整理了Java中io.vertx.core.json.Json.decodeValue()方法的一些代码示例,展示了Json.decodeValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Json.decodeValue()方法的具体详情如下:
包路径:io.vertx.core.json.Json
类名称:Json
方法名:decodeValue

Json.decodeValue介绍

[英]Decode a given JSON buffer to a POJO of the given class type.
[中]将给定的JSON缓冲区解码为给定类类型的POJO。

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

private void fromJson(String json) {
 list = Json.decodeValue(json, List.class);
}

代码示例来源:origin: eclipse-vertx/vert.x

private void fromJson(String json) {
 map = Json.decodeValue(json, Map.class);
}

代码示例来源:origin: eclipse-vertx/vert.x

private void fromBuffer(Buffer buf) {
 list = Json.decodeValue(buf, List.class);
}

代码示例来源:origin: eclipse-vertx/vert.x

private void fromBuffer(Buffer buf) {
 map = Json.decodeValue(buf, Map.class);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public <T> T mapTo(TypeReference<T> type) {
 if (buffer != null) {
  try {
   return Json.mapper.readValue(buffer.asParser(), type);
  } catch (Exception e) {
   throw new DecodeException(e.getMessage());
  }
 } else {
  return Json.decodeValue(String.valueOf(value), type);
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public <T> T mapTo(Class<T> type) {
 if (buffer != null) {
  try {
   return Json.mapper.readValue(buffer.asParser(), type);
  } catch (Exception e) {
   throw new DecodeException(e.getMessage());
  }
 } else {
  return Json.decodeValue(String.valueOf(value), type);
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

private <T> void assertDecodeValue(Buffer buffer, T expected, TypeReference<T> ref) {
  Type type = ref.getType();
  Class<?> clazz = type instanceof Class ? (Class<?>) type : (Class<?>) ((ParameterizedType) type).getRawType();
  assertEquals(expected, Json.decodeValue(buffer, clazz));
  assertEquals(expected, Json.decodeValue(buffer, ref));
  assertEquals(expected, Json.decodeValue(buffer.toString(StandardCharsets.UTF_8), clazz));
  assertEquals(expected, Json.decodeValue(buffer.toString(StandardCharsets.UTF_8), ref));
  Buffer nullValue = Buffer.buffer("null");
  assertNull(Json.decodeValue(nullValue, clazz));
  assertNull(Json.decodeValue(nullValue, ref));
  assertNull(Json.decodeValue(nullValue.toString(StandardCharsets.UTF_8), clazz));
  assertNull(Json.decodeValue(nullValue.toString(StandardCharsets.UTF_8), ref));
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testGenericDecoding() {
 Pojo original = new Pojo();
 original.value = "test";
 String json = Json.encode(Collections.singletonList(original));
 List<Pojo> correct;
 correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {});
 assertTrue(((List)correct).get(0) instanceof Pojo);
 assertEquals(original.value, correct.get(0).value);
 // same must apply if instead of string we use a buffer
 correct = Json.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {});
 assertTrue(((List)correct).get(0) instanceof Pojo);
 assertEquals(original.value, correct.get(0).value);
 List incorrect = Json.decodeValue(json, List.class);
 assertFalse(incorrect.get(0) instanceof Pojo);
 assertTrue(incorrect.get(0) instanceof Map);
 assertEquals(original.value, ((Map)(incorrect.get(0))).get("value"));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testBytesDecoding() {
 Pojo original = new Pojo();
 original.bytes = TestUtils.randomByteArray(12);
 Pojo decoded = Json.decodeValue("{\"bytes\":\"" + Base64.getEncoder().encodeToString(original.bytes) + "\"}", Pojo.class);
 assertArrayEquals(original.bytes, decoded.bytes);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testNullBytesDecoding() {
 Pojo original = new Pojo();
 Pojo decoded = Json.decodeValue("{\"bytes\":null}", Pojo.class);
 assertEquals(original.bytes, decoded.bytes);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testInstantDecoding() {
 Pojo original = new Pojo();
 original.instant = Instant.from(ISO_INSTANT.parse("2018-06-20T07:25:38.397Z"));
 Pojo decoded = Json.decodeValue("{\"instant\":\"2018-06-20T07:25:38.397Z\"}", Pojo.class);
 assertEquals(original.instant, decoded.instant);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testNullInstantDecoding() {
 Pojo original = new Pojo();
 Pojo decoded = Json.decodeValue("{\"instant\":null}", Pojo.class);
 assertEquals(original.instant, decoded.instant);
}

代码示例来源:origin: io.vertx/vertx-core

private void fromBuffer(Buffer buf) {
 map = Json.decodeValue(buf, Map.class);
}

代码示例来源:origin: io.vertx/vertx-core

private void fromJson(String json) {
 list = Json.decodeValue(json, List.class);
}

代码示例来源:origin: io.vertx/vertx-core

@Override
public <T> T mapTo(Class<T> type) {
 if (buffer != null) {
  try {
   return Json.mapper.readValue(buffer.asParser(), type);
  } catch (Exception e) {
   throw new DecodeException(e.getMessage());
  }
 } else {
  return Json.decodeValue(String.valueOf(value), type);
 }
}

代码示例来源:origin: io.vertx/vertx-core

@Override
public <T> T mapTo(TypeReference<T> type) {
 if (buffer != null) {
  try {
   return Json.mapper.readValue(buffer.asParser(), type);
  } catch (Exception e) {
   throw new DecodeException(e.getMessage());
  }
 } else {
  return Json.decodeValue(String.valueOf(value), type);
 }
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testBytesDecoding() {
 Pojo original = new Pojo();
 original.bytes = TestUtils.randomByteArray(12);
 Pojo decoded = Json.decodeValue("{\"bytes\":\"" + Base64.getEncoder().encodeToString(original.bytes) + "\"}", Pojo.class);
 assertArrayEquals(original.bytes, decoded.bytes);
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testInstantDecoding() {
 Pojo original = new Pojo();
 original.instant = Instant.from(ISO_INSTANT.parse("2018-06-20T07:25:38.397Z"));
 Pojo decoded = Json.decodeValue("{\"instant\":\"2018-06-20T07:25:38.397Z\"}", Pojo.class);
 assertEquals(original.instant, decoded.instant);
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testNullInstantDecoding() {
 Pojo original = new Pojo();
 Pojo decoded = Json.decodeValue("{\"instant\":null}", Pojo.class);
 assertEquals(original.instant, decoded.instant);
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testNullBytesDecoding() {
 Pojo original = new Pojo();
 Pojo decoded = Json.decodeValue("{\"bytes\":null}", Pojo.class);
 assertEquals(original.bytes, decoded.bytes);
}

相关文章