JSON-java icon indicating copy to clipboard operation
JSON-java copied to clipboard

JSONObject.optString()/getString() removes all spaces from actual value

Open vlOd2 opened this issue 2 years ago • 2 comments

JSONObject.getString() removes all spaces from actual value I provided a sample of my code that retrieves the values using optString() (getString() had the same result)

How my config system works: Pretty much the values are stored/loaded from a class with static values (this makes it easier since I also have a plugin system, and also so I can provide default values)

I have no clue why it doesn't work and trims down the spaces For example: Actual Value: &cInvalid command! See &b/help&c for help Retreived value: &cInvalidcommand!See&b/help&cforhelp

public Configuration serverConfig;

public void start() {
        ....
	File serverConfigFile = new File("server.json");
	if (!serverConfigFile.exists()) serverConfigFile.createNewFile();
	loadConfigs();
        ....
}
			
public void loadConfigs() {
	....
	if (this.serverConfig.cfgJsonObject.has("chatInvalidCommandMsg"))
		ServerConfig.chatInvalidCommandMsg = this.serverConfig.cfgJsonObject.optString("chatInvalidCommandMsg");
	if (this.serverConfig.cfgJsonObject.has("serverFullKickMsg"))
		ServerConfig.serverFullKickMsg = this.serverConfig.cfgJsonObject.optString("serverFullKickMsg");
	if (this.serverConfig.cfgJsonObject.has("serverMaxConnectionsKickMsg"))
		ServerConfig.serverMaxConnectionsKickMsg = this.serverConfig.cfgJsonObject.optString("serverMaxConnectionsKickMsg");
	if (this.serverConfig.cfgJsonObject.has("serverShutdownKickMsg"))
		ServerConfig.serverShutdownKickMsg = this.serverConfig.cfgJsonObject.optString("serverShutdownKickMsg");
	....
	this.serverConfig.cfgJsonObject.put("chatInvalidCommandMsg", 
		ServerConfig.chatInvalidCommandMsg);
	this.serverConfig.cfgJsonObject.put("serverFullKickMsg", 
		ServerConfig.serverFullKickMsg);
	this.serverConfig.cfgJsonObject.put("serverMaxConnectionsKickMsg", 
		ServerConfig.serverMaxConnectionsKickMsg);
	this.serverConfig.cfgJsonObject.put("serverShutdownKickMsg", 
		ServerConfig.serverShutdownKickMsg);
	this.serverConfig.save();
	....
}

and here is the "Configuration" class

public class Configuration {
	public JSONObject cfgJsonObject = new JSONObject();
	private File cfgFile;
	
	public Configuration(File file) {
		this.cfgFile = file;
	}

	public boolean load() {
		try {
			Scanner scanner = new Scanner(new FileReader(this.cfgFile));
			StringBuilder stringBuilder = new StringBuilder();
			
			while (scanner.hasNext()) stringBuilder.append(scanner.next());
			scanner.close();
			
			String fileContent = stringBuilder.toString();
			if (fileContent.isEmpty()) fileContent = "{}";
			this.cfgJsonObject = new JSONObject(fileContent);
			
			return true;
		} catch (Exception ex) {
			Logging.logSevere("Unable to load a configuration file!");
			ex.printStackTrace();
			return false;
		}
	} 
	
	public void save() {
		try {
			String newFileContent = this.cfgJsonObject.toString(4);
			FileWriter fileWriter = new FileWriter(this.cfgFile);
			fileWriter.write(newFileContent);
			fileWriter.close();
		} catch (Exception ex) {
			Logging.logSevere("Unable to save a configuration file!");
			ex.printStackTrace();
		}
	}
	
	public void reload() {
		load();
		save();
	}
}

and here is the "ServerConfig" class

public class ServerConfig {
	....
	public static String chatInvalidCommandMsg = "&cInvalid command! See &b/help&c for help";
	public static String serverFullKickMsg = "Server full!";
	public static String serverMaxConnectionsKickMsg = "Connection limit exceeded!";
	public static String serverShutdownKickMsg = "Server shutting down!";
}

vlOd2 avatar Sep 20 '22 18:09 vlOd2

@Test
public void test() {
    String key = "chatInvalidCommandMsg";
    String value = "&cInvalid command! See &b/help&c for help";
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(key,value);
    String resultOpt = jsonObject.optString(key);
    String resultGet = jsonObject.getString(key);
    assertEquals(resultOpt, value);
    assertEquals(resultGet, value);

    System.out.println("ResultOpt " + resultOpt);
    System.out.println("ResultGet " + resultGet);
}

}

Output:
ResultOpt &cInvalid command! See &b/help&c for help ResultGet &cInvalid command! See &b/help&c for help

What are you doing differently?

stleary avatar Sep 25 '22 17:09 stleary

I think I know what it is, its the way I read the file?

On Sun, Sep 25, 2022, 20:55 Sean Leary @.***> wrote:

@Test public void test() { String key = "chatInvalidCommandMsg"; String value = "&cInvalid command! See &b/help&c for help"; JSONObject jsonObject = new JSONObject(); jsonObject.put(key,value); String resultOpt = jsonObject.optString(key); String resultGet = jsonObject.getString(key); assertEquals(resultOpt, value); assertEquals(resultGet, value);

System.out.println("ResultOpt" + resultOpt);
System.out.println("ResultGet" + resultGet);

}

}

Output: ResultOpt&cInvalid command! See &b/help&c for help ResultGet&cInvalid command! See &b/help&c for help

What are you doing differently?

— Reply to this email directly, view it on GitHub https://github.com/stleary/JSON-java/issues/689#issuecomment-1257245038, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP56BRCWDFTYCD2R5F4BFADWACGZ5ANCNFSM6AAAAAAQRKZ2VE . You are receiving this because you authored the thread.Message ID: @.***>

vlOd2 avatar Sep 29 '22 15:09 vlOd2

Closed due to lack of activity.

stleary avatar Nov 18 '22 01:11 stleary