Java Code Examples for com.google.gson.JsonPrimitive#getAsBoolean()

The following examples show how to use com.google.gson.JsonPrimitive#getAsBoolean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicJsonUtils.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
private static Object convertValue(JsonElement value) {
  if (value.isJsonNull()) {
    return null;
  } else if (value.isJsonPrimitive()) {
    JsonPrimitive prim = value.getAsJsonPrimitive();
    if (prim.isBoolean()) {
      return prim.getAsBoolean();
    } else if (prim.isNumber()) {
      Number n = prim.getAsNumber();
      if (n.doubleValue() == n.intValue()) {
        return n.intValue();
      } else {
        return n.doubleValue();
      }
    } else if (prim.isString()) {
      return prim.getAsString();
    } else {
      throw new RuntimeException("Unrecognized value: " + value);
    }
  } else {
    return value.toString();
  }
}
 
Example 2
Source File: JsonAttributeValueSerialization.java    From XACML with MIT License 6 votes vote down vote up
private static StdAttributeValue<?> parsePrimitiveAttribute(JsonPrimitive jsonPrimitive) {
	try {
		if (jsonPrimitive.isString()) {
			return new StdAttributeValue<>(XACML3.ID_DATATYPE_STRING, jsonPrimitive.getAsString());
		} else if (jsonPrimitive.isBoolean()) {
			return new StdAttributeValue<>(XACML3.ID_DATATYPE_BOOLEAN, jsonPrimitive.getAsBoolean());
		} else if (jsonPrimitive.isNumber()) {
			Number number = jsonPrimitive.getAsNumber();
			logger.debug("Number is {} {} ceil {}", number.doubleValue(), number.longValue(), Math.ceil(number.doubleValue()));
			if (Math.ceil(number.doubleValue()) == number.longValue()) {
				return new StdAttributeValue<>(XACML3.ID_DATATYPE_INTEGER, DataTypeInteger.newInstance().convert(jsonPrimitive.getAsInt()));
			} else {
				return new StdAttributeValue<>(XACML3.ID_DATATYPE_DOUBLE, jsonPrimitive.getAsDouble());
			}
		}
	} catch (DataTypeException e) {
		logger.error("Parse primitive failed", e);
	}
	return null;
}
 
Example 3
Source File: Utilities.java    From synthea with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a JsonPrimitive into a primitive Boolean, Double, or String.
 *
 * @param p
 *          : JsonPrimitive
 * @return Boolean, Double, or String
 */
public static Object primitive(JsonPrimitive p) {
  Object retVal = null;
  if (p.isBoolean()) {
    retVal = p.getAsBoolean();
  } else if (p.isNumber()) {
    double doubleVal = p.getAsDouble();

    if (doubleVal == Math.rint(doubleVal)) {
      retVal = (int) doubleVal;
    } else {
      retVal = doubleVal;
    }
  } else if (p.isString()) {
    retVal = p.getAsString();
  }
  return retVal;
}
 
Example 4
Source File: ParameterAdapter.java    From activitystreams with Apache License 2.0 6 votes vote down vote up
private Object deserialize(
  JsonDeserializationContext context,
  JsonElement el) {
    if (el.isJsonArray()) {
      return context.deserialize(el, Iterable.class);
    } else if (el.isJsonObject()) {
      return context.deserialize(el, ASObject.class);
    } else if (el.isJsonPrimitive()) {
      JsonPrimitive p = el.getAsJsonPrimitive();
      if (p.isBoolean())
        return p.getAsBoolean();
      else if (p.isNumber())
        return p.getAsNumber();
      else
        return p.getAsString();
    } else return null;
}
 
Example 5
Source File: LwM2mNodeDeserializer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Object deserializeValue(JsonPrimitive val, org.eclipse.leshan.core.model.ResourceModel.Type expectedType) {
    switch (expectedType) {
    case BOOLEAN:
        return val.getAsBoolean();
    case STRING:
        return val.getAsString();
    case INTEGER:
        return val.getAsLong();
    case FLOAT:
        return val.getAsDouble();
    case TIME:
    case OPAQUE:
    default:
        // TODO we need to better handle this.
        return val.getAsString();
    }
}
 
Example 6
Source File: ASObjectAdapter.java    From activitystreams with Apache License 2.0 5 votes vote down vote up
@Override
protected Object doForward(JsonPrimitive b) {
  if (b.isBoolean())
    return b.getAsBoolean();
  else if (b.isNumber())
    return b.getAsNumber();
  else 
    return b.getAsString();
}
 
Example 7
Source File: JsonUtil.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static JsonPrimitive deepCopy(JsonPrimitive element) {
	if (element.isBoolean())
		return new JsonPrimitive(element.getAsBoolean());
	if (element.isNumber())
		return new JsonPrimitive(element.getAsNumber());
	return new JsonPrimitive(element.getAsString());
}
 
Example 8
Source File: EventDeserializer.java    From easypost-java with MIT License 5 votes vote down vote up
private Object deserializeJsonPrimitive(JsonPrimitive element) {
	if (element.isBoolean()) {
		return element.getAsBoolean();
	} else if (element.isNumber()) {
		return element.getAsNumber();
	} else {
		return element.getAsString();
	}
}
 
Example 9
Source File: QueryOptions.java    From yawp with MIT License 5 votes vote down vote up
private Object getJsonObjectValue(JsonElement jsonElement) {
    if (jsonElement.isJsonArray()) {
        return getJsonObjectValueForArrays(jsonElement);
    }
    if (jsonElement.isJsonNull()) {
        return null;
    }

    JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();

    if (jsonPrimitive.isNumber()) {
        if (jsonPrimitive.getAsString().indexOf(".") != -1) {
            return jsonPrimitive.getAsDouble();
        }
        return jsonPrimitive.getAsLong();
    }

    if (jsonPrimitive.isString()) {
        return jsonPrimitive.getAsString();
    }

    if (jsonPrimitive.isBoolean()) {
        return jsonPrimitive.getAsBoolean();
    }

    // TODO timestamp
    throw new RuntimeException("Invalid json value: " + jsonPrimitive.getAsString());
}
 
Example 10
Source File: ConfigurationDeserializer.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private Object deserialize(JsonPrimitive primitive) {
    if (primitive.isString()) {
        return primitive.getAsString();
    } else if (primitive.isNumber()) {
        return primitive.getAsBigDecimal();
    } else if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else {
        throw new IllegalArgumentException("Unsupported primitive: " + primitive);
    }
}
 
Example 11
Source File: JsonElementResolver.java    From trimou with Apache License 2.0 5 votes vote down vote up
private Object unwrapJsonPrimitive(JsonPrimitive jsonPrimitive) {
    if (jsonPrimitive.isBoolean()) {
        return jsonPrimitive.getAsBoolean();
    } else if (jsonPrimitive.isString()) {
        return jsonPrimitive.getAsString();
    } else if (jsonPrimitive.isNumber()) {
        return jsonPrimitive.getAsNumber();
    }
    return jsonPrimitive;
}
 
Example 12
Source File: ConfigurationDeserializer.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private Object deserialize(JsonPrimitive primitive) {
    if (primitive.isString()) {
        return primitive.getAsString();
    } else if (primitive.isNumber()) {
        return primitive.getAsBigDecimal();
    } else if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else {
        throw new IllegalArgumentException("Unsupported primitive: " + primitive);
    }
}
 
Example 13
Source File: GsonObjectDeserializer.java    From qaf with MIT License 5 votes vote down vote up
public static Object read(JsonElement in) {

		if (in.isJsonArray()) {
			List<Object> list = new ArrayList<Object>();
			JsonArray arr = in.getAsJsonArray();
			for (JsonElement anArr : arr) {
				list.add(read(anArr));
			}
			return list;
		} else if (in.isJsonObject()) {
			Map<String, Object> map = new LinkedTreeMap<String, Object>();
			JsonObject obj = in.getAsJsonObject();
			Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
			for (Map.Entry<String, JsonElement> entry : entitySet) {
				map.put(entry.getKey(), read(entry.getValue()));
			}
			return map;
		} else if (in.isJsonPrimitive()) {
			JsonPrimitive prim = in.getAsJsonPrimitive();
			if (prim.isBoolean()) {
				return prim.getAsBoolean();
			} else if (prim.isString()) {
				return prim.getAsString();
			} else if (prim.isNumber()) {
				if (prim.getAsString().contains("."))
					return prim.getAsDouble();
				else {
					return prim.getAsLong();
				}
			}
		}
		return null;
	}
 
Example 14
Source File: ModuleRegistry.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Object getJsonValue(String key, JsonPrimitive elem) {
	// TODO: Move to utils
	if (elem.isString())
		return elem.getAsString();
	else if (elem.isNumber())
		return elem.getAsNumber();
	else if (elem.isBoolean())
		return elem.getAsBoolean();
	// ... TODO: Add more data types.

	String value = elem.getAsString();
	Wizardry.LOGGER.warn("| | |_ WARNING! Using fallback as string for parameter '" + key + "' having value '" + value + "'.");
	return value;
}
 
Example 15
Source File: XGsonBuilder.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Boolean extractBoolean(JsonElement jsonElement, String name) {
	if ((null != jsonElement) && jsonElement.isJsonObject() && StringUtils.isNotEmpty(name)) {
		JsonElement element = extract(jsonElement, name);
		if (null != element && element.isJsonPrimitive()) {
			JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive();
			if (jsonPrimitive.isBoolean())
				return jsonPrimitive.getAsBoolean();
		}
	}
	return null;
}
 
Example 16
Source File: AbstractGsonConverter.java    From helper with MIT License 5 votes vote down vote up
@Override
public Object unwarpPrimitive(JsonPrimitive primitive) {
    if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else if (primitive.isNumber()) {
        return primitive.getAsNumber();
    } else if (primitive.isString()) {
        return primitive.getAsString();
    } else {
        throw new IllegalArgumentException("Unknown primitive type: " + primitive);
    }
}
 
Example 17
Source File: JsonUtil.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static boolean equal(JsonPrimitive e1, JsonPrimitive e2) {
	if (e1.isBoolean() && e2.isBoolean())
		return e1.getAsBoolean() == e2.getAsBoolean();
	if (e1.isNumber() && e2.isNumber())
		return e1.getAsNumber().doubleValue() == e2.getAsNumber().doubleValue();
	return e1.getAsString().equals(e2.getAsString());
}
 
Example 18
Source File: SNodeGson.java    From swellrt with Apache License 2.0 3 votes vote down vote up
public static SNode castToSNode(JsonElement object) {

    Preconditions.checkArgument(object != null, "Null argument");

    if (object.isJsonPrimitive()) {

      JsonPrimitive primitiveObject = object.getAsJsonPrimitive();

      if (primitiveObject.isBoolean()) {

        return new SPrimitive(primitiveObject.getAsBoolean(), new SNodeAccessControl());

      } else if (primitiveObject.isNumber()) {

        return new SPrimitive(primitiveObject.getAsDouble(), new SNodeAccessControl());

      } else if (primitiveObject.isString()) {

        return new SPrimitive(primitiveObject.isString(), new SNodeAccessControl());

      }

    } else if (object.isJsonObject()) {

      return SMapGson.create(object.getAsJsonObject());

    } else if (object.isJsonArray()) {

      return SListGson.create(object.getAsJsonArray());

    }

    throw new IllegalStateException("Unable to cast object to JS native SNode");
  }
 
Example 19
Source File: BetterJsonObject.java    From Hyperium with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * The optional boolean method, returns the default value if
 * the key is null, empty or the data does not contain
 * the key. This will also return the default value if
 * the data value is not a boolean
 *
 * @param key the key the value will be loaded from
 * @return the value in the json data set or the default if the key cannot be found
 */
public boolean optBoolean(String key, boolean value) {
    if (key == null || key.isEmpty() || !has(key)) return value;
    JsonPrimitive primitive = asPrimitive(get(key));
    return primitive != null && primitive.isBoolean() ? primitive.getAsBoolean() : value;
}
 
Example 20
Source File: JsonUtils.java    From yandex-money-sdk-java with MIT License 2 votes vote down vote up
/**
 * Gets nullable Boolean from a JSON object.
 *
 * @param object json object
 * @param memberName member's name
 * @return {@link Boolean} value
 */
public static Boolean getBoolean(JsonObject object, String memberName) {
    JsonPrimitive primitive = getPrimitiveChecked(object, memberName);
    return primitive == null ? null : primitive.getAsBoolean();
}