{-# LANGUAGE OverloadedStrings #-}
module Data.RDF.Encoder.NQuads (
encodeRDFGraph
, encodeRDFGraphs
, encodeTriple
, encodeQuad
) where
import qualified Data.ByteString.Builder as B
import Data.RDF.Types
import Data.RDF.Encoder.Common
encodeTriple :: Triple -> B.Builder
encodeTriple (Triple s p o) = encodeSubject s
<> B.byteString " "
<> encodePredicate p
<> B.byteString " "
<> encodeObject o
<> B.byteString " .\n"
encodeQuad :: Quad -> B.Builder
encodeQuad (Quad t Nothing) = encodeTriple t
encodeQuad (Quad (Triple s p o) (Just g)) = encodeSubject s
<> B.byteString " "
<> encodePredicate p
<> B.byteString " "
<> encodeObject o
<> B.byteString " "
<> encodeEscapedIRI g
<> B.byteString " .\n"
encodeRDFGraph :: RDFGraph -> B.Builder
encodeRDFGraph (RDFGraph Nothing ts) = mconcat $ map encodeTriple ts
encodeRDFGraph (RDFGraph (Just g) ts) = let qs = map (\t -> Quad t (Just g)) ts
in mconcat $ map encodeQuad qs
encodeRDFGraphs :: Foldable f => f RDFGraph -> B.Builder
encodeRDFGraphs = foldMap encodeRDFGraph