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

Add ability to change json stringify / json parse methods for DataConnection #592

Merged
merged 2 commits into from
Nov 1, 2019

Conversation

afrokick
Copy link
Member

@afrokick afrokick commented Nov 1, 2019

Related to #582

By default, dataConnection uses JSON.parse and JSON.stringify methods when read/write data.

I added two fields to DataConnection:

DataConnection#stringify: (data: any) => string;

DataConnection#parse: (data: string) => any;

You can use, for example, https://github.com/WebReflection/flatted to support circular JSON parser.

import Peer from 'peerjs';
import { parse, stringify } from 'flatted/esm';

...

const dataConnection = peer.connect('123', { serialization: 'json' });

dataConnection.stringify = stringify;
dataConnection.parse = parse;

...

On another side:

import Peer from 'peerjs';
import { parse, stringify } from 'flatted/esm';

...

peer.on('connection', dataConnection => {
  dataConnection.stringify = stringify;
  dataConnection.parse = parse;
});

...

@kidandcat
Copy link
Member

You should remove "json" from the methods name: stringify and parse, because with this change you can use any encode you want, not need to forcibly use JSON.

@afrokick afrokick merged commit 0645dc3 into peers:master Nov 1, 2019
@afrokick afrokick deleted the feature/fix-582 branch November 1, 2019 19:16
@afrokick afrokick mentioned this pull request Dec 24, 2019
@afrokick afrokick mentioned this pull request Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants