Skip to content

Commit

Permalink
serialize now omits _id if the object is referenced only once
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-src-client-serialize.js,AUTO-COMMIT-test-serialize-test.js,
  • Loading branch information
onsetsu committed Nov 30, 2023
1 parent 8e4e4d9 commit 1ec894b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/client/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ import { uuid } from 'utils';
export function serialize(obj, outerReplacer) {
const references = new Map();

const seenOnce = new Set();
const seenManyTimes = new Set();
function countReferences(value) {
if (value instanceof Object && !(value instanceof Function)) {
if (seenOnce.has(value)) {
seenManyTimes.add(value)
// Skip counting for already seen objects to avoid infinite loops
return;
}
seenOnce.add(value);

// Recursively count references for object properties
for (let key of Object.keys(value)) {
countReferences(value[key]);
}
}
return value;
}

// First pass to count references
countReferences(obj);

function replacer(key, value) {
if (outerReplacer) {
value = outerReplacer.call(this, key, value);
Expand All @@ -17,14 +39,18 @@ export function serialize(obj, outerReplacer) {

if (value instanceof Object && !(value instanceof Function)) {
if (!references.has(value)) {
// 1st occurence: remember you saw that one
const id = uuid();
references.set(value, id);
const needsId = seenManyTimes.has(value);
let id
if (needsId) {
// 1st occurence of many: remember you saw that one
id = uuid();
references.set(value, id);
}

if (Array.isArray(value)) {
return { $id: id, $array: [...value] };
return needsId ? { $id: id, $array: [...value] } : value;
} else {
const result = Object.assign({ $id: id }, value);
const result = Object.assign(needsId ? { $id: id } : {}, value);

const classToRemember = value.__proto__.constructor;
if (classToRemember !== Object) {
Expand Down Expand Up @@ -62,6 +88,7 @@ export function deserialize(json, classes = {}, outerReviver) {
}

if (value.$ref) {
// we have a ref before its definition -> create a stub based on the type
return idToObj.getOrCreate(value.$ref, () => value.$isArray ? [] : {});
}

Expand Down
21 changes: 21 additions & 0 deletions test/serialize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,25 @@ describe('simple serialization with JSON.{parse,stringify}', () => {
expect(revivedO.sub.o2 === o.sub.o2).to.be.true;
});
});

describe('do not use $id unless its necessary', () => {
it('not in output', () => {
const simpleObject = { prop: [1, 2, { c: 42}] }

const jsonString = serialize(simpleObject);
expect(jsonString).not.to.match(/\$id/)
});

it('only use $id when reference a thing multiple times', () => {
const singleReffed = {
multiReffed: {}
}
singleReffed.secondRef = singleReffed.multiReffed

const plainCopy = JSON.parse(serialize(singleReffed))
expect(plainCopy).not.to.have.property('$id');
expect(plainCopy.multiReffed).to.have.property('$id');
});
});

});

0 comments on commit 1ec894b

Please sign in to comment.