I'm running into an issue while trying to add text to templates, via passed parameters, that are then added to a template canvas, at runtime. The code works great until it encounters certain special characters, such as the degree symbol, at which point it starts throwing WARN Vision.TemplateCanvas - Unable to parse parameters as JSON on row X
(where X is the row #). MalformedJsonException: Invalid escape sequence at line 1 column 35
.
The code I've been working with to add a line to a dataset that tells the template canvas how to create the cell is below:
rowData = {"rid":5,"description":"45° angle"}
def buildDescCell(this):
tid = "rid" + str(this.rowData["rid"]) + "-" + str(1) # A unique name for the template being added to the canvas
template = "TATemplate" # The name of the template being used; this one's a TextArea, but same happens with basic Labels
constraints = "grow,cell 1 1 1 1,w 50px" #This line is simplified for this example...
inputStr = this.rowData["description"].replace('°', '') #Works until the .replace() is removed/commented out
params = {"tempID": tid ,"text": inputStr} #Parameters to be passed to the template being added to the TemplateCanvas
cList = [tid,template,constraints,0,0,None,None,params] #The final construction of the TemplateCanvas data for a single cell to be added
return cList #Returned data is added to tableData = [] via tableData.append(data)
Now the above code works as it is, but as soon as you remove/comment out the .replace('°', '')
call on 'inputStr' is when the MalformedJsonException errors start getting thrown by the TemplateCanvas object.
To put the data into the TemplateCanvas, I convert it to a usable dataset using:
dsHeader = ["name","template","layout","x","y","width","height","parameters"]
def getTableDataSet(this):
data = system.dataset.toDataSet(this.dsHeader, this.tableData)
print data.getValueAt(66,7) # The line in question; Returns: {'tempID': 'rid5-1', 'text': '45\xc2\xb0 Angle'}
return data # Inside of a button `TemplateCanvas.templates = <this returned dataSet>` applies the dataset to the template canvas in order to display the cells.
What am I doing wrong that's causing this to throw the exception? I'm guessing I need to encode the symbol differently, but I'm not sure what is acceptable to get it to stop throwing json exceptions but still display the ° symbol.
I've tried:
import json
...
json.dumps(inputStr)
...
which gets rid of the error, but then I get the string: "45\u00b0 Angle"
showing up in my TextArea/Label, "" included. I've also tried unicode(inputStr,"utf-8")
, but that breaks even more text in other cells. I've run out of ideas to try...
I've seen this thread: Degree Symbol not supported in system.util.jsonDecode? But still can't seem to get anything working. I feel like I'm missing something right in front of my face, but am too frustrated to see it...