Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: FileBox.fromJSON() & fileBox.toJSON() #25

Closed
huan opened this issue Feb 12, 2020 · 0 comments
Closed

New feature: FileBox.fromJSON() & fileBox.toJSON() #25

huan opened this issue Feb 12, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@huan
Copy link
Owner

huan commented Feb 12, 2020

After enabling serialization for FileBox, we can transfer the FileBox on the wire.

Talk is cheap, show me the code

const fileBox = FileBox.fromQRCode('https://github.com')

const jsonText = JSON.stringify(fileBox)
// the above code equals to the following line of code:
// const jsonText = fileBox.toJSON()

// we will get the serialized data for this FileBox:
console.log(jsonText)

// restore our fleBox:
const newFileBox = FileBox.fromJSON(jsonText)

Limitation

Because we want to enable the JSON.stringify(fileBox), which will call fileBox.toJSON(), so the toJSON() can not be async, which means we can only support limited FileBoxType(s):

  1. FileBoxType.Base64
  2. FileBoxType.Url
  3. FileBoxType.QRCode

For other types like FileBoxType.Flie, FileBoxType.Buffer, FileBoxType.Stream, etc, we need to transform them to FileBoxType.Base64 before we call toJSON:

const fileBoxBefore = FileBox.fromFile('./test.txt')
const base64 = await fileBoxBefore.toBase64()

const fileBoxAfter = FleBox.fromBase64(base64, 'test.txt')
// fileBoxAfter will be serializable

console.log(JSON.stringify(fileBoxAfter))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant