Skip to content

Commit 8cdd6eb

Browse files
committed
fix: only invoke beforeStringify if first
1 parent 30599bc commit 8cdd6eb

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-flat-serializer",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "A typescript library to serialize/deserialize classes to/from string in a flat format. Supports inheritance, circular reference and more",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/flat-serializer.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,27 @@ export function stringify(obj: any, options?: StringifyOptions): string {
135135
first = true;
136136
outputArray.push(
137137
JSON.stringify(inputArray[i++], (key, value) => {
138-
if (value != null && typeof value === 'object') {
139-
for (const childKey of Object.keys(value)) {
140-
const propertyMetadata = getFlatPropertyMetadata(value, childKey);
141-
const collectionMetadata = getFlatCollectionMetadata(value, childKey);
142-
143-
if (propertyMetadata != null && propertyMetadata.beforeStringify) {
144-
value[childKey] = propertyMetadata.beforeStringify(value[childKey]);
145-
}
146-
147-
if (collectionMetadata != null) {
148-
value[childKey] = convertCollectionToArray(value[childKey], collectionMetadata.collectionType);
138+
if (first) {
139+
if (value != null && typeof value === 'object' && !(value instanceof Array)) {
140+
for (const childKey of Object.keys(value)) {
141+
const propertyMetadata = getFlatPropertyMetadata(value, childKey);
142+
const collectionMetadata = getFlatCollectionMetadata(value, childKey);
143+
144+
if (propertyMetadata != null && propertyMetadata.beforeStringify) {
145+
value[childKey] = propertyMetadata.beforeStringify(value[childKey]);
146+
}
147+
148+
if (collectionMetadata != null) {
149+
value[childKey] = convertCollectionToArray(value[childKey], collectionMetadata.collectionType);
150+
}
149151
}
150152
}
151-
}
152153

153-
if (first) {
154154
first = false;
155155
return formatValue(value);
156156
}
157157

158-
if (obj == null) return value;
158+
if (value == null) return value;
159159

160160
if (typeof value === 'object') {
161161
if (positionsByObj.has(value)) {

0 commit comments

Comments
 (0)