File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
3
+ // eslint-disable-next-line
4
+ const ASCII_ESCAPE = / [ \u0000 - \u001f \u0022 \u005c ] /
5
+
3
6
module . exports = class Serializer {
4
7
constructor ( options ) {
5
8
switch ( options && options . rounding ) {
@@ -89,6 +92,7 @@ module.exports = class Serializer {
89
92
90
93
asString ( str ) {
91
94
const len = str . length
95
+
92
96
if ( len === 0 ) {
93
97
return '""'
94
98
} else if ( len < 42 ) {
@@ -116,7 +120,7 @@ module.exports = class Serializer {
116
120
}
117
121
}
118
122
return ( last === - 1 && ( '"' + str + '"' ) ) || ( '"' + result + str . slice ( last ) + '"' )
119
- } else if ( len < 5000 && str . isWellFormed ( ) ) {
123
+ } else if ( len < 5000 && str . isWellFormed ( ) && ASCII_ESCAPE . test ( str ) === false ) {
120
124
// Only use the regular expression for shorter input. The overhead is otherwise too much.
121
125
return '"' + str + '"'
122
126
} else {
Original file line number Diff line number Diff line change @@ -34,6 +34,21 @@ test('serialize short string', (t) => {
34
34
t . assert . equal ( JSON . parse ( output ) , input )
35
35
} )
36
36
37
+ test ( 'serialize medium string' , ( t ) => {
38
+ t . plan ( 2 )
39
+
40
+ const schema = {
41
+ type : 'string'
42
+ }
43
+
44
+ const input = new Array ( 150 ) . fill ( '\x00' ) . join ( '' )
45
+ const stringify = build ( schema )
46
+ const output = stringify ( input )
47
+
48
+ t . assert . equal ( output , `"${ new Array ( 150 ) . fill ( '\\u0000' ) . join ( '' ) } "` )
49
+ t . assert . equal ( JSON . parse ( output ) , input )
50
+ } )
51
+
37
52
test ( 'serialize long string' , ( t ) => {
38
53
t . plan ( 2 )
39
54
You can’t perform that action at this time.
0 commit comments