@@ -53,9 +53,6 @@ public static Map<String, Object> compact(Object input, Object context, JsonLdOp
5353 if (context instanceof Map
5454 && ((Map <String , Object >) context ).containsKey (JsonLdConsts .CONTEXT )) {
5555 context = ((Map <String , Object >) context ).get (JsonLdConsts .CONTEXT );
56- if (context instanceof String ) {
57- context = new LinkedList <>(Arrays .asList ((String ) context ));
58- }
5956 }
6057 Context activeCtx = new Context (opts );
6158 activeCtx = activeCtx .parse (context );
@@ -75,19 +72,12 @@ public static Map<String, Object> compact(Object input, Object context, JsonLdOp
7572 compacted = tmp ;
7673 }
7774 }
78- if (compacted != null && context != null ) {
79- // TODO: figure out if we can make "@context" appear at the start of
80- // the keySet
81- if ((context instanceof Map && !((Map <String , Object >) context ).isEmpty ())
82- || (context instanceof List && !((List <Object >) context ).isEmpty ())) {
83-
84- if (context instanceof List && ((List <Object >) context ).size () == 1
85- && opts .getCompactArrays ()) {
86- ((Map <String , Object >) compacted ).put (JsonLdConsts .CONTEXT ,
87- ((List <Object >) context ).get (0 ));
88- } else {
89- ((Map <String , Object >) compacted ).put (JsonLdConsts .CONTEXT , context );
90- }
75+ if (compacted != null ) {
76+ final Object returnedContext = returnedContext (context , opts );
77+ if (returnedContext != null ) {
78+ // TODO: figure out if we can make "@context" appear at the start of
79+ // the keySet
80+ ((Map <String , Object >) compacted ).put (JsonLdConsts .CONTEXT , returnedContext );
9181 }
9282 }
9383
@@ -324,14 +314,18 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
324314 // to a new empty
325315 // context, otherwise.
326316 final JsonLdApi api = new JsonLdApi (expandedInput , opts );
327- final Context activeCtx = api . context
328- . parse ((( Map < String , Object >) frame ). get ( JsonLdConsts . CONTEXT ) );
317+ final Object context = (( Map < String , Object >) frame ). get ( JsonLdConsts . CONTEXT );
318+ final Context activeCtx = api . context . parse (context );
329319 final List <Object > framed = api .frame (expandedInput , expandedFrame );
330320 if (opts .getPruneBlankNodeIdentifiers ()) {
331321 JsonLdUtils .pruneBlankNodes (framed );
332322 }
333323 Object compacted = api .compact (activeCtx , null , framed , opts .getCompactArrays ());
334- final Map <String , Object > rval = activeCtx .serialize ();
324+ final Map <String , Object > rval = newMap ();
325+ final Object returnedContext = returnedContext (context , opts );
326+ if (returnedContext != null ) {
327+ rval .put (JsonLdConsts .CONTEXT , context );
328+ }
335329 final boolean addGraph = ((!(compacted instanceof List )) && !opts .getOmitGraph ());
336330 if (addGraph && !(compacted instanceof List )) {
337331 final List <Object > tmp = new ArrayList <Object >();
@@ -348,6 +342,22 @@ public static Map<String, Object> frame(Object input, Object frame, JsonLdOption
348342 return rval ;
349343 }
350344
345+ private static Object returnedContext (Object context , JsonLdOptions opts ) {
346+ if (context != null &&
347+ ((context instanceof Map && !((Map <String , Object >) context ).isEmpty ())
348+ || (context instanceof List && !((List <Object >) context ).isEmpty ())
349+ || (context instanceof String && !((String ) context ).isEmpty ()))) {
350+
351+ if (context instanceof List && ((List <Object >) context ).size () == 1
352+ && opts .getCompactArrays ()) {
353+ return ((List <Object >) context ).get (0 );
354+ }
355+ return context ;
356+ } else {
357+ return null ;
358+ }
359+ }
360+
351361 /**
352362 * A registry for RDF Parsers (in this case, JSONLDSerializers) used by
353363 * fromRDF if no specific serializer is specified and options.format is set.
0 commit comments