JSON file formatting

Hi guys!

How to format JSON File to make it more human readable?
Here is my output file: http://pastebin.com/Ee4t9ZGU
Here is how it looks in the SDK:

I use this lib to make the json file: https://code.google.com/p/json-simple/

Or possibly you can recommend another JSON lib which can make it more human readable?

My code to save it to JSON:

[java]
private void saveSweFile(String pathToSave) {
JSONObject saveSWEJson = new JSONObject();

    saveSettings(pathToSave, saveSWEJson);

    //save scenes
    JSONObject allScenes = new JSONObject();

    for (String sceneName : scenesList.keySet()) {
        JSONObject sceneToSave = new JSONObject();

        // save scene states
        Node sceneNode = scenesList.get(sceneName).getSceneNode();
        Object isActScene = sceneNode.getUserData("isActive");
        sceneToSave.put("isActive", (Boolean) isActScene);
        Object isEnScene = sceneNode.getUserData("isEnabled");
        sceneToSave.put("isEnabled", (Boolean) isEnScene);


        //save LayerGroups
        JSONObject allLayersGroups = new JSONObject();
        for (EditorLayersGroup layersGroup : scenesList.get(sceneName).getLayerGroupsList().values()) {
            JSONObject layersGroupToSave = new JSONObject();

            // save layerGroup states
            Node layersGroupNode = layersGroup.getLayersGroupNode();
            Object isActLayersGroup = layersGroupNode.getUserData("isActive");
            layersGroupToSave.put("isActive", (Boolean) isActLayersGroup);
            Object isEnLayersGroup = layersGroupNode.getUserData("isEnabled");
            layersGroupToSave.put("isEnabled", (Boolean) isEnLayersGroup);

            //save layers
            JSONObject allLayers = new JSONObject();
            for (Node layerNode : layersGroup.getLayers()) {
                JSONObject layerToSave = new JSONObject();

                // save layer states
                Object isActLayer = layerNode.getUserData("isActive");
                layerToSave.put("isActive", (Boolean) isActLayer);
                Object isEnLayer = layerNode.getUserData("isEnabled");
                layerToSave.put("isEnabled", (Boolean) isEnLayer);
                Object isLockedLayer = layerNode.getUserData("isLocked");
                layerToSave.put("isLocked", (Boolean) isLockedLayer);

                // save ID entities
                JSONObject entitiesToSave = new JSONObject();
                for (Spatial sp : layerNode.getChildren()) {
                    JSONObject entityJSON = new JSONObject();

                    Object idObj = sp.getUserData("EntityID");
                    long idLong = (Long) idObj;

                    // save name
                    EntityNameComponent nameComp = (EntityNameComponent) base.getEntityManager().getComponent(idLong, EntityNameComponent.class);
                    entityJSON.put("IDName", nameComp.getName());

// EntityModelPathComponent pathComp = (EntityModelPathComponent) base.getEntityManager().getComponent(idLong, EntityModelPathComponent.class);
entityJSON.put(“IDModel”, nameComp.getName().substring(0, nameComp.getName().indexOf("_IDX")));

                    // save transforms
                    Transform trID = sp.getWorldTransform();

// trID.getRotation().inverseLocal();
JSONObject transformToSave = new JSONObject();
transformToSave.put(“translationX”, String.valueOf(trID.getTranslation().getX()));
transformToSave.put(“translationY”, String.valueOf(trID.getTranslation().getY()));
transformToSave.put(“translationZ”, String.valueOf(trID.getTranslation().getZ()));
transformToSave.put(“rotationX”, String.valueOf(trID.getRotation().getX()));
transformToSave.put(“rotationY”, String.valueOf(trID.getRotation().getY()));
transformToSave.put(“rotationZ”, String.valueOf(trID.getRotation().getZ()));
transformToSave.put(“rotationW”, String.valueOf(trID.getRotation().getW()));
transformToSave.put(“scaleX”, String.valueOf(trID.getScale().getX()));
transformToSave.put(“scaleY”, String.valueOf(trID.getScale().getY()));
transformToSave.put(“scaleZ”, String.valueOf(trID.getScale().getZ()));
entityJSON.put(“IDTransform”, transformToSave);

                    // seve data components of entity
                    ConcurrentHashMap<String, String> entityData = base.getDataManager().getEntityData(idLong);
                    JSONObject dataComponentsToSave = new JSONObject();
                    for (String strKey : entityData.keySet()) {
                        dataComponentsToSave.put(strKey, entityData.get(strKey));
                    }
                    entityJSON.put("IDDataComponents", dataComponentsToSave);

                    entitiesToSave.put(String.valueOf(idLong), entityJSON);
                }

                layerToSave.put("Entities", entitiesToSave);
                allLayers.put(layerNode.getName(), layerToSave);
            }
            layersGroupToSave.put("Layers", allLayers);
            allLayersGroups.put(layersGroup.getLayersGroupName(), layersGroupToSave);
        }
        sceneToSave.put("LayersGroups", allLayersGroups);
        allScenes.put(sceneName, sceneToSave);
    }

    saveSWEJson.put("Scenes", allScenes);
    saveJsonFile(pathToSave + ".swe", saveSWEJson);
    System.out.println("File saved: " + pathToSave + ".swe");
}

protected void saveJsonFile(String pathToSave, JSONObject saveJson) {
    try {
        File saveFile = new File(pathToSave);
        saveFile.setReadable(true);
        saveFile.setWritable(true);

        FileWriter fileToSave = new FileWriter(saveFile);
        fileToSave.write(saveJson.toJSONString());
        fileToSave.flush();
        fileToSave.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

[/java]

Thank you.

I’m using GSON and it has an option to enable pretty printing
[java]
GsonBuilder builder = new GsonBuilder().setPrettyPrinting();
[/java]

Also, my lightweight JSON library pretty prints by default. Source is here:
https://sourceforge.net/p/meta-jb/svn/4368/tree/trunk/dev/ext/json/

Jar (and javadocs, source tarball, etc.) are here:
https://sourceforge.net/p/meta-jb/svn/4368/tree/trunk/dev/m2-repo/org/meta-jb/meta-jb-json/1.0.1/

Also, it works with plain java.util.Map and java.util.List and doesn’t require any special JSONObjects or anything. Consequently, it’s the tightest implementation I’ve found. (of course I wrote it so I would say that… but I originally wrote it for a reason after all)

1 Like

Thanks guys!