Property Name Formatting
By default, py-serializable uses it’s py_serializable.formatters.CamelCasePropertyNameFormatter formatter for
translating actual Python property names to element names in either JSON or XML.
py-serializable includes a number of name formatters out of the box, but you can also create your own if required.
Included Formatters
py-serializable includes three common formatters out of the box.
Camel Case Formatter:
py_serializable.formatters.CamelCasePropertyNameFormatter(the default)Kebab Case Formatter:
py_serializable.formatters.KebabCasePropertyNameFormatterSnake Case Formatter:
py_serializable.formatters.SnakeCasePropertyNameFormatter
A summary of how these differ is included in the below table.
Python Property Name |
Camel Case |
Kebab Case |
Snake Case |
|---|---|---|---|
books |
books |
books |
books |
big_book |
bigBook |
big-book |
big_book |
a_very_big_book |
aVeryBigBook |
a-very-big-book |
a_very_big_book |
Changing the Formatter
You can change the formatter being used by easily. The example below changes the formatter to be Snake Case.
from py_serializable.formatters import CurrentFormatter, SnakeCasePropertyNameFormatter
CurrentFormatter.formatter = SnakeCasePropertyNameFormatter
Custom Formatters
If none of the included formatters work for you, why not write your own?
The only requirement is that it extends py_serializable.formatters.BaseNameFormatter!