diff --git a/src/main/java/foundation/identity/jsonld/JsonLDObject.java b/src/main/java/foundation/identity/jsonld/JsonLDObject.java index d0033db..c61f320 100644 --- a/src/main/java/foundation/identity/jsonld/JsonLDObject.java +++ b/src/main/java/foundation/identity/jsonld/JsonLDObject.java @@ -88,7 +88,19 @@ public JsonLDObject build() { if (this.base != null) { JsonLDUtils.jsonLdAddAll(this.jsonLdObject, this.base.getJsonObject()); } if (this.defaultContexts) { List contexts = new ArrayList<>(JsonLDObject.getDefaultJsonLDContexts(this.jsonLdObject.getClass())); if (this.contexts != null) contexts.addAll(this.contexts); if (! contexts.isEmpty()) this.contexts = contexts; } if (this.defaultTypes) { List types = new ArrayList<>(JsonLDObject.getDefaultJsonLDTypes(this.jsonLdObject.getClass())); if (this.types != null) types.addAll(this.types); if (! types.isEmpty()) this.types = types; } - if (this.contexts != null) if (this.forceContextsArray) JsonLDUtils.jsonLdAddAsJsonArray(this.jsonLdObject, Keywords.CONTEXT, this.contexts.stream().map(JsonLDUtils::uriToString).collect(Collectors.toList())); else JsonLDUtils.jsonLdAdd(this.jsonLdObject, Keywords.CONTEXT, this.contexts.stream().map(JsonLDUtils::uriToString).collect(Collectors.toList())); + if (this.contexts != null) { + List existingContexts = this.contexts.stream().map(JsonLDUtils::uriToString).collect(Collectors.toList()); + if (this.forceContextsArray) { + JsonLDUtils.jsonLdAddAsJsonArray(this.jsonLdObject, Keywords.CONTEXT, existingContexts); + } else { + if(this.jsonLdObject.getContexts()==null || this.jsonLdObject.getContexts().isEmpty()){ + JsonLDUtils.jsonLdAdd(this.jsonLdObject, Keywords.CONTEXT, existingContexts); + }else{ + JsonLDUtils.jsonLdAddAsJsonArray(this.jsonLdObject, Keywords.CONTEXT, existingContexts); + } + } + } + if (this.types != null) if (this.forceTypesArray) JsonLDUtils.jsonLdAddAsJsonArray(this.jsonLdObject, JsonLDKeywords.JSONLD_TERM_TYPE, this.types); else JsonLDUtils.jsonLdAdd(this.jsonLdObject, JsonLDKeywords.JSONLD_TERM_TYPE, this.types); if (this.id != null) JsonLDUtils.jsonLdAdd(this.jsonLdObject, JsonLDKeywords.JSONLD_TERM_ID, JsonLDUtils.uriToString(this.id)); if (this.properties != null) JsonLDUtils.jsonLdAddAll(this.jsonLdObject, this.properties);