GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — stack/json_utils.py at master · samlet/stack Expand file tree/ Copy pathjson_utils.pyMore file actions 44 lines (36 loc) · 1.47 KB / Copy pathjson_utils.pyFile metadata and controls44 lines (36 loc) · 1.47 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 from typing import Text, Any, Dict, List, Union, Optional import os import json import simplejson from io_utils import read_file, write_to_file ## dumped = json_to_string({"ok": 'JSON posted'}) def json_to_string(obj, **kwargs): indent = kwargs.pop("indent", 2) ensure_ascii = kwargs.pop("ensure_ascii", False) return json.dumps(obj, indent=indent, ensure_ascii=ensure_ascii, **kwargs) def read_json_file(filename): """Read json from a file.""" content = read_file(filename) try: return simplejson.loads(content) except ValueError as e: raise ValueError("Failed to read json from '{}'. Error: " "{}".format(os.path.abspath(filename), e)) def write_json_to_file(filename:Text, obj:Any, **kwargs): """Write an object as a json string to a file.""" write_to_file(filename, json_to_string(obj, **kwargs)) def json_object(obj): return json.dumps(obj, default=lambda o: o.__dict__, sort_keys=True, indent=4) # write_json=lambda f,o: write_json_to_file(f,o) class SetEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, set): return list(obj) return json.JSONEncoder.default(self, obj) def write_json(filename, o): import io with io.open(filename, 'w', encoding="utf-8") as f: f.write(json.dumps(o, indent=2, ensure_ascii=False, cls=SetEncoder)) pretty_json=lambda o: json.dumps(o, indent=2, ensure_ascii=False) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |