// translate the json for an elective to the class attributes protected void translateJson(JSONObject winJson) { String startDate, endDate; handle = winJson.get("handle").isString().stringValue(); id = (int) winJson.get("id").isNumber().doubleValue(); complete = winJson.get("complete").isBoolean().booleanValue(); // what are the dates that this elective spans? JSONString firstPeriod = winJson.get("firstPeriod").isString(); if (firstPeriod != null) { String startStr = firstPeriod.stringValue(); Date start = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").parse(startStr); startDate = DateTimeFormat.getFormat("yyyy-MM-dd").format(start); } else { startDate = "None"; } JSONString lastPeriod = winJson.get("lastPeriod").isString(); if (lastPeriod != null) { String endStr = lastPeriod.stringValue(); Date end = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").parse(endStr); endDate = DateTimeFormat.getFormat("yyyy-MM-dd").format(end); } else { endDate = "None"; } // the header is a summary: [date range] time, complete (id) header = "Elective [" + startDate + " - " + endDate + "] (" + Integer.toString(id) + "): "; }
@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(); }
public static <KeyType, ValueType> Map<KeyType, ValueType> toMap( JSONValue value, AbstractJsonEncoderDecoder<KeyType> keyEncoder, AbstractJsonEncoderDecoder<ValueType> valueEncoder, Style style) { if (value == null || value.isNull() != null) { return null; } switch (style) { case DEFAULT: case SIMPLE: { JSONObject object = value.isObject(); if (object == null) { throw new DecodingException("Expected a json object, but was given: " + value); } HashMap<KeyType, ValueType> rc = new HashMap<KeyType, ValueType>(object.size() * 2); for (String key : object.keySet()) { rc.put(keyEncoder.decode(key), valueEncoder.decode(object.get(key))); } return rc; } case JETTISON_NATURAL: { JSONObject object = value.isObject(); if (object == null) { throw new DecodingException("Expected a json object, but was given: " + value); } value = object.get("entry"); if (value == null) { throw new DecodingException("Expected an entry array not found"); } JSONArray entries = value.isArray(); if (entries == null) { throw new DecodingException("Expected an entry array, but was given: " + value); } HashMap<KeyType, ValueType> rc = new HashMap<KeyType, ValueType>(object.size() * 2); for (int i = 0; i < entries.size(); i++) { JSONObject entry = entries.get(i).isObject(); if (entry == null) throw new DecodingException("Expected an entry object, but was given: " + value); JSONValue key = entry.get("key"); if (key == null) throw new DecodingException("Expected an entry key field not found"); JSONString k = key.isString(); if (k == null) throw new DecodingException( "Expected an entry key to be a string, but was given: " + value); rc.put(keyEncoder.decode(k.stringValue()), valueEncoder.decode(entry.get("value"))); } return rc; } default: throw new UnsupportedOperationException( "The encoding style is not yet supported: " + style.name()); } }
public static String jsonValueToString(JSONValue jsonValue) { if (jsonValue != null) { JSONString jsonString = jsonValue.isString(); if (jsonString == null) { return null; } else { return jsonString.stringValue(); } } return null; }
@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 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); }
@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(); }
/** * 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; }
@Override public void buildWidget(JSONObject jsonObj, Widget parent) { VkMenuBarHorizontal tree = (VkMenuBarHorizontal) parent; addAttributes(jsonObj, parent); JSONArray items = jsonObj.get("items").isArray(); for (int i = 0; i < items.size(); i++) { JSONObject item = items.get(i).isObject(); JSONValue js = item.get("js"); if (js != null) ((VkMenuBarHorizontalEngine) VkStateHelper.getInstance() .getWidgetEngineMapping() .getEngineMap() .get(((IVkWidget) tree).getWidgetName())) .addMenuItem( tree, item.get("html").isString().stringValue(), item.get("js").isString().stringValue()); else if (item.containsKey("child")) { JSONObject childObj = item.get("child").isObject(); JSONString widgetName = childObj.get("widgetName").isString(); Widget widget = VkStateHelper.getInstance().getEngine().getWidget(widgetName.stringValue()); VkStateHelper.getInstance().getEngine().addWidget(widget, ((IVkPanel) tree)); VkStateHelper.getInstance() .getWidgetEngineMapping() .getEngineMap() .get(((IVkWidget) widget).getWidgetName()) .buildWidget(childObj, widget); // addAttributes(childObj, widget); } else if (item.get("separator") == null) { VkMenuBarHorizontal subTree = (VkMenuBarHorizontal) VkStateHelper.getInstance() .getEngine() .getWidget(VkMenuBarVertical.NAME); // all submenus are vertical // addAttributes(item.get("menu").isObject(), subTree); tree.addItem(new MenuItem(item.get("html").isString().stringValue(), subTree)); VkStateHelper.getInstance() .getWidgetEngineMapping() .getEngineMap() .get(((IVkWidget) tree).getWidgetName()) .buildWidget(item.get("menu").isObject(), subTree); } else addSeparator(tree); } }
@Override public Date decode(JSONValue value) throws DecodingException { if (value == null || value.isNull() != null) { return null; } String format = Defaults.getDateFormat(); if (format == null) { JSONNumber num = value.isNumber(); if (num == null) { throw new DecodingException("Expected a json number, but was given: " + value); } return new Date((long) num.doubleValue()); } JSONString str = value.isString(); if (str == null) { throw new DecodingException("Expected a json string, but was given: " + value); } return DateTimeFormat.getFormat(format).parse(str.stringValue()); }
private VerticalPanel listPermissions(final JSONObject entityJsonObject) { VerticalPanel vp = new VerticalPanel(); JSONArray jsonArray = (JSONArray) entityJsonObject.get("Permissions"); if (jsonArray != null) { for (int i = 0; i < jsonArray.size(); i++) { JSONString contactJson = (JSONString) jsonArray.get(i); // String entityName = ConvertJson.getStringValue(contactJson, // "entityName"); HTML permission = new HTML(contactJson.stringValue()); vp.add(permission); } } return vp; }
public void parseJSON(JSONObject sourceObj) { JSONString type = (JSONString) sourceObj.get("type"); JSONString componentID = (JSONString) sourceObj.get("componentID"); JSONString componentDescription = (JSONString) sourceObj.get("componentDescription"); JSONString fileName = (JSONString) sourceObj.get("fileName"); JSONString loadFunction = (JSONString) sourceObj.get("loadFunction"); JSONNumber xcoor = (JSONNumber) sourceObj.get("xcoor"); JSONNumber ycoor = (JSONNumber) sourceObj.get("ycoor"); setComponentID(componentID.stringValue()); setComponentDescription(componentDescription.stringValue()); setFileName(fileName.stringValue()); setLeft(xcoor.doubleValue()); setTop(ycoor.doubleValue()); JSONArray ports = (JSONArray) sourceObj.get("ports"); for (int i = 0; i < ports.size(); i++) { JSONObject portobj = (JSONObject) ports.get(i); JSONString porttype = (JSONString) portobj.get("porttype"); // JSONString dataSet = (JSONString) portobj.get("dataSet"); JSONArray dataDefinition = (JSONArray) portobj.get("dataSet"); for (int j = 0; j < dataDefinition.size(); j++) { JSONObject entry = (JSONObject) dataDefinition.get(j); JSONNumber order = (JSONNumber) entry.get("order"); JSONString fieldName = (JSONString) entry.get("fieldName"); JSONString fieldType = (JSONString) entry.get("fieldType"); getPort(porttype.stringValue()) .getDataSet() .add( new DataDefinitionEntry( Integer.parseInt(order.toString()), fieldName.stringValue(), fieldType.stringValue())); } } }