Smithy JSON
smithy-json provides robust JSON serialization and deserialization capabilities specifically for Smithy tooling within the Python ecosystem. It allows conversion between Smithy's abstract syntax tree (AST) representation (Nodes) and JSON strings. As part of the broader `smithy-python` project, it currently stands at version 0.2.2 and undergoes active development with frequent releases aligning with the main project.
Common errors
-
ModuleNotFoundError: No module named 'smithy_python_types'
cause The `smithy-python-types` package, which defines the core `Node` types, is a required dependency but is not installed or not accessible.fixEnsure `smithy-python-types` is installed. If you installed `smithy-json` directly, it should pull `smithy-python-types` as a dependency. If not, run `pip install smithy-python-types` or `pip install smithy-json`. -
AttributeError: 'dict' object has no attribute 'json_kind'
cause You are attempting to pass a standard Python dictionary or list directly to `JsonEmitter` or `JsonParser`, but `smithy-json` expects `smithy_python_types.main.Node` objects.fixConvert your Python data structures into `smithy_python_types.main.Node` objects (e.g., `Node.object({'key': Node.string('value')})`, `Node.array([Node.number(1)])`) before using them with `JsonEmitter` or `JsonParser`. -
TypeError: 'JsonEmitter' object is not callable
cause You are trying to call `JsonEmitter()` without its required `emit` method, or `JsonParser()` without its `parse` method, possibly confusing the class with a function.fixRemember that `JsonEmitter` and `JsonParser` are classes. You must instantiate them first (e.g., `emitter = JsonEmitter()`) and then call their respective methods (`emitter.emit(node, output_buffer)` or `parser.parse(input_buffer)`).
Warnings
- breaking The `smithy-json` library, being in a 0.x.x version series, is subject to frequent API changes. Backwards compatibility is not guaranteed between minor versions (e.g., 0.2.x to 0.3.x).
- gotcha `smithy-json` is tightly coupled with `smithy-python-codegen-core` and `smithy-python-types`. Mismatched versions of these packages can lead to runtime errors or unexpected behavior.
Install
-
pip install smithy-json
Imports
- JsonEmitter
from smithy_json.emitters import JsonEmitter
- JsonParser
from smithy_json.parser import JsonParser
- Node
from smithy_python_types.main import Node
Quickstart
import io
from smithy_python_types.main import Node
from smithy_json.emitters import JsonEmitter
# Create a Smithy Node representing a complex JSON object
node = Node.object({
"greeting": Node.string("Hello, Smithy!"),
"count": Node.number(123),
"enabled": Node.boolean(True),
"items": Node.array([
Node.string("item1"),
Node.object({"id": Node.number(1)}),
Node.null()
])
})
# Instantiate the JSON Emitter
emitter = JsonEmitter()
# Create a buffer to write the JSON output to
output_buffer = io.BytesIO()
# Emit the Node as JSON into the buffer
emitter.emit(node, output_buffer)
# Get the bytes from the buffer and decode to a string
json_output = output_buffer.getvalue().decode('utf-8')
print("Generated JSON:")
print(json_output)
# Example of parsing (optional)
# from smithy_json.parser import JsonParser
# parser = JsonParser()
# input_json = b'{"parsed_key": "parsed_value"}'
# parsed_node = parser.parse(io.BytesIO(input_json))
# print("\nParsed Node:", parsed_node)