From 2740fac9272f1911678adf2cfe8f69fb88f645ba Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 22 Sep 2024 15:36:02 -0400 Subject: [PATCH 1/5] Add examples to README --- crates/jiter-python/README.md | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/crates/jiter-python/README.md b/crates/jiter-python/README.md index af2ba3c..7f76b46 100644 --- a/crates/jiter-python/README.md +++ b/crates/jiter-python/README.md @@ -55,3 +55,57 @@ def cache_usage() -> int: Size of the string cache in bytes. """ ``` +## Examples + +The main function provided by jiter is `from_json()`, which accepts a bytes object containing JSON and returns a Python dictionary, list or other value. + +```python +import jiter + +json_data = b'{"name": "John", "age": 30}' +parsed_data = jiter.from_json(json_data) +print(parsed_data) # Output: {'name': 'John', 'age': 30} +``` +### Handling Partial JSON + +Incomplete JSON objects can be parsed using the `partial_mode=` parameter. + +```python +import jiter + +partial_json = b'{"name": "John", "age": 30, "city": "New Yor' + +# Raise error on incomplete JSON +try: + jiter.from_json(partial_json, partial_mode='off') +except ValueError as e: + print(f"Error: {e}") + +# Parse incomplete JSON, discarding incomplete last field +result = jiter.from_json(partial_json, partial_mode='on') +print(result) # Output: {'name': 'John', 'age': 30} + +# Parse incomplete JSON, including incomplete last field +result = jiter.from_json(partial_json, partial_mode='trailing-strings') +print(result) # Output: {'name': 'John', 'age': 30, 'city': 'New Yor'} +``` + +### Catching Duplicate Keys + +The `catch_duplicate_keys=True` option can be used to raise a `ValueError` if an object contains duplicate keys. + +```python +import jiter + +json_with_dupes = b'{"foo": 1, "foo": 2}' + +# Default behavior (last value wins) +result = jiter.from_json(json_with_dupes) +print(result) # Output: {'foo': 2} + +# Catch duplicate keys +try: + jiter.from_json(json_with_dupes, catch_duplicate_keys=True) +except ValueError as e: + print(f"Error: {e}") +``` From 44f26c1a8c1e1a1dcb2358e2e7c7fc6dfeb60fc4 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 22 Sep 2024 15:37:31 -0400 Subject: [PATCH 2/5] Jiter not jiter Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> --- crates/jiter-python/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/jiter-python/README.md b/crates/jiter-python/README.md index 7f76b46..b038f17 100644 --- a/crates/jiter-python/README.md +++ b/crates/jiter-python/README.md @@ -57,7 +57,7 @@ def cache_usage() -> int: ``` ## Examples -The main function provided by jiter is `from_json()`, which accepts a bytes object containing JSON and returns a Python dictionary, list or other value. +The main function provided by Jiter is `from_json()`, which accepts a bytes object containing JSON and returns a Python dictionary, list or other value. ```python import jiter From 0387228a4f706844f87346e18038a9c7310ac180 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 22 Sep 2024 21:05:46 -0400 Subject: [PATCH 3/5] Update crates/jiter-python/README.md Co-authored-by: Samuel Colvin --- crates/jiter-python/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/jiter-python/README.md b/crates/jiter-python/README.md index b038f17..a9c10aa 100644 --- a/crates/jiter-python/README.md +++ b/crates/jiter-python/README.md @@ -66,6 +66,7 @@ json_data = b'{"name": "John", "age": 30}' parsed_data = jiter.from_json(json_data) print(parsed_data) # Output: {'name': 'John', 'age': 30} ``` + ### Handling Partial JSON Incomplete JSON objects can be parsed using the `partial_mode=` parameter. From 0c3e1268a0ca93524dfd46f98c1dad99239221c6 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 22 Sep 2024 21:05:51 -0400 Subject: [PATCH 4/5] Update crates/jiter-python/README.md Co-authored-by: Samuel Colvin --- crates/jiter-python/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/jiter-python/README.md b/crates/jiter-python/README.md index a9c10aa..8c82ac3 100644 --- a/crates/jiter-python/README.md +++ b/crates/jiter-python/README.md @@ -83,7 +83,7 @@ except ValueError as e: print(f"Error: {e}") # Parse incomplete JSON, discarding incomplete last field -result = jiter.from_json(partial_json, partial_mode='on') +result = jiter.from_json(partial_json, partial_mode=True) print(result) # Output: {'name': 'John', 'age': 30} # Parse incomplete JSON, including incomplete last field From 06c0ee0b2f4b09814d92e402f53a1155bdaff177 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 22 Sep 2024 21:05:58 -0400 Subject: [PATCH 5/5] Update crates/jiter-python/README.md Co-authored-by: Samuel Colvin --- crates/jiter-python/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/jiter-python/README.md b/crates/jiter-python/README.md index 8c82ac3..3c5e62e 100644 --- a/crates/jiter-python/README.md +++ b/crates/jiter-python/README.md @@ -78,7 +78,7 @@ partial_json = b'{"name": "John", "age": 30, "city": "New Yor' # Raise error on incomplete JSON try: - jiter.from_json(partial_json, partial_mode='off') + jiter.from_json(partial_json, partial_mode=False) except ValueError as e: print(f"Error: {e}")