示例#1
0
 public String toJson(ObjectMapper mapper) {
   ObjectNode rootNode = mapper.createObjectNode();
   ArrayNode keysNode = rootNode.putArray("keys");
   for (Object key : keys) {
     keysNode.addPOJO(key);
   }
   try {
     return mapper.writeValueAsString(rootNode);
   } catch (Exception e) {
     throw Exceptions.propagate(e);
   }
 }
  /**
   * Simple test to verify that byte[] values can be handled properly when converting, as long as
   * there is metadata (from POJO definitions).
   */
  public void testIssue709() throws Exception {
    byte[] inputData = new byte[] {1, 2, 3};
    ObjectNode node = MAPPER.createObjectNode();
    node.put("data", inputData);
    Issue709Bean result = MAPPER.treeToValue(node, Issue709Bean.class);
    String json = MAPPER.writeValueAsString(node);
    Issue709Bean resultFromString = MAPPER.readValue(json, Issue709Bean.class);
    Issue709Bean resultFromConvert = MAPPER.convertValue(node, Issue709Bean.class);

    // all methods should work equally well:
    Assert.assertArrayEquals(inputData, resultFromString.data);
    Assert.assertArrayEquals(inputData, resultFromConvert.data);
    Assert.assertArrayEquals(inputData, result.data);
  }