Search in sources :

Example 1 with JsonLdOptions

use of com.github.jsonldjava.core.JsonLdOptions in project jena by apache.

the class ExJsonLD method controllingJsonLDApiOptions.

/**
     *  the JSON-LD java API (that jena uses for JSON-LD I/O) defines a set of options
     *  that can be customized
     */
void controllingJsonLDApiOptions() {
    Model m = aSimpleModel();
    m.setNsPrefix("ex", "http://www.ex.com/");
    m.setNsPrefix("sh", "http://schema.org/");
    DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
    JsonLDWriteContext ctx = new JsonLDWriteContext();
    JsonLdOptions opts = new JsonLdOptions();
    ctx.setOptions(opts);
    // default is true
    opts.setCompactArrays(false);
    System.out.println("\n--- COMPACT with CompactArrays false: there is an @graph node");
    write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 2 with JsonLdOptions

use of com.github.jsonldjava.core.JsonLdOptions in project jena by apache.

the class JsonLDWriter method defaultJsonLdOptions.

// jena is not using same default as JSONLD-java
// maybe we should have, but it's too late now:
// changing it now would imply some unexpected changes in current users' outputs
private static JsonLdOptions defaultJsonLdOptions(String baseURI) {
    JsonLdOptions opts = new JsonLdOptions(baseURI);
    // this is NOT jsonld-java's default
    opts.useNamespaces = true;
    // opts.setUseRdfType(true); // false -> use "@type"
    // this is NOT jsonld-java's default
    opts.setUseNativeTypes(true);
    // this is jsonld-java's default           
    opts.setCompactArrays(true);
    return opts;
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions)

Example 3 with JsonLdOptions

use of com.github.jsonldjava.core.JsonLdOptions in project jena by apache.

the class JsonLDReader method read$.

private void read$(Object jsonObject, String baseURI, ContentType ct, final StreamRDF output, Context context) {
    output.start();
    try {
        JsonLdTripleCallback callback = new JsonLdTripleCallback() {

            @Override
            public Object call(RDFDataset dataset) {
                // Copy across namespaces
                for (Entry<String, String> namespace : dataset.getNamespaces().entrySet()) {
                    output.prefix(namespace.getKey(), namespace.getValue());
                }
                // Copy triples and quads
                for (String gn : dataset.keySet()) {
                    Object x = dataset.get(gn);
                    if ("@default".equals(gn)) {
                        @SuppressWarnings("unchecked") List<Map<String, Object>> triples = (List<Map<String, Object>>) x;
                        for (Map<String, Object> t : triples) {
                            Node s = createNode(t, "subject");
                            Node p = createNode(t, "predicate");
                            Node o = createNode(t, "object");
                            Triple triple = profile.createTriple(s, p, o, -1, -1);
                            output.triple(triple);
                        }
                    } else {
                        @SuppressWarnings("unchecked") List<Map<String, Object>> quads = (List<Map<String, Object>>) x;
                        Node g = createURI(gn);
                        for (Map<String, Object> q : quads) {
                            Node s = createNode(q, "subject");
                            Node p = createNode(q, "predicate");
                            Node o = createNode(q, "object");
                            Quad quad = profile.createQuad(g, s, p, o, -1, -1);
                            output.quad(quad);
                        }
                    }
                }
                return null;
            }
        };
        JsonLdOptions options = new JsonLdOptions(baseURI);
        options.useNamespaces = true;
        JsonLdProcessor.toRDF(jsonObject, callback, options);
    } catch (JsonLdError e) {
        errorHandler.error(e.getMessage(), -1, -1);
        throw new RiotException(e);
    }
    output.finish();
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) Quad(org.apache.jena.sparql.core.Quad) Node(org.apache.jena.graph.Node) JsonLdTripleCallback(com.github.jsonldjava.core.JsonLdTripleCallback) JsonLdError(com.github.jsonldjava.core.JsonLdError) Triple(org.apache.jena.graph.Triple) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) List(java.util.List) Map(java.util.Map)

Example 4 with JsonLdOptions

use of com.github.jsonldjava.core.JsonLdOptions in project jena by apache.

the class JsonLDWriter method toJsonLDJavaAPI.

/**
     * the JsonLD-java API object corresponding to a dataset and a JsonLD format.
     */
public static Object toJsonLDJavaAPI(RDFFormat.JSONLDVariant variant, DatasetGraph dataset, PrefixMap prefixMap, String baseURI, Context jenaContext) throws JsonLdError, JsonParseException, IOException {
    JsonLdOptions opts = getJsonLdOptions(baseURI, jenaContext);
    // we can benefit from the fact we know that there are no duplicates in the jsonld RDFDataset that we create
    // (optimization in jsonld-java 0.8.3)
    // see https://github.com/jsonld-java/jsonld-java/pull/173
    // with this, we cannot call the json-ld fromRDF method that assumes no duplicates in RDFDataset
    // Object obj = JsonLdProcessor.fromRDF(dataset, opts, new JenaRDF2JSONLD()) ;
    final RDFDataset jsonldDataset = (new JenaRDF2JSONLD()).parse(dataset);
    // JsonLdApi.fromRDF(RDFDataset, boolean) is "experimental" rather than "deprecated"
    @SuppressWarnings("deprecation") Object // true because we know that we don't have any duplicate in jsonldDataset
    obj = (new JsonLdApi(opts)).fromRDF(jsonldDataset, true);
    if (variant.isExpand()) {
    // nothing more to do
    } else if (variant.isFrame()) {
        Object frame = null;
        if (jenaContext != null) {
            frame = jenaContext.get(JSONLD_FRAME);
        }
        if (frame == null) {
            throw new IllegalArgumentException("No frame object found in jena Context");
        }
        if (frame instanceof String) {
            frame = JsonUtils.fromString((String) frame);
        }
        obj = JsonLdProcessor.frame(obj, frame, opts);
    } else {
        // compact or flatten
        // we need a (jsonld) context. Get it from jenaContext, or create one:
        Object ctx = getJsonldContext(dataset, prefixMap, jenaContext);
        if (variant.isCompact()) {
            obj = JsonLdProcessor.compact(obj, ctx, opts);
        } else if (variant.isFlatten()) {
            obj = JsonLdProcessor.flatten(obj, ctx, opts);
        } else {
            throw new IllegalArgumentException("Unexpected " + RDFFormat.JSONLDVariant.class.getName() + ": " + variant);
        }
        // replace @context in output?
        if (jenaContext != null) {
            Object ctxReplacement = jenaContext.get(JSONLD_CONTEXT_SUBSTITUTION);
            if (ctxReplacement != null) {
                if (obj instanceof Map) {
                    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) obj;
                    if (map.containsKey("@context")) {
                        map.put("@context", JsonUtils.fromString(ctxReplacement.toString()));
                    }
                }
            }
        }
    }
    return obj;
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) Util.isLangString(org.apache.jena.rdf.model.impl.Util.isLangString) Util.isSimpleString(org.apache.jena.rdf.model.impl.Util.isSimpleString) LinkedHashMap(java.util.LinkedHashMap) PrefixMap(org.apache.jena.riot.system.PrefixMap) Map(java.util.Map) JsonLdApi(com.github.jsonldjava.core.JsonLdApi) RDFFormat(org.apache.jena.riot.RDFFormat)

Example 5 with JsonLdOptions

use of com.github.jsonldjava.core.JsonLdOptions in project jena by apache.

the class TestJsonLDWriter method jsonldOptions.

/** Test passing a JsonLdOptions through Context */
@Test
public final void jsonldOptions() {
    Model m = ModelFactory.createDefaultModel();
    String ns = "http://schema.org/";
    Resource s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Jane Doe");
    m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
    m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
    // our default uses true for compactArrays
    String jsonld = toString(m, RDFFormat.JSONLD, null);
    // compactArrays is true -> no "@graph"
    assertFalse(jsonld.contains("@graph"));
    // compactArrays is true -> string, not an array for props with one value
    assertTrue(jsonld.contains("\"jobTitle\" : \"Professor\""));
    // now output using a value for JsonLdOptions in Context that sets compactArrays to false
    JsonLDWriteContext jenaCtx = new JsonLDWriteContext();
    JsonLdOptions opts = new JsonLdOptions(null);
    opts.setCompactArrays(false);
    jenaCtx.setOptions(opts);
    jsonld = toString(m, RDFFormat.JSONLD, jenaCtx);
    // compactArrays is false -> a "@graph" node
    assertTrue(jsonld.contains("@graph"));
    // compactArrays is false -> an array for all props, even when there's only one value
    assertTrue(jsonld.contains("\"jobTitle\" : [ \"Professor\" ]"));
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) JsonString(org.apache.jena.atlas.json.JsonString) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Aggregations

JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)6 RDFDataset (com.github.jsonldjava.core.RDFDataset)2 Map (java.util.Map)2 Model (org.apache.jena.rdf.model.Model)2 JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)2 JsonLdApi (com.github.jsonldjava.core.JsonLdApi)1 JsonLdError (com.github.jsonldjava.core.JsonLdError)1 JsonLdTripleCallback (com.github.jsonldjava.core.JsonLdTripleCallback)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Activate (org.apache.felix.scr.annotations.Activate)1 JsonString (org.apache.jena.atlas.json.JsonString)1 BaseTest (org.apache.jena.atlas.junit.BaseTest)1 Node (org.apache.jena.graph.Node)1 Triple (org.apache.jena.graph.Triple)1 Resource (org.apache.jena.rdf.model.Resource)1 Util.isLangString (org.apache.jena.rdf.model.impl.Util.isLangString)1 Util.isSimpleString (org.apache.jena.rdf.model.impl.Util.isSimpleString)1 RDFFormat (org.apache.jena.riot.RDFFormat)1 PrefixMap (org.apache.jena.riot.system.PrefixMap)1