File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ class BaseConfigJSONMixin(DataClassJSONMixin):
3030
3131 class Config (BaseConfig ):
3232 omit_none = True
33+ serialize_by_alias = True
3334
3435 @classmethod
3536 def from_json_file (cls , file_path : Path ) -> Self :
Original file line number Diff line number Diff line change 11import json
2- from dataclasses import dataclass
2+ from dataclasses import dataclass , field
33from pathlib import Path
44from typing import Any
55
@@ -109,3 +109,19 @@ def test_json_mixin_to_string() -> None:
109109 cfg = SampleJsonConfig (name = "a" , count = 1 )
110110
111111 assert cfg .to_string () == cfg .to_json_string ()
112+
113+
114+ @dataclass
115+ class ConfigWithAlias (BaseConfigJSONMixin ):
116+ internal_field : str = field (metadata = {"alias" : "externalName" })
117+ regular_field : int = 0
118+
119+
120+ def test_json_mixin_serialize_by_alias () -> None :
121+ cfg = ConfigWithAlias (internal_field = "value" , regular_field = 42 )
122+ json_str = cfg .to_json_string ()
123+ parsed = json .loads (json_str )
124+
125+ assert "externalName" in parsed
126+ assert parsed ["externalName" ] == "value"
127+ assert "internal_field" not in parsed
You can’t perform that action at this time.
0 commit comments