feat: simple serialization defaults #28
Merged
+776
−55
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces new JSON serialization utilities for converting Python objects to JSON formats. The main additions are:
serialize_defaults()- A default handler function forjson.dumps()that handles Pydantic models, dataclasses, enums, datetime objects, and other complex typesserialize_json()- A convenience wrapper that combinesjson.dumps()withserialize_defaults()This PR consolidates existing serialization logic: The previously private
_simple_serialize_defaults()function fromtracing/_utils.pyhas been moved to a new public location atuipath.core.utils.serialization.json, made public (removed underscore prefix), renamed toserialize_defaults(), enhanced with a convenience wrapperserialize_json(), and the tracing module has been refactored to use the new centralized implementation.What Changed
New Package Structure
New Files
src/uipath/core/utils/serialization/__init__.py- Package exportssrc/uipath/core/utils/serialization/json.py- Public serialization utilities with comprehensive docstringsserialize_defaults()- Default handler for json.dumps()serialize_json()- Convenience wrapper combining json.dumps() + serialize_defaults()tests/utils/__init__.py- Test packagetests/utils/test_serialization.py- 35 comprehensive test cases covering all serialization scenariosRefactored Files
src/uipath/core/utils/__init__.py- Now exportsserialize_defaultsandserialize_jsonsrc/uipath/core/tracing/_utils.py- Removed duplicate_simple_serialize_defaults()function, now imports and usesserialize_json()from the new utils module. Removed unnecessary imports (dataclasses, datetime, timezone, enum, zoneinfo, pydantic, cast). Updatedformat_args_for_trace_json()andformat_object_for_trace_json()to useserialize_json().Key Features
The
serialize_defaults()function handles:dictdictlistdictto_dict()ordict()methodsstr()fallbackThe
serialize_json()convenience function:json.dumps(obj, default=serialize_defaults)How It Works
Using
serialize_defaults()as default handlerWhen
json.dumps()encounters a non-JSON-serializable type, it callsserialize_defaults()to convert it.Using
serialize_json()convenience functionThis is equivalent to
json.dumps(data, default=serialize_defaults).How
json.dumps()withdefaultWorksWhen you call
json.dumps(data, default=serialize_defaults), here's what happens:serialize_defaults(that_object)Example Flow
Internal processing:
Result:
{ "user": { "name": "Alice", "created_at": "2024-01-15T10:00:00" }, "count": 42 }Test Coverage
All 35 tests use
serialize_json()and verify the final deserialized JSON structure:Usage Examples
Basic Usage with serialize_json()
Advanced Usage with serialize_defaults()
Design Decisions
serialization/package for future expansion (e.g., XML, YAML, etc.)json.pyto clearly indicate JSON-specific serializationserialize_defaults()(removed "simple" prefix) as it's no longer internalserialize_json()for the common use case (cleaner than repeatedjson.dumps(..., default=...))listanddictto avoid unnecessary conversionsserialize_json()to ensure real-world usage worksserialize_json()directlyImpact
tracing/_utils.py(private function)uipath.core.utils.serializationwith comprehensive testsserialize_json()wrapperStats
Related
This function consolidates serialization logic previously only available in tracing utilities and will be used across the codebase for consistent JSON serialization of complex Python objects.