@SuppressWarnings("unchecked")
    public static JSONObject getJsonObject(Map<String, Object> messageMap) {
      JSONObject jsonObj = new JSONObject();
      for (Map.Entry<String, Object> entry : messageMap.entrySet()) {
        JSONValue jsonVal = null;
        if (entry.getValue() == null) {
          jsonVal = JSONNull.getInstance();
        } else if (entry.getValue() instanceof Boolean) {
          jsonVal = JSONBoolean.getInstance((Boolean) entry.getValue());
        } else if (entry.getValue() instanceof Integer) {
          jsonVal = new JSONNumber((Integer) entry.getValue());
        } else if (entry.getValue() instanceof String) {
          jsonVal = new JSONString((String) entry.getValue());
        } else if (entry.getValue() instanceof List) {
          jsonVal = getJsonArray((List<Object>) entry.getValue());
        } else if (entry.getValue() instanceof Map) {
          jsonVal = getJsonObject((Map<String, Object>) entry.getValue());
        } else {
          throw new IllegalStateException("Invalid object encountered");
        }
        jsonObj.put(entry.getKey(), jsonVal);
      }
      return jsonObj;
    }