GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stack/json_utils.py at master · samlet/stack

Expand file tree

/

json_utils.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
44 lines (36 loc) · 1.47 KB
/

json_utils.py

Copy path

File metadata and controls

44 lines (36 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.