From fbea9e58d2e5a18f0c7d9f8e64890ac6b7bc0984 Mon Sep 17 00:00:00 2001 From: Vadim Kiryukhin Date: Wed, 13 Jun 2012 23:52:07 -0700 Subject: [PATCH] native JSON support is added --- vkbeautify.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/vkbeautify.js b/vkbeautify.js index fb67919..2b4f9c2 100644 --- a/vkbeautify.js +++ b/vkbeautify.js @@ -1,7 +1,7 @@ /** * vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON and CSS formats. * -* Version - 0.97.00.beta +* Version - 0.98.00.beta * Copyright (c) 2012 Vadim Kiryukhin * vkiryukhin @ gmail.com * http://www.eslinstructor.net/vkbeautify/ @@ -152,6 +152,21 @@ vkbeautify.prototype.xml = function(text,step) { vkbeautify.prototype.json = function(text,step) { + // Attempt to process using the native JSON first + if ( window.JSON && window.JSON.stringify ) { + if ( typeof text === "string" ) { + return JSON.stringify(JSON.parse(text), null, step); + } + if ( typeof text === "object" ) { + return JSON.stringify(text, null, step); + } + return null; + } + + // there is no native JSON object, so let's use regexp + + if ( typeof text !== "string" || !text ) return null; + var ar = this.jsonmin(text).replace(/\{/g,"~::~{~::~") .replace(/\[/g,"[~::~") .replace(/\}/g,"~::~}")