List of usage examples for javax.json JsonValue getValueType
ValueType getValueType();
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private static long getJsonLong(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); }//from ww w. ja v a 2 s. c om if (ret.getValueType() != JsonValue.ValueType.NUMBER) { throw new ChaincodeCollectionConfigurationException( format("property %s wrong type expected number got %s", prop, ret.getValueType().name())); } return Long.parseLong(ret.toString()); }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private static int getJsonInt(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); }/* ww w.j a va 2 s . c o m*/ if (ret.getValueType() != JsonValue.ValueType.NUMBER) { throw new ChaincodeCollectionConfigurationException( format("property %s wrong type expected number got %s", prop, ret.getValueType().name())); } return Integer.parseInt(ret.toString()); }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private static JsonObject getJsonObject(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); }//from w w w . j a v a 2 s . c om if (ret.getValueType() != JsonValue.ValueType.OBJECT) { throw new ChaincodeCollectionConfigurationException( format("property %s wrong type expected object got %s", prop, ret.getValueType().name())); } return ret.asJsonObject(); }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private static JsonArray getJsonArray(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); }//from w w w . j a va 2 s. c o m if (ret.getValueType() != JsonValue.ValueType.ARRAY) { throw new ChaincodeCollectionConfigurationException( format("property %s wrong type expected array got %s", prop, ret.getValueType().name())); } return ret.asJsonArray(); }
From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java
private static String getJsonString(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException { JsonValue ret = obj.get(prop); if (ret == null) { throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop)); }/* w ww. j a va 2 s .co m*/ if (ret.getValueType() != JsonValue.ValueType.STRING) { throw new ChaincodeCollectionConfigurationException( format("property %s wrong type expected string got %s", prop, ret.getValueType().name())); } return obj.getString(prop); }
From source file:de.pksoftware.springstrap.cms.util.CmsUtils.java
public static String getModelProperty(CmsModel model, String path, String defaultValue) throws InvalidPathException { if (null == model) { return defaultValue; }//from w w w . j a v a 2 s .co m Pattern pattern = Pattern.compile("^(\\/\\w+)+$"); Matcher matcher = pattern.matcher(path); if (!matcher.matches()) { throw new RuntimeException( "Path must start with /, must not end with / and must only contain letters and numbers."); } String value = defaultValue; logger.info("Trying to get " + model.getModelName() + ">" + path); String data = model.getData(); String[] pathParts = path.split("\\/"); int partsLength = pathParts.length; JsonReader jsonReader = Json.createReader(new StringReader(data)); JsonObject jsonObject = jsonReader.readObject(); boolean pointerIsArray = false; Object pointer = jsonObject; for (int i = 1; i < partsLength; i++) { String pathPart = pathParts[i]; logger.info("Testing property: " + pathPart); JsonValue jsonValue = null; Assert.notNull(pointer); if (pointerIsArray) { if (!NumberUtils.isNumber(pathPart)) { throw new InvalidPathException("Path element '" + pathPart + "' should be numeric."); } jsonValue = ((JsonArray) pointer).get(Integer.parseInt(pathPart)); } else { jsonValue = ((JsonObject) pointer).get(pathPart); } if (null == jsonValue) { logger.info(model.getModelName() + " has no property: " + path); break; } JsonValue.ValueType valueType = jsonValue.getValueType(); if (i < partsLength - 1) { //Must be Object or Array if (valueType == JsonValue.ValueType.ARRAY) { logger.info(pathPart + " is an array."); pointer = (JsonArray) jsonValue; pointerIsArray = true; } else if (valueType == JsonValue.ValueType.OBJECT) { logger.info(pathPart + " is an object."); pointer = (JsonObject) jsonValue; pointerIsArray = false; } else { throw new InvalidPathException("Path element '" + pathPart + "' should be a be an object or array, but " + valueType.toString() + " found."); } } else { if (valueType == JsonValue.ValueType.STRING) { logger.info(pathPart + " is an string."); value = ((JsonString) jsonValue).getString(); break; } else { throw new InvalidPathException("Path element '" + pathPart + "' should be a string, but " + valueType.toString() + " found."); } } } jsonReader.close(); return value; }
From source file:org.apache.unomi.services.services.SegmentServiceImpl.java
public static void dumpJSON(JsonValue tree, String key, String depthPrefix) { if (key != null) logger.info(depthPrefix + "Key " + key + ": "); switch (tree.getValueType()) { case OBJECT:// ww w.j av a 2 s .c om logger.info(depthPrefix + "OBJECT"); JsonObject object = (JsonObject) tree; for (String name : object.keySet()) dumpJSON(object.get(name), name, depthPrefix + " "); break; case ARRAY: logger.info(depthPrefix + "ARRAY"); JsonArray array = (JsonArray) tree; for (JsonValue val : array) dumpJSON(val, null, depthPrefix + " "); break; case STRING: JsonString st = (JsonString) tree; logger.info(depthPrefix + "STRING " + st.getString()); break; case NUMBER: JsonNumber num = (JsonNumber) tree; logger.info(depthPrefix + "NUMBER " + num.toString()); break; case TRUE: case FALSE: case NULL: logger.info(depthPrefix + tree.getValueType().toString()); break; } }
From source file:org.hyperledger.fabric.sdk.NetworkConfig.java
private static JsonObject getJsonValueAsObject(JsonValue value) { return (value != null && value.getValueType() == ValueType.OBJECT) ? value.asJsonObject() : null; }
From source file:org.hyperledger.fabric.sdk.NetworkConfig.java
private static JsonArray getJsonValueAsArray(JsonValue value) { return (value != null && value.getValueType() == ValueType.ARRAY) ? value.asJsonArray() : null; }
From source file:org.hyperledger.fabric.sdk.NetworkConfig.java
private static String getJsonValueAsNumberString(JsonValue value) { return (value != null && value.getValueType() == ValueType.NUMBER) ? value.toString() : null; }