Skip to content

Commit

Permalink
Improve Json-LD encoder based on Circe (#4278)
Browse files Browse the repository at this point in the history
* Improve Json-LD encoder based on Circe

---------

Co-authored-by: Simon Dumas <[email protected]>
  • Loading branch information
imsdu and Simon Dumas authored Sep 18, 2023
1 parent 93ac73d commit 86a4463
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.{ContextValue, RemoteCon
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.{CompactedJsonLd, ExpandedJsonLd}
import ch.epfl.bluebrain.nexus.delta.rdf.syntax._
import ch.epfl.bluebrain.nexus.delta.rdf.{IriOrBNode, RdfError}
import io.circe.Encoder
import io.circe.{Encoder, Json}
import io.circe.syntax._
import monix.bio.IO

Expand Down Expand Up @@ -137,21 +137,31 @@ object JsonLdEncoder {
value: A
)(implicit opts: JsonLdOptions, api: JsonLdApi, rcr: RemoteContextResolution): IO[RdfError, CompactedJsonLd] =
for {
expanded <- expand(value)
compacted <- expanded.toCompacted(context(value))
(expanded, context) <- expandAndExtractContext(value)
compacted <- expanded.toCompacted(context)
} yield compacted

override def expand(
value: A
)(implicit opts: JsonLdOptions, api: JsonLdApi, rcr: RemoteContextResolution): IO[RdfError, ExpandedJsonLd] = {
val json = value.asJson.replaceContext(context(value).contextObj)
ExpandedJsonLd(json).map {
case expanded if fId(value).isBNode && expanded.rootId.isIri => expanded
case expanded => expanded.replaceId(fId(value))
}
)(implicit opts: JsonLdOptions, api: JsonLdApi, rcr: RemoteContextResolution): IO[RdfError, ExpandedJsonLd] =
expandAndExtractContext(value).map(_._1)

private def expandAndExtractContext(
value: A
)(implicit opts: JsonLdOptions, api: JsonLdApi, rcr: RemoteContextResolution) = {
val json = value.asJson
val context = contextFromJson(json)
ExpandedJsonLd(json.replaceContext(context.contextObj))
.map {
case expanded if fId(value).isBNode && expanded.rootId.isIri => expanded
case expanded => expanded.replaceId(fId(value))
}
.map(_ -> context)
}

override def context(value: A): ContextValue = value.asJson.topContextValueOrEmpty merge ctx
override def context(value: A): ContextValue = contextFromJson(value.asJson)

private def contextFromJson(json: Json): ContextValue = json.topContextValueOrEmpty merge ctx
}

implicit val jsonLdEncoderUnit: JsonLdEncoder[Unit] = new JsonLdEncoder[Unit] {
Expand Down

0 comments on commit 86a4463

Please sign in to comment.