Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Gson Formatting

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Gson Formatting

    I am using Gson for the settings file of my project. Currently, i'm using the gson.toJson(settings) method. Is there any way to get this to return as a string-array or something so that I can write it multi line? I would like to make it more readable by the end user.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Gson Formatting

    If you can show the java code involved, we could help you.

  3. #3
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Gson Formatting

    ...
    public void saveSettings(){
       Gson gson = new Gson();
       BufferedWriter writer = null;
    		try {
    			writer = new BufferedWriter(new FileWriter(configFile));
    			if(configFile.exists()){
    				configFile.delete();
    			}
    			writer.write(gson.toJson(settings/*is a class*/));
    			writer.close();
    		}catch(Exception e){
     
    		}
    }
    ...
    which produces JSon but all in one line. Is there a way around this? Or would I have to make a custom writer/parser?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Gson Formatting

    gson.toJson(settings/*is a class*/));
    Are you asking how to change the contents of what is returned by the toJson() method?
    If you know the format of the String(I assume its a String) you should be able to reformat it the way you want.

  5. #5
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question Re: Gson Formatting

    Sorry to bump, but I've recently started up on the project again. I ended up starting over as the gui didn't scale -_- (forgot about that)

    My new save method
    ...
    protected void saveSettings(){
    		try{
    			File oldFile = new File(settingsFile.toString() + ".old");
    			if(oldFile.exists()){
    				oldFile.delete();
    			}
    			settingsFile.renameTo(oldFile);
    			Gson constructor = new Gson();
    			BufferedWriter out2 = new BufferedWriter(new FileWriter(settingsFile));
    		    out2.write(constructor.toJson(settings));
    		    out2.close();
    		}catch(Exception ex){
    			cout.err("Unable to write settings file!");
    			ex.printStackTrace();
    		}
    	}
    ...

    Would churn out this atm:
    Quote Originally Posted by settings.mcnn
    {"REPOSITORIES":{"Test Repository":"http://dl.dropbox.com/u/1650058/test.repo","Test 2":"http://dl.dropbox.com/u/1650058/test.repo"},"checkForUpdates":false,"warnBadLinks" :true,"autoRefresh":false,"refreshRate":182601,"MC NN_VERSION":"0.8-beta","SETTINGS_FILE_VERSION":"3","REPO_FILE_VERSI ON":"2"}
    I'm trying to make it more user friendly for a user to manually edit it if needed. Is there anything built into Gson (or realitvely easy to add) to make it more along the lines of:
    {
       "REPOSITORIES":
          {
            "Test Repository":"http://dl.dropbox.com/u/1650058/test.repo",
            "Test 2":"http://dl.dropbox.com/u/1650058/test.repo"
          },
       "checkForUpdates":false,
       "warnBadLinks":true,
       "autoRefresh":false,
       "refreshRate":182601,
       "MCNN_VERSION":"0.8-beta",
       "SETTINGS_FILE_VERSION":"3",
       "REPO_FILE_VERSION":"2"
    }

    I'm thinking about manually adding System.getProperty("line.separator") after each "," but that wouldn't work for every case, and it wouldn't handle indentation. It's not critical to my program (it functions fine now), but again, trying to add polish to it.

Similar Threads

  1. Help with formatting spacing on a calender grid please!
    By bulx0001 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 27th, 2011, 12:53 AM
  2. HSSF cell date formatting in java
    By masepogurajesh in forum Java SE APIs
    Replies: 1
    Last Post: March 15th, 2011, 08:45 AM
  3. Formatting output of a Double
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:20 PM
  4. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM