Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,18 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
var laterCode = ''

Object.keys(schema.properties || {}).forEach((key, i, a) => {
// Using obj.key !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
// Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
code += `
if (obj.${key} !== undefined) {
if (obj['${key}'] !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use the same accessor here?

Copy link
Member Author

@SimenB SimenB May 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accessor is in the nested function, not up here.

json += '${$asString(key)}:'
`

if (schema.properties[key]['$ref']) {
schema.properties[key] = refFinder(schema.properties[key]['$ref'], fullSchema, externalSchema)
}

const result = nested(laterCode, name, '.' + key, schema.properties[key], externalSchema, fullSchema)
const result = nested(laterCode, name, key, schema.properties[key], externalSchema, fullSchema)

code += result.code
laterCode = result.laterCode
Expand Down Expand Up @@ -392,6 +392,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
var code = ''
var funcName
const type = schema.type
const accessor = key.indexOf('[') === 0 ? key : `['${key}']`
switch (type) {
case 'null':
code += `
Expand All @@ -400,36 +401,36 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
break
case 'string':
code += `
json += $asString(obj${key})
json += $asString(obj${accessor})
`
break
case 'integer':
code += `
json += $asInteger(obj${key})
json += $asInteger(obj${accessor})
`
break
case 'number':
code += `
json += $asNumber(obj${key})
json += $asNumber(obj${accessor})
`
break
case 'boolean':
code += `
json += $asBoolean(obj${key})
json += $asBoolean(obj${accessor})
`
break
case 'object':
funcName = (name + key).replace(/[-.\[\]]/g, '') // eslint-disable-line
laterCode = buildObject(schema, laterCode, funcName, externalSchema, fullSchema)
code += `
json += ${funcName}(obj${key})
json += ${funcName}(obj${accessor})
`
break
case 'array':
funcName = (name + key).replace(/[-.\[\]]/g, '') // eslint-disable-line
laterCode = buildArray(schema, laterCode, funcName, externalSchema, fullSchema)
code += `
json += ${funcName}(obj${key})
json += ${funcName}(obj${accessor})
`
break
default:
Expand Down
12 changes: 12 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ buildTest({
'type': 'null'
}, null)

buildTest({
'title': 'deep object with weird keys',
'type': 'object',
'properties': {
'@version': {
'type': 'integer'
}
}
}, {
'@version': 1
})

buildTest({
'title': 'with null',
'type': 'object',
Expand Down