public static BigDecimal toBigDecimal(JSONValue value) {
   JSONNumber number = value.isNumber();
   if (number == null) {
     throw new DecodingException("Expected a json number, but was given: " + value);
   }
   return new BigDecimal(value.toString());
 }
Example #2
0
  public static List<MarshalledMessage> decodePayload(final String value) {
    if (value == null || value.trim().length() == 0) return Collections.emptyList();

    /**
     * We have to do a two-stage decoding of the message. We cannot fully decode the message here,
     * as we cannot be sure the destination endpoint exists within this Errai bundle. So we extract
     * the ToSubject field and send the unparsed JSON object onwards.
     */
    JSONValue val = null;

    try {
      val = JSONParser.parseStrict(value);
    } catch (ClassCastException e) {
      if (!GWT.isProdMode()) {
        System.out.println("*** working around devmode bug ***");
        val = JSONParser.parseStrict(value);
      }
    }

    if (val == null) {
      return Collections.emptyList();
    }
    final JSONArray arr = val.isArray();
    if (arr == null) {
      throw new RuntimeException("unrecognized payload" + val.toString());
    }
    final ArrayList<MarshalledMessage> list = new ArrayList<MarshalledMessage>();
    unwrap(list, arr);
    return list;
  }
Example #3
0
    @Override
    public void onResponseReceived(Request request, Response response) {
      JSONValue j = JSONParser.parseStrict(response.getText());
      JSONObject obj = j.isObject();
      if (obj != null && obj.containsKey("error")) {
        Window.alert(obj.get("error").isString().stringValue());
        changeButtonSelection();
        setTextEnabled(false);
        clearTextBoxes();
        singleSelectionModel.clear();
      } else {
        List<OrganismInfo> organismInfoList =
            OrganismInfoConverter.convertJSONStringToOrganismInfoList(response.getText());
        dataGrid.setSelectionModel(singleSelectionModel);
        MainPanel.getInstance().getOrganismInfoList().clear();
        MainPanel.getInstance().getOrganismInfoList().addAll(organismInfoList);
        changeButtonSelection();
        OrganismChangeEvent organismChangeEvent = new OrganismChangeEvent(organismInfoList);
        organismChangeEvent.setAction(OrganismChangeEvent.Action.LOADED_ORGANISMS);
        Annotator.eventBus.fireEvent(organismChangeEvent);

        // in the case where we just add one . . .we should refresh the app state
        if (organismInfoList.size() == 1) {
          MainPanel.getInstance().getAppState();
        }
      }
      if (savingNewOrganism) {
        savingNewOrganism = false;
        setNoSelection();
        changeButtonSelection(false);
        loadingDialog.hide();
      }
    }
        @Override
        public BigInteger decode(JSONValue value) throws DecodingException {
          if (value == null || value.isNull() != null) {
            return null;
          }
          JSONNumber number = value.isNumber();
          if (number == null) {
            JSONString str = value.isString();
            if (str == null) {
              throw new DecodingException(
                  "Expected a json number r string, but was given: " + value);
            }

            // Doing a straight conversion from string to BigInteger will
            // not work for large values
            // So we convert to BigDecimal first and then convert it to
            // BigInteger.
            return new BigDecimal(str.stringValue()).toBigInteger();
          }

          // Doing a straight conversion from string to BigInteger will not
          // work for large values
          // So we convert to BigDecimal first and then convert it to
          // BigInteger.
          return new BigDecimal(value.toString()).toBigInteger();
        }
  /**
   * Returns the JSONObject at a given array index, or null if there is no JSONObject at that index.
   *
   * @param array
   * @param index
   * @return
   */
  public static JSONObject getObjectAt(JSONArray array, int index) {
    JSONValue element = array.get(index);

    if (element == null) {
      return null;
    } else {
      return element.isObject();
    }
  }
Example #6
0
 public static JSONArray parseJSONArray(String jsonText) {
   if (jsonText != null && 0 < jsonText.length()) {
     JSONValue jsonValue = JSONParser.parse(jsonText);
     if (jsonValue != null) {
       return jsonValue.isArray();
     }
   }
   return null;
 }
  /**
   * Creates a JSON object from a string. If the string parses, but doesn't contain a JSON object,
   * null is returned.
   *
   * @param json
   * @return
   */
  public static JSONObject getObject(final String json) {
    JSONValue val = JSONParser.parseStrict(json);

    if (val == null) {
      return null;
    } else {
      return val.isObject();
    }
  }
Example #8
0
 //
 // Utility methods, also useful when parsing third-party JSON
 //
 public static JSONObject parseJSONObject(String jsonText) {
   if (jsonText != null && 0 < jsonText.length()) {
     // TODO: use GWT json utils class to do safe parsing
     JSONValue jsonValue = JSONParser.parse(jsonText);
     if (jsonValue != null) {
       return jsonValue.isObject();
     }
   }
   return null;
 }
Example #9
0
 /**
  * 静态函数:根据输入的Json字符串,解析成一个完整的菜单树
  *
  * @param menuStr Json字符串
  * @return 菜单树形根节点
  */
 public static GWTMenu GetMenuList(String menuStr) {
   // 解析菜单
   JSONValue obj1 = JSONParser.parse(menuStr);
   JSONArray menuList = obj1.isArray();
   GWTMenu menu = new GWTMenu();
   for (int i = 0; i < menuList.size(); i++) {
     AddGWTMenu(menu, menuList.get(i));
   }
   return menu;
 }
Example #10
0
 private static void unwrap(final List<MarshalledMessage> messages, final JSONArray val) {
   for (int i = 0; i < val.size(); i++) {
     final JSONValue v = val.get(i);
     if (v.isArray() != null) {
       unwrap(messages, v.isArray());
     } else {
       messages.add(new MarshalledMessageImpl((JSONObject) v));
     }
   }
 }
 @Override
 public Long decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   final JSONString valueString = value.isString();
   if (valueString != null) {
     return Long.parseLong(valueString.stringValue());
   }
   return (long) toDouble(value);
 }
Example #12
0
  private ResultMessage processResponse(Request request, Response response)
      throws ResultMessageException {
    JSONValue jsonValue = JSONParser.parseStrict(response.getText());

    ResultMessage result = new ResultMessage(BusinessObject.create(jsonValue.isObject()));

    if (result.getStatus() != 0) {
      handleResultError(result);
    }
    return result;
  }
 @Override
 public Document decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   JSONString str = value.isString();
   if (str == null) {
     throw new DecodingException("Expected a json string, but was given: " + value);
   }
   return XMLParser.parse(str.stringValue());
 }
 @Override
 public Boolean decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   JSONBoolean bool = value.isBoolean();
   if (bool == null) {
     throw new DecodingException("Expected a json boolean, but was given: " + value);
   }
   return bool.booleanValue();
 }
Example #15
0
 /**
  * Factory method.
  *
  * @param <J> type of {@link JavaScriptObject} returned by this method
  * @param pJson the JSON representation of a {@link com.tj.civ.client.model.jso.CbGameJSO}
  * @return a new instance, or <code>null</code> if the instance could not be created
  */
 @SuppressWarnings("unchecked")
 public static <J extends JavaScriptObject> J createFromJson(final String pJson) {
   J result = null;
   JSONValue v = JSONParser.parseStrict(pJson);
   if (v != null) {
     JSONObject obj = v.isObject();
     if (obj != null) {
       result = (J) obj.getJavaScriptObject().cast();
     }
   }
   return result;
 }
  /**
   * @param jsonObj
   * @param key
   * @return
   */
  public static JSONArray getArray(final JSONObject jsonObj, final String key) {
    JSONArray ret = null; // assume failure

    if (jsonObj != null && key != null) {
      JSONValue val = jsonObj.get(key);

      if (val != null) {
        ret = val.isArray();
      }
    }

    return ret;
  }
 @Override
 public String decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   JSONString str = value.isString();
   if (str == null) {
     if (value.isBoolean() != null || value.isNumber() != null) {
       return value.toString();
     }
     throw new DecodingException("Expected a json string, but was given: " + value);
   }
   return str.stringValue();
 }
 public static double toDouble(JSONValue value) {
   JSONNumber number = value.isNumber();
   if (number == null) {
     JSONString val = value.isString();
     if (val != null) {
       try {
         return Double.parseDouble(val.stringValue());
       } catch (NumberFormatException e) {
         // just through exception below
       }
     }
     throw new DecodingException("Expected a json number, but was given: " + value);
   }
   return number.doubleValue();
 }
 @Override
 public UserDetails deserialize(String json) {
   JSONValue parsed = JSONParser.parseStrict(json);
   JSONObject jsonObj = parsed.isObject();
   UserDetailsDTO user = new UserDetailsDTO();
   if (jsonObj != null) {
     user.setUsername(jsonObj.get(UserDetailsDTO.USERNAME_FIELD).toString());
     JSONArray array = jsonObj.get(UserDetailsDTO.AUTHORITIES_FIELD).isArray();
     for (int i = 0; i < array.size(); i++) {
       JSONValue obj = array.get(i);
       user.getAuthorities().add(obj.toString());
     }
   }
   return user;
 }
 @Override
 public Character decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   return (char) toDouble(value);
 }
 static JSONArray asArray(JSONValue value) {
   JSONArray array = value.isArray();
   if (array == null) {
     throw new DecodingException("Expected a json array, but was given: " + value);
   }
   return array;
 }
 public static JSONObject toObject(JSONValue value) {
   JSONObject object = value.isObject();
   if (object == null) {
     throw new DecodingException("Expected a json object, but was given: " + object);
   }
   return object;
 }
Example #23
0
  @Override
  protected void onInit(final JSONValue data) {

    try {
      final JSONArray criteria = data.isArray();
      this.criteria.clear();

      for (int i = 0; i < criteria.size(); i++) {
        final JSONObject searchTypeJSON = criteria.get(i).isObject();
        final Map<String, Criterion> searchType = new LinkedHashMap<String, Criterion>();

        for (int j = 0; j < searchTypeJSON.get("elements").isArray().size(); j++) {
          final Criterion criterion = Criterion.readMe(j, searchTypeJSON);
          if (j == 0
              && (selectedCriterion == null && i == 0
                  || selectedCriterion != null
                      && criterion.displaytype.equalsIgnoreCase(selectedCriterion.displaytype))) {
            selectedCriterion = criterion;
            selectedCriterionHistory.put(selectedCriterion.displaytype, selectedCriterion);
          }
          searchType.put(criterion.getName(), criterion);
        }
        this.criteria.put(
            searchTypeJSON.get("displaytype").isString().stringValue().trim(), searchType);
      }
    } catch (final Exception e) {
      getLogger().severe(getClass().getName().replace("Impl", "") + ".onInit() : " + e);
    }
  }
 @Override
 public BigDecimal decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   return toBigDecimal(value);
 }
 public static int getSize(JSONValue value) {
   if (value == null || value.isNull() != null) {
     return 0;
   }
   JSONArray array = asArray(value);
   return array.size();
 }
 @Override
 public Float decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   return (float) toDouble(value);
 }
  /**
   * Parse a string from a JSON object
   *
   * @param jsonObj object to parse.
   * @param key key for string to retrieve.
   * @return desired string. Empty string on failure.
   */
  public static String getString(final JSONObject jsonObj, final String key) {
    String ret = ""; // assume failure

    if (jsonObj != null && key != null) {
      JSONValue val = jsonObj.get(key);

      if (val != null && val.isNull() == null) {
        JSONString strVal = val.isString();

        if (strVal != null) {
          ret = strVal.stringValue();
        }
      }
    }

    return ret;
  }
Example #28
0
 public static Boolean jsonValueToBoolean(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONBoolean jsonBoolean = jsonValue.isBoolean();
     if (jsonBoolean != null) {
       return jsonBoolean.booleanValue();
     }
   }
   return null;
 }
Example #29
0
 public static Long jsonValueToLong(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONNumber jsonNumber = jsonValue.isNumber();
     if (jsonNumber != null) {
       return (long) jsonNumber.doubleValue();
     }
   }
   return null;
 }
Example #30
0
 public static Integer jsonValueToInteger(JSONValue jsonValue) {
   if (jsonValue != null) {
     JSONNumber jsonNumber = jsonValue.isNumber();
     if (jsonNumber != null) {
       return (int) jsonNumber.doubleValue();
     }
   }
   return null;
 }