Github user afs commented on a diff in the pull request:

    https://github.com/apache/jena/pull/171#discussion_r82185017
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java 
---
    @@ -113,61 +109,45 @@ private void serialize(Writer writer, DatasetGraph 
dataset, PrefixMap prefixMap,
         }
     
         private static void addPrefixes(Map<String, Object> ctx, PrefixMap 
prefixMap) {
    -        Map<String, IRI> pmap = prefixMap.getMapping() ;
    -        for ( Entry<String, IRI> e : pmap.entrySet() ) {
    -            String key = e.getKey() ;
    -            if ( key.isEmpty() )
    -                // Prefix "" is not allowed in JSON-LD
    -                continue ;
    -            IRI iri = e.getValue() ;
    -            ctx.put(e.getKey(), e.getValue().toString()) ;
    -        }
    +        prefixMap.getMapping().forEach((prefix, uri) -> ctx.put(prefix, 
uri.toString()));
    +        // Prefix "" is not allowed in JSON-LD
    +        ctx.remove("");
         }
     
         private static void addProperties(final Map<String, Object> ctx, Graph 
graph) {
             // Add some properties directly so it becomes "localname": ....
    -        final Set<String> dups = new HashSet<>() ;
    -        Consumer<Triple> x = new Consumer<Triple>() {
    -            @Override
    -            public void accept(Triple item) {
    -                Node p = item.getPredicate() ;
    -                Node o = item.getObject() ;
    -                if ( p.equals(RDF.type.asNode()) )
    -                    return ;
    -                String x = p.getLocalName() ;
    -                if ( dups.contains(x) )
    -                    return ;
    -
    -                if ( ctx.containsKey(x) ) {
    -                    // Check different URI
    -                    // pmap2.remove(x) ;
    -                    // dups.add(x) ;
    -                } else if ( o.isBlank() || o.isURI() ) {
    -                    // add property as a property (the object is an IRI)
    -                    Map<String, Object> x2 = new LinkedHashMap<>() ;
    -                    x2.put("@id", p.getURI()) ;
    -                    x2.put("@type", "@id") ;
    -                    ctx.put(x, x2) ;
    -                } else if ( o.isLiteral() ) {
    -                    String literalDatatypeURI = o.getLiteralDatatypeURI() ;
    -                    if ( literalDatatypeURI != null ) {
    -                        // add property as a typed attribute (the object 
is a
    -                        // typed literal)
    -                        Map<String, Object> x2 = new LinkedHashMap<>() ;
    -                        x2.put("@id", p.getURI()) ;
    -                        if (! isLangString(o) && ! isSimpleString(o) ) 
    -                            // RDF 1.1 : Skip if rdf:langString or 
xsd:string.
    -                            x2.put("@type", literalDatatypeURI) ; 
    -                        ctx.put(x, x2) ;
    -                    } else {
    -                        // add property as an untyped attribute (the 
object is
    -                        // an untyped literal)
    -                        ctx.put(x, p.getURI()) ;
    -                    }
    +        Consumer<Triple> addToContext = triple -> {
    +            Node p = triple.getPredicate();
    +            Node o = triple.getObject();
    +            String localName = p.getLocalName();
    +
    +            if (o.isBlank() || o.isURI()) {
    +                // add property as a property (the object is an IRI)
    +                Map<String, Object> x2 = new LinkedHashMap<>();
    +                x2.put("@id", p.getURI());
    +                x2.put("@type", "@id");
    +                ctx.put(localName, x2);
    +            } else if (o.isLiteral()) {
    +                String literalDatatypeURI = o.getLiteralDatatypeURI();
    +                if (literalDatatypeURI != null) {
    +                    // add property as a typed attribute (the object is a 
typed literal)
    +                    Map<String, Object> x2 = new LinkedHashMap<>();
    +                    x2.put("@id", p.getURI());
    +                    if (!(isLangString(o) || isSimpleString(o)))
    +                        // RDF 1.1 : Skip if rdf:langString or xsd:string.
    +                        x2.put("@type", literalDatatypeURI);
    +                    ctx.put(localName, x2);
    +                } else {
    +                    // add property as an untyped attribute (the object is 
an untyped literal)
    +                    ctx.put(localName, p.getURI());
                     }
                 }
    -        } ;
    -
    -        Iter.iter(graph.find(null, null, null)).apply(x) ;
    +        };
    +        graph.find(ANY)
    +            .filterDrop(t -> {
    +                Node pred = t.getPredicate();
    +                return pred.equals(RDF.type.asNode()) || 
ctx.containsKey(pred.getLocalName());
    +            })
    +            .forEachRemaining(addToContext) ;
         }
    --- End diff --
    
    I think it was clearer to have the `if ( p.equals(RDF.type.asNode()) )` in 
teh Consumer because all the logic is in one place.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to