Skip to content

Commit

Permalink
feat: add support for JavaScript object
Browse files Browse the repository at this point in the history
  • Loading branch information
dermasmid committed Feb 10, 2022
1 parent 8ce41ee commit 37dbf04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ You can pass the json in any of the following formats:
| `keep_running`| `bool` | ✔️ | Whether to keep the server running. Defaults to `False`. |
| `run_in_thread`| `bool` | ✔️ | Whether to run the server in a separate thread. Defaults to `False`. |
| `is_csv`| `bool` | ✔️ | Whether the data is csv data. Defaults to `False`. |
| `is_ndjson`| `bool` | ✔️ | Whether the data is Newline Delimited JSON . Defaults to `False`. |
| `is_js_object`| `bool` | ✔️ | Whether the data is a JavaScript Object. Defaults to `False`. |
| `title`| `str` | ✔️ | A title to display in the browser. |
| `port`| `int` | ✔️ | specify which port to use. |

Expand All @@ -78,16 +80,19 @@ You can pass the json in any of the following formats:

| parameter | description |
| --------- | ----------------------------------------------------------------------|
| `data` | The data in any of [these](#formats-you-can-pass-the-json-as) formats.|
| `-o` | Add a button that will output the json back to the console. |
| `-b` | Keep running in background. |
| `-c` | Get JSON input from clipboard. |
| `-k` | Keep alive. |
| `-e` | Edit mode. |
| `-p` | Server port. |
| `--out` | File to output when in edit mode. |
| `-t` | Title to display in browser window. |
| `--csv` | Input is CSV. |
| `data` | The data in any of [these](#formats-you-can-pass-the-json-as) formats |
| `-o` | Add a button that will output the json back to the console |
| `-b` | Keep running in background |
| `-c` | Get JSON input from clipboard |
| `-k` | Keep alive |
| `-e` | Edit mode |
| `-n` | Don't launch browser |
| `-p` | Server port |
| `--out` | File to output when in edit mode |
| `-t` | Title to display in browser window |
| `--csv` | Input is CSV |
| `--js` | Input is a JavaScript Object |
| `--ndjson`| Input is Newline Delimited JSON |


## Build
Expand Down
29 changes: 27 additions & 2 deletions jsoneditor/jsoneditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import mimetypes
import os
import random
import subprocess
import sys
import threading
import webbrowser
from collections.abc import Mapping
from io import TextIOWrapper
from typing import Union, Callable, Any
from typing import Any, Callable, Union
from urllib.parse import urlparse
from wsgiref.simple_server import WSGIRequestHandler, make_server

Expand Down Expand Up @@ -52,6 +53,7 @@ def __init__(
run_in_thread: bool = False,
is_csv: bool = False,
is_ndjson: bool = False,
is_js_object: bool = False,
title: str = None,
port: int = None,
no_browser: bool = False,
Expand All @@ -63,6 +65,7 @@ def __init__(
self.run_in_thread = run_in_thread
self.is_csv = is_csv
self.is_ndjson = is_ndjson
self.is_js_object = is_js_object
self.title = title
self.no_browser = no_browser
self.get_random_port(port)
Expand Down Expand Up @@ -143,6 +146,20 @@ def load_json(self, source):
elif isinstance(source, TextIOWrapper):
lines = source.readlines()
result = list(json.loads(line) for line in lines)
elif self.is_js_object:
try:
result = json.loads(
subprocess.run(
["node", "-e", f"console.log(JSON.stringify({source}))"],
capture_output=True,
check=True,
).stdout.decode()
)
except FileNotFoundError:
print(
"You need to have Nodejs installed to be able to use JavaScript Objects."
)
sys.exit(1)
else:
if isinstance(source, str):
try:
Expand Down Expand Up @@ -241,6 +258,7 @@ def editjson(
run_in_thread: bool = False,
is_csv: bool = False,
is_ndjson: bool = False,
is_js_object: bool = False,
title: str = None,
port: int = None,
no_browser: bool = False,
Expand All @@ -256,6 +274,7 @@ def editjson(
run_in_thread,
is_csv,
is_ndjson,
is_js_object,
title,
port,
no_browser,
Expand Down Expand Up @@ -301,7 +320,7 @@ def main() -> None:
)
parser.add_argument(
"-o",
help="Add a button that will output the json back to the console",
help="Add a button that will output the JSON back to the console",
action="store_true",
)
parser.add_argument("-b", help="Keep running in background", action="store_true")
Expand All @@ -313,6 +332,9 @@ def main() -> None:
parser.add_argument("--out", help="File to output when in edit mode")
parser.add_argument("-t", help="Title to display in browser window")
parser.add_argument("--csv", help="Input is CSV", action="store_true")
parser.add_argument(
"--js", help="Input is a JavaScript Object", action="store_true"
)
parser.add_argument(
"--ndjson", help="Input is Newline Delimited JSON", action="store_true"
)
Expand Down Expand Up @@ -340,6 +362,9 @@ def main() -> None:
if args.csv:
options["is_csv"] = True

if args.js:
options["is_js_object"] = True

if args.ndjson:
options["is_ndjson"] = True

Expand Down

0 comments on commit 37dbf04

Please sign in to comment.