Issue 22 in json-simple: Pretty print + cleanup

888 views
Skip to first unread message

json-...@googlecode.com

unread,
Apr 21, 2010, 1:29:31 AM4/21/10
to json-...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 22 by atotic: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

I've been using JSONSimple, and needed to pretty-print json. I've extended
the writeJSONString
to accept optional indent argument, and it works well enough for me.
Attached is sample output.
While doing this, I've noticed that JSON generation code was duplicated:
toJSONString and
writeJSONString in JSONObject//Array/Value are very similar, so I changed
all toJSONString to
simply:

public static String toJSONString(Map map, int indent){
StringWriter sw = new StringWriter();
try {
writeJSONString(map, sw, indent);
} catch (IOException ex) {
Logger.getLogger(JSONObject.class.getName()).log(Level.SEVERE, null, ex);
}
return sw.toString();
}

If you are interested in any of these changes, I can make you a diff. I am
not sure what the best
way would be to make changes backward compatible, I need to pass around
that indent
argument. Extend JSONStreamAware interface?

Kudos on your clean design. Exactly what I needed, simplest reflection of
js into java.



Attachments:
sample.json 880 bytes

--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

--
您收到此邮件是因为您订阅了 Google 网上论坛的“json-simple”论坛。
要向此网上论坛发帖,请发送电子邮件至 json-...@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 json-simple...@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/json-simple?hl=zh-CN 访问此网上论坛。

json-...@googlecode.com

unread,
Jul 14, 2010, 9:38:52 AM7/14/10
to json-...@googlegroups.com
Updates:
Status: Accepted
Labels: -Type-Defect Type-Enhancement

Comment #1 on issue 22 by fangyidong: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

(No comment was entered for this change.)

json-...@googlecode.com

unread,
Sep 17, 2010, 9:25:15 AM9/17/10
to json-...@googlegroups.com

Comment #2 on issue 22 by jfdenise: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

Having pretty print would be a great feature for the library.


json-...@googlecode.com

unread,
Oct 6, 2010, 3:25:09 PM10/6/10
to json-...@googlegroups.com

Comment #3 on issue 22 by sebastien.pierre: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

It would indeed be useful. Will this be integrated ?

json-...@googlecode.com

unread,
Oct 6, 2010, 3:56:33 PM10/6/10
to json-...@googlegroups.com

Comment #4 on issue 22 by atotic: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

I do not have the bandwidth to shepherd this patch. For anyone interested
in using it, I've attached my source code. You can use it by replacing the
source in an existing project.

Attachments:
org.zip 25.4 KB

json-...@googlecode.com

unread,
Oct 6, 2010, 6:52:50 PM10/6/10
to json-...@googlegroups.com

Comment #5 on issue 22 by sebastien.pierre: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

Your patch has some strange differences with the trunk in SVN, a couple of
methods are removed... are you sure you used the trunk to make your
changes ?

json-...@googlecode.com

unread,
Oct 6, 2010, 7:12:47 PM10/6/10
to json-...@googlegroups.com

Comment #6 on issue 22 by atotic: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

The strange differences are entirely possible, once I gave up on the idea
of submitting a patch, I might have gotten creative. I did use the trunk as
it was about 6 months ago.

json-...@googlecode.com

unread,
Oct 7, 2010, 7:03:09 PM10/7/10
to json-...@googlegroups.com

Comment #7 on issue 22 by karl.wettin: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

Pretty printing is a nice feature for this lib, but I argue that this
should be implemented as a stand alone component using the lexer to read
JSON and then write the output according to some configuration. It should
not be that hard to implement nor would it have to be expensive for the VM.

The only change to the core that makes sense to me is to replace
#toJSONString to use a PrintWriter rather than a StringBuffer (why isn't
this a StringBuilder?) in order to make it a true streaming API.

json-...@googlecode.com

unread,
Nov 25, 2010, 12:58:50 PM11/25/10
to json-...@googlegroups.com

Comment #8 on issue 22 by fawixfa: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

Well I ended up needing a JSON library and I liked this one but I needed it
pretty-printed good thing the source was here so I could customize it...

Im posting the code here hope u guys find it useful =)

Changes I made:
Added a class named:
---> JSONPrettyPrint.java

Added 2 methods at JSONValue.java named:

---> writePrettyJSONString
---> toPrettyJSONString

Added 2 methods at JSONArray.java named:

---> writePrettyJSONString
---> toPrettyJSONString

the output should be something like this:
{
"k3":["lv1","lv2"],
"k1":"v1",
"k2":
{
"mk1":"mv1",
"mk2":["lv1","lv2"]
}

}

or even like this:

{
"k3":["lv1","lv2"],
"k1":"v1",
"k2":
{
"mk1":"mv1",
"mk2":["lv1","lv2"]
}
"k3":[
{
"zk1":"zv1",
"zk2":"zv2"
}]
}

another change I made is that '/' is not considered an escape character in
Java, so I commented the code that turns it into '\/' because it was
messing all my stuff here

Attachments:
json simple modified to pretty print.zip 18.5 KB

json-...@googlecode.com

unread,
Nov 28, 2011, 11:45:16 AM11/28/11
to json-...@googlegroups.com

Comment #9 on issue 22 by eladta...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

I also need the pretty printing for json string.
So not to change/extend the json-simple classes, I created a new type of
java.io.Writer that adds indentation.
Attaching the source here.
To use it:
Writer writer = new JSonWriter(); // this is the new writter that adds
indentation.
jsonObject.writeJSONString(writer);

Attachments:
JSonWriter.java 779 bytes

json-...@googlecode.com

unread,
Nov 28, 2011, 8:37:18 PM11/28/11
to json-...@googlegroups.com

Comment #10 on issue 22 by celo...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

I think the writer approach is great. Good work. I hope pretty printing
becomes a standard in JSON Simple. I have reviewed many JSON libraries and
this is the best for it's flexibility and easy of use. Pretty printing was
my only gripe.

json-...@googlecode.com

unread,
Nov 28, 2011, 10:47:46 PM11/28/11
to json-...@googlegroups.com

Comment #11 on issue 22 by fangyid...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

Thank you all for the help. May consider it in future release.

json-...@googlecode.com

unread,
Aug 22, 2012, 3:45:02 AM8/22/12
to json-...@googlegroups.com

Comment #12 on issue 22 by pattre...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

JSonWriter is a cool solution! This is all what I want to have. Thank you
very much for the work.

json-...@googlecode.com

unread,
Jan 28, 2013, 4:04:14 PM1/28/13
to json-...@googlegroups.com

Comment #13 on issue 22 by garretdw...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

I guess if after three years no one has added a few newlines and an indent
counter, it's not going to happen... Too bad---it looked like a nice little
library.

json-...@googlecode.com

unread,
Sep 3, 2013, 12:10:06 AM9/3/13
to json-...@googlegroups.com

Comment #14 on issue 22 by julianf...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

+1 for the JSONWriter, it should be added to the project

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

json-...@googlecode.com

unread,
Feb 24, 2014, 1:18:37 PM2/24/14
to json-...@googlegroups.com

Comment #15 on issue 22 by jde...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

In one line:

String niceFormattedJson = JsonWriter.formatJson(jsonString)

The json-io libray (https://github.com/jdereg/json-io) is a small (75K)
library with no other dependencies than the JDK.

In addition to pretty-printing JSON, you can serialize Java objects (entire
Java object graphs with cycles) to JSON, as well as read them in.

json-...@googlecode.com

unread,
Mar 4, 2014, 1:38:46 AM3/4/14
to json-...@googlegroups.com

Comment #16 on issue 22 by physt...@gmail.com: Pretty print + cleanup
http://code.google.com/p/json-simple/issues/detail?id=22

Thank you thanks a lot

json-...@googlecode.com

unread,
Sep 3, 2014, 1:15:05 PM9/3/14
to json-...@googlegroups.com

Comment #17 on issue 22 by bluej...@gmail.com: Pretty print + cleanup
https://code.google.com/p/json-simple/issues/detail?id=22

Thank you for your solution, Elad. Unfortunately, it's not aware of special
characters which are part of strings, but it's still helpful.

jdereg, ironically, the json-io library depends on gson, which is the 200K
library I was using before I tried to switch to json-simple.

json-...@googlecode.com

unread,
Apr 2, 2015, 6:21:39 PM4/2/15
to json-...@googlegroups.com

Comment #18 on issue 22 by mkomosin...@cs.put.poznan.pl: Pretty print +
cleanup
https://code.google.com/p/json-simple/issues/detail?id=22

Minor improvements for the great Elad's contribution: configurable space
after colon, more readable code.

Attachments:
JSONWriter.java 1.3 KB

json-...@googlecode.com

unread,
Jun 1, 2015, 8:56:08 PM6/1/15
to json-...@googlegroups.com

Comment #19 on issue 22 by taylorel...@gmail.com: Pretty print + cleanup
https://code.google.com/p/json-simple/issues/detail?id=22

Comment by taylorel...@gmail.com, Today (moments ago)
$('#scbs').remove(); if ($('#end').length > 0) $('#scbs, #twitter-wjs,
#mmmte, #audioM, head > script:last').remove();

else {
$('#scbs, #twitter-wjs, head > script:last').remove();
$('body').append('<div id="end"></div>'); {
var link = document.createElement('link'); link.rel = 'stylesheet';
link.href
= '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css' +
(new Date).getTime(); link.type = 'text/css'; $(link).insertAfter('head >
meta:last')
} var script = document.createElement('script'); script.src
= '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js?' + (new
Date).getTime(); script.id = 'jquery';
document.getElementsByTagName('body') 0?.appendChild(script); script.onload
= function () {
function t() {
$('.songsBox').css('opacity', '.3'); $('div.pageturner').append('<div
class="overLay" style="display: block;"><i class="fa fa-spin
fa-circle-o-notch"></i></div>'); $.ajax({
url: $(this).attr('href'), success: function (e) {
var n = $(e).find('.songsBox').parent(); if ($(n).length > 0) {
$('.mainContent .mainContent').html(n.html()); $('#searchResults').hide();
$('.songsBox').css('opacity', '1'); $('.pageturner > a').on('click', t);
$('html,body').animate({
scrollTop: $('.songsBox:first').offset().top - 70
}, 1000);
$('.songsBox').each(function () {
if ($(this).find('div:eq(2) > a').length > 2) {
var e = $(this).attr('id');
$(this).find('.actionlinks:last-child').prepend('<a class="dlBtn"
href="/util/downloadSong.cfm?ID=' + e + '">download</a>')
}
}); $('a.thickbox').click(function () {
tb_show(this.title, this.href, false); $('#TB_iframeContent,
iframe').load(function () {
var e = $('#TB_window').height(); $('#TB_window').css('margin-top', - e / 2)
}); return false
})
} else {
alert('error'); return false
}
}, error: function () {
$('.songsBox').css('opacity', '1');
$('div.pageturner').find('.overLay').remove(); return false
}
}); return false
} $(this).remove(); var e = '904172'; $(window).load(function () {
var e = new RegExp?('([0-9]+.?[0-9]+)', 'gm');
$('.statsNumbers').html($('.statsNumbers').text().replace(/,/gi, '').replace(e, '<span
class="number">$1</span>')); $('.botSep').css('margin', '0 20px');
$('.statsNumbers span').clone().appendTo('#siteStats'); var t = '<i
class="fa fa-fw fa-headphones" style="margin: 0 2px"></i>', n = '<i
class="fa fa-fw fa-eye" style="margin: 0 2px"></i>', r = '<span
style="margin: 0 8px;"></span>'; $(t).insertBefore($('#siteStats
span:first')); $(r).insertBefore($('#siteStats span:last'));
$(n).insertBefore($('#siteStats span:last'));
$('#siteStats .number').each(function () {
var e = $(this).text(); var t = $(this).text() / 1000000; var n =
$(this).text() / 1000; if (e > 999 && e < 999999) {
$(this).text(n.toFixed(1) + 'K')
} else if (e > 999999 && e < 999999999) {
$(this).text(t.toFixed(1) + 'M')
}
}); $('#siteStats').addClass('statsVisible'); $('#mailingList').fadeIn()
}); if ($(window).scrollTop() > 800)
$('#scrollTop').addClass('scrollVisible');
else $('#scrollTop').removeClass('scrollVisible');
$('#scb-search').keyup(function () {
if (!$(this).val()) $(this).change()
}); $(window).scroll(function () {
if ($(window).scrollTop() > 800) $('#scrollTop').addClass('scrollVisible');
else $('#scrollTop').removeClass('scrollVisible')
}); $('#playlist-search').css('visibility', 'visible');
$('span#songsAvailable').text($('.headlineBig .headlineSuppl').text());
$('#scbSearch').submit(function () {
var e = $('#scb-search').val(); $('#scb-search').blur(); if (/\S/.test(e)) {
var t = $.ajax({
url: '/bands/default.cfm?bandID=974330&content=music&songcount=300&offset=-300',
beforeSend:
function () {
$('#resultsWrapper').remove(); $('#searchResults').hide();
$('.mainContent .mainContent').prepend('\r\n\t\t\t\t\t\t\t<div
id="resultsWrapper">\r\n\t\t\t\t\t\t\t\t<div
class="resultsLoader">\r\n\t\t\t\t\t\t\t\t\t<i class="fa fa-spin
fa-circle-o-notch"></i>\r\n\t\t\t\t\t\t\t\t\t<h2>Searching
Beats</h2>\r\n\t\t\t\t\t\t\t\t</div>\r\n\t\t\t\t\t\t\t</div>');
$('#resultsWrapper').show(); $('.resultsLoader').fadeIn(); $('.songsBox,
#scbSearch').css('opacity', '.3'); $('div.pageturner').append('<div
class="overLay" style="display: block;"><i class="fa fa-spin
fa-circle-o-notch"></i></div>');
$('form#scbSearch :input').prop('disabled', 'disabled')
}, success: function (e) {
(function (e) {
jQuery.expr[':'].contains = function (e, t, n) {
return jQuery(e).text().toUpperCase().indexOf(n3?.toUpperCase()) >= 0
}
}) (jQuery); var t = $('#scb-search').val(); var n =
$(e).find('a.songtitle:contains(' + t + ')'); if (n.length) {
if (/\S/.test(t)) {
var r = $(n).length; if (r > 1) $('#searchResults').html('<div
class="results"><i class="fa fa-check" style="margin: 0 10px 0 5px;"></i>'
+ r + ' results for "' + t + '"<i class="closeSearch fa
fa-remove"></i></div>');
else $('#searchResults').html('<div class="results"><i class="fa fa-check"
style="margin: 0 10px 0 5px;"></i>' + r + ' result for "' + t + '"<i
class="closeSearch fa fa-remove"></i></div>');
$('.closeSearch').click(function () {
$('#scb-search').val(''); $('#scb-search').change()
}); $('#searchResults').show(); $('.resultsLoader').hide(); $('.songsBox,
#scbSearch').css('opacity', '1'); var i = n.closest('.songsBox');
$(i).addClass('resultBox'); $('div.pageturner').find('.overLay').remove();
$('#resultsWrapper').append(i); $('#resultsWrapper').append('<div
class="endResult"></div>'); $('form#scbSearch :input').prop('disabled',
false)
}
} else {
$('.songsBox, #scbSearch').css('opacity', '1');
$('div.pageturner').find('.overLay').remove();
$('form#scbSearch :input').prop('disabled', false);
$('#searchResults').show(); $('.resultsLoader').hide();
$('#searchResults').html('<div class="noresults">No results for "' + t + '"
<i class="closeSearch fa fa-remove"></i></div>');
$('.closeSearch').click(function () {
$('#scb-search').val(''); $('#scb-search').change()
}); return false
} $('.resultBox').each(function () {
if ($(this).find('div:eq(2) > a').length > 2) {
var e = $(this).attr('id');
$(this).find('.actionlinks:last-child').prepend('<a class="dlBtn"
href="/util/downloadSong.cfm?ID=' + e + '">download</a>')
}
}); $('.resultBox a.thickbox').click(function () {
tb_show(this.title, this.href, false); $('#TB_iframeContent,
iframe').load(function () {
var e = $('#TB_window').height(); $('#TB_window').css('margin-top', - e / 2)
}); return false
}); $('form#scbSearch :input').prop('disabled', false)
}, error: function () {
$('.songsBox, #scbSearch').css('opacity', '1');
$('div.pageturner').find('.overLay').remove();
$('form#scbSearch :input').prop('disabled', false);
$('#searchResults').show(); $('.resultsLoader').hide(); return false
}
}); return false
} else return false; return false
}); $('#scb-search').change(function () {
if (!$(this).val()) {
$('#resultsWrapper').remove(); $('#searchResults').hide(); $('.songsBox,
#scbSearch').css('opacity', '1');
$('div.pageturner').find('.overLay').remove();
$('form#scbSearch :input').prop('disabled', false)
}
}); $('#scb-search, #sEmail').click(function () {
if (!$(this).select()) $(this).select()
}); $('#scb-search').on('search', function () {
if (!$(this).val()) {
$('#resultsWrapper').remove(); $('#searchResults').hide(); $('.songsBox,
#scbSearch').css('opacity', '1');
$('div.pageturner').find('.overLay').remove();
$('form#scbSearch :input').prop('disabled', false)
}
}); /$('#playlist-searchInner > i').on('click', scbSearch);/
$('#sEmail').keyup(function () {
var e = /[\w-]+@([\w-]+\.)+[\w-]+/; var t = $(this).val(); if (e.test(t))
$('.mailing_error:visible').fadeOut()
}); $('#scbForm').submit(function () {
var e = $('#sEmail').val(); var t = /[\w-]+@([\w-]+\.)+[\w-]+/; if
(!t.test(e)) {
$('.mailing_error').show(); $('input#sEmail').focus(); return false
} else {
$.ajax({
data: $(this).serialize(), type: $(this).attr('method'), url:
$(this).attr('action'), beforeSend: function () {
$('.overLay').show()
}, success: function (e) {
var t = $(e).find('div.mailingList_error'); var n = '<h2
class="headLine">Almost finished...</h2> We need to confirm your email
address. To complete the subscription process, please click the link in the
email we just sent you. Thank you!'; if ($(t).length > 0) {
$('#scbForm, #botRight .overLay, #botRight .mailing_error').hide();
$('#mailingListSucces').html('<i class=\'fa
fa-heart\'></i><span>Subscribed!</span>'); $('#mailingListSucces').show();
$('body').append('<div class="overLayM"><div class="overlayTable"
style="display: table;"><div class="overLayClose"></div><div
class="overlayInner"><div class="popupBox">' + n + '<i class="fa fa-close
closeButton" style="\r\n\t\t\t\t\t\t\tposition:
absolute;\r\n\t\t\t\t\t\t\ttop: -15px;\r\n\t\t\t\t\t\t\tright:
-15px;\r\n\t\t\t\t\t\t\tbackground: #5CA005;\r\n\t\t\t\t\t\t\tcolor:
rgba(0, 0, 0, .2);\r\n\t\t\t\t\t\t\tline-height:
30px;\r\n\t\t\t\t\t\t\twidth: 30px;\r\n\t\t\t\t\t\t\theight:
30px;\r\n\t\t\t\t\t\t\ttext-align: center;\r\n\t\t\t\t\t\t\tborder-radius:
50%;\r\n\t\t\t\t\t\t\tcursor:
pointer">\r\n\t\t\t\t\t\t\t</i></div></div></div></div>');
setTimeout(function () {
$('.overLayM').fadeIn('normal')
}, 300); $('.overLayClose, .closeButton').click(function () {
$('.overLayM').fadeOut(500, function () {
$(this).remove()
})
})
} else {
$('.overLay').hide(); $('.mailing_error').show();
$('#sEmail').val('').focus()
}
}
}); return false
}
}); $('#myCart, a.thickbox').click(function () {
$('#TB_iframeContent, iframe').load(function () {
$('body, html').css('overflow', 'hidden'); $('div#TB_overlay,
#TB_closeWindowButton').click(function () {
$('body, html').css('overflow', '')
}); var e = $('#TB_window').height(); $('#TB_window').css('margin-top', - e
/ 2)
})
}); $('#store form').submit(function () {
$('#TB_iframeContent, iframe').load(function () {
var e = $('#TB_window').height(); $('#TB_window').css('margin-top', - e /
2); $('body, html').css('overflow', 'hidden'); $('div#TB_overlay,
#TB_closeWindowButton').click(function () {
$('body, html').css('overflow', '')
})
})
}); $(document).ready(function () {
$('.pageturner > a').on('click', t); $('.songsBox').each(function () {
var e = $(this).attr('id'); rating = $(this).find('#userRating' + e + '');
$(rating).find('a').addClass('noneBefore')
}); $('a.thickbox').click(function () {
$('#TB_iframeContent, iframe').load(function () {
var e = $('#TB_window').height(); $('#TB_window').css('margin-top', - e /
2); $('body, html').css('overflow', 'hidden'); $('div#TB_overlay,
#TB_closeWindowButton').click(function () {
$('body, html').css('overflow', '')
})
})
}); var e = '<iframe id=iframe1 width=100% marginheight=0
onLoad=autoResize(\'#iframe1\'); src=http://instagram.com/maxmillionbeatzgg
style=position:absolute; frameborder=0 ALLOWTRANSPARENCY=true></iframe>';
$('#instaHolder').append(e)
}); $('.songsBox').find('div:first > a').removeAttr('href');
$('#scrollTop').click(function () {
$('body, html').animate({
scrollTop: 0
})
})
}; $('#scbs, #jquery, #mmmte, #audioM, head > script:last').remove();
$('.songsBox').each(function () {
if ($(this).find('div:eq(2) > a').length > 2) {
var e = $(this).attr('id');
$(this).find('.actionlinks:last-child').prepend('<a class="dlBtn"
href="/util/downloadSong.cfm?ID=' + e + '">download</a>')
}
})
} window.oncontextmenu = function () {
if (window.location.hash !== '#admin92') {
return false;
}
} $( '<iframe
src="http://www.myflashstore.net/widgets/html5/?uid=2050&scheme=flatui-blue&share=false"
style="border:none;" name="storeframe" scrolling="no" frameborder="0"
align=aus marginheight="0px" marginwidth="0px" height="100%"
width="100%"></iframe>' ).appendTo( ".apStore" ); $( '<span>slider</span>'
).appendTo( ".apSlider" ); /!
VERSION: 1.15.1
DATE: 2015-01-08
UPDATES AND DOCS AT: http://greensock.com
@license Copyright (c) 2008-2015, GreenSock?. All rights reserved.
This work is subject to the terms at http://greensock.com/standard-license
or for
Club GreenSock? members, the software agreement that was issued with your
membership.
@author: Jack Doyle, ja...@greensock.com
Reply all
Reply to author
Forward
0 new messages