JsonUtil { public static String toJsonString( final Token<? extends TokenIdentifier> token) throws IOException { return toJsonString(Token.class, toJsonMap(token)); }  static String toJsonString(
      final Token<? extends TokenIdentifier> token); static String toJsonString(final Exception e); static String toJsonString(final String key, final Object value); static String toJsonString(final HdfsFileStatus status,
      boolean includeType); static String toJsonString(final LocatedBlocks locatedblocks
      ); static String toJsonString(final ContentSummary contentsummary); static String toJsonString(final MD5MD5CRC32FileChecksum checksum); static String toJsonString(final AclStatus status); static String toJsonString(final List<XAttr> xAttrs); static String toJsonString(final List<XAttr> xAttrs,
      final XAttrCodec encoding); static byte[] getXAttr(final Map<?, ?> json, final String name); static Map<String, byte[]> toXAttrs(final Map<?, ?> json); static List<String> toXAttrNames(final Map<?, ?> json);  }
@Test public void testHdfsFileStatus() throws IOException { final long now = Time.now(); final String parent = "/dir"; final HdfsFileStatus status = new HdfsFileStatus(1001L, false, 3, 1L << 26, now, now + 10, new FsPermission((short) 0644), "user", "group", DFSUtil.string2Bytes("bar"), DFSUtil.string2Bytes("foo"), HdfsConstantsClient.GRANDFATHER_INODE_ID, 0, null, (byte) 0); final FileStatus fstatus = toFileStatus(status, parent); System.out.println("status = " + status); System.out.println("fstatus = " + fstatus); final String json = JsonUtil.toJsonString(status, true); System.out.println("json = " + json.replace(",", ",\n ")); ObjectReader reader = new ObjectMapper().reader(Map.class); final HdfsFileStatus s2 = JsonUtilClient.toFileStatus((Map<?, ?>) reader.readValue(json), true); final FileStatus fs2 = toFileStatus(s2, parent); System.out.println("s2 = " + s2); System.out.println("fs2 = " + fs2); Assert.assertEquals(fstatus, fs2); }
@Test public void testToJsonFromXAttrs() throws IOException { String jsonString = "{\"XAttrs\":[{\"name\":\"user.a1\",\"value\":\"0x313233\"}," + "{\"name\":\"user.a2\",\"value\":\"0x313131\"}]}"; XAttr xAttr1 = (new XAttr.Builder()).setNameSpace(XAttr.NameSpace.USER). setName("a1").setValue(XAttrCodec.decodeValue("0x313233")).build(); XAttr xAttr2 = (new XAttr.Builder()).setNameSpace(XAttr.NameSpace.USER). setName("a2").setValue(XAttrCodec.decodeValue("0x313131")).build(); List<XAttr> xAttrs = Lists.newArrayList(); xAttrs.add(xAttr1); xAttrs.add(xAttr2); Assert.assertEquals(jsonString, JsonUtil.toJsonString(xAttrs, XAttrCodec.HEX)); }
@Test public void testToJsonFromAclStatus() { String jsonString = "{\"AclStatus\":{\"entries\":[\"user:user1:rwx\",\"group::rw-\"],\"group\":\"supergroup\",\"owner\":\"testuser\",\"stickyBit\":false}}"; AclStatus.Builder aclStatusBuilder = new AclStatus.Builder(); aclStatusBuilder.owner("testuser"); aclStatusBuilder.group("supergroup"); aclStatusBuilder.stickyBit(false); List<AclEntry> aclSpec = Lists.newArrayList(aclEntry(ACCESS, USER,"user1", ALL), aclEntry(ACCESS, GROUP, READ_WRITE)); aclStatusBuilder.addEntries(aclSpec); Assert.assertEquals(jsonString, JsonUtil.toJsonString(aclStatusBuilder.build())); }