Example usage for com.google.gson JsonArray iterator

List of usage examples for com.google.gson JsonArray iterator

Introduction

In this page you can find the example usage for com.google.gson JsonArray iterator.

Prototype

public Iterator<JsonElement> iterator() 

Source Link

Document

Returns an iterator to navigate the elements of the array.

Usage

From source file:org.opengrid.data.impl.PlenarioDataProvider.java

private OpenGridDataset getPlenarioDataset(JsonObject datasets, String datasetId) {
    OpenGridDataset dataset = new OpenGridDataset();

    try {//www .  j  a  v  a 2s. c o  m
        JsonArray objects = (JsonArray) datasets.get("objects");

        Iterator itr = objects.iterator();

        String defaultAttribution = properties.getStringProperty("plenario.attribution.default");
        int counter = 0;
        String defaultDataset = properties.getStringProperty("plenario.dataset.default");
        while (itr.hasNext()) {
            JsonObject object = (JsonObject) itr.next();
            String datasetName = object.get("dataset_name").toString().replace("\"", "");

            if (datasetName.equals(datasetId)) {
                dataset = getOpenGridDatasetFromPlenarioObject(defaultAttribution, defaultDataset, object, true,
                        false, counter);
                break;
            }
            counter++;
        }

        return dataset;

    } catch (Exception e) {
        throw new ServiceException("Error accessing Plenar.io Datasets API.");
    }
}

From source file:org.openhab.binding.gardena.internal.model.deser.PropertyValueDeserializer.java

License:Open Source License

@Override
public PropertyValue deserialize(JsonElement element, Type type, JsonDeserializationContext ctx)
        throws JsonParseException {
    if (element.isJsonObject()) {
        JsonObject jsonObj = element.getAsJsonObject();
        if (jsonObj.has(PROPERTY_DURATION)) {
            long duration = jsonObj.get(PROPERTY_DURATION).getAsLong();
            if (duration != 0) {
                duration = Math.round(duration / 60.0);
            }//  w  w  w. ja v a 2  s .c o  m
            return new PropertyValue(String.valueOf(duration));
        } else if (jsonObj.has(PROPERTY_TYPE)) {
            return new PropertyValue(jsonObj.get(PROPERTY_TYPE).getAsString());
        } else if (jsonObj.has(PROPERTY_MAC) && jsonObj.has(PROPERTY_ISCONNECTED)) {
            // ignore known gateway properties
            return new PropertyValue();
        } else {
            logger.warn("Unsupported json value object, returning empty value");
            return new PropertyValue();
        }

    } else if (element.isJsonArray()) {
        JsonArray jsonArray = element.getAsJsonArray();
        return new PropertyValue(StringUtils.join(jsonArray.iterator(), ","));
    } else {
        return new PropertyValue(element.getAsString());
    }
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public ArrayList<Record> getRecordArrayList(ValueType valueType) throws ClassCastException {

    ArrayList<Record> recordList = new ArrayList<Record>();

    JsonElement jse = jsonObject.get(Const.RECORDS);
    if (jse != null && jse.isJsonArray()) {
        JsonArray jsa = jse.getAsJsonArray();

        Iterator<JsonElement> iteratorJsonArray = jsa.iterator();
        while (iteratorJsonArray.hasNext()) {
            recordList.add(getRecord(valueType));
        }/*from w  ww  .  j av a 2 s.  c o  m*/
    }
    if (recordList.size() == 0) {
        recordList = null;
    }
    return recordList;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public ArrayList<String> getStringArrayList(String listName) {

    ArrayList<String> resultList = new ArrayList<String>();

    JsonElement jse = jsonObject.get(listName);
    if (jse != null && jse.isJsonArray()) {
        JsonArray jsa = jse.getAsJsonArray();

        Iterator<JsonElement> iteratorJsonArray = jsa.iterator();
        while (iteratorJsonArray.hasNext()) {
            resultList.add(iteratorJsonArray.next().toString());
        }//from w  w  w .  j  ava  2 s  . c om
    }
    if (resultList.size() == 0) {
        resultList = null;
    }
    return resultList;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public ArrayList<RestChannel> getRestChannelArrayList() throws ClassCastException {

    ArrayList<RestChannel> recordList = new ArrayList<RestChannel>();
    JsonElement jse = jsonObject.get("records");
    JsonArray jsa;

    if (!jse.isJsonNull() && jse.isJsonArray()) {

        jsa = jse.getAsJsonArray();//  w  ww . java2s  .c  o  m
        Iterator<JsonElement> jseIterator = jsa.iterator();

        while (jseIterator.hasNext()) {
            RestChannel rc = new RestChannel();
            JsonObject jsoIterated = jseIterator.next().getAsJsonObject();
            rc.setId(jsoIterated.get(Const.ID).getAsString());
            rc.setType(gson.fromJson(jsoIterated.get(Const.TYPE), ValueType.class)); // TODO: need valueType in json
            // or something else
            // otherwise null pointer
            // exception
            rc.setRecord(getRecord(jsoIterated, rc.getType()));

            recordList.add(rc);
        }
    }
    if (recordList.size() == 0) {
        return null;
    }
    return recordList;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public List<DeviceScanInfo> getDeviceScanInfoList() {

    List<DeviceScanInfo> returnValue = new ArrayList<DeviceScanInfo>();
    JsonElement jse = jsonObject.get(Const.CHANNELS); // TODO: another name?
    JsonArray jsa;

    if (jse.isJsonArray()) {
        jsa = jse.getAsJsonArray();//from   w  ww.j  av a 2s . c o  m
        Iterator<JsonElement> jseIterator = jsa.iterator();
        while (jseIterator.hasNext()) {

            JsonObject jso = jseIterator.next().getAsJsonObject();
            // String id = jso.get(Const.ID).getAsString();
            String deviceAddress = jso.get(Const.DEVICEADDRESS).getAsString();
            String settings = jso.get(Const.SETTINGS).getAsString();
            String description = jso.get(Const.DESCRIPTION).getAsString();
            returnValue.add(new DeviceScanInfo(deviceAddress, settings, description));
        }
    } else {
        returnValue = null;
    }
    return returnValue;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public List<ChannelScanInfo> getChannelScanInfoList() {

    List<ChannelScanInfo> returnValue = new ArrayList<ChannelScanInfo>();
    JsonElement jse = jsonObject.get(Const.CHANNELS); // TODO: another name?
    JsonArray jsa;

    if (jse.isJsonArray()) {
        jsa = jse.getAsJsonArray();/*  w w  w  .j av  a 2  s  .c om*/
        Iterator<JsonElement> jseIterator = jsa.iterator();
        while (jseIterator.hasNext()) {

            JsonObject jso = jseIterator.next().getAsJsonObject();
            String channelAddress = jso.get(Const.CHANNELADDRESS).getAsString();
            ValueType valueType = ValueType.valueOf(jso.get(Const.VALUETYPE).getAsString());
            int valueTypeLength = jso.get(Const.VALUETYPELENGTH).getAsInt();
            String description = jso.get(Const.DESCRIPTION).getAsString();
            returnValue.add(new ChannelScanInfo(channelAddress, description, valueType, valueTypeLength));
        }
    } else {
        returnValue = null;
    }
    return returnValue;
}

From source file:org.openmuc.framework.server.restws.JsonHelper.java

License:Open Source License

public static ArrayList<RestChannel> channelsJsonToRecords(String jsontext) {
    ArrayList<RestChannel> recList = new ArrayList<RestChannel>();
    JsonArray jsa = gson.fromJson(jsontext, JsonArray.class);
    RestChannel rc = new RestChannel();
    while (jsa.iterator().hasNext()) {
        rc = gson.fromJson(jsa.iterator().next().getAsJsonObject(), RestChannel.class);
        recList.add(rc);//  w w  w.j  a va2 s  . co  m
    }
    if (recList.size() == 0) {
        return null;
    }
    return recList;
}

From source file:org.opentdc.base.store.StoreManager.java

License:Open Source License

/**
 * Find objects matching the query.// w w w  .  jav  a  2  s.  c o  m
 * 
 * @param context
 * @param path
 * @param queryType
 * @param query
 * @param position
 * @param size
 * @param classOfT
 * @return
 * @throws IOException
 */
public static <T> List<T> find(ServletContext context, String path, String query, int position, int size,
        Comparator<T> comparator, Class<T> classOfT) throws Exception {
    Object matchingElements = JsonPath.using(jsonProvider)
            .parse(new SequenceInputStream(new JsonResourceEnumeration(context, path)))
            .read("$.object" + (query == null ? "" : "[?" + query + "]"));
    JsonArray objects = null;
    if (matchingElements instanceof JsonArray) {
        objects = (JsonArray) matchingElements;
    } else {
        objects = ((JsonObject) matchingElements).getAsJsonArray("elements");
    }
    List<T> resultSet = new ArrayList<T>();
    for (Iterator<JsonElement> i = objects.iterator(); i.hasNext();) {
        JsonObject json = (JsonObject) i.next();
        resultSet.add(json.has("members") ? JsonFormat.toObject(json.get("members"), classOfT)
                : JsonFormat.toObject(json, classOfT));
    }
    if (comparator != null) {
        Collections.sort(resultSet, comparator);
    }
    return position < resultSet.size()
            ? resultSet.subList(position, Math.min(position + size, resultSet.size()))
            : Collections.<T>emptyList();
}

From source file:org.qcert.runtime.BinaryOperators.java

License:Apache License

public static JsonElement bag_minus(JsonElement e1, JsonElement e2) {
    final DataComparator comp = DataComparator.getComparator();
    // Note that collections are immutable, so we need to copy
    // before removing stuff
    final JsonArray ec1 = copyArray(asColl(e1));
    final JsonArray ec2 = asColl(e2);

    for (final JsonElement elem2 : ec2) {
        final Iterator<JsonElement> it = ec1.iterator();
        while (it.hasNext()) {
            JsonElement elem1 = it.next();
            if (comp.compare(elem1, elem2) == 0) {
                it.remove();//from  w w  w.  j  a  v a2  s .  co m
            }
        }
    }
    return ec1;
}