Example usage for com.google.gwt.json.client JSONValue isString

List of usage examples for com.google.gwt.json.client JSONValue isString

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue isString.

Prototype

public JSONString isString() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONString.

Usage

From source file:org.jboss.errai.enterprise.client.jaxrs.JacksonTransformer.java

License:Apache License

/**
 * The transformation from Errai JSON to Jackson's JSON contains the following steps:
 * <ul>/*w  w w .j a  va2 s . c  o  m*/
 * <li>For all JSON objects, recursively remove the Errai specific OBJECT_ID and ENCODED_TYPE
 * values</li>
 * <li>Keep a reference to the removed OBJECT_IDs, so back-references can be resolved</li>
 * <li>If an array is encountered, process all its elements, then remove the Errai specific
 * QUALIFIED_VALUE key, by associating its actual value with the object's key directly: "list":
 * {"^Value": ["e1","e2"]} becomes "list": ["e1","e2"]</li>
 * <li>If an enum is encountered, remove the Errai specific ENUM_STRING_VALUE key, by associating
 * its actual value with the object's key directly: "gender": {"^EnumStringValue": "MALE"} becomes
 * "gender": "MALE"</li>
 * <li>If a number is encountered, remove the Errai specific NUMERIC_VALUE key, by associating its
 * actual value with the object's key directly: "id": {"^NumValue": "1"} becomes "id": "1"</li>
 * <li>If a date is encountered, remove the Errai specific QUALIFIED_VALUE key, by associating its
 * actual value with the object's key directly and turning it into a JSON number</li>
 * <li>If EMBEDDED_JSON is encountered, turn in into standard json</li>
 * </ul>
 * 
 * @param val
 *          the JSON value to transform
 * @param key
 *          the key of the JSON value to transform
 * @param parent
 *          the parent object of the current value
 * @param objectCache
 *          a cache for removed objects, that is used to resolve backreferences
 * @return the modified JSON value
 */
private static JSONValue toJackson(JSONValue val, String key, JSONObject parent,
        Map<String, JSONValue> objectCache) {
    JSONObject obj;
    if ((obj = val.isObject()) != null) {
        JSONValue objectIdVal = obj.get(OBJECT_ID);
        if (objectIdVal != null) {
            String objectId = objectIdVal.isString().stringValue();
            if (!objectId.equals("-1")) {
                JSONValue backRef = objectCache.get(objectId);
                if (backRef != null) {
                    if (parent != null) {
                        parent.put(key, backRef);
                    } else {
                        return backRef.isObject();
                    }
                } else {
                    objectCache.put(objectId, obj);
                }
            }
        }

        JSONValue encType = obj.get(ENCODED_TYPE);
        obj.put(OBJECT_ID, null);
        obj.put(ENCODED_TYPE, null);

        for (String k : obj.keySet()) {
            JSONArray arr;
            if ((arr = obj.get(k).isArray()) != null) {
                for (int i = 0; i < arr.size(); i++) {
                    if (arr.get(i).isObject() != null && arr.get(i).isObject().get(NUMERIC_VALUE) != null) {
                        arr.set(i, arr.get(i).isObject().get(NUMERIC_VALUE));
                    } else if (arr.get(i).isObject() != null) {
                        arr.set(i, toJackson(arr.get(i), null, null, objectCache));
                    }
                }

                if (k.equals(QUALIFIED_VALUE)) {
                    if (parent != null) {
                        parent.put(key, arr);
                    } else {
                        return arr;
                    }
                }
            } else if (k.equals(ENUM_STRING_VALUE) || k.equals(NUMERIC_VALUE)) {
                if (parent != null) {
                    parent.put(key, obj.get(k));
                }
            } else if (k.equals(QUALIFIED_VALUE)) {
                if (parent != null) {
                    if (encType.isString().stringValue().equals("java.util.Date")) {
                        String dateValue = obj.get(k).isString().stringValue();
                        parent.put(key, new JSONNumber(Double.parseDouble(dateValue)));
                    } else {
                        parent.put(key, obj.get(k));
                    }
                }
            } else if (k.startsWith(SerializationParts.EMBEDDED_JSON)) {
                final JSONValue newKey = JSONParser
                        .parseStrict((k.substring(SerializationParts.EMBEDDED_JSON.length())));
                JSONValue value = obj.get(k);
                JSONObject tmpObject = new JSONObject();
                toJackson(newKey, QUALIFIED_VALUE, tmpObject, objectCache);

                String embeddedKey = null;
                JSONValue qualVal = tmpObject.get(QUALIFIED_VALUE);
                if (qualVal.isString() != null) {
                    embeddedKey = qualVal.isString().stringValue();
                } else {
                    embeddedKey = qualVal.toString();
                }
                obj.put(embeddedKey, value);
            }

            toJackson(obj.get(k), k, obj, objectCache);
        }
    }

    return cleanUpEmbeddedJson(obj);
}

From source file:org.jbpm.console.ng.df.client.filter.FilterSettingsJSONMarshaller.java

License:Apache License

public FilterSettings fromJsonString(String jsonString) {
    DisplayerSettings displayerSettings = _displayerJsonMarshaller.fromJsonString(jsonString);
    FilterSettings tableSettings = FilterSettings.cloneFrom(displayerSettings);

    JSONObject parseResult = JSONParser.parseStrict(jsonString).isObject();
    if (parseResult != null) {

        JSONValue value = parseResult.get(TABLE_NAME);
        tableSettings.setTableName(//from   w  w  w .j  a v a2  s .  co m
                value != null && value.isString() != null ? value.isString().stringValue() : null);

        value = parseResult.get(TABLE_DESCR);
        tableSettings.setTableDescription(
                value != null && value.isString() != null ? value.isString().stringValue() : null);

        value = parseResult.get(EDIT_ENABLED);
        tableSettings.setEditable(
                value != null && value.isBoolean() != null ? value.isBoolean().booleanValue() : true);
    }

    return tableSettings;
}

From source file:org.jbpm.console.ng.df.client.filter.json.DataSetLookupJSONMarshaller.java

License:Apache License

private ColumnFilter parseColumnFilter(JSONObject columnFilterJson) {
    if (columnFilterJson == null)
        return null;

    String columnId = null;/*from   w w w .jav a 2s.co m*/
    String functionType = null;

    JSONValue value = columnFilterJson.get(COLUMNID);
    if (checkNotNull(value, false, FiltersConstants.INSTANCE.json_datasetlookup_columnfilter_null_columnid())) {
        columnId = value.isString() != null ? value.isString().stringValue() : null;
    }

    value = columnFilterJson.get(FUNCTION_TYPE);
    if (checkNotNull(value, false,
            FiltersConstants.INSTANCE.json_datasetlookup_columnfilter_null_functiontype())) {
        functionType = value.isString() != null ? value.isString().stringValue() : null;
    }

    value = columnFilterJson.get(FUNCTION_TERMS);

    if (isCoreFilter(functionType)) {
        CoreFunctionFilter cff = new CoreFunctionFilter();
        cff.setColumnId(columnId);
        cff.setType(CoreFunctionType.getByName(functionType));

        if (checkNotNull(value, false,
                FiltersConstants.INSTANCE.json_datasetlookup_corefunction_null_params())) {
            cff.setParameters(parseCoreFunctionParameters(value.isArray()).toArray(new Comparable[] {}));
        }

        return cff;

    } else if (isLogicalFilter(functionType)) {
        LogicalExprFilter lef = new LogicalExprFilter();
        lef.setColumnId(columnId);
        lef.setLogicalOperator(LogicalExprType.getByName(functionType));

        if (checkNotNull(value, false, FiltersConstants.INSTANCE.json_datasetlookup_logexpr_null_params())) {
            // Logical expression terms are an an array of column filters
            lef.setLogicalTerms(parseColumnFilters(value.isArray()));
        }

        return lef;
    } else
        throw new RuntimeException(FiltersConstants.INSTANCE.json_datasetlookup_columnfilter_wrong_type());
}

From source file:org.jbpm.console.ng.df.client.filter.json.DataSetLookupJSONMarshaller.java

License:Apache License

private DataSetGroup parseDataSetGroup(JSONObject dataSetGroupJson) {
    if (dataSetGroupJson == null)
        return null;

    DataSetGroup dataSetGroup = new DataSetGroup();

    dataSetGroup.setColumnGroup(null);//from  w w  w.j  a va2 s . com
    JSONValue value = dataSetGroupJson.get(COLUMNGROUP);
    if (value != null)
        dataSetGroup.setColumnGroup(parseColumnGroup(value.isObject()));

    List<GroupFunction> groupFunctions = parseGroupFunctions(
            (value = dataSetGroupJson.get(GROUPFUNCTIONS)) != null ? value.isArray() : null);
    if (groupFunctions != null)
        dataSetGroup.getGroupFunctions().addAll(groupFunctions);

    dataSetGroup.setSelectedIntervalList(null);
    value = dataSetGroupJson.get(SELECTEDINTERVALS);
    if (value != null)
        dataSetGroup.setSelectedIntervalList(parseSelectedIntervals(value.isArray()));

    dataSetGroup.setJoin(false);
    value = dataSetGroupJson.get(JOIN);
    if (value != null)
        dataSetGroup.setJoin(Boolean.valueOf(value.isString().stringValue()));

    return dataSetGroup;
}

From source file:org.jbpm.console.ng.df.client.filter.json.DataSetLookupJSONMarshaller.java

License:Apache License

private ColumnGroup parseColumnGroup(JSONObject columnGroupJson) {
    if (columnGroupJson == null)
        return null;
    ColumnGroup columnGroup = new ColumnGroup();
    JSONValue value = columnGroupJson.get(SOURCEID);
    columnGroup.setSourceId(value != null ? value.isString().stringValue() : null);
    columnGroup.setColumnId(/*  ww  w.ja va  2s. co m*/
            (value = columnGroupJson.get(COLUMNID)) != null ? value.isString().stringValue() : null);
    columnGroup.setStrategy((value = columnGroupJson.get(GROUPSTRATEGY)) != null
            ? GroupStrategy.getByName(value.isString().stringValue())
            : null);
    columnGroup.setMaxIntervals((value = columnGroupJson.get(MAXINTERVALS)) != null
            ? Integer.parseInt(value.isString().stringValue())
            : -1);
    columnGroup.setIntervalSize(
            (value = columnGroupJson.get(INTERVALSIZE)) != null ? value.isString().stringValue() : null);
    columnGroup.setEmptyIntervalsAllowed((value = columnGroupJson.get(EMPTYINTERVALS)) != null
            ? Boolean.valueOf(value.isString().stringValue())
            : false);
    columnGroup.setAscendingOrder(
            (value = columnGroupJson.get(ASCENDING)) != null ? Boolean.valueOf(value.isString().stringValue())
                    : false);
    columnGroup.setFirstMonthOfYear((value = columnGroupJson.get(FIRSTMONTHOFYEAR)) != null
            ? Month.getByName(value.isString().stringValue())
            : null);
    columnGroup.setFirstDayOfWeek((value = columnGroupJson.get(FIRSTDAYOFWEEK)) != null
            ? DayOfWeek.getByName(value.isString().stringValue())
            : null);
    return columnGroup;
}

From source file:org.jbpm.console.ng.df.client.filter.json.DisplayerSettingsJSONMarshaller.java

License:Apache License

public DisplayerSettings fromJsonString(String jsonString) {
    DisplayerSettings ds = new DisplayerSettings();

    if (!StringUtils.isBlank(jsonString)) {

        JSONObject parseResult = JSONParser.parseStrict(jsonString).isObject();

        if (parseResult != null) {

            // UUID
            JSONValue uuidValue = parseResult.get(SETTINGS_UUID);
            ds.setUUID(uuidValue != null && uuidValue.isString() != null ? uuidValue.isString().stringValue()
                    : null);//from  w  w w  . ja v  a  2  s .  c om

            // First look if a dataset 'on-the-fly' has been specified
            JSONValue data = parseResult.get(DATASET_PREFIX);
            if (data != null) {
                DataSet dataSet = dataSetJSONMarshaller.fromJson(data.isObject());
                ds.setDataSet(dataSet);

                // Remove from the json input so that it doesn't end up in the settings map.
                parseResult.put(DATASET_PREFIX, null);

                // If none was found, look for a dataset lookup definition
            } else if ((data = parseResult.get(DATASET_LOOKUP_PREFIX)) != null) {
                DataSetLookup dataSetLookup = dataSetLookupJSONMarshaller.fromJson(data.isObject());
                ds.setDataSetLookup(dataSetLookup);

                // Remove from the json input so that it doesn't end up in the settings map.
                parseResult.put(DATASET_LOOKUP_PREFIX, null);
            } else {
                throw new RuntimeException(
                        FiltersConstants.INSTANCE.json_displayersettings_dataset_lookup_notspecified());
            }

            // Parse the columns settings
            JSONValue columns = parseResult.get(COLUMNS_PREFIX);
            if (columns != null) {
                List<ColumnSettings> columnSettingsList = parseColumnsFromJson(columns.isArray());
                ds.setColumnSettingsList(columnSettingsList);

                // Remove from the json input so that it doesn't end up in the settings map.
                parseResult.put(COLUMNS_PREFIX, null);
            }

            // Now parse all other settings
            ds.setSettingsFlatMap(parseSettingsFromJson(parseResult));
        }
    }
    return ds;
}

From source file:org.jbpm.console.ng.df.client.filter.json.DisplayerSettingsJSONMarshaller.java

License:Apache License

private String getNodeValue(JSONObject node, String path) {
    if (node == null || StringUtils.isBlank(path))
        return null;

    int separatorIndex = path.lastIndexOf('.');
    String subNodesPath = separatorIndex > 0 ? path.substring(0, separatorIndex) : "";
    String leaf = separatorIndex > 0 ? path.substring(separatorIndex + 1) : path;

    JSONObject childNode = findNode(node, subNodesPath, false);
    String value = null;//from   ww w.ja v  a2s.c o  m
    if (childNode != null) {
        JSONValue jsonValue = childNode.get(leaf);
        if (jsonValue != null && jsonValue.isString() != null)
            value = jsonValue.isString().stringValue();
    }
    return value;
}

From source file:org.jbpm.console.ng.df.client.filter.json.DisplayerSettingsJSONMarshaller.java

License:Apache License

private List<ColumnSettings> parseColumnsFromJson(JSONArray columnsJsonArray) {
    List<ColumnSettings> columnSettingsList = new ArrayList<ColumnSettings>();
    if (columnsJsonArray == null)
        return columnSettingsList;

    for (int i = 0; i < columnsJsonArray.size(); i++) {
        JSONObject columnJson = columnsJsonArray.get(i).isObject();
        ColumnSettings columnSettings = new ColumnSettings();
        columnSettingsList.add(columnSettings);

        JSONValue value = columnJson.get(COLUMN_ID);
        if (value == null)
            throw new RuntimeException(FiltersConstants.INSTANCE.json_columnsettings_null_columnid());
        columnSettings.setColumnId(value.isString().stringValue());

        value = columnJson.get(COLUMN_NAME);
        if (value != null)
            columnSettings.setColumnName(value.isString().stringValue());

        value = columnJson.get(COLUMN_EXPRESSION);
        if (value != null)
            columnSettings.setValueExpression(value.isString().stringValue());

        value = columnJson.get(COLUMN_PATTERN);
        if (value != null)
            columnSettings.setValuePattern(value.isString().stringValue());

        value = columnJson.get(COLUMN_EMPTY);
        if (value != null)
            columnSettings.setEmptyTemplate(value.isString().stringValue());
    }/*from  ww w . j  ava  2 s. c  om*/
    return columnSettingsList;
}

From source file:org.jbpm.console.ng.df.client.filter.json.DisplayerSettingsJSONMarshaller.java

License:Apache License

private void fillRecursive(String parentPath, JSONObject json, Map<String, String> settings) {
    String sb = StringUtils.isBlank(parentPath) ? "" : parentPath + ".";
    for (String key : json.keySet()) {
        String path = sb + key;//from   w  ww  .  jav a  2s  .co m
        JSONValue value = json.get(key);
        if (value.isObject() != null)
            fillRecursive(path, value.isObject(), settings);
        else if (value.isString() != null)
            settings.put(path, value.isString().stringValue());
    }
}

From source file:org.jbpm.console.ng.gc.forms.client.display.displayers.util.JSNIHelper.java

License:Apache License

public Map<String, String> parseParams(JSONObject jsonParams) {
    Map<String, String> params = new HashMap<String, String>();

    for (String key : jsonParams.keySet()) {
        JSONValue value = jsonParams.get(key);
        if (value != null) {
            if (value.isString() != null)
                params.put(key, value.isString().stringValue());
            else/*from   w w w. jav a2 s  .com*/
                params.put(key, value.toString());
        }
    }

    return params;
}