METADATA 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. Metadata-Version: 2.4
  2. Name: jiter
  3. Version: 0.12.0
  4. Classifier: Development Status :: 4 - Beta
  5. Classifier: Programming Language :: Python
  6. Classifier: Programming Language :: Python :: 3
  7. Classifier: Programming Language :: Python :: 3 :: Only
  8. Classifier: Programming Language :: Python :: 3.9
  9. Classifier: Programming Language :: Python :: 3.10
  10. Classifier: Programming Language :: Python :: 3.11
  11. Classifier: Programming Language :: Python :: 3.12
  12. Classifier: Programming Language :: Python :: 3.13
  13. Classifier: Programming Language :: Python :: 3.14
  14. Classifier: Programming Language :: Python :: Implementation :: CPython
  15. Classifier: Programming Language :: Python :: Implementation :: GraalPy
  16. Classifier: Intended Audience :: Developers
  17. Classifier: Intended Audience :: Information Technology
  18. Classifier: Intended Audience :: System Administrators
  19. Classifier: License :: OSI Approved :: MIT License
  20. Classifier: Operating System :: Unix
  21. Classifier: Operating System :: POSIX :: Linux
  22. Classifier: Environment :: Console
  23. Classifier: Environment :: MacOS X
  24. Classifier: Topic :: File Formats :: JSON
  25. Classifier: Framework :: Pydantic :: 2
  26. License-File: LICENSE
  27. Summary: Fast iterable JSON parser.
  28. Home-Page: https://github.com/pydantic/jiter/
  29. Author-email: Samuel Colvin <s@muelcolvin.com>
  30. Requires-Python: >=3.9
  31. Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
  32. # jiter
  33. [![CI](https://github.com/pydantic/jiter/workflows/CI/badge.svg?event=push)](https://github.com/pydantic/jiter/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
  34. [![pypi](https://img.shields.io/pypi/v/jiter.svg)](https://pypi.python.org/pypi/jiter)
  35. [![versions](https://img.shields.io/pypi/pyversions/jiter.svg)](https://github.com/pydantic/jiter)
  36. [![license](https://img.shields.io/github/license/pydantic/jiter.svg)](https://github.com/pydantic/jiter/blob/main/LICENSE)
  37. This is a standalone version of the JSON parser used in `pydantic-core`. The recommendation is to only use this package directly if you do not use `pydantic`.
  38. The API is extremely minimal:
  39. ```python
  40. def from_json(
  41. json_data: bytes,
  42. /,
  43. *,
  44. allow_inf_nan: bool = True,
  45. cache_mode: Literal[True, False, "all", "keys", "none"] = "all",
  46. partial_mode: Literal[True, False, "off", "on", "trailing-strings"] = False,
  47. catch_duplicate_keys: bool = False,
  48. float_mode: Literal["float", "decimal", "lossless-float"] = "float",
  49. ) -> Any:
  50. """
  51. Parse input bytes into a JSON object.
  52. Arguments:
  53. json_data: The JSON data to parse
  54. allow_inf_nan: Whether to allow infinity (`Infinity` an `-Infinity`) and `NaN` values to float fields.
  55. Defaults to True.
  56. cache_mode: cache Python strings to improve performance at the cost of some memory usage
  57. - True / 'all' - cache all strings
  58. - 'keys' - cache only object keys
  59. - False / 'none' - cache nothing
  60. partial_mode: How to handle incomplete strings:
  61. - False / 'off' - raise an exception if the input is incomplete
  62. - True / 'on' - allow incomplete JSON but discard the last string if it is incomplete
  63. - 'trailing-strings' - allow incomplete JSON, and include the last incomplete string in the output
  64. catch_duplicate_keys: if True, raise an exception if objects contain the same key multiple times
  65. float_mode: How to return floats: as a `float`, `Decimal` or `LosslessFloat`
  66. Returns:
  67. Python object built from the JSON input.
  68. """
  69. def cache_clear() -> None:
  70. """
  71. Reset the string cache.
  72. """
  73. def cache_usage() -> int:
  74. """
  75. get the size of the string cache.
  76. Returns:
  77. Size of the string cache in bytes.
  78. """
  79. ```
  80. ## Examples
  81. 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.
  82. ```python
  83. import jiter
  84. json_data = b'{"name": "John", "age": 30}'
  85. parsed_data = jiter.from_json(json_data)
  86. print(parsed_data) # Output: {'name': 'John', 'age': 30}
  87. ```
  88. ### Handling Partial JSON
  89. Incomplete JSON objects can be parsed using the `partial_mode=` parameter.
  90. ```python
  91. import jiter
  92. partial_json = b'{"name": "John", "age": 30, "city": "New Yor'
  93. # Raise error on incomplete JSON
  94. try:
  95. jiter.from_json(partial_json, partial_mode=False)
  96. except ValueError as e:
  97. print(f"Error: {e}")
  98. # Parse incomplete JSON, discarding incomplete last field
  99. result = jiter.from_json(partial_json, partial_mode=True)
  100. print(result) # Output: {'name': 'John', 'age': 30}
  101. # Parse incomplete JSON, including incomplete last field
  102. result = jiter.from_json(partial_json, partial_mode='trailing-strings')
  103. print(result) # Output: {'name': 'John', 'age': 30, 'city': 'New Yor'}
  104. ```
  105. ### Catching Duplicate Keys
  106. The `catch_duplicate_keys=True` option can be used to raise a `ValueError` if an object contains duplicate keys.
  107. ```python
  108. import jiter
  109. json_with_dupes = b'{"foo": 1, "foo": 2}'
  110. # Default behavior (last value wins)
  111. result = jiter.from_json(json_with_dupes)
  112. print(result) # Output: {'foo': 2}
  113. # Catch duplicate keys
  114. try:
  115. jiter.from_json(json_with_dupes, catch_duplicate_keys=True)
  116. except ValueError as e:
  117. print(f"Error: {e}")
  118. ```