Wednesday 10 November 2010

JadaSite internationalization within JSON problem

(JadaSite 2.03)

While opening Items for editing, I got error: Unexcepted Error: unable to show selected category.
I have Categories that include some Serbian UTF-8 characters.

The problem is that length of JSON data response is calculated as length of JSON string, and the same string is then transfered via HTTP as bytes. This results in missing characters at the end of JSON output, and therefore the unrecognized input on edit page.

For fix: JSON is heavily used in stated way within the code, and this problem can be solved only by large refactoring. The quick fix would be to change JSONEscapeObject class - method toHtmlString. Check the difference in length between string and byte array, and add the difference as spaces at the end of string afterwards:



// if this is less or equal to 0 it is ok; else, add this much spaces at the end
// so the length of string would represent the size of byte array from the string itself
// (without trailing spaces that would be added here)
public String toHtmlString() {
    String output = toString();
    output = output.replaceAll("'", "\\\\'");
    //---additional code
    int additionalByteSymbolsCount = output.getBytes().length - output.length();
    for (int i = 0; i < additionalByteSymbolsCount; i++) {
        output += ' ';
    }
    //---
    return output;
}

This also may (and will) fix some other problems (i.e. I could not select some categories from the list for the item because category names contained Serbian letters).

No comments:

Popular Posts