|
@@ -38,6 +38,12 @@ import java.util.*;
|
|
public class JsonUtil {
|
|
public class JsonUtil {
|
|
private static final Object[] EMPTY_OBJECT_ARRAY = {};
|
|
private static final Object[] EMPTY_OBJECT_ARRAY = {};
|
|
|
|
|
|
|
|
+ // Reuse ObjectMapper instance for improving performance.
|
|
|
|
+ // ObjectMapper is thread safe as long as we always configure instance
|
|
|
|
+ // before use. We don't have a re-entrant call pattern in WebHDFS,
|
|
|
|
+ // so we just need to worry about thread-safety.
|
|
|
|
+ private static final ObjectMapper MAPPER = new ObjectMapper();
|
|
|
|
+
|
|
/** Convert a token object to a Json string. */
|
|
/** Convert a token object to a Json string. */
|
|
public static String toJsonString(final Token<? extends TokenIdentifier> token
|
|
public static String toJsonString(final Token<? extends TokenIdentifier> token
|
|
) throws IOException {
|
|
) throws IOException {
|
|
@@ -72,9 +78,8 @@ public class JsonUtil {
|
|
public static String toJsonString(final String key, final Object value) {
|
|
public static String toJsonString(final String key, final Object value) {
|
|
final Map<String, Object> m = new TreeMap<String, Object>();
|
|
final Map<String, Object> m = new TreeMap<String, Object>();
|
|
m.put(key, value);
|
|
m.put(key, value);
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
|
try {
|
|
try {
|
|
- return mapper.writeValueAsString(m);
|
|
|
|
|
|
+ return MAPPER.writeValueAsString(m);
|
|
} catch (IOException ignored) {
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
@@ -116,10 +121,9 @@ public class JsonUtil {
|
|
m.put("fileId", status.getFileId());
|
|
m.put("fileId", status.getFileId());
|
|
m.put("childrenNum", status.getChildrenNum());
|
|
m.put("childrenNum", status.getChildrenNum());
|
|
m.put("storagePolicy", status.getStoragePolicy());
|
|
m.put("storagePolicy", status.getStoragePolicy());
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
|
try {
|
|
try {
|
|
return includeType ?
|
|
return includeType ?
|
|
- toJsonString(FileStatus.class, m) : mapper.writeValueAsString(m);
|
|
|
|
|
|
+ toJsonString(FileStatus.class, m) : MAPPER.writeValueAsString(m);
|
|
} catch (IOException ignored) {
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
@@ -331,9 +335,8 @@ public class JsonUtil {
|
|
new TreeMap<String, Map<String, Object>>();
|
|
new TreeMap<String, Map<String, Object>>();
|
|
finalMap.put(AclStatus.class.getSimpleName(), m);
|
|
finalMap.put(AclStatus.class.getSimpleName(), m);
|
|
|
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
|
try {
|
|
try {
|
|
- return mapper.writeValueAsString(finalMap);
|
|
|
|
|
|
+ return MAPPER.writeValueAsString(finalMap);
|
|
} catch (IOException ignored) {
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
@@ -371,8 +374,7 @@ public class JsonUtil {
|
|
final XAttrCodec encoding) throws IOException {
|
|
final XAttrCodec encoding) throws IOException {
|
|
final Map<String, Object> finalMap = new TreeMap<String, Object>();
|
|
final Map<String, Object> finalMap = new TreeMap<String, Object>();
|
|
finalMap.put("XAttrs", toJsonArray(xAttrs, encoding));
|
|
finalMap.put("XAttrs", toJsonArray(xAttrs, encoding));
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
|
- return mapper.writeValueAsString(finalMap);
|
|
|
|
|
|
+ return MAPPER.writeValueAsString(finalMap);
|
|
}
|
|
}
|
|
|
|
|
|
public static String toJsonString(final List<XAttr> xAttrs)
|
|
public static String toJsonString(final List<XAttr> xAttrs)
|
|
@@ -381,11 +383,10 @@ public class JsonUtil {
|
|
for (XAttr xAttr : xAttrs) {
|
|
for (XAttr xAttr : xAttrs) {
|
|
names.add(XAttrHelper.getPrefixedName(xAttr));
|
|
names.add(XAttrHelper.getPrefixedName(xAttr));
|
|
}
|
|
}
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
|
- String ret = mapper.writeValueAsString(names);
|
|
|
|
|
|
+ String ret = MAPPER.writeValueAsString(names);
|
|
final Map<String, Object> finalMap = new TreeMap<String, Object>();
|
|
final Map<String, Object> finalMap = new TreeMap<String, Object>();
|
|
finalMap.put("XAttrNames", ret);
|
|
finalMap.put("XAttrNames", ret);
|
|
- return mapper.writeValueAsString(finalMap);
|
|
|
|
|
|
+ return MAPPER.writeValueAsString(finalMap);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|