NowanIlfideme / pydantic-yaml

YAML support for Pydantic models
MIT License
135 stars 11 forks source link

to_yaml_str() changes order of key #112

Open dinjazelena opened 11 months ago

dinjazelena commented 11 months ago

Hey, how can i keep my order of keys same as in model? This method changes it.

NowanIlfideme commented 11 months ago

Since we currently dump to JSON (to support custom JSON dumping and preserve it in YAML), and JSON doesn't preserve key order, pydantic-yaml doesn't support model key order, I'm afraid. This is something that is definitely planned at some point, but not currently in v1. It's simple to implement for trivial cases, but supporting all that Pydantic supports would make this really tricky.

cjshrader commented 1 week ago

Hey there,

Has there been any change on this front? I too would like the keys to be in the same order as the model which just makes things easier to read for the user. Problem is, as you surely know, there's no easy way to do this with json.loads.