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 Map<String, Object> getEcPolicyAsMap(
      final ErasureCodingPolicy ecPolicy); static String toJsonString(final DirectoryListing listing); static String toJsonString(final LocatedBlocks locatedblocks
      ); static String toJsonString(final ContentSummary contentsummary); static String toJsonString(final QuotaUsage quotaUsage); static String toJsonString(final MD5MD5CRC32FileChecksum checksum); static String toJsonString(final AclStatus status); static String toJsonString(final List<XAttr> xAttrs, 
      final XAttrCodec encoding); static String toJsonString(final List<XAttr> xAttrs); static String toJsonString(Object obj); static String toJsonString(BlockStoragePolicy[] storagePolicies); static String toJsonString(BlockStoragePolicy storagePolicy); static String toJsonString(FsServerDefaults serverDefaults); static String toJsonString(SnapshotDiffReport diffReport); static String toJsonString(
      SnapshottableDirectoryStatus[] snapshottableDirectoryList); static String toJsonString(SnapshotStatus[] snapshotList); static String toJsonString(BlockLocation[] locations);  }
@Test public void testHdfsFileStatusWithEcPolicy() throws IOException { final long now = Time.now(); final String parent = "/dir"; ErasureCodingPolicy dummyEcPolicy = new ErasureCodingPolicy("ecPolicy1", new ECSchema("EcSchema", 1, 1), 1024 * 2, (byte) 1); final HdfsFileStatus status = new HdfsFileStatus.Builder() .length(1001L) .replication(3) .blocksize(1L << 26) .mtime(now) .atime(now + 10) .perm(new FsPermission((short) 0644)) .owner("user") .group("group") .symlink(DFSUtil.string2Bytes("bar")) .path(DFSUtil.string2Bytes("foo")) .fileId(HdfsConstants.GRANDFATHER_INODE_ID) .ecPolicy(dummyEcPolicy) .flags(EnumSet.allOf(Flags.class)) .build(); 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 ")); 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(status.getErasureCodingPolicy(), s2.getErasureCodingPolicy()); Assert.assertEquals(fstatus, fs2); }
@Test public void testHdfsFileStatusWithoutEcPolicy() throws IOException { final long now = Time.now(); final String parent = "/dir"; ErasureCodingPolicy dummyEcPolicy = new ErasureCodingPolicy("ecPolicy1", new ECSchema("EcSchema", 1, 1), 1024 * 2, (byte) 1); final HdfsFileStatus status = new HdfsFileStatus.Builder() .length(1001L) .replication(3) .blocksize(1L << 26) .mtime(now) .atime(now + 10) .perm(new FsPermission((short) 0644)) .owner("user") .group("group") .symlink(DFSUtil.string2Bytes("bar")) .path(DFSUtil.string2Bytes("foo")) .fileId(HdfsConstants.GRANDFATHER_INODE_ID) .build(); Assert.assertTrue(status.getErasureCodingPolicy() == null); 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 ")); 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 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())); }
@Test public void testToJsonFromContentSummary() { String jsonString = "{\"ContentSummary\":{\"directoryCount\":33333,\"ecPolicy\":" + "\"RS-6-3-1024k\",\"fileCount\":22222,\"length\":11111," + "\"quota\":44444,\"snapshotDirectoryCount\":1," + "\"snapshotFileCount\":2,\"snapshotLength\":10," + "\"snapshotSpaceConsumed\":30,\"spaceConsumed\":55555," + "\"spaceQuota\":66666,\"typeQuota\":{}}}"; long length = 11111; long fileCount = 22222; long directoryCount = 33333; long quota = 44444; long spaceConsumed = 55555; long spaceQuota = 66666; String ecPolicy = "RS-6-3-1024k"; long snapshotLength = 10; long snapshotFileCount = 2; long snapshotDirectoryCount = 1; long snapshotSpaceConsumed = 30; ContentSummary contentSummary = new ContentSummary.Builder().length(length) .fileCount(fileCount).directoryCount(directoryCount).quota(quota) .spaceConsumed(spaceConsumed).spaceQuota(spaceQuota) .erasureCodingPolicy(ecPolicy).snapshotLength(snapshotLength) .snapshotFileCount(snapshotFileCount) .snapshotDirectoryCount(snapshotDirectoryCount) .snapshotSpaceConsumed(snapshotSpaceConsumed).build(); Assert.assertEquals(jsonString, JsonUtil.toJsonString(contentSummary)); }
@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)); }