Code cleanup

This commit is contained in:
Benoit Marty 2019-07-09 17:38:30 +02:00
parent e9700e04d8
commit 5e832e07cd

View File

@ -51,23 +51,20 @@ object JsonCanonicalizer {
} }
/** /**
* Canonicalize a JsonElement element * Canonicalize a JSON element
* *
* @param src the src * @param src the src
* @return the canonicalize element * @return the canonicalize element
*/ */
private fun canonicalizeRecursive(src: Any): String { private fun canonicalizeRecursive(any: Any): String {
// sanity check when (any) {
when (src) {
is JSONArray -> { is JSONArray -> {
// Canonicalize each element of the array // Canonicalize each element of the array
val srcArray = src as JSONArray?
val result = StringBuilder("[") val result = StringBuilder("[")
for (i in 0 until srcArray!!.length()) { for (i in 0 until any.length()) {
result.append(canonicalizeRecursive(srcArray.get(i))) result.append(canonicalizeRecursive(any.get(i)))
if (i < srcArray.length() - 1) { if (i < any.length() - 1) {
result.append(",") result.append(",")
} }
} }
@ -77,11 +74,11 @@ object JsonCanonicalizer {
return result.toString() return result.toString()
} }
is JSONObject -> { is JSONObject -> {
// Sort the attributes by name, and the canonicalize each element of the object // Sort the attributes by name, and the canonicalize each element of the JSONObject
val result = StringBuilder("{") val result = StringBuilder("{")
val attributes = TreeSet<String>() val attributes = TreeSet<String>()
for (entry in src.keys()) { for (entry in any.keys()) {
attributes.add(entry) attributes.add(entry)
} }
@ -90,7 +87,7 @@ object JsonCanonicalizer {
.append(attribute.value) .append(attribute.value)
.append("\"") .append("\"")
.append(":") .append(":")
.append(canonicalizeRecursive(src[attribute.value])) .append(canonicalizeRecursive(any[attribute.value]))
if (attribute.index < attributes.size - 1) { if (attribute.index < attributes.size - 1) {
result.append(",") result.append(",")
@ -101,8 +98,8 @@ object JsonCanonicalizer {
return result.toString() return result.toString()
} }
is String -> return JSONObject.quote(src) is String -> return JSONObject.quote(any)
else -> return src.toString() else -> return any.toString()
} }
} }