Socket
Socket
Sign inDemoInstall

is-json

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-json - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

12

index.js

@@ -12,16 +12,16 @@

module.exports = isJson;
module.exports = isJSON;
function isJson (str, pass_objects) {
function isJSON (str, pass_object) {
if (pass_object && isObject(str)) return true;
if (pass_objects && isObject(str)) return true;
if (!isString(str)) return false;
// https://github.com/douglascrockford/JSON-js/blob/master/json2.js#L451
return /^[\],:{}\s]*$/
.test(str
.replace(/""/, '"""')
.replace(/""/, '"""') // deal with some bad structure ex: "a":"obja""b":[0],
.replace(/\\["\\\/bfnrtu]/g, '@')
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''));
}
}
{
"name": "is-json",
"version": "1.1.1",
"version": "1.1.2",
"description": "check if a string is a valid JSON string without using Try/Catch",

@@ -25,3 +25,6 @@ "main": "index.js",

},
"homepage": "https://github.com/joaquimserafim/is-json"
"homepage": "https://github.com/joaquimserafim/is-json",
"devDependencies": {
"tape": "^2.13.1"
}
}

@@ -15,3 +15,3 @@ # is-json

isJson(str*, [passObjects=bool])
isJSON(str*, [passObjects=bool])

@@ -21,3 +21,3 @@ *with `passObjects = true` can pass a JSON object in `str`, default to `false`

var isJson = require('is-json');
var isJSON = require('is-json');

@@ -29,12 +29,12 @@ var good_json = '{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}';

console.log(isJson(good_json)); // true
console.log(isJson(bad_json)); // false
console.log(isJson(str_number)); // false
console.log(isJSON(good_json)); // true
console.log(isJSON(bad_json)); // false
console.log(isJSON(str_number)); // false
// v1.1 now can check is a JSON object
// check is an object
var json = {a: 12, b: [1,2,3]};
var object = {a: 12, b: [1,2,3]};
console.log(isJson(json, true)); // true
console.log(isJSON(object, true)); // true

@@ -1,23 +0,22 @@

var test = require('assert');
var test = require('tape');
var isJSON = require('../');
var isJson = require('../');
test('performe isJSON verifications', function (t) {
t.plan(11);
console.log('Running tests...');
[
null,
false,
'',
'normal string',
12121212,
1212.12,
[1,2,3],
'{"a":"obja""b":[0,1,2],"c":{"d":"some object"}}',// bad json
{a: 12, b: [1,2,3]},// json object
'{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}'// this will pass
].forEach(function (ele, i) {
test.equal(i === 8 ? isJson(ele, true) : isJson(ele), i < 8 ? false : true, 'Only the two last elements are a valid JSON');
t.deepEqual(isJSON(null), false, '`null`, should return false');
t.deepEqual(isJSON(false), false, '`false`, should return false');
t.deepEqual(isJSON(''), false, '`\'\'`, should return false');
t.deepEqual(isJSON('normal string'), false, '\'normal string\', should return false');
t.deepEqual(isJSON(2014), false, '`2014`, should return false');
t.deepEqual(isJSON(2014.5), false, '`2014.5`, should return false');
t.deepEqual(isJSON([1,2,3,4]), false, '`[1,2,3,4]`, should return false');
t.deepEqual(isJSON('{"a":"obja""b":[0,1,2],"c":{"d":"some object"}}'), false,
'a bad JSON `\'{"a":"obja""b":[0,1,2],"c":{"d":"some object"}}\', should return false');
t.deepEqual(isJSON({a: 12, b: [1,2,3]}), false, 'a JSON object `{a: 12, b: [1,2,3]},`, should return false');
t.deepEqual(isJSON({a: 12, b: [1,2,3]}, true), true,
'a JSON object `{a: 12, b: [1,2,3]}` but pass the 2 arg as true (check objects too), should return true');
t.deepEqual(isJSON('{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}'), true,
'`{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}`, should return true');
});
console.log('finish.');

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc