Java Code Examples for javax.json.JsonValue#getValueType()

The following examples show how to use javax.json.JsonValue#getValueType() . 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: JsonFormatterTest.java    From jboss-logmanager-ext with Apache License 2.0 6 votes vote down vote up
private static Map<String, String> getMap(final JsonObject json, final Key key) {
    final String name = getKey(key);
    if (json.containsKey(name) && !json.isNull(name)) {
        final Map<String, String> result = new LinkedHashMap<>();
        final JsonObject mdcObject = json.getJsonObject(name);
        for (String k : mdcObject.keySet()) {
            final JsonValue value = mdcObject.get(k);
            if (value.getValueType() == ValueType.STRING) {
                result.put(k, value.toString().replace("\"", ""));
            } else {
                result.put(k, value.toString());
            }
        }
        return result;
    }
    return Collections.emptyMap();
}
 
Example 2
Source File: VisibilityService.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
private Object mapValue(final JsonValue value) {
    switch (value.getValueType()) {
    case ARRAY:
        return value.asJsonArray().stream().map(this::mapValue).collect(toList());
    case STRING:
        return JsonString.class.cast(value).getString();
    case TRUE:
        return true;
    case FALSE:
        return false;
    case NUMBER:
        return JsonNumber.class.cast(value).doubleValue();
    case NULL:
        return null;
    case OBJECT:
    default:
        return value;
    }
}
 
Example 3
Source File: JavaxJsonDeserializer.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
private Object deserializeArray( ModuleDescriptor module, ArrayType arrayType, JsonValue json )
{
    if( arrayType.isArrayOfPrimitiveBytes() && json.getValueType() == JsonValue.ValueType.STRING )
    {
        byte[] bytes = asString( json ).getBytes( UTF_8 );
        return Base64.getDecoder().decode( bytes );
    }
    if( json.getValueType() == JsonValue.ValueType.ARRAY )
    {
        CollectionType collectionType = CollectionType.listOf( arrayType.collectedType() );
        List<Object> collection = (List<Object>) deserializeCollection( module,
                                                                        collectionType,
                                                                        requireJsonArray( json ) );
        Object array = Array.newInstance( arrayType.collectedType().primaryType(), collection.size() );
        for( int idx = 0; idx < collection.size(); idx++ )
        {
            Array.set( array, idx, collection.get( idx ) );
        }
        return array;
    }
    throw new SerializationException( "Don't know how to deserialize " + arrayType + " from " + json );
}
 
Example 4
Source File: JsonValueResolver.java    From trimou with Apache License 2.0 6 votes vote down vote up
private Object unwrapJsonValueIfNecessary(JsonValue jsonValue) {
    switch (jsonValue.getValueType()) {
    case STRING:
        return ((JsonString) jsonValue).getString();
    case NUMBER:
        return ((JsonNumber) jsonValue).bigDecimalValue();
    case TRUE:
        return Boolean.TRUE;
    case FALSE:
        return Boolean.FALSE;
    case NULL:
        return Placeholder.NULL;
    default:
        return jsonValue;
    }

}
 
Example 5
Source File: GeoJsonReader.java    From geojson with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getTags(final JsonObject feature) {
    final Map<String, String> tags = new TreeMap<>();

    if (feature.containsKey(PROPERTIES) && !feature.isNull(PROPERTIES)) {
        JsonValue properties = feature.get(PROPERTIES);
        if(properties != null && properties.getValueType().equals(JsonValue.ValueType.OBJECT)) {
            for (Map.Entry<String, JsonValue> stringJsonValueEntry : properties.asJsonObject().entrySet()) {
                final JsonValue value = stringJsonValueEntry.getValue();

                if (value instanceof JsonString) {
                    tags.put(stringJsonValueEntry.getKey(), ((JsonString) value).getString());
                } else if (value instanceof JsonStructure) {
                    Logging.warn(
                        "The GeoJSON contains an object with property '" + stringJsonValueEntry.getKey()
                            + "' whose value has the unsupported type '" + value.getClass().getSimpleName() + "'. That key-value pair is ignored!"
                    );
                } else if (value.getValueType() != JsonValue.ValueType.NULL) {
                    tags.put(stringJsonValueEntry.getKey(), value.toString());
                }
            }
        }
    }
    return tags;
}
 
Example 6
Source File: DynamicTypeAnalyzer.java    From jaxrs-analyzer with Apache License 2.0 6 votes vote down vote up
private TypeIdentifier analyzeInternal(final JsonValue jsonValue) {
    switch (jsonValue.getValueType()) {
        case ARRAY:
            return analyzeInternal((JsonArray) jsonValue);
        case OBJECT:
            return analyzeInternal((JsonObject) jsonValue);
        case STRING:
            return TypeIdentifier.ofType(Types.STRING);
        case NUMBER:
            return TypeIdentifier.ofType(Types.DOUBLE);
        case TRUE:
        case FALSE:
            return TypeIdentifier.ofType(Types.PRIMITIVE_BOOLEAN);
        case NULL:
            return TypeIdentifier.ofType(Types.OBJECT);
        default:
            throw new IllegalArgumentException("Unknown JSON value type provided");
    }
}
 
Example 7
Source File: JavaxJson.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
/**
 * Require a {@link JsonValue} to be a {@link JsonArray}.
 *
 * @param json the JSON value
 * @return the JSON array
 * @throws JsonException if it is not
 */
public static JsonArray requireJsonArray( JsonValue json )
{
    if( json.getValueType() != JsonValue.ValueType.ARRAY )
    {
        throw new JsonException( "Expected a JSON array but got " + json );
    }
    return (JsonArray) json;
}
 
Example 8
Source File: JavaxJson.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
/**
 * Require a {@link JsonValue} to be a {@link JsonStructure}.
 *
 * @param json the JSON value
 * @return the JSON structure
 * @throws JsonException if it is not
 */
public static JsonStructure requireJsonStructure( JsonValue json )
{
    if( json.getValueType() != JsonValue.ValueType.OBJECT && json.getValueType() != JsonValue.ValueType.ARRAY )
    {
        throw new JsonException( "Expected a JSON object or array but got " + json );
    }
    return (JsonStructure) json;
}
 
Example 9
Source File: ChaincodeCollectionConfiguration.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
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));
    }
    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();

}
 
Example 10
Source File: JsonpRuntime.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public JsonValue getProperty(JsonValue value, JsonValue name) {
  if (value.getValueType() == OBJECT) {
    return nodeOrNullNode(((JsonObject) value).get(toString(name)));
  } else {
    return JsonValue.NULL;
  }
}
 
Example 11
Source File: RecordJsonGenerator.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public JsonGenerator write(final JsonValue value) {
    switch (value.getValueType()) {
    case ARRAY:
        arrayBuilder.add(Collection.class.cast(value));
        break;
    case OBJECT:
        Record r = recordConverters.toRecord(mappingRegistry, value, () -> jsonb, () -> factory);
        arrayBuilder.add(factory.newRecordBuilder(r.getSchema(), r));
        break;
    case STRING:
        arrayBuilder.add(JsonString.class.cast(value).getString());
        break;
    case NUMBER:
        arrayBuilder.add(JsonNumber.class.cast(value).numberValue().doubleValue());
        break;
    case TRUE:
        arrayBuilder.add(true);
        break;
    case FALSE:
        arrayBuilder.add(false);
        break;
    case NULL:
        break;
    default:
        throw new IllegalStateException("Unexpected value: " + value.getValueType());
    }
    return this;
}
 
Example 12
Source File: JsonpRuntime.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Collection<JsonValue> getPropertyNames(JsonValue value) {
  if (value.getValueType() == OBJECT) {
    Set<String> nameSet = ((JsonObject) value).keySet();
    List<JsonValue> names = new ArrayList<>(nameSet.size());
    for(String n : nameSet) {
      names.add(createString(n));
    }
    return names;
  } else {
    return Collections.emptyList();
  }
}
 
Example 13
Source File: ChaincodeCollectionConfiguration.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
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));
    }
    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());

}
 
Example 14
Source File: VertxJson.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static void copy(JsonArray array, javax.json.JsonArray origin) {
    for (int i = 0; i < origin.size(); i++) {
        JsonValue value = origin.get(i);
        JsonValue.ValueType kind = value.getValueType();
        switch (kind) {
            case STRING:
                array.add(origin.getString(i));
                break;
            case TRUE:
                array.add(true);
                break;
            case FALSE:
                array.add(false);
                break;
            case NULL:
                array.addNull();
                break;
            case NUMBER:
                JsonNumber number = origin.getJsonNumber(i);
                if (number.isIntegral()) {
                    array.add(number.longValue());
                } else {
                    array.add(number.doubleValue());
                }
                break;
            case ARRAY:
                JsonArray newArray = new JsonArray();
                copy(newArray, origin.getJsonArray(i));
                array.add(newArray);
                break;
            case OBJECT:
                JsonObject newObject = new JsonObject();
                copy(newObject, origin.getJsonObject(i));
                array.add(newObject);
                break;
            default:
                throw new IllegalArgumentException("Unknown JSON Value " + kind);
        }
    }
}
 
Example 15
Source File: JSONManyAssociationState.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private JsonArray getReferences()
{
    JsonValue references = entityState.state().getJsonObject( JSONKeys.VALUE ).get( stateName );
    if( references != null && references.getValueType() == JsonValue.ValueType.ARRAY )
    {
        return (JsonArray) references;
    }
    return jsonFactories.builderFactory().createArrayBuilder().build();
}
 
Example 16
Source File: JSONContentHandler.java    From jcypher with Apache License 2.0 5 votes vote down vote up
@Override
public PathInfo getPathInfo(String colKey) {
	PathInfo pathInfo = null;
	int colIdx = getColumnIndex(colKey);
	if (colIdx == -1)
		throw new RuntimeException("no result column: " + colKey);
	JsonObject dataObject = (JsonObject) this.jsonValue;
	JsonArray restArray = getRestArray(dataObject);
	JsonValue restValue = getRestValue(restArray, colIdx);
	restValue = this.handleArrayCase(restValue);
	
	if (restValue.getValueType() == ValueType.OBJECT) {
		JsonObject pathObject = (JsonObject) restValue;
		String str = pathObject.getString("start");
		long startId = Long.parseLong(str.substring(str.lastIndexOf('/') + 1));
		str = pathObject.getString("end");
		long endId = Long.parseLong(str.substring(str.lastIndexOf('/') + 1));
		JsonArray rels = pathObject.getJsonArray("relationships");
		List<Long> relIds = new ArrayList<Long>();
		int sz = rels.size();
		for (int i = 0; i < sz; i++) {
			String rel = rels.getString(i);
			long rid = Long.parseLong(rel.substring(rel.lastIndexOf('/') + 1));
			relIds.add(Long.valueOf(rid));
		}
		pathInfo = new PathInfo(startId, endId, relIds, pathObject);
	}
	return pathInfo;
}
 
Example 17
Source File: OpenAPIGenerator.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
private boolean ensureObject(final JsonValue value) {
    if (value.getValueType() != JsonValue.ValueType.OBJECT) {
        throw new IllegalArgumentException("Not an object: " + value);
    }
    return true;
}
 
Example 18
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static String getJsonValueAsNumberString(JsonValue value) {
    return (value != null && value.getValueType() == ValueType.NUMBER) ? value.toString() : null;
}
 
Example 19
Source File: JsonpRuntime.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Number toNumber(JsonValue n) {
  return (n.getValueType() == NUMBER) ? ((JsonNumber) n).numberValue() : null;
}
 
Example 20
Source File: JSONContentHandler.java    From jcypher with Apache License 2.0 4 votes vote down vote up
private JsonValue handleArrayCase(JsonValue obj) {
	if (obj.getValueType() == ValueType.ARRAY && ((JsonArray)obj).size() > 0)
		return ((JsonArray)obj).get(0);
	return obj;
}