From 5262ee0e3315ad82fd7730d9776106aac3435037 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 18 Jan 2025 20:18:58 +0100 Subject: [PATCH 1/3] feat: format dirty --- README.md | 22 ++++++++++++++++++++-- benchmark/bench.js | 16 ++++++++++++++++ index.js | 2 ++ lib/serializer.js | 7 ++++++- test/basic.test.js | 6 ++++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4bc8f0bd..4e873d83 100644 --- a/README.md +++ b/README.md @@ -641,8 +641,26 @@ const stringify = fastJson({ type: 'object', properties: { 'code': { - type: 'string', - format 'unsafe' + type: 'string', + format: 'unsafe' + } + } +}) +``` + + +#### Dirty string +String known to contain non-printable characters or surrogate pairs. + +Example: +```javascript +const stringify = fastJson({ + title: 'Example Schema', + type: 'object', + properties: { + 'code': { + type: 'string', + format: 'dirty' } } }) diff --git a/benchmark/bench.js b/benchmark/bench.js index acb87b94..770843fd 100644 --- a/benchmark/bench.js +++ b/benchmark/bench.js @@ -55,6 +55,14 @@ const benchmarks = [ }, input: 'hello world' }, + { + name: 'dirty short string', + schema: { + type: 'string', + format: 'dirty' + }, + input: 'hello\nworld' + }, { name: 'short string with double quote', schema: { @@ -92,6 +100,14 @@ const benchmarks = [ }, input: longString }, + { + name: 'dirty long string', + schema: { + type: 'string', + format: 'dirty' + }, + input: longString + '\n' + }, { name: 'number', schema: { diff --git a/index.js b/index.js index 2390c28a..304b67a2 100644 --- a/index.js +++ b/index.js @@ -736,6 +736,8 @@ function buildSingleTypeSerializer (context, location, input) { return `json += serializer.asTime(${input})` } else if (schema.format === 'unsafe') { return `json += serializer.asUnsafeString(${input})` + } else if (schema.format === 'dirty') { + return `json += JSON.stringify(${input})` } else { return ` if (typeof ${input} !== 'string') { diff --git a/lib/serializer.js b/lib/serializer.js index df81960e..b5f60bf1 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -2,6 +2,7 @@ // eslint-disable-next-line const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/ +const MAGIC_NUMBER = 42 module.exports = class Serializer { constructor (options) { @@ -92,7 +93,7 @@ module.exports = class Serializer { asString (str) { const len = str.length - if (len < 42) { + if (len < MAGIC_NUMBER) { // magically escape strings for json // relying on their charCodeAt // everything below 32 needs JSON.stringify() @@ -129,6 +130,10 @@ module.exports = class Serializer { return '"' + str + '"' } + asDirty (str) { + return JSON.stringify(str) + } + getState () { return this._options } diff --git a/test/basic.test.js b/test/basic.test.js index 754f4489..0c3a6c0b 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -24,6 +24,12 @@ buildTest({ format: 'unsafe' }, 'hello world') +buildTest({ + title: 'string', + type: 'string', + format: 'dirty' +}, 'hello\nworld') + buildTest({ title: 'basic', type: 'object', From 2fec47619e6d19ed46686c420028021541b2b8a2 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 18 Jan 2025 20:35:11 +0100 Subject: [PATCH 2/3] fix: revert magic number --- lib/serializer.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index b5f60bf1..c5950bda 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -2,7 +2,6 @@ // eslint-disable-next-line const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/ -const MAGIC_NUMBER = 42 module.exports = class Serializer { constructor (options) { @@ -93,7 +92,7 @@ module.exports = class Serializer { asString (str) { const len = str.length - if (len < MAGIC_NUMBER) { + if (len < 42) { // magically escape strings for json // relying on their charCodeAt // everything below 32 needs JSON.stringify() From 7a2a31add85286c7019634daaf78f53096b403d6 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 18 Jan 2025 20:43:19 +0100 Subject: [PATCH 3/3] fix: call serializer.asDirty --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 304b67a2..c0d77ca9 100644 --- a/index.js +++ b/index.js @@ -737,7 +737,7 @@ function buildSingleTypeSerializer (context, location, input) { } else if (schema.format === 'unsafe') { return `json += serializer.asUnsafeString(${input})` } else if (schema.format === 'dirty') { - return `json += JSON.stringify(${input})` + return `json += serializer.asDirty(${input})` } else { return ` if (typeof ${input} !== 'string') {