Skip to content

Instantly share code, notes, and snippets.

@hello-josh
hello-josh / encoded.py
Last active August 29, 2015 14:07
Set jsonpickle to encode html tags to be html safe
>>> import jsonpickle
>>> import simplejson.encoder
>>> t = '<script src="http://www.google.com/auth.js"></script>'
>>> jsonpickle.encode(t, unpicklable=True)
'"<script src=\\"http://www.google.com/auth.js\\"></script>"'
>>> jsonpickle.set_encoder_options('simplejson', cls=simplejson.encoder.JSONEncoderForHTML)
>>> jsonpickle.encode(t, unpicklable=True)
'"\\u003cscript src=\\"http://www.google.com/auth.js\\"\\u003e\\u003c/script\\u003e"'
>>> import datetime
>>> d = {'html': t, 'date': datetime.datetime.now()}