Open mrkvon opened 1 year ago
After a bit of research, i was able to do it as follows:
// stringifying objects with circular reference, according to MDN:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references
const getCircularReplacer = () => {
const seen = new WeakSet()
return (key: string, value: unknown) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return
}
seen.add(value)
}
return value
}
}
const ldo2json = <T,>(ldo: LinkedDataObject<T>) =>
JSON.parse(JSON.stringify(ldo, getCircularReplacer()))
// usage
// get LDO foaf profile
const profile = await FoafProfileFactory.parse(
webId,
rawProfile,
{ baseIRI: session.webId },
)
// convert it to plain javascript object
console.log(ldo2json(profile))
Still, any better solutions would be appreciated!
I was going to suggest using JSON.stringify as well. Unfortunately, $toJsonLd() needed to be removed because a third party library (jsonld-streaming-parser) had dependency problems with webpack. I will check to see if it's fixed next time I publish LDO.
I'm trying to use LDO in a Solid React project, and I want to store data in redux. For this I need to convert LDO (which is a
Proxy
) to plain object. Is it currently possible?I tried to use
ldo.$toJsonLd()
, but I get errorNot Implemented
.Is
$toJsonLD
planned, or can this be achieved in some other way?Btw i love the API and potential of this library. Great work!