Changeset 4788

Show
Ignore:
Timestamp:
06/20/08 04:49:59 (2 months ago)
Author:
chrisz
Message:

Encode the output using json.encoding (utf-8 by default).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/TurboJson/branches/1.1/turbojson/jsonsupport.py

    r4728 r4788  
    66class JsonSupport(object): 
    77 
     8    encoding = 'utf-8' 
     9 
    810    def __init__(self, extra_vars_func=None, options=None): 
    911        opts = {} 
     
    1214                continue 
    1315            opt = option[5:] 
    14             if opt == 'assume_encoding': 
    15                 opt = 'encoding' 
    16             opts[opt] = options[option] 
     16            if opt == 'encoding': 
     17                self.encoding = options[option] or self.encoding 
     18            else: 
     19                if opt == 'assume_encoding': 
     20                    opt = 'encoding' 
     21                opts[opt] = options[option] 
    1722        self.encoder = jsonify.GenericJSON(**opts) 
    1823 
     
    3136        """ 
    3237        [info.pop(item) for item in info.copy() if (item.startswith("tg_") and item != "tg_flash")] 
    33         return self.encoder.encode(info) 
     38        output = self.encoder.encode(info) 
     39        if isinstance(output, unicode): 
     40            output = output.encode(self.encoding) 
     41        return output 
    3442 
    3543    def get_content_type(self, **kw): 
  • projects/TurboJson/trunk/turbojson/jsonsupport.py

    r4729 r4788  
    66class JsonSupport(object): 
    77 
     8    encoding = 'utf-8' 
     9 
    810    def __init__(self, extra_vars_func=None, options=None): 
    911        opts = {} 
     
    1214                continue 
    1315            opt = option[5:] 
    14             if opt == 'assume_encoding': 
    15                 opt = 'encoding' 
    16             opts[opt] = options[option] 
     16            if opt == 'encoding': 
     17                self.encoding = options[option] or self.encoding 
     18            else: 
     19                if opt == 'assume_encoding': 
     20                    opt = 'encoding' 
     21                opts[opt] = options[option] 
    1722        self.encoder = jsonify.GenericJSON(**opts) 
    1823 
     
    3338            if item.startswith("tg_") and item != "tg_flash": 
    3439                del info[item] 
    35         return self.encoder.encode(info) 
     40        output = self.encoder.encode(info) 
     41        if isinstance(output, unicode): 
     42            output = output.encode(self.encoding) 
     43        return output