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
41 changes: 1 addition & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,46 +514,7 @@ const stringify = fastJson(schema, { schema: externalSchema })

<a name="long"></a>
#### Long integers
By default the library will handle automatically [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) from Node.js v10.3 and above.
If you can't use BigInts in your environment, long integers (64-bit) are also supported using the [long](https://github.com/dcodeIO/long.js) module.
Example:
```javascript
// => using native BigInt
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
id: {
type: 'integer'
}
}
})

const obj = {
id: 18446744073709551615n
}

console.log(stringify(obj)) // '{"id":18446744073709551615}'

// => using the long library
const Long = require('long')

const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
id: {
type: 'integer'
}
}
})

const obj = {
id: Long.fromString('18446744073709551615', true)
}

console.log(stringify(obj)) // '{"id":18446744073709551615}'
```
By default the library will handle automatically [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).

<a name="integer"></a>
#### Integers
Expand Down
65 changes: 11 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ const fjsCloned = Symbol('fast-json-stringify.cloned')
const validate = require('./schema-validator')
let stringSimilarity = null

let isLong
try {
isLong = require('long').isLong
} catch (e) {
isLong = null
}

const addComma = `
if (addComma) {
json += ','
Expand Down Expand Up @@ -121,8 +114,6 @@ function build (schema, options) {
})()


var isLong = ${isLong ? isLong.toString() : false}

function parseInteger(int) { return Math.${intParseFunctionName}(int) }
`

Expand Down Expand Up @@ -286,9 +277,7 @@ function $asNull () {
}

function $asInteger (i) {
if (isLong && isLong(i)) {
return i.toString()
} else if (typeof i === 'bigint') {
if (typeof i === 'bigint') {
return i.toString()
} else if (Number.isInteger(i)) {
return $asNumber(i)
Expand Down Expand Up @@ -547,22 +536,11 @@ function additionalProperty (location) {
} else if (type === 'integer') {
code += `
var t = Number(obj[keys[i]])
if (!isNaN(t)) {
${addComma}
json += $asString(keys[i]) + ':' + t
}
`
if (isLong) {
code += `
if (isLong(obj[keys[i]]) || !isNaN(t)) {
${addComma}
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
}
`
} else {
code += `
if (!isNaN(t)) {
${addComma}
json += $asString(keys[i]) + ':' + t
}
`
}
} else if (type === 'number') {
code += `
var t = Number(obj[keys[i]])
Expand Down Expand Up @@ -764,33 +742,12 @@ function buildCode (location, code, laterCode, name) {
} else if (type === 'integer') {
code += `
var rendered = false
`
if (isLong) {
code += `
if (isLong(obj[${sanitized}])) {
${addComma}
json += ${asString} + ':' + obj[${sanitized}].toString()
rendered = true
} else {
var t = Number(obj[${sanitized}])
if (!isNaN(t)) {
${addComma}
json += ${asString} + ':' + t
rendered = true
}
}
`
} else {
code += `
var t = Number(obj[${sanitized}])
if (!isNaN(t)) {
${addComma}
json += ${asString} + ':' + t
rendered = true
}
`
}
code += `
var t = Number(obj[${sanitized}])
if (!isNaN(t)) {
${addComma}
json += ${asString} + ':' + t
rendered = true
}
if (rendered) {
`
} else {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"benchmark": "^2.1.4",
"compile-json-stringify": "^0.1.2",
"is-my-json-valid": "^2.20.0",
"long": "^4.0.0",
"moment": "^2.24.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
Expand Down
74 changes: 0 additions & 74 deletions test/bigint.js

This file was deleted.

76 changes: 76 additions & 0 deletions test/bigint.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict'

const t = require('tap')
const test = t.test
const build = require('..')

test('render a bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'bigint',
type: 'integer'
}

const stringify = build(schema)
const output = stringify(1615n)

t.equal(output, '1615')
})

test('render an object with a bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'object with bigint',
type: 'object',
properties: {
id: {
type: 'integer'
}
}
}

const stringify = build(schema)
const output = stringify({
id: 1615n
})

t.equal(output, '{"id":1615}')
})

test('render an array with a bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'array with bigint',
type: 'array',
items: {
type: 'integer'
}
}

const stringify = build(schema)
const output = stringify([1615n])

t.equal(output, '[1615]')
})

test('render an object with an additionalProperty of type bigint as JSON', (t) => {
t.plan(1)

const schema = {
title: 'object with bigint',
type: 'object',
additionalProperties: {
type: 'integer'
}
}

const stringify = build(schema)
const output = stringify({
num: 1615n
})

t.equal(output, '{"num":1615}')
})
11 changes: 1 addition & 10 deletions test/integer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

const t = require('tap')
const test = t.test
const semver = require('semver')
const validator = require('is-my-json-valid')
const proxyquire = require('proxyquire')
const build = proxyquire('..', { long: null })
const build = require('..')

test('render an integer as JSON', (t) => {
t.plan(2)
Expand Down Expand Up @@ -130,10 +128,3 @@ test('render an object with an additionalProperty of type integer as JSON', (t)
t.equal(output, '{"num":1615}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

if (semver.gt(process.versions.node, '10.3.0')) {
require('./bigint')(t.test, build)
} else {
t.pass('Skip because Node version < 10.4')
t.end()
}
Loading