Skip to content

Instantly share code, notes, and snippets.

@hellt
Last active June 30, 2019 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellt/d44b58c2f0c8b5f1be46047c9916aa82 to your computer and use it in GitHub Desktop.
Save hellt/d44b58c2f0c8b5f1be46047c9916aa82 to your computer and use it in GitHub Desktop.
pycatj-web-main
import os
import sys
import json
import io
here = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(here, "vendored"))
# now it is allowed to add a non-std package
from pycatj import pycatj
def pycatjify(request):
# default data value
data = json.loads(
'{"data":"test_value","somenumber":123,"a_dict":{"asd":"123","qwe":[1,2,3],"nested_dict":{"das":31,"qwe":"asd"}}}'
)
# default root value
root = "my_dict"
# if request object exists and the keys `data` and `root` are inside of it
# rewrite the default values for `data` and `root`
print("Incoming request body: ", request.get_data())
rj = request.get_json()
if rj:
data = rj
if "pycatj_data" in rj:
print("pycatj_data key is found in the request body")
data = rj["pycatj_data"]
if "root" in rj:
print("root key is found in the request body")
root = rj["root"]
result = io.StringIO()
pycatj.process_dict(data, root, result)
return json.dumps({"data": result.getvalue()})
if __name__ == "__main__":
print(pycatjify(None))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment