Last active
August 29, 2015 14:07
-
-
Save hello-josh/275f6ddefe8295ab72e5 to your computer and use it in GitHub Desktop.
Set jsonpickle to encode html tags to be html safe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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()} | |
>>> jsonpickle.encode(d, unpicklable=False) | |
'{"date": "2014-10-02 13:44:11.481521", "html": "\\u003cscript src=\\"http://www.google.com/auth.js\\"\\u003e\\u003c/script\\u003e"}' | |
# new shell to undo set_encoder_options and test | |
>>> import jsonpickle | |
>>> import simplejson | |
>>> import json | |
>>> d = '{"date": "2014-10-02 13:44:11.481521", "html": "\\u003cscript src=\\"http://www.google.com/auth.js\\"\\u003e\\u003c/script\\u003e"}' | |
>>> jsonpickle.decode(d) | |
{u'date': u'2014-10-02 13:44:11.481521', u'html': u'<script src="http://www.google.com/auth.js"></script>'} | |
>>> json.loads(d) | |
{u'date': u'2014-10-02 13:44:11.481521', u'html': u'<script src="http://www.google.com/auth.js"></script>'} | |
>>> simplejson.loads(d) | |
{u'date': u'2014-10-02 13:44:11.481521', u'html': u'<script src="http://www.google.com/auth.js"></script>'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment