pydantic-core
pydantic-core copied to clipboard
allow sorting keys on to_json and to_python by passing in sort_keys
Hello Pydantic Team! This is my first time contributing to a Rust and Pyo3 related repo. I am also new in Rust. Do you think this PR will make sense? Since I have been trying to do model_dump_json with sort keys too.
This feature should simulate the same as how we use json.dumps(data, sort_keys=True)
Will sort from:
{
'field_123': b'test_123',
'field_b': 12,
'field_a': b'test',
'field_c': {'mango': 2, 'banana': 3, 'apple': 1},
'field_n': [
{'mango': 3, 'banana': 2, 'apple': 1},
[{'mango': 3, 'banana': 2, 'apple': 1}, {'d': 3, 'b': 2, 'a': 1}],
3,
],
'field_d': [
{'d': 3, 'b': 2, 'a': {'nested3': 3, 'nested1': 1, 'nested2': 2}},
[[{'mango': 3, 'banana': 2, 'apple': 1}], {'d': 3, 'b': 2, 'a': 1}],
3,
],
'field_none': None,
}
To:
assert s.to_python(m, exclude_none=True, sort_keys=True) == snapshot(
{
'field_123': b'test_123',
'field_a': b'test',
'field_b': 12,
'field_c': {'apple': 1, 'banana': 3, 'mango': 2},
'field_n': [
{'apple': 1, 'banana': 2, 'mango': 3},
[{'apple': 1, 'banana': 2, 'mango': 3}, {'a': 1, 'b': 2, 'd': 3}],
3,
],
'field_d': [
{'a': {'nested1': 1, 'nested2': 2, 'nested3': 3}, 'b': 2, 'd': 3},
[[{'apple': 1, 'banana': 2, 'mango': 3}], {'a': 1, 'b': 2, 'd': 3}],
3,
],
}
)
Take note that
field_dis extra and still manage to sort- sorting recursively for both defined schema and extras
- sorting including dictionary in array or nested array array
Please let me know if I miss out any other features that sort_keys=True is suppose to do!
Thanks!
Change Summary
allow sorting keys on to_json and to_python by passing in sort_keys
Related issue number
should fix https://github.com/pydantic/pydantic/issues/7424 Might need to create another MR on Python repo though, need to check.
Checklist
- [x] Unit tests for the changes exist
- [x] Documentation reflects the changes where applicable
- [X] Pydantic tests pass with this
pydantic-core(except for expected changes) - [x] My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers
Selected Reviewer: @davidhewitt