Search in sources :

Example 1 with JsonStructure

use of jakarta.json.JsonStructure in project JsonPath by jayway.

the class JakartaJsonProvider method proxyAll.

private JsonStructure proxyAll(JsonStructure jsonStruct) {
    if (jsonStruct == null) {
        return null;
    } else if (jsonStruct instanceof JsonArrayProxy) {
        return (JsonArray) jsonStruct;
    } else if (jsonStruct instanceof JsonArray) {
        List<Object> array = new ArrayList<>();
        for (JsonValue v : (JsonArray) jsonStruct) {
            if (v instanceof JsonStructure) {
                v = proxyAll((JsonStructure) v);
            }
            array.add(v);
        }
        return new JsonArrayProxy(jsonBuilderFactory.createArrayBuilder(array).build());
    } else if (jsonStruct instanceof JsonObjectProxy) {
        return (JsonObject) jsonStruct;
    } else if (jsonStruct instanceof JsonObject) {
        Map<String, Object> map = new LinkedHashMap<>();
        for (Map.Entry<String, JsonValue> e : ((JsonObject) jsonStruct).entrySet()) {
            JsonValue v = e.getValue();
            if (v instanceof JsonStructure) {
                v = proxyAll((JsonStructure) v);
            }
            map.put(e.getKey(), v);
        }
        return new JsonObjectProxy(jsonBuilderFactory.createObjectBuilder(map).build());
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ArrayList(java.util.ArrayList) JsonValue(jakarta.json.JsonValue) JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) LinkedHashMap(java.util.LinkedHashMap) JsonArray(jakarta.json.JsonArray) JsonObject(jakarta.json.JsonObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonStructure(jakarta.json.JsonStructure)

Example 2 with JsonStructure

use of jakarta.json.JsonStructure in project jena by apache.

the class LangJSONLD11 method extractPrefixes.

/**
 * JSON-LD does not define prefixes.
 * <p>
 * The use of "prefix:localname" happens for any definition of "prefix" in the
 * {@literal @context} even if intended for a URI e.g a property.
 * </p>
 * <p>
 * We could extract any {"key" : "value"} from the context but we add a pragmatic
 * filter to to see if the URI value ends in "#", "/" or ":" (for "urn:" and "did:"
 * cases).
 * </p>
 * <p>
 * In addition, {@literal @vocab} becomes prefix "".
 * </p>
 */
private static void extractPrefixes(Document document, BiConsumer<String, String> action) {
    try {
        JsonStructure js = document.getJsonContent().get();
        JsonValue jv = js.asJsonObject().get(Keywords.CONTEXT);
        extractPrefixes(jv, action);
    } catch (Throwable ex) {
        Log.warn(LangJSONLD11.class, "Unexpected problem while extracting prefixes: " + ex.getMessage(), ex);
    }
}
Also used : JsonValue(jakarta.json.JsonValue) JsonStructure(jakarta.json.JsonStructure)

Example 3 with JsonStructure

use of jakarta.json.JsonStructure in project JsonPath by json-path.

the class JakartaMappingProvider method mapImpl.

private Object mapImpl(Object source, final Type targetType) {
    if (source == null || source == JsonValue.NULL) {
        return null;
    }
    if (source == JsonValue.TRUE) {
        if (Boolean.class.equals(targetType)) {
            return Boolean.TRUE;
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON boolean (true) cannot be mapped to " + className);
        }
    }
    if (source == JsonValue.FALSE) {
        if (Boolean.class.equals(targetType)) {
            return Boolean.FALSE;
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON boolean (false) cannot be mapped to " + className);
        }
    } else if (source instanceof JsonString) {
        if (String.class.equals(targetType)) {
            return ((JsonString) source).getChars();
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON string cannot be mapped to " + className);
        }
    } else if (source instanceof JsonNumber) {
        JsonNumber jsonNumber = (JsonNumber) source;
        if (jsonNumber.isIntegral()) {
            return mapIntegralJsonNumber(jsonNumber, getRawClass(targetType));
        } else {
            return mapDecimalJsonNumber(jsonNumber, getRawClass(targetType));
        }
    }
    if (source instanceof JsonArrayBuilder) {
        source = ((JsonArrayBuilder) source).build();
    } else if (source instanceof JsonObjectBuilder) {
        source = ((JsonObjectBuilder) source).build();
    }
    if (source instanceof Collection) {
        // this covers both List<JsonValue> and JsonArray from JSON-P spec
        Class<?> rawTargetType = getRawClass(targetType);
        Type targetTypeArg = getFirstTypeArgument(targetType);
        Collection<Object> result = newCollectionOfType(rawTargetType);
        for (Object srcValue : (Collection<?>) source) {
            if (srcValue instanceof JsonObject) {
                if (targetTypeArg != null) {
                    result.add(mapImpl(srcValue, targetTypeArg));
                } else {
                    result.add(srcValue);
                }
            } else {
                result.add(unwrapJsonValue(srcValue));
            }
        }
        return result;
    } else if (source instanceof JsonObject) {
        if (targetType instanceof Class) {
            if (jsonToClassMethod != null) {
                try {
                    JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
                    return jsonToClassMethod.invoke(jsonb, jsonParser, (Class<?>) targetType);
                } catch (Exception e) {
                    throw new MappingException(e);
                }
            } else {
                try {
                    // Fallback databinding approach for JSON-B API implementations without
                    // explicit support for use of JsonParser in their public API. The approach
                    // is essentially first to serialize given value into JSON, and then bind
                    // it to data object of given class.
                    String json = source.toString();
                    return jsonb.fromJson(json, (Class<?>) targetType);
                } catch (JsonbException e) {
                    throw new MappingException(e);
                }
            }
        } else if (targetType instanceof ParameterizedType) {
            if (jsonToTypeMethod != null) {
                try {
                    JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
                    return jsonToTypeMethod.invoke(jsonb, jsonParser, (Type) targetType);
                } catch (Exception e) {
                    throw new MappingException(e);
                }
            } else {
                try {
                    // Fallback databinding approach for JSON-B API implementations without
                    // explicit support for use of JsonParser in their public API. The approach
                    // is essentially first to serialize given value into JSON, and then bind
                    // the JSON string to data object of given type.
                    String json = source.toString();
                    return jsonb.fromJson(json, (Type) targetType);
                } catch (JsonbException e) {
                    throw new MappingException(e);
                }
            }
        } else {
            throw new MappingException("JSON object cannot be databind to " + targetType);
        }
    } else {
        return source;
    }
}
Also used : JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonbException(jakarta.json.bind.JsonbException) JsonException(jakarta.json.JsonException) NoSuchElementException(java.util.NoSuchElementException) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonNumber(jakarta.json.JsonNumber) JsonbException(jakarta.json.bind.JsonbException) Collection(java.util.Collection) JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) JsonParser(jakarta.json.stream.JsonParser) JsonStructure(jakarta.json.JsonStructure)

Example 4 with JsonStructure

use of jakarta.json.JsonStructure in project JsonPath by jayway.

the class JakartaMappingProvider method mapImpl.

private Object mapImpl(Object source, final Type targetType) {
    if (source == null || source == JsonValue.NULL) {
        return null;
    }
    if (source == JsonValue.TRUE) {
        if (Boolean.class.equals(targetType)) {
            return Boolean.TRUE;
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON boolean (true) cannot be mapped to " + className);
        }
    }
    if (source == JsonValue.FALSE) {
        if (Boolean.class.equals(targetType)) {
            return Boolean.FALSE;
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON boolean (false) cannot be mapped to " + className);
        }
    } else if (source instanceof JsonString) {
        if (String.class.equals(targetType)) {
            return ((JsonString) source).getChars();
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON string cannot be mapped to " + className);
        }
    } else if (source instanceof JsonNumber) {
        JsonNumber jsonNumber = (JsonNumber) source;
        if (jsonNumber.isIntegral()) {
            return mapIntegralJsonNumber(jsonNumber, getRawClass(targetType));
        } else {
            return mapDecimalJsonNumber(jsonNumber, getRawClass(targetType));
        }
    }
    if (source instanceof JsonArrayBuilder) {
        source = ((JsonArrayBuilder) source).build();
    } else if (source instanceof JsonObjectBuilder) {
        source = ((JsonObjectBuilder) source).build();
    }
    if (source instanceof Collection) {
        // this covers both List<JsonValue> and JsonArray from JSON-P spec
        Class<?> rawTargetType = getRawClass(targetType);
        Type targetTypeArg = getFirstTypeArgument(targetType);
        Collection<Object> result = newCollectionOfType(rawTargetType);
        for (Object srcValue : (Collection<?>) source) {
            if (srcValue instanceof JsonObject) {
                if (targetTypeArg != null) {
                    result.add(mapImpl(srcValue, targetTypeArg));
                } else {
                    result.add(srcValue);
                }
            } else {
                result.add(unwrapJsonValue(srcValue));
            }
        }
        return result;
    } else if (source instanceof JsonObject) {
        if (targetType instanceof Class) {
            if (jsonToClassMethod != null) {
                try {
                    JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
                    return jsonToClassMethod.invoke(jsonb, jsonParser, (Class<?>) targetType);
                } catch (Exception e) {
                    throw new MappingException(e);
                }
            } else {
                try {
                    // Fallback databinding approach for JSON-B API implementations without
                    // explicit support for use of JsonParser in their public API. The approach
                    // is essentially first to serialize given value into JSON, and then bind
                    // it to data object of given class.
                    String json = source.toString();
                    return jsonb.fromJson(json, (Class<?>) targetType);
                } catch (JsonbException e) {
                    throw new MappingException(e);
                }
            }
        } else if (targetType instanceof ParameterizedType) {
            if (jsonToTypeMethod != null) {
                try {
                    JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
                    return jsonToTypeMethod.invoke(jsonb, jsonParser, (Type) targetType);
                } catch (Exception e) {
                    throw new MappingException(e);
                }
            } else {
                try {
                    // Fallback databinding approach for JSON-B API implementations without
                    // explicit support for use of JsonParser in their public API. The approach
                    // is essentially first to serialize given value into JSON, and then bind
                    // the JSON string to data object of given type.
                    String json = source.toString();
                    return jsonb.fromJson(json, (Type) targetType);
                } catch (JsonbException e) {
                    throw new MappingException(e);
                }
            }
        } else {
            throw new MappingException("JSON object cannot be databind to " + targetType);
        }
    } else {
        return source;
    }
}
Also used : JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonbException(jakarta.json.bind.JsonbException) JsonException(jakarta.json.JsonException) NoSuchElementException(java.util.NoSuchElementException) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonNumber(jakarta.json.JsonNumber) JsonbException(jakarta.json.bind.JsonbException) Collection(java.util.Collection) JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) JsonParser(jakarta.json.stream.JsonParser) JsonStructure(jakarta.json.JsonStructure)

Example 5 with JsonStructure

use of jakarta.json.JsonStructure in project JsonPath by json-path.

the class JakartaJsonProvider method proxyAll.

private JsonStructure proxyAll(JsonStructure jsonStruct) {
    if (jsonStruct == null) {
        return null;
    } else if (jsonStruct instanceof JsonArrayProxy) {
        return (JsonArray) jsonStruct;
    } else if (jsonStruct instanceof JsonArray) {
        List<Object> array = new ArrayList<>();
        for (JsonValue v : (JsonArray) jsonStruct) {
            if (v instanceof JsonStructure) {
                v = proxyAll((JsonStructure) v);
            }
            array.add(v);
        }
        return new JsonArrayProxy(jsonBuilderFactory.createArrayBuilder(array).build());
    } else if (jsonStruct instanceof JsonObjectProxy) {
        return (JsonObject) jsonStruct;
    } else if (jsonStruct instanceof JsonObject) {
        Map<String, Object> map = new LinkedHashMap<>();
        for (Map.Entry<String, JsonValue> e : ((JsonObject) jsonStruct).entrySet()) {
            JsonValue v = e.getValue();
            if (v instanceof JsonStructure) {
                v = proxyAll((JsonStructure) v);
            }
            map.put(e.getKey(), v);
        }
        return new JsonObjectProxy(jsonBuilderFactory.createObjectBuilder(map).build());
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ArrayList(java.util.ArrayList) JsonValue(jakarta.json.JsonValue) JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) LinkedHashMap(java.util.LinkedHashMap) JsonArray(jakarta.json.JsonArray) JsonObject(jakarta.json.JsonObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonStructure(jakarta.json.JsonStructure)

Aggregations

JsonStructure (jakarta.json.JsonStructure)5 JsonObject (jakarta.json.JsonObject)4 JsonString (jakarta.json.JsonString)4 JsonValue (jakarta.json.JsonValue)3 JsonArray (jakarta.json.JsonArray)2 JsonArrayBuilder (jakarta.json.JsonArrayBuilder)2 JsonException (jakarta.json.JsonException)2 JsonNumber (jakarta.json.JsonNumber)2 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)2 JsonbException (jakarta.json.bind.JsonbException)2 JsonParser (jakarta.json.stream.JsonParser)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2