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

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

Introduction

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

Prototype

public JSONArray isArray() 

Source Link

Document

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

Usage

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

License:Apache License

/**
 * The transformation from Jackson's JSON to Errai JSON contains the following steps:
 * <ul>//from   ww  w. jav  a 2  s. c om
 * <li>Recursively add an incremented OBJECT_ID to every JSON object</li>
 * <li>If a number is encountered, wrap it in a new JSON object with an OBJECT_ID and
 * NUMERIC_VALUE property</li>
 * <li>If an array is encountered, wrap it in a new JSON object with an OBJECT_ID and
 * QUALIFIED_VALUE property</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 objectId
 *          last used object id
 * @return modified JSON value
 */
private static JSONValue fromJackson(JSONValue val, String key, JSONObject parent, int[] objectId) {
    JSONObject obj;
    JSONNumber num;
    JSONArray arr;

    if ((obj = val.isObject()) != null) {
        obj.put(OBJECT_ID, new JSONString(new Integer(++objectId[0]).toString()));

        for (String k : obj.keySet()) {
            fromJackson(obj.get(k), k, obj, objectId);
        }
    } else if ((num = val.isNumber()) != null) {
        JSONObject numObject = new JSONObject();
        numObject.put(OBJECT_ID, new JSONString(new Integer(++objectId[0]).toString()));
        numObject.put(NUMERIC_VALUE, num);
        if (parent != null) {
            parent.put(key, numObject);
        } else {
            val = numObject;
        }
    } else if ((arr = val.isArray()) != null) {
        JSONObject arrayObject = new JSONObject();
        arrayObject.put(OBJECT_ID, new JSONString(new Integer(++objectId[0]).toString()));
        arrayObject.put(QUALIFIED_VALUE, arr);
        if (parent != null) {
            parent.put(key, arrayObject);
        } else {
            val = arrayObject;
        }

        for (int i = 0; i < arr.size(); i++) {
            arr.set(i, fromJackson(arr.get(i), QUALIFIED_VALUE, null, objectId));
        }
    }

    return val;
}

From source file:org.jboss.errai.jpa.client.local.ErraiIdentifiableType.java

License:Apache License

/**
 * Converts the given JSONValue, which represents an instance of this entity
 * type, into the actual instance of this entity type that exists in the given
 * EntityManager's persistence context. References to other entities are
 * recursively retrieved from the EntityManager.
 *
 * @param em/*from  ww w .j  a v  a 2 s .  c  o m*/
 *          The EntityManager that owns this entity type and houses the
 *          persistence context.
 * @param jsonValue
 *          A value that represents an instance of this entity type.
 * @return A managed entity that is in the given EntityManager's persistence
 *         context.
 */
@Override
public X fromJson(EntityManager em, JSONValue jsonValue) {
    final ErraiEntityManager eem = (ErraiEntityManager) em;

    Key<X, ?> key = keyFromJson(jsonValue);

    X entity = eem.getPartiallyConstructedEntity(key);
    if (entity != null) {
        return entity;
    }

    entity = newInstance();
    try {
        eem.putPartiallyConstructedEntity(key, entity);
        for (Attribute<? super X, ?> a : getAttributes()) {
            ErraiAttribute<? super X, ?> attr = (ErraiAttribute<? super X, ?>) a;
            JSONValue attrJsonValue = jsonValue.isObject().get(attr.getName());

            // this attribute did not exist when the entity was originally persisted; skip it.
            if (attrJsonValue == null)
                continue;

            switch (attr.getPersistentAttributeType()) {
            case ELEMENT_COLLECTION:
            case EMBEDDED:
            case BASIC:
                parseInlineJson(entity, attr, attrJsonValue, eem);
                break;

            case MANY_TO_MANY:
            case MANY_TO_ONE:
            case ONE_TO_MANY:
            case ONE_TO_ONE:
                if (attr instanceof ErraiSingularAttribute) {
                    parseSingularJsonReference(entity, (ErraiSingularAttribute<? super X, ?>) attr,
                            attrJsonValue, eem);
                } else if (attr instanceof ErraiPluralAttribute) {
                    parsePluralJsonReference(entity, (ErraiPluralAttribute<? super X, ?, ?>) attr,
                            attrJsonValue.isArray(), eem);
                } else {
                    throw new PersistenceException("Unknown attribute type " + attr);
                }
            }
        }
        return entity;
    } finally {
        eem.removePartiallyConstructedEntity(key);
    }
}

From source file:org.jboss.gwt.elemento.sample.common.TodoItemRepository.java

License:Open Source License

private LinkedHashMap<String, TodoItem> load() {
    LinkedHashMap<String, TodoItem> items = new LinkedHashMap<>();
    if (storage != null) {
        String json = storage.getItem(key);
        if (json != null) {
            JSONValue jsonValue = JSONParser.parseStrict(json);
            if (jsonValue != null) {
                JSONArray jsonArray = jsonValue.isArray();
                if (jsonArray != null) {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        AutoBean<TodoItem> bean = AutoBeanCodex.decode(beanFactory, TodoItem.class,
                                jsonArray.get(i).toString());
                        TodoItem todoItem = bean.as();
                        items.put(todoItem.getId(), todoItem);
                    }//from   w  w  w .j a v a  2  s . c  o  m
                }
            }
        }
    }
    return items;
}

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

License:Apache License

public DataSetLookup fromJson(JSONObject json) {
    if (json == null)
        return null;
    DataSetLookup dataSetLookup = new DataSetLookup();

    dataSetLookup.setDataSetUUID(json.get(UUID) != null ? json.get(UUID).isString().stringValue() : null);
    dataSetLookup.setNumberOfRows(/*from  w  w w.j av  a 2 s  . co m*/
            json.get(ROWCOUNT) != null ? Integer.parseInt(json.get(ROWCOUNT).isString().stringValue(), 10)
                    : -1);
    dataSetLookup.setRowOffset(
            json.get(ROWOFFSET) != null ? Integer.parseInt(json.get(ROWOFFSET).isString().stringValue(), 10)
                    : 0);

    List<DataSetOp> dataSetOpList = dataSetLookup.getOperationList();

    Collection c = null;
    JSONValue array = json.get(FILTEROPS);

    if ((c = parseFilterOperations(array != null ? array.isArray() : null)) != null)
        dataSetOpList.addAll(c);

    if ((c = parseGroupOperations((array = json.get(GROUPOPS)) != null ? array.isArray() : null)) != null)
        dataSetOpList.addAll(c);

    if ((c = parseSortOperations((array = json.get(SORTOPS)) != null ? array.isArray() : null)) != null)
        dataSetOpList.addAll(c);

    return dataSetLookup;
}

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  ww w.  j  a  v a2 s  .c om*/
    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);/* w ww.  ja v  a 2  s. c  o m*/
    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.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);/*  w  w w. j a va 2 s  .co m*/

            // 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.formapi.client.form.FormRepresentationDecoderClient.java

License:Apache License

public List<FBScript> decodeScripts(JSONValue json) throws FormEncodingException {
    List<FBScript> retval = new ArrayList<FBScript>();
    if (json != null && json.isArray() != null) {
        JSONArray array = json.isArray();
        for (int index = 0; index < array.size(); index++) {
            JSONValue elem = array.get(index);
            JSONObject jsonObj = elem.isObject();
            retval.add((FBScript) decode(asMap(jsonObj)));
        }/*from w w w.j  a  v a 2  s .c  om*/
    }
    return retval;
}

From source file:org.jbpm.formapi.client.form.FormRepresentationDecoderClient.java

License:Apache License

public List<FormItemRepresentation> decodeItems(JSONValue json) throws FormEncodingException {
    List<FormItemRepresentation> retval = new ArrayList<FormItemRepresentation>();
    if (json != null && json.isArray() != null) {
        JSONArray array = json.isArray();
        for (int index = 0; index < array.size(); index++) {
            JSONValue elem = array.get(index);
            JSONObject jsonObj = elem.isObject();
            retval.add((FormItemRepresentation) decode(asMap(jsonObj)));
        }//from  w w  w. j  a v  a2 s  .c o m
    }
    return retval;
}

From source file:org.jbpm.formapi.client.form.FormRepresentationDecoderClient.java

License:Apache License

public List<FBValidation> decodeValidations(JSONValue json) throws FormEncodingException {
    List<FBValidation> retval = new ArrayList<FBValidation>();
    if (json != null && json.isArray() != null) {
        JSONArray array = json.isArray();
        for (int index = 0; index < array.size(); index++) {
            JSONValue elem = array.get(index);
            JSONObject jsonObj = elem.isObject();
            retval.add((FBValidation) decode(asMap(jsonObj)));
        }//from  w  w w  .  j  a  v  a  2 s.  com
    }
    return retval;
}