Search in sources :

Example 6 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError in project stanbol by apache.

the class JsonLdSerializingProvider method serialize.

@Override
public void serialize(OutputStream serializedGraph, Graph tc, String formatIdentifier) {
    ClerezzaRDFParser serializer = new ClerezzaRDFParser();
    try {
        long start = System.currentTimeMillis();
        Object output = JsonLdProcessor.fromRDF(tc, serializer);
        if (MODE_EXPAND.equalsIgnoreCase(mode)) {
            logger.debug(" - mode: {}", MODE_EXPAND);
            output = JsonLdProcessor.expand(output, opts);
        }
        if (MODE_FLATTEN.equalsIgnoreCase(mode)) {
            logger.debug(" - mode: {}", MODE_FLATTEN);
            // TODO: Allow inframe config
            final Object inframe = null;
            output = JsonLdProcessor.flatten(output, inframe, opts);
        }
        if (MODE_COMPACT.equalsIgnoreCase(mode)) {
            logger.debug(" - mode: {}", MODE_COMPACT);
            // TODO: collect namespaces used in the triples in the ClerezzaRDFParser
            final Map<String, Object> localCtx = new HashMap<String, Object>();
            localCtx.put("@context", DEFAULT_NAMESPACES);
            output = JsonLdProcessor.compact(output, localCtx, opts);
        }
        Writer writer = new OutputStreamWriter(serializedGraph, UTF8);
        logger.debug(" - prettyPrint: {}", prettyPrint);
        if (prettyPrint) {
            JsonUtils.writePrettyPrint(writer, output);
        } else {
            JsonUtils.write(writer, output);
        }
        if (logger.isDebugEnabled()) {
            logger.debug(" - serialized {} triples in {}ms", serializer.getCount(), System.currentTimeMillis() - start);
        }
    } catch (JsonLdError e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) JsonLdError(com.github.jsonldjava.core.JsonLdError)

Example 7 with JsonLdError

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

the class JsonLDReader method read$.

private void read$(Object jsonObject, JsonLdOptions options, final StreamRDF output) {
    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;
            }
        };
        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) List(java.util.List) Map(java.util.Map)

Example 8 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError in project aic-praise by aic-sri-international.

the class JSONLDJavaUtil method getFlattenedExpandedJSONLDJavaObject.

private static Object getFlattenedExpandedJSONLDJavaObject(Object jsonldJavaObject) throws Error {
    Map context = new HashMap();
    JsonLdOptions options = new JsonLdOptions();
    Object flattened;
    try {
        Object expanded = JsonLdProcessor.expand(jsonldJavaObject, options);
        flattened = JsonLdProcessor.flatten(expanded, context, options);
    } catch (JsonLdError e) {
        throw new Error(e);
    }
    return flattened;
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) HashMap(java.util.HashMap) JsonLdError(com.github.jsonldjava.core.JsonLdError) LinkedDataObject(com.sri.ai.praise.other.integration.linkeddata.api.LinkedDataObject) Map(java.util.Map) HashMap(java.util.HashMap) JsonLdError(com.github.jsonldjava.core.JsonLdError)

Example 9 with JsonLdError

use of com.github.jsonldjava.core.JsonLdError 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)

Aggregations

JsonLdError (com.github.jsonldjava.core.JsonLdError)9 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)6 HashMap (java.util.HashMap)5 RDFDataset (com.github.jsonldjava.core.RDFDataset)3 IOException (java.io.IOException)3 Map (java.util.Map)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonLdTripleCallback (com.github.jsonldjava.core.JsonLdTripleCallback)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Node (org.apache.jena.graph.Node)2 Triple (org.apache.jena.graph.Triple)2 Quad (org.apache.jena.sparql.core.Quad)2 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 LinkedDataObject (com.sri.ai.praise.other.integration.linkeddata.api.LinkedDataObject)1