com.cisco.trex.stateful.api.lowlevel.GsonUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trex-java-sdk Show documentation
Show all versions of trex-java-sdk Show documentation
Java client SDK provides an implementation for TRex RPC APIs
package com.cisco.trex.stateful.api.lowlevel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class GsonUtil {
private static Comparator getComparator() {
Comparator c =
new Comparator() {
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
};
return c;
}
public static void sort(JsonElement e) {
if (e.isJsonNull()) {
return;
}
if (e.isJsonPrimitive()) {
return;
}
if (e.isJsonArray()) {
JsonArray a = e.getAsJsonArray();
for (Iterator it = a.iterator(); it.hasNext(); ) {
sort(it.next());
}
return;
}
if (e.isJsonObject()) {
Map tm = new TreeMap(getComparator());
for (Map.Entry en : e.getAsJsonObject().entrySet()) {
tm.put(en.getKey(), en.getValue());
}
for (Map.Entry en : tm.entrySet()) {
e.getAsJsonObject().remove(en.getKey());
e.getAsJsonObject().add(en.getKey(), en.getValue());
sort(en.getValue());
}
return;
}
}
}