Skip to content

Commit f7ea9fa

Browse files
authored
Serialize additional properties at the end of the json (#206)
* Serialize additional properties at the end of the json * Updated test * Updated README
1 parent 7dcf65f commit f7ea9fa

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"
220220
If *additionalProperties* is not present or is set to `false`, every property that is not explicitly listed in the *properties* and *patternProperties* objects,will be ignored, as described in <a href="#missingFields">Missing fields</a>.
221221
Missing fields are ignored to avoid having to rewrite objects before serializing. However, other schema rules would throw in similar situations.
222222
If *additionalProperties* is set to `true`, it will be used by `JSON.stringify` to stringify the additional properties. If you want to achieve maximum performance, we strongly encourage you to use a fixed schema where possible.
223+
The additional properties will always be serialzied at the end of the object.
223224
Example:
224225
```javascript
225226
const stringify = fastJson({
@@ -252,7 +253,7 @@ const obj = {
252253
nomatchint: 313
253254
}
254255

255-
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
256+
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313"}'
256257
```
257258

258259
#### AnyOf

index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,15 +685,13 @@ function buildCodeWithAllOfs (schema, code, laterCode, name, externalSchema, ful
685685
}
686686

687687
function buildInnerObject (schema, name, externalSchema, fullSchema) {
688-
var laterCode = ''
689-
var code = ''
688+
var result = buildCodeWithAllOfs(schema, '', '', name, externalSchema, fullSchema)
690689
if (schema.patternProperties) {
691-
code += addPatternProperties(schema, externalSchema, fullSchema)
690+
result.code += addPatternProperties(schema, externalSchema, fullSchema)
692691
} else if (schema.additionalProperties && !schema.patternProperties) {
693-
code += addAdditionalProperties(schema, externalSchema, fullSchema)
692+
result.code += addAdditionalProperties(schema, externalSchema, fullSchema)
694693
}
695-
696-
return buildCodeWithAllOfs(schema, code, laterCode, name, externalSchema, fullSchema)
694+
return result
697695
}
698696

699697
function addIfThenElse (schema, name, externalSchema, fullSchema) {

test/additionalProperties.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('additionalProperties', (t) => {
1919
})
2020

2121
const obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: { a: true } }
22-
t.equal('{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}', stringify(obj))
22+
t.equal('{"str":"test","foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]"}', stringify(obj))
2323
})
2424

2525
test('additionalProperties should not change properties', (t) => {
@@ -38,7 +38,7 @@ test('additionalProperties should not change properties', (t) => {
3838
})
3939

4040
const obj = { foo: '42', ofoo: 42 }
41-
t.equal('{"ofoo":42,"foo":"42"}', stringify(obj))
41+
t.equal('{"foo":"42","ofoo":42}', stringify(obj))
4242
})
4343

4444
test('additionalProperties should not change properties and patternProperties', (t) => {
@@ -62,7 +62,7 @@ test('additionalProperties should not change properties and patternProperties',
6262
})
6363

6464
const obj = { foo: '42', ofoo: 42, test: '42' }
65-
t.equal('{"ofoo":"42","test":42,"foo":"42"}', stringify(obj))
65+
t.equal('{"foo":"42","ofoo":"42","test":42}', stringify(obj))
6666
})
6767

6868
test('additionalProperties set to true, use of fast-safe-stringify', (t) => {

test/patternProperties.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('patternProperties', (t) => {
2121
})
2222

2323
const obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: { a: true }, notMe: false }
24-
t.equal(stringify(obj), '{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}')
24+
t.equal(stringify(obj), '{"str":"test","foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]"}')
2525
})
2626

2727
test('patternProperties should not change properties', (t) => {
@@ -42,7 +42,7 @@ test('patternProperties should not change properties', (t) => {
4242
})
4343

4444
const obj = { foo: '42', ofoo: 42 }
45-
t.equal(stringify(obj), '{"ofoo":42,"foo":"42"}')
45+
t.equal(stringify(obj), '{"foo":"42","ofoo":42}')
4646
})
4747

4848
test('patternProperties - string coerce', (t) => {

0 commit comments

Comments
 (0)