Skip to content

Commit

Permalink
Added json export too
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Nov 4, 2021
1 parent 2cbf0bb commit 7c30dab
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ The behavior is the same found in *JSON* when it comes to *Array*, so that unsup
#### toJSON

If `lossy` option is not enough, `json` will actually enforce `lossy` and also check for `toJSON` method when objects are parsed.

Alternative, the `json` exports combines all features:

```js
import {stringify, parse} from '@ungap/structured-clone/json';

parse(stringify({any: 'serializable'}));
```
12 changes: 12 additions & 0 deletions cjs/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const {deserialize} = require('./deserialize.js');
const {serialize} = require('./serialize.js');

const {parse: $parse, stringify: $stringify} = JSON;
const options = {json: true, lossy: true};

const parse = str => deserialize($parse(str));
exports.parse = parse;

const stringify = str => $stringify(serialize(str, options));
exports.stringify = stringify;
5 changes: 2 additions & 3 deletions cjs/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ const serializer = (strict, json, $, _) => {
* like JSON stringify would behave. Symbol and Function will be discarded.
* @returns {Record[]}
*/
const serialize = (value, options = {}) => {
const serialize = (value, {json, lossy} = {}) => {
const _ = [];
const json = !!options.json;
return serializer(!(json || options.lossy), json, new Map, _)(value), _;
return serializer(!(json || lossy), !!json, new Map, _)(value), _;
};
exports.serialize = serialize;
9 changes: 9 additions & 0 deletions esm/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {deserialize} from './deserialize.js';
import {serialize} from './serialize.js';

const {parse: $parse, stringify: $stringify} = JSON;
const options = {json: true, lossy: true};

export const parse = str => deserialize($parse(str));

export const stringify = str => $stringify(serialize(str, options));
5 changes: 2 additions & 3 deletions esm/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ const serializer = (strict, json, $, _) => {
* like JSON stringify would behave. Symbol and Function will be discarded.
* @returns {Record[]}
*/
export const serialize = (value, options = {}) => {
export const serialize = (value, {json, lossy} = {}) => {
const _ = [];
const json = !!options.json;
return serializer(!(json || options.lossy), json, new Map, _)(value), _;
return serializer(!(json || lossy), !!json, new Map, _)(value), _;
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"import": "./esm/index.js",
"default": "./cjs/index.js"
},
"./json": {
"import": "./esm/json.js",
"default": "./cjs/json.js"
},
"./package.json": "./package.json"
},
"directories": {
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let {serialize, deserialize, default: structuredClone} = require('../cjs');
let {stringify, parse} = require('../cjs/json');

const assert = (expected, result, message = '') => {
if (!Object.is(expected, result)) {
Expand Down Expand Up @@ -139,3 +140,11 @@ assert(lossy[2].size, 0);
assert(lossy[3].size, 0);
assert(JSON.stringify(lossy[4]), '{}');
assert(lossy[5], 'OK');

const lossy2 = parse(stringify(lossy));
assert(lossy2[0], 1);
assert(lossy2[1], null);
assert(lossy2[2].size, 0);
assert(lossy2[3].size, 0);
assert(JSON.stringify(lossy2[4]), '{}');
assert(lossy2[5], 'OK');

0 comments on commit 7c30dab

Please sign in to comment.