Java Code Examples for com.github.jsonldjava.core.JsonLdProcessor#flatten()

The following examples show how to use com.github.jsonldjava.core.JsonLdProcessor#flatten() . 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: JSONLDOAAnnotationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotation oaAnnotation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 2
Source File: JSONLDOABatchOperationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OABatchOperation oaBatchOperation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaBatchOperation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 3
Source File: JSONLDOAAnnotationPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationPage oaAnnotationPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 4
Source File: JSONLDOAAnnotationCollectionMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationCollection oaAnnotationCollection, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationCollection.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 5
Source File: JSONLDOAStatisticsPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAStatisticsPage oaStatisticsPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaStatisticsPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 6
Source File: JSONLDOAAnnotationHistoryMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationHistory oaAnnotationHistory, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationHistory.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    JSONLDProfile.Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(JSONLDProfile.Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(JSONLDProfile.Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(JSONLDProfile.Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 7
Source File: JSONLDW3CAnnotationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotation w3cAnnotation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 8
Source File: JSONLDW3CBatchOperationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CBatchOperation w3cBatchOperation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cBatchOperation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 9
Source File: JSONLDW3CAnnotationPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationPage w3cAnnotationPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 10
Source File: JSONLDW3CAnnotationCollectionMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationCollection w3cAnnotationCollection, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationCollection.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 11
Source File: JSONLDW3CStatisticsPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CStatisticsPage w3cStatisticsPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cStatisticsPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 12
Source File: JSONLDW3CAnnotationHistoryMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationHistory w3cAnnotationHistory, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationHistory.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    JSONLDProfile.Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(JSONLDProfile.Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(JSONLDProfile.Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(JSONLDProfile.Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 13
Source File: JSONLDWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void endRDF() throws RDFHandlerException {
	checkWritingStarted();
	final JSONLDInternalRDFParser serialiser = new JSONLDInternalRDFParser();
	try {
		Object output = JsonLdProcessor.fromRDF(model, serialiser);

		final JSONLDMode mode = getWriterConfig().get(JSONLDSettings.JSONLD_MODE);

		final JsonLdOptions opts = new JsonLdOptions();
		// opts.addBlankNodeIDs =
		// getWriterConfig().get(BasicParserSettings.PRESERVE_BNODE_IDS);
		WriterConfig writerConfig = getWriterConfig();
		opts.setCompactArrays(writerConfig.get(JSONLDSettings.COMPACT_ARRAYS));
		opts.setProduceGeneralizedRdf(writerConfig.get(JSONLDSettings.PRODUCE_GENERALIZED_RDF));
		opts.setUseRdfType(writerConfig.get(JSONLDSettings.USE_RDF_TYPE));
		opts.setUseNativeTypes(writerConfig.get(JSONLDSettings.USE_NATIVE_TYPES));
		// opts.optimize = getWriterConfig().get(JSONLDSettings.OPTIMIZE);

		if (writerConfig.get(JSONLDSettings.HIERARCHICAL_VIEW)) {
			output = JSONLDHierarchicalProcessor.fromJsonLdObject(output);
		}

		if (baseURI != null && writerConfig.get(BasicWriterSettings.BASE_DIRECTIVE)) {
			opts.setBase(baseURI);
		}
		if (mode == JSONLDMode.EXPAND) {
			output = JsonLdProcessor.expand(output, opts);
		}
		// TODO: Implement inframe in JSONLDSettings
		final Object inframe = null;
		if (mode == JSONLDMode.FLATTEN) {
			output = JsonLdProcessor.flatten(output, inframe, opts);
		}
		if (mode == JSONLDMode.COMPACT) {
			final Map<String, Object> ctx = new LinkedHashMap<>();
			addPrefixes(ctx, model.getNamespaces());
			final Map<String, Object> localCtx = new HashMap<>();
			localCtx.put(JsonLdConsts.CONTEXT, ctx);

			output = JsonLdProcessor.compact(output, localCtx, opts);
		}
		if (writerConfig.get(BasicWriterSettings.PRETTY_PRINT)) {
			JsonUtils.writePrettyPrint(writer, output);
		} else {
			JsonUtils.write(writer, output);
		}

	} catch (JsonLdError | IOException e) {
		throw new RDFHandlerException("Could not render JSONLD", e);
	}
}