本文整理了Java中com.google.gson.JsonElement.isJsonPrimitive()
方法的一些代码示例,展示了JsonElement.isJsonPrimitive()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonElement.isJsonPrimitive()
方法的具体详情如下:
包路径:com.google.gson.JsonElement
类名称:JsonElement
方法名:isJsonPrimitive
[英]provides check for verifying if this element is a primitive or not.
[中]提供检查以验证此元素是否为基元。
代码示例来源:origin: relayrides/pushy
@Override
public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
final Date date;
if (json.isJsonPrimitive()) {
date = new Date(this.timeUnit.toMillis(json.getAsLong()));
} else if (json.isJsonNull()) {
date = null;
} else {
throw new JsonParseException("Dates represented as time since the epoch must either be numbers or null.");
}
return date;
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public boolean isBooleanArray() {
for (JsonElement element : array) {
if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isBoolean()) {
return false;
}
}
return true;
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public boolean isStringArray() {
for (JsonElement element : array) {
if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isString()) {
return false;
}
}
return true;
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public boolean isNumberArray() {
for (JsonElement element : array) {
if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isNumber()) {
return false;
}
}
return true;
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
/**
* Gets the value of the given member as a boolean, null if it doesn't exist
*/
public static Boolean getAsBoolean(JsonObject o, String member) {
if (o.has(member)) {
JsonElement e = o.get(member);
if (e != null && e.isJsonPrimitive()) {
return e.getAsBoolean();
} else {
return null;
}
} else {
return null;
}
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
/**
* Gets the value of the given member as a Long, null if it doesn't exist
*/
public static Long getAsLong(JsonObject o, String member) {
if (o.has(member)) {
JsonElement e = o.get(member);
if (e != null && e.isJsonPrimitive()) {
return e.getAsLong();
} else {
return null;
}
} else {
return null;
}
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
/**
* Gets the value of the given member as a string, null if it doesn't exist
*/
public static String getAsString(JsonObject o, String member) {
if (o.has(member)) {
JsonElement e = o.get(member);
if (e != null && e.isJsonPrimitive()) {
return e.getAsString();
} else {
return null;
}
} else {
return null;
}
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
private static String nullSafeGetString(JsonObject obj, String field) {
return obj.has(field) && obj.get(field).isJsonPrimitive() ? obj.get(field).getAsString() : null;
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
/**
* Gets the value of the given member (expressed as integer seconds since epoch) as a Date
*/
public static Date getAsDate(JsonObject o, String member) {
if (o.has(member)) {
JsonElement e = o.get(member);
if (e != null && e.isJsonPrimitive()) {
return new Date(e.getAsInt() * 1000L);
} else {
return null;
}
} else {
return null;
}
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
@Override
public List<GrantedAuthority> getAuthorities(JsonObject introspectionResponse) {
List<GrantedAuthority> auth = new ArrayList<>(getAuthorities());
if (introspectionResponse.has("scope") && introspectionResponse.get("scope").isJsonPrimitive()) {
String scopeString = introspectionResponse.get("scope").getAsString();
Set<String> scopes = OAuth2Utils.parseParameterList(scopeString);
for (String scope : scopes) {
auth.add(new SimpleGrantedAuthority("OAUTH_SCOPE_" + scope));
}
}
return auth;
}
代码示例来源:origin: apache/incubator-gobblin
public JsonSchema getItemsWithinDataType() {
JsonElement element = this.getDataType().get(ARRAY_ITEMS_KEY);
if (element.isJsonObject()) {
return new JsonSchema(element.getAsJsonObject());
}
if (element.isJsonPrimitive()) {
return buildBaseSchema(Type.valueOf(element.getAsString().toUpperCase()));
}
throw new UnsupportedOperationException("Array items can only be defined using JsonObject or JsonPrimitive.");
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public final boolean isBytes() {
if (getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isString()) {
if (cachedDecodedBytes != null) {
return true;
}
try {
cachedDecodedBytes = BaseEncoding.base64().decode(getElement().getAsString());
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
return false;
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public final boolean isString() {
return getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isString();
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public final boolean isNumber() {
return getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isNumber();
}
代码示例来源:origin: json-path/JsonPath
@Override
public int length(final Object obj) {
if (isArray(obj)) {
return toJsonArray(obj).size();
} else if (isMap(obj)) {
return toJsonObject(obj).entrySet().size();
} else {
if (obj instanceof JsonElement) {
JsonElement element = toJsonElement(obj);
if (element.isJsonPrimitive()) {
return element.toString().length();
}
}
}
throw new JsonPathException("length operation can not applied to " + obj != null ? obj.getClass().getName()
: "null");
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public final boolean isBoolean() {
return getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isBoolean();
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public BehaviorNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
BehaviorNode node;
if (json.isJsonPrimitive()) {
node = getPrimitiveNode(json, context);
} else {
node = getCompositeNode(json, context);
}
node = createNode(node);
return node;
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public PixelFormat deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
return new PixelFormat().withDepthBits(json.getAsInt());
}
return new PixelFormat().withDepthBits(24);
}
代码示例来源:origin: json-path/JsonPath
public Object unwrap(final Object o) {
if (o == null) {
return null;
}
if (!(o instanceof JsonElement)) {
return o;
}
JsonElement e = (JsonElement) o;
if (e.isJsonNull()) {
return null;
} else if (e.isJsonPrimitive()) {
JsonPrimitive p = e.getAsJsonPrimitive();
if (p.isString()) {
return p.getAsString();
} else if (p.isBoolean()) {
return p.getAsBoolean();
} else if (p.isNumber()) {
return unwrapNumber(p.getAsNumber());
}
}
return o;
}
代码示例来源:origin: MovingBlocks/Terasology
@Test
public void testSerializer() {
BigIntegerBase64Serializer serializer = new BigIntegerBase64Serializer();
BigInteger data = new BigInteger("123456789123456789123456789123456789");
JsonElement serialized = serializer.serialize(data, null, null);
assertTrue(serialized.isJsonPrimitive());
assertTrue(((JsonPrimitive) serialized).isString());
assertEquals(data, serializer.deserialize(serialized, null, null));
}
}
内容来源于网络,如有侵权,请联系作者删除!