Example usage for com.google.gson JsonElement getAsJsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

From source file:com.tomeokin.lspush.data.support.DateTypeConverter.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return new Date(json.getAsJsonPrimitive().getAsLong());
}

From source file:com.twitter.common.thrift.text.TTextProtocol.java

License:Apache License

@Override
public TField readFieldBegin() throws TException {
    String name = null;//w ww.ja v  a2s  .c o m

    if (!getCurrentContext().hasMoreChildren()) {
        return new TField("", UNUSED_TYPE, (short) 0);
    }

    getCurrentContext().read();

    JsonElement jsonName = getCurrentContext().getCurrentChild();

    if (!jsonName.getAsJsonPrimitive().isString()) {
        throw new RuntimeException("Expected String for a field name");
    }

    return getCurrentContext().getTFieldByName(jsonName.getAsJsonPrimitive().getAsString());
}

From source file:com.uprizer.sensearray.freetools.json.GsonPrettyPrinter.java

License:Open Source License

private List<String> toStringList(final JsonElement je) {
    if (je.isJsonPrimitive())
        return Collections.singletonList(je.getAsJsonPrimitive().toString());
    if (je.isJsonArray()) {
        final JsonArray jsonArray = je.getAsJsonArray();
        return arrayToStringList(jsonArray);
    } else if (je.isJsonObject()) {
        final JsonObject jsonObject = je.getAsJsonObject();
        return objectToStringList(jsonObject);
    } else if (je.isJsonNull()) {
        return Collections.singletonList("null");
    } else {/*from   w  w w  .j  a  va2 s.  c o  m*/
        throw new RuntimeException("Unsupported Json element: " + je.getClass().getName());
    }
}

From source file:com.vmware.xenon.common.serialization.ObjectCollectionTypeConverter.java

License:Open Source License

@Override
public Collection<Object> deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    if (!json.isJsonArray()) {
        throw new JsonParseException("Expecting a json array object but found: " + json);
    }// w w  w  .j a  v  a 2  s  .  c  o m

    Collection<Object> result;
    if (TYPE_SET.equals(type)) {
        result = new HashSet<>();
    } else if (TYPE_LIST.equals(type) || TYPE_COLLECTION.equals(type)) {
        result = new LinkedList<>();
    } else {
        throw new JsonParseException("Unexpected target type: " + type);
    }

    JsonArray jsonArray = json.getAsJsonArray();
    for (JsonElement entry : jsonArray) {
        if (entry.isJsonNull()) {
            result.add(null);
        } else if (entry.isJsonPrimitive()) {
            JsonPrimitive elem = entry.getAsJsonPrimitive();
            Object value = null;
            if (elem.isBoolean()) {
                value = elem.getAsBoolean();
            } else if (elem.isString()) {
                value = elem.getAsString();
            } else if (elem.isNumber()) {
                // We don't know if this is an integer, long, float or double...
                BigDecimal num = elem.getAsBigDecimal();
                try {
                    value = num.longValueExact();
                } catch (ArithmeticException e) {
                    value = num.doubleValue();
                }
            } else {
                throw new RuntimeException("Unexpected value type for json element:" + elem);
            }
            result.add(value);
        } else {
            // keep JsonElement to prevent stringified json issues
            result.add(entry);
        }
    }
    return result;
}

From source file:com.voxelplugineering.voxelsniper.service.persistence.JsonDataSource.java

License:Open Source License

/**
 * Recursively converts the given {@link JsonElement} to a
 * {@link DataContainer}./*from w ww. ja v  a 2s. c  o  m*/
 * 
 * @param rootelement The element to convert
 * @return The new {@link DataContainer}
 */
public DataContainer toContainer(JsonElement rootelement) {
    DataContainer container = new MemoryContainer("");

    if (rootelement.isJsonObject()) {
        JsonObject root = rootelement.getAsJsonObject();
        for (Entry<String, JsonElement> entry : root.entrySet()) {
            String key = entry.getKey();
            JsonElement element = entry.getValue();
            if (element.isJsonPrimitive()) {
                JsonPrimitive primitive = element.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    container.writeBoolean(key, primitive.getAsBoolean());
                } else if (primitive.isString()) {
                    container.writeString(key, primitive.getAsString());
                } else if (primitive.isNumber()) {
                    container.writeNumber(key, primitive.getAsNumber());
                }
            } else if (element.isJsonObject()) {
                container.writeContainer(key, toContainer(element));
            }
        }
    }

    return container;

}

From source file:com.voxelplugineering.voxelsniper.service.persistence.JsonDataSourceReader.java

License:Open Source License

/**
 * Recursively converts the given {@link JsonElement} to a {@link DataContainer}.
 * //from   ww  w.j a v a2  s  .c  o  m
 * @param rootelement The element to convert
 * @return The new {@link DataContainer}
 */
public DataContainer toContainer(JsonElement rootelement) {
    DataContainer container = new MemoryContainer("");

    if (rootelement.isJsonObject()) {
        JsonObject root = rootelement.getAsJsonObject();
        for (Entry<String, JsonElement> entry : root.entrySet()) {
            String key = entry.getKey();
            JsonElement element = entry.getValue();
            if (element.isJsonPrimitive()) {
                JsonPrimitive primitive = element.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    container.setBoolean(key, primitive.getAsBoolean());
                } else if (primitive.isString()) {
                    container.setString(key, primitive.getAsString());
                } else if (primitive.isNumber()) {
                    container.setNumber(key, primitive.getAsNumber());
                }
            } else if (element.isJsonObject()) {
                container.setContainer(key, toContainer(element));
            }
        }
    }

    return container;

}

From source file:com.yandex.money.api.typeadapters.AllowedMoneySourceTypeAdapter.java

License:Open Source License

@Override
public AllowedMoneySource deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return AllowedMoneySource.parse(json.getAsJsonPrimitive().getAsString());
}

From source file:com.yandex.money.api.typeadapters.model.AllowedMoneySourceTypeAdapter.java

License:Open Source License

@Override
public AllowedMoneySource deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return AllowedMoneySource.parseOrThrow(json.getAsJsonPrimitive().getAsString());
}

From source file:com.zuoxiaolong.blog.sdk.BlogInterfaceHandler.java

License:Apache License

/**
 * ?????//from  w  w  w .ja v a2  s .com
 *
 * @return
 */
public static List<ArticleRankResponseDto> getArticleRank() throws Exception {
    HttpRequest request = new HttpRequest();
    String responseString = request.doGet(BlogSdkPropertiesUtil.getProperty("articleRankUrl"));
    System.out.println(responseString);

    // Creates the json object which will manage the information received
    GsonBuilder gsonBuilder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = gsonBuilder.create();
    Type articleRankResponseDtoListType = new TypeToken<List<ArticleRankResponseDto>>() {
    }.getType();
    List<ArticleRankResponseDto> articleRankResponseDtos = gson.fromJson(responseString,
            articleRankResponseDtoListType);
    return articleRankResponseDtos;
}

From source file:Days.Day12.java

public int parseElement(JsonElement element) {
    int totaal = 0;
    if (element.isJsonArray()) {
        totaal += parseArray(element.getAsJsonArray());
    } else if (element.isJsonObject()) {
        totaal += parseObject(element.getAsJsonObject());
    } else if (element.isJsonPrimitive()) {
        totaal += parsePrimitive(element.getAsJsonPrimitive());
    }/* ww  w  .ja va  2s.com*/
    return totaal;
}