Example usage for com.google.gson JsonElement getAsJsonArray

List of usage examples for com.google.gson JsonElement getAsJsonArray

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonArray.

Prototype

public JsonArray getAsJsonArray() 

Source Link

Document

convenience method to get this element as a JsonArray .

Usage

From source file:com.sldeditor.exportdata.esri.symbols.MultiLayerMarkerSymbol.java

License:Open Source License

/**
 * Convert./*from w ww  .  j a  v a2 s .c o m*/
 *
 * @param rule the rule
 * @param element the element
 * @param layerName the layer name
 * @param transparency the transparency
 */
@Override
public void convert(Rule rule, JsonElement element, String layerName, int transparency) {
    if (rule == null)
        return;
    if (element == null)
        return;

    JsonArray layerArray = element.getAsJsonArray();

    List<Symbolizer> symbolizerList = rule.symbolizers();

    if (layerArray.size() > 0) {
        for (int index = 0; index < layerArray.size(); index++) {
            JsonObject obj = layerArray.get(index).getAsJsonObject();

            List<Graphic> markerList = SymbolManager.getInstance()
                    .getMarkerList(obj.get(MultiLayerMarkerSymbolKeys.MARKER).getAsJsonObject());

            if (markerList != null) {
                for (Graphic marker : markerList) {
                    PointSymbolizer pointSymbolizer = styleFactory.createPointSymbolizer(marker, null);

                    symbolizerList.add(pointSymbolizer);
                }
            }
        }
    }
}

From source file:com.slx.funstream.utils.ChatMessageDeserializer.java

License:Apache License

@Override
public ChatMessage[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json.isJsonArray()) {
        JsonArray jsonMessages = json.getAsJsonArray();
        ChatMessage[] messages = new ChatMessage[jsonMessages.size()];

        for (int i = 0; i < jsonMessages.size(); i++) {
            JsonObject jsonMessage = jsonMessages.get(i).getAsJsonObject();
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setId(jsonMessage.get(ID).getAsLong());
            chatMessage.setChannel(jsonMessage.get(CHANNEL).getAsLong());
            chatMessage/*from w  w w  .  j a  va  2  s .  c om*/
                    .setFrom(context.deserialize(jsonMessage.get(FROM).getAsJsonObject(), CurrentUser.class));
            chatMessage.setTo(context.deserialize(jsonMessage.get(TO).getAsJsonObject(), CurrentUser.class));
            chatMessage.setText(jsonMessage.get(TEXT).getAsString());
            chatMessage.setTime(jsonMessage.get(TIME).getAsString());
            messages[i] = chatMessage;
        }
        return messages;
    }
    return null;

}

From source file:com.slx.funstream.utils.SmileDeserializer.java

License:Apache License

@Override
public List<Smile> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    List<Smile> smileList = new ArrayList<>();
    JsonArray smileysJson = json.getAsJsonArray();

    for (int i = 0; i < smileysJson.size(); i++) {
        JsonObject smileyJson = smileysJson.get(i).getAsJsonObject();
        Smile smile = new Smile();
        smile.setId(smileyJson.get(SMILE_ID).getAsInt());
        smile.setTab(smileyJson.get(SMILE_TAB).getAsInt());
        smile.setLevel(smileyJson.get(SMILE_LEVEL).getAsInt());
        smile.setHeight(smileyJson.get(SMILE_HEIGHT).getAsInt());
        String code = PATTERN_COLOMN + smileyJson.get(SMILE_CODE).getAsString() + PATTERN_COLOMN;
        smile.setCode(code);//  w  w w .  ja  v a2s  .co  m
        smile.setUrl(smileyJson.get(SMILE_URL).getAsString());
        //         smile.setPosition(smileJson.get(SMILE_POS).getAsInt());
        smile.setHeight(smileyJson.get(SMILE_HEIGHT).getAsInt());
        smile.setWidth(smileyJson.get(SMILE_WIDTH).getAsInt());
        smile.setAnimated(smileyJson.get(SMILE_ANIMATED).getAsBoolean());

        smileList.add(smile);
    }

    return smileList;
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

License:Open Source License

private @CheckForNull Object toSimpleJavaType(JsonElement jsonValue) {
    if (jsonValue == null)
        return null; //VR
    Object value = null;/*from w w w.  j  a  v  a 2  s .c  om*/
    if (!jsonValue.isJsonNull()) {
        if (jsonValue.isJsonPrimitive()) {
            JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
            if (primitive.isBoolean()) {
                value = Boolean.valueOf(primitive.getAsBoolean());
            } else if (primitive.isNumber()) {
                value = Double.valueOf(primitive.getAsDouble());
            } else if (primitive.isString()) {
                value = primitive.getAsString();
            } else {
                throw UnexpectedException.forUnexpectedCodeBranchExecuted();
            }
        } else if (jsonValue.isJsonArray()) {
            //This simply does not work (?) 
            JsonArray array = jsonValue.getAsJsonArray();
            Object[] result = new Object[array.size()];
            for (int i = 0; i < array.size(); i++) {
                result[i] = toSimpleJavaType(array.get(i));
            }
            value = result;
        } else if (jsonValue.isJsonObject()) {
            //This simply does not work (?)
            //value = getGson().fromJson(jsonValue, Map.class );

            JsonObject obj = jsonValue.getAsJsonObject();
            Iterator<Entry<String, JsonElement>> properties = obj.entrySet().iterator();
            Map<String, Object> result = new HashMap<String, Object>();
            while (properties.hasNext()) {
                Entry<String, JsonElement> property = properties.next();
                JsonElement propertyValue = property.getValue();
                result.put(property.getKey(), toSimpleJavaType(propertyValue));
            }
            value = result;
        } else {
            throw UnexpectedException.forUnexpectedCodeBranchExecuted();
        }
    }

    return value;
}

From source file:com.springer.api.services.impl.BaseSpringerQuery.java

License:Apache License

@Override
public PagedList<E> list() {
    InputStream jsonContent = null;
    try {/*from   w w w .j a v  a 2 s .c o m*/
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            JsonObject object = response.getAsJsonObject();
            setApiKey(object.get("apiKey").getAsString());
            setQuery(object.get("query").getAsString());
            List<Cursor> cursors = getGsonBuilder().create().fromJson(object.get("result"),
                    new TypeToken<List<Cursor>>() {
                    }.getType());
            PagedArrayList<E> responseList = new PagedArrayList<E>();
            ;
            if (!cursors.isEmpty()) {
                responseList.setCursor(cursors.get(0));
            }
            JsonElement jsonElement = object.get("records");
            if (jsonElement.isJsonArray()) {
                JsonArray results = jsonElement.getAsJsonArray();
                for (JsonElement result : results) {
                    E element = unmarshall(result);
                    responseList.add(element);
                }
            }

            jsonElement = object.get("facets");

            if (jsonElement != null) {
                List<Facet> facets = getGsonBuilder().create().fromJson(jsonElement,
                        new TypeToken<List<Facet>>() {
                        }.getType());
                setFacets(facets);
            }

            return responseList;
        }
        throw new SpringerException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new SpringerException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.squareup.wire.MessageTypeAdapter.java

License:Apache License

private Object parseValue(Label label, Type valueType, JsonElement valueElement) {
    if (label.isRepeated()) {
        List<Object> valueList = new ArrayList<Object>();
        for (JsonElement element : valueElement.getAsJsonArray()) {
            valueList.add(readJson(valueType, element));
        }// w  w w. jav  a2  s . c o m
        return valueList;
    } else {
        return readJson(valueType, valueElement);
    }
}

From source file:com.stackmob.sdk.model.StackMobModel.java

License:Apache License

protected void fillFieldFromJson(String jsonName, JsonElement json) throws StackMobException {
    try {/* w w  w .jav a  2  s. co  m*/
        if (jsonName.equals(getIDFieldName())) {
            // The id field is special, its name doesn't match the field
            setID(json.getAsJsonPrimitive().getAsString());
        } else {
            // undo the toLowerCase we do when sending out the json
            String fieldName = getFieldName(jsonName);
            if (fieldName != null) {
                Field field = getField(fieldName);
                field.setAccessible(true);
                if (getMetadata(fieldName) == MODEL) {
                    // Delegate any expanded relations to the appropriate object
                    StackMobModel relatedModel = (StackMobModel) field.get(this);
                    // If there's a model with the same id, keep it. Otherwise create a new one
                    if (relatedModel == null || !relatedModel.hasSameID(json)) {
                        relatedModel = (StackMobModel) field.getType().newInstance();
                    }
                    relatedModel.fillFromJson(json);
                    field.set(this, relatedModel);
                } else if (getMetadata(fieldName) == MODEL_ARRAY) {
                    Class<? extends StackMobModel> actualModelClass = (Class<? extends StackMobModel>) SerializationMetadata
                            .getComponentClass(field);
                    Collection<StackMobModel> existingModels = getFieldAsCollection(field);
                    List<StackMobModel> newModels = updateModelListFromJson(json.getAsJsonArray(),
                            existingModels, actualModelClass);
                    setFieldFromList(field, newModels, actualModelClass);
                } else {
                    // Let gson do its thing
                    field.set(this, gson.fromJson(json, field.getType()));
                }
            }
        }
    } catch (NoSuchFieldException ignore) {
    } catch (IllegalAccessException e) {
        throw new StackMobException(e.getMessage());
    } catch (InstantiationException e) {
        throw new StackMobException(e.getMessage());
    }
}

From source file:com.strato.hidrive.api.JSONDataReader.java

License:Apache License

@Override
public List<DataReader> readDataReaderListWithName(String name) {
    if (!isFieldExists(name)) {
        return null;
    }/*from   w  w  w  .ja v  a  2  s .c  o m*/

    JsonArray defaultValue = null;
    JsonArray array = get(name, defaultValue, new ElementValue<JsonArray>() {
        @Override
        public JsonArray value(JsonElement jsonElement) {
            return jsonElement.getAsJsonArray();
        }
    });

    List<DataReader> dataReaders = new ArrayList<DataReader>();
    for (JsonElement jsonObject : array) {
        dataReaders.add(new JSONDataReader((JsonObject) jsonObject));
    }
    return dataReaders;

}

From source file:com.sugestio.client.SugestioClient.java

License:Open Source License

/**
 * Get personal recommendations for the given user.
 * @param userid the user/* w  ww  . j ava2  s  .  com*/
 * @param parameters query parameters
 * @return recommendations
 * @throws Exception
 */
public List<Recommendation> getRecommendations(String userid, Map<String, String> parameters) throws Exception {

    JsonElement response = doGet("/users/" + userid + "/recommendations.json", parameters, false);

    if (response != null) {
        Type listType = new TypeToken<List<Recommendation>>() {
        }.getType();
        return gson.fromJson(response.getAsJsonArray(), listType);
    } else {
        return new ArrayList<Recommendation>();
    }

}