7

I use json-simple and want to have pretty-print for debugging purposes.

Here is a very relavant SO question: Pretty-Print JSON in Java

However the answer in the given thread, not only fixes the indentation but also changes the order of the items to [a ... z] using the string order of the keys.

Is there any way to fix the indentation without changing the order of the items in my JSONObject?

Example:

JSONObject myJSon = new JSONObject();
myJSon.put("zzz", 1);
myJSon.put("aaa", 1);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println( gson.toJson(myJSon) );

Output:

{
  "aaa": 1,
  "zzz": 1
}

Desired output:

{
  "zzz": 1,
  "aaa": 1
}

Edit: I'm using: org.json.simple.JSONObject

Community
  • 1
  • 1
Sait
  • 19,045
  • 18
  • 72
  • 99
  • See if this can help you: http://stackoverflow.com/questions/6541757/when-using-spring-mvc-for-rest-how-do-you-enable-jackson-to-pretty-print-render/6541956#6541956 – carl-lopez Jul 22 '13 at 22:19
  • @carl-lopez What does something about Jackson have to do with Gson? – Brian Roach Jul 22 '13 at 22:29
  • Can be replaced with, as you could think – carl-lopez Jul 22 '13 at 22:31
  • To the OP: You can't short of modifying the source for Gson (or writing your own printing code). That's part of "pretty printing". – Brian Roach Jul 22 '13 at 22:33
  • What is the JSONObject class? Is it com.google.gson.JsonObject class? – Michał Ziober Jul 22 '13 at 22:38
  • @MichałZiober, `org.json.simple.JSONObject` – Sait Jul 22 '13 at 22:41
  • 1
    Erm, you're using two completely different JSON parsing libraries; why? The only reason this is even working is because `org.json.simple.JSONObject` extends `HashMap`. (Good eye @MichałZiober - I missed that). – Brian Roach Jul 23 '13 at 00:21
  • possible duplicate of [JSONObject : Why JSONObject changing the order of attributes](http://stackoverflow.com/questions/17229418/jsonobject-why-jsonobject-changing-the-order-of-attributes) – Leo Jan 26 '14 at 13:19

2 Answers2

9

org.json.simple.JSONObject class extends java.util.HashMap and this is the reason why you see this order of the properties on output. setPrettyPrinting() method doesn't change the order. You can remove it from source code and nothing change. If you want to keep the order you can use java.util.LinkedHashMap instead of org.json.simple.JSONObject.

Simple example:

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonProgram {

    public static void main(String[] args) throws Exception {
        Map<String, Integer> myJSon = new LinkedHashMap<String, Integer>();
        myJSon.put("zzz", 1);
        myJSon.put("aaa", 1);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(myJSon));
    }
}
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • I'm having problems to download and edit the source codes of simple-json using Maven. Any further reading? – Sait Jul 22 '13 at 23:16
  • Why do you want to change the library? This is not a solution. Why you do not want to change your source code? Can you show to us your real source code? – Michał Ziober Jul 22 '13 at 23:35
  • My source code has nothing fancy. Only many JJSONArrays and SONObjects inside other JSONObjects. It is why I was considering to change the library. What you mean by saying 'This is not a solution.' I thought changing `HashMap` to `LinkedHashMap` in the original implementation of `simple-json` would solve the issue. Do you mean it is not a best practice or do you mean it is not going work at all? – Sait Jul 22 '13 at 23:59
  • You have to change JSONObjects into LinkedHashMap in your source code. I can't believe that you have so many JSONObjects that easier will be change library than your source code. If you want to change library - OK, it will be working, but is really problematic. How many instances of JSONObjects you have in your source code?Replacing is really easy because LinkedHashMap has "put" method too. So you have to change only the type of object. – Michał Ziober Jul 23 '13 at 06:49
0

No. JSONObject is a HashMap, there is no order maintained of the entries you put into it.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50