[SOLVED] Convert line breaks to \n and export it to json file without losing the \n



  • Hi everyone :)

    I have what seem to be a simple problem, but I can't figure how to do it.
    In my game, the user can write a text in a good old Flash TextField. I used that because you can press enter or quote keys and it works (also copy/paste and mouse selection). Therefore, users can type a story with some line breaks in it.

    At some point, I export that story from the game to a JSON file. That's where I have my problem. Line breaks are displayed as is, and the json file can't be loaded back into the game afterwards. So I have to replace each and every line breaks in the story by \n (new line).

    example:

    {"story":"once upon a time
    there was a princess"}
    

    Should be

    {"story":"once upon a time\nthere was a princess"}
    

    So I wrote a little convertor to do that:

    private var txtStory:TextField;
    private function formatStory():String
    {
    	var str:String = "";
    	for (i in 0 ... txtStory.numLines)
    	{
    		str += txtStory.getLineText(i);
    		if (i < (txtStory.numLines - 1)) str += "\n";
    	}
    	return str;
    }
    

    The problem is that the string seems to automatically replaces the \n by a visual line break, so I go back to step one...
    How can I keep my \n and export it to the JSON file?



  • After some research before sleeping, it appears that it should work if I escape the \ instead of the n character.
    So replacing the line break by \\n instead of \n should prevent the string from replacing \n by ASCII line break, and it should leave \n as two literal characters.

    That should also work with quote, using \\"
    I'll try tomorow.

    EDIT:

    And it's working!
    I had to cut the last char of the string because TextField.getLineText() keeps the invisible line break character.

    public static function escape(text:String):String
    {
    	var textField:TextField = new TextField();
    	textField.text = text;
    	var string:String = "";
    
    	for (i in 0 ... textField.numLines)
    	{
    		var line:String = textField.getLineText(i);
    		line = line.substring(0, line.length - 1);
    		if (i < (textField.numLines - 1)) line += "\\n";
    		string += line;
    	}
    	return string;
    }
    

    I'm sure that can be optimized...

    EDIT2:

    Adding string = StringTools.quoteWinArg(string, false); before return seems to escape the other letters correctly.
    It also adds quote at the start and end of the string. All is ready for JSON export!



  • Escaping string is a mess...
    Here's my final(?) function with some last minute surprises in it:

    /*
    ** Prepare a String for JSON export
    ** ex:
    **	Once upon
    **	a "time"
    ** Becomes:
    **	"Once upon\na \"time\""
    */
    public static function escape(text:String):String
    {
    	var textField:TextField = new TextField();
    	textField.text = text;
    	var string:String = "";
    	if (textField.numLines > 1)
    	{
    		for (i in 0 ... textField.numLines)
    		{
    			var line:String = textField.getLineText(i);
    			if (i < (textField.numLines - 1))
    			{
    				line = line.substring(0, line.length - 1);
    				line += "\\n";
    			}
    			string += line;
    		}
    	}
    	else
    	{
    		string = text;
    	}
    	string = StringTools.quoteWinArg(string, false);
    	if (string.substring(0, 1) != "\"") string = "\"" + string + "\"";
    	return string;
    }
    

    As you can see, I've taken account of TextField that have only one line.
    And sometimes quoteWinArg don't add quotes at the start and end of string. So I'm adding them manually. Once again, WHY?

    Anyway, it works. I move on something else.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.