| OLD | NEW |
| 1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import functools | 5 import functools |
| 6 import collections | 6 import collections |
| 7 import contextlib | 7 import contextlib |
| 8 import json | 8 import json |
| 9 | 9 |
| 10 from recipe_engine import recipe_api | 10 from recipe_engine import recipe_api |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 super(JsonApi, self).__init__(**kwargs) | 75 super(JsonApi, self).__init__(**kwargs) |
| 76 @functools.wraps(json.dumps) | 76 @functools.wraps(json.dumps) |
| 77 def dumps(*args, **kwargs): | 77 def dumps(*args, **kwargs): |
| 78 kwargs['sort_keys'] = True | 78 kwargs['sort_keys'] = True |
| 79 kwargs.setdefault('default', config_types.json_fixup) | 79 kwargs.setdefault('default', config_types.json_fixup) |
| 80 return json.dumps(*args, **kwargs) | 80 return json.dumps(*args, **kwargs) |
| 81 self.dumps = dumps | 81 self.dumps = dumps |
| 82 | 82 |
| 83 @classmethod | 83 @classmethod |
| 84 def loads(self, data, **kwargs): | 84 def loads(self, data, **kwargs): |
| 85 def strip_unicode(obj): | 85 return recipe_util.strip_unicode(json.loads(data, **kwargs)) |
| 86 if isinstance(obj, unicode): | |
| 87 return obj.encode('utf-8', 'replace') | |
| 88 | |
| 89 if isinstance(obj, list): | |
| 90 return map(strip_unicode, obj) | |
| 91 | |
| 92 if isinstance(obj, dict): | |
| 93 new_obj = type(obj)( | |
| 94 (strip_unicode(k), strip_unicode(v)) for k, v in obj.iteritems() ) | |
| 95 return new_obj | |
| 96 | |
| 97 return obj | |
| 98 | |
| 99 return strip_unicode(json.loads(data, **kwargs)) | |
| 100 | 86 |
| 101 def is_serializable(self, obj): | 87 def is_serializable(self, obj): |
| 102 """Returns True if the object is JSON-serializable.""" | 88 """Returns True if the object is JSON-serializable.""" |
| 103 try: | 89 try: |
| 104 self.dumps(obj) | 90 self.dumps(obj) |
| 105 return True | 91 return True |
| 106 except Exception: | 92 except Exception: |
| 107 return False | 93 return False |
| 108 | 94 |
| 109 @recipe_util.returns_placeholder | 95 @recipe_util.returns_placeholder |
| (...skipping 19 matching lines...) Expand all Loading... |
| 129 """ | 115 """ |
| 130 import shutil | 116 import shutil |
| 131 import sys | 117 import sys |
| 132 shutil.copy(sys.argv[1], sys.argv[2]) | 118 shutil.copy(sys.argv[1], sys.argv[2]) |
| 133 """, | 119 """, |
| 134 args=[path, | 120 args=[path, |
| 135 self.output(add_json_log=add_json_log, name=output_name)], | 121 self.output(add_json_log=add_json_log, name=output_name)], |
| 136 add_python_log=False, | 122 add_python_log=False, |
| 137 **kwargs | 123 **kwargs |
| 138 ) | 124 ) |
| OLD | NEW |