com.github.jsonldjava.core.JsonLdOptions Java Examples

The following examples show how to use com.github.jsonldjava.core.JsonLdOptions. 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: JsonldBeanDeserializerModifier.java    From jackson-jsonld with MIT License 6 votes vote down vote up
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    Object input = parseJsonldObject(jp);
    if(input == null) {
        return super.deserialize(jp, ctxt);
    }
    try {
        JsonLdOptions options = new JsonLdOptions();
        Object context = contextSupplier.get();
        if(context instanceof JsonNode){
            context = parseJsonldObject(initParser(mapper.treeAsTokens((JsonNode)context)));
        }
        Object obj = JsonLdProcessor.compact(input, context, options);
        JsonParser newParser = initParser(mapper.getFactory().createParser(mapper.valueToTree(obj).toString()));
        return super.deserialize(newParser, ctxt);
    } catch (JsonLdError e) {
        throw new JsonGenerationException("Failed to flatten json-ld", e);
    }
}
 
Example #2
Source File: IOHelper.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Return the current prefixes as a JSON-LD string.
 *
 * @return the current prefixes as a JSON-LD string
 * @throws IOException on any error
 */
public String getContextString() throws IOException {
  try {
    Object compact =
        JsonLdProcessor.compact(
            JsonUtils.fromString("{}"), context.getPrefixes(false), new JsonLdOptions());
    return JsonUtils.toPrettyString(compact);
  } catch (Exception e) {
    throw new IOException(jsonldContextCreationError, e);
  }
}
 
Example #3
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);
	}
}
 
Example #4
Source File: AbstractMessageConverter.java    From elucidate-server with MIT License 4 votes vote down vote up
protected AbstractMessageConverter(MediaType... supportedMediaTypes) {
    super(supportedMediaTypes);
    this.jsonLdOptions = new JsonLdOptions();
}