Skip to content

Commit

Permalink
jsonld: allow converting quad values to their json-ld equivalents
Browse files Browse the repository at this point in the history
Signed-off-by: Iddan Aaronsohn <[email protected]>
  • Loading branch information
iddan authored and dennwc committed Sep 23, 2019
1 parent cb29b84 commit 5de4bca
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions jsonld/jsonld.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ func toTerm(v quad.Value) gojsonld.Term {
}
}

func typedStringToJSON(v quad.TypedString) map[string]string {
return map[string]string{
"@value": string(v.Value),
"@type": string(v.Type),
}
}

// FromValue converts quad value to a JSON-LD compatible object.
func FromValue(v quad.Value) interface{} {
switch v := v.(type) {
case quad.IRI:
return map[string]string{
"@id": string(v),
}
case quad.BNode:
return map[string]string{
"@id": v.String(),
}
case quad.String:
return string(v)
case quad.TypedString:
return typedStringToJSON(v)
case quad.LangString:
return map[string]string{
"@value": string(v.Value),
"@language": string(v.Lang),
}
case quad.TypedStringer:
return typedStringToJSON(v.TypedString())
default:
return v.String()
}
}

func toValue(t gojsonld.Term) quad.Value {
switch t := t.(type) {
case *gojsonld.Resource:
Expand Down

0 comments on commit 5de4bca

Please sign in to comment.