Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #79 (closed defect: fixed)

Opened 14 years ago

Last modified 12 years ago

JSON should use the same encoding as Kid

Reported by: kevin Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TurboGears Version:
Severity: normal Keywords:
Cc:

Description

Unicode values should be encoding using utf8 by default, but should use the same encoding as the Kid rendering.

Change History

comment:1 Changed 14 years ago by kevin

  • Milestone set to 0.9

comment:2 Changed 14 years ago by tom@…

I've seen inconsistent parsing of JS charsets by browsers - some respect the content-type header, some use the charset of the requesting page. I'd suggest you encode all non-ascii characters using \xXXXX notation and send pure ascii.

comment:3 Changed 14 years ago by daf

JSON needs to be patched for this, I have found no updates to let JSON-py be aware of encodings.

I have made a little patch involving 2 files:

look for view.py into the kid package and modify it after updating with patch for ticket #58

def render(info, format="html", fragment=False, template=None):
    """Renders data in the desired format.

    @param info: the data itself
    @type info: dict
    @param format: "html" or "json"
    @type format: string
    @param fragment: passed through to tell the template if only a
                     fragment of a page is desired
    @type fragment: bool
    @param template: name of the template to use
    @type template: string
    """
    if format == "json":
        return json.write(info, encoding=defaultencoding)
    else:

Now get json.py, and find the line defining method write, add an extra kw param encoding

def write(self, obj, escaped_forward_slash=False, encoding="utf-8"):
        self._encoding = encoding
        self._escaped_forward_slash = escaped_forward_slash
        self._results = []

in method _write look for a part like this, you will have to replace a part of elif clause testing types.StringType?.. types.UnicodeType?:

            obj = obj.replace('\r', r'\r')
	    obj = obj.replace('\t', r'\t')
            self._append(obj.decode(self._encoding))
            self._append('"')

Finally update one of the last functions of the file

def write(obj, escaped_forward_slash=False, encoding="utf-8"):
    return JsonWriter().write(obj, escaped_forward_slash, encoding=encoding)

That will do for now..

comment:4 Changed 14 years ago by daf

sorry.. first write method is part of JsonWriter? class, the last one is a function

comment:5 Changed 13 years ago by bob@…

  • Status changed from new to closed
  • Resolution set to fixed

With the switch to simple_json, the encoding is ASCII by default with unicode escapes so this ticket is no longer relevant.

Note: See TracTickets for help on using tickets.