All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cisco.trex.stateful.api.lowlevel.GsonUtil Maven / Gradle / Ivy

There is a newer version: 1.69
Show newest version
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;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy