Skip to content

Commit ca87d6e

Browse files
committed
feat: add support for yaml
1 parent d1e9303 commit ca87d6e

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ You can pass the json in any of the following formats:
7070
| `keep_running`| `bool` | ✔️ | Whether to keep the server running. Defaults to `False`. |
7171
| `run_in_thread`| `bool` | ✔️ | Whether to run the server in a separate thread. Defaults to `False`. |
7272
| `is_csv`| `bool` | ✔️ | Whether the data is csv data. Defaults to `False`. |
73+
| `is_yaml`| `bool` | ✔️ | Whether the data is yaml data. Defaults to `False`. |
7374
| `is_ndjson`| `bool` | ✔️ | Whether the data is Newline Delimited JSON . Defaults to `False`. |
7475
| `is_js_object`| `bool` | ✔️ | Whether the data is a JavaScript Object. Defaults to `False`. |
7576
| `title`| `str` | ✔️ | A title to display in the browser. |
@@ -91,6 +92,7 @@ You can pass the json in any of the following formats:
9192
| `--out` | File to output when in edit mode |
9293
| `-t` | Title to display in browser window |
9394
| `--csv` | Input is CSV |
95+
| `--yaml` | Input is YAML |
9496
| `--js` | Input is a JavaScript Object |
9597
| `--ndjson`| Input is Newline Delimited JSON |
9698

jsoneditor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .jsoneditor import editjson, main
22

3-
__version__ = "1.5.1"
3+
__version__ = "1.6.0"

jsoneditor/jsoneditor.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import csv
44
import json
55
import mimetypes
6+
import yaml
67
import os
78
import random
89
import subprocess
@@ -52,6 +53,7 @@ def __init__(
5253
keep_running: bool = False,
5354
run_in_thread: bool = False,
5455
is_csv: bool = False,
56+
is_yaml: bool = False,
5557
is_ndjson: bool = False,
5658
is_js_object: bool = False,
5759
title: str = None,
@@ -64,6 +66,7 @@ def __init__(
6466
self.keep_running = keep_running
6567
self.run_in_thread = run_in_thread
6668
self.is_csv = is_csv
69+
self.is_yaml = is_yaml
6770
self.is_ndjson = is_ndjson
6871
self.is_js_object = is_js_object
6972
self.title = title
@@ -129,6 +132,8 @@ def is_file(source: str) -> bool:
129132
def detect_source_by_filename(self, source: str):
130133
if source.endswith(".csv"):
131134
self.is_csv = True
135+
elif source.endswith(".yaml"):
136+
self.is_yaml = True
132137
elif any(source.endswith(ext) for ext in [".ndjson", ".jsonl"]):
133138
self.is_ndjson = True
134139

@@ -140,6 +145,10 @@ def load_json(self, source):
140145
result = list(csv.DictReader(source.split("\n")))
141146
elif isinstance(source, TextIOWrapper):
142147
result = list(csv.DictReader(source))
148+
elif self.is_yaml:
149+
result = list(filter(bool, yaml.load_all(source, Loader=yaml.UnsafeLoader)))
150+
if len(result) == 1:
151+
result = result[0]
143152
elif self.is_ndjson:
144153
if isinstance(source, str):
145154
lines = source.splitlines()
@@ -257,6 +266,7 @@ def editjson(
257266
keep_running: bool = False,
258267
run_in_thread: bool = False,
259268
is_csv: bool = False,
269+
is_yaml: bool = False,
260270
is_ndjson: bool = False,
261271
is_js_object: bool = False,
262272
title: str = None,
@@ -273,6 +283,7 @@ def editjson(
273283
keep_running,
274284
run_in_thread,
275285
is_csv,
286+
is_yaml,
276287
is_ndjson,
277288
is_js_object,
278289
title,
@@ -332,6 +343,7 @@ def main() -> None:
332343
parser.add_argument("--out", help="File to output when in edit mode")
333344
parser.add_argument("-t", help="Title to display in browser window")
334345
parser.add_argument("--csv", help="Input is CSV", action="store_true")
346+
parser.add_argument("--yaml", help="Input is YAML", action="store_true")
335347
parser.add_argument(
336348
"--js", help="Input is a JavaScript Object", action="store_true"
337349
)
@@ -362,6 +374,9 @@ def main() -> None:
362374
if args.csv:
363375
options["is_csv"] = True
364376

377+
if args.yaml:
378+
options["is_yaml"] = True
379+
365380
if args.js:
366381
options["is_js_object"] = True
367382

@@ -376,7 +391,7 @@ def main() -> None:
376391
elif args.c:
377392
options["data"] = pyperclip.paste()
378393
else:
379-
raise ValueError("No data passed")
394+
raise ValueError("No data passed.")
380395

381396
if args.e:
382397

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
requests
22
pyperclip
3+
pyyaml

0 commit comments

Comments
 (0)