diff --git a/src/cid.js b/src/cid.js
index 7e6c2d01..a13d4ec8 100644
--- a/src/cid.js
+++ b/src/cid.js
@@ -35,6 +35,22 @@ export const format = (link, base) => {
}
}
+/**
+ * @template {API.UnknownLink} Link
+ * @param {Link} link
+ * @returns {API.LinkJSON}
+ */
+export const toJSON = (link) => ({
+ '/': format(link)
+})
+
+/**
+ * @template {API.UnknownLink} Link
+ * @param {API.LinkJSON} json
+ */
+export const fromJSON = (json) =>
+ CID.parse(json['/'])
+
/** @type {WeakMap>} */
const cache = new WeakMap()
@@ -200,11 +216,7 @@ export class CID {
}
toJSON () {
- return {
- code: this.code,
- version: this.version,
- hash: this.multihash.bytes
- }
+ return { '/': format(this) }
}
link () {
diff --git a/src/link.js b/src/link.js
index a52bf173..0cd0a849 100644
--- a/src/link.js
+++ b/src/link.js
@@ -1,7 +1,7 @@
// Linter can see that API is used in types.
// eslint-disable-next-line
import * as API from "./link/interface.js"
-import { CID, format } from './cid.js'
+import { CID, format, toJSON, fromJSON } from './cid.js'
// This way TS will also expose all the types from module
export * from './link/interface.js'
@@ -73,7 +73,7 @@ export const isLink = value => {
*/
export const parse = (source, base) => CID.parse(source, base)
-export { format }
+export { format, toJSON, fromJSON }
/**
* Decoded a CID from its binary representation. The byte array must contain
diff --git a/src/link/interface.ts b/src/link/interface.ts
index af8b7e47..06955a2c 100644
--- a/src/link/interface.ts
+++ b/src/link/interface.ts
@@ -35,12 +35,15 @@ export interface Link<
equals: (other: unknown) => other is Link
toString: (base?: MultibaseEncoder) => ToString, Prefix>
- toJSON: () => { version: V, code: Format, hash: Uint8Array }
link: () => Link
toV1: () => Link
}
+export interface LinkJSON {
+ '/': ToString
+}
+
export interface LegacyLink extends Link {
}
diff --git a/test/test-cid.spec.js b/test/test-cid.spec.js
index b1958b5a..cf59103a 100644
--- a/test/test-cid.spec.js
+++ b/test/test-cid.spec.js
@@ -482,13 +482,10 @@ describe('CID', () => {
it('toJSON()', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)
- const json = cid.toJSON()
- assert.deepStrictEqual(
- { ...json, hash: null },
- { code: 112, version: 1, hash: null }
- )
- assert.ok(equals(json.hash, hash.bytes))
+ assert.deepStrictEqual(cid.toJSON(), {
+ '/': cid.toString()
+ })
})
it('asCID', async () => {
diff --git a/test/test-link.spec.js b/test/test-link.spec.js
index cec2f7c2..7fdc1f05 100644
--- a/test/test-link.spec.js
+++ b/test/test-link.spec.js
@@ -89,6 +89,42 @@ describe('Link', () => {
assert.ok(t2)
})
})
+
+ describe('toJSON', () => {
+ assert.deepStrictEqual(Link.toJSON(Link.parse(h1)), {
+ '/': h1
+ })
+
+ assert.deepStrictEqual(Link.toJSON(Link.parse(h4)), {
+ '/': h4
+ })
+ })
+
+ describe('fromJSON', () => {
+ assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
+ '/': h1
+ }))
+
+ assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
+ '/': h1,
+ // @ts-expect-error
+ foo: 1
+ }))
+
+ assert.deepStrictEqual(Link.parse(h4), Link.fromJSON({
+ '/': h4
+ }))
+ })
+
+ describe('JSON.stringify', () => {
+ assert.equal(JSON.stringify(Link.parse(h1)), JSON.stringify({
+ '/': h1
+ }))
+
+ assert.equal(JSON.stringify(Link.parse(h4)), JSON.stringify({
+ '/': h4
+ }))
+ })
})
describe('decode', () => {