Skip to content

Commit 24a8b7c

Browse files
committed
Replace JSON Model Serialization
1 parent 809fc3d commit 24a8b7c

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

cppython/plugins/cmake/builder.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def write_provider_preset(provider_directory: Path, data: CMakeSyncData) -> None
2323

2424
json_path = provider_directory / f'{data.provider_name}.json'
2525

26-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
26+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False)
2727
with open(json_path, 'w', encoding='utf8') as file:
28-
json.dump(serialized, file, ensure_ascii=False, indent=4)
28+
file.write(serialized)
2929

3030
@staticmethod
3131
def write_cppython_preset(
@@ -44,9 +44,9 @@ def write_cppython_preset(
4444

4545
cppython_json_path = cppython_preset_directory / 'cppython.json'
4646

47-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
47+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False)
4848
with open(cppython_json_path, 'w', encoding='utf8') as file:
49-
json.dump(serialized, file, ensure_ascii=False, indent=4)
49+
file.write(serialized)
5050

5151
return cppython_json_path
5252

@@ -63,8 +63,12 @@ def write_root_presets(preset_file: Path, _: Path) -> None:
6363
preset_file: Preset file to modify
6464
"""
6565
with open(preset_file, encoding='utf-8') as file:
66-
initial_root_preset = json.load(file)
66+
initial_json = file.read()
6767

68+
initial_root_preset = CMakePresets.model_validate_json(initial_json)
69+
70+
# Only write the file if the contents have changed
6871
if (root_preset := deepcopy(initial_root_preset)) != initial_root_preset:
6972
with open(preset_file, 'w', encoding='utf-8') as file:
70-
json.dump(root_preset, file, ensure_ascii=False, indent=4)
73+
preset = root_preset.model_dump_json(exclude_none=True)
74+
file.write(preset)

cppython/plugins/cmake/resolution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ def resolve_cmake_data(data: dict[str, Any], core_data: CorePluginData) -> CMake
3030
modified_preset_dir.parent.mkdir(parents=True, exist_ok=True)
3131
with modified_preset_dir.open('w', encoding='utf-8') as file:
3232
presets_dict = CMakePresets().model_dump_json(exclude_none=True)
33-
json.dump(presets_dict, file, ensure_ascii=False, indent=4)
33+
file.write(presets_dict)
3434

3535
return CMakeData(preset_file=modified_preset_dir, configuration_name=parsed_data.configuration_name)

cppython/plugins/cmake/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CMakePresets(CPPythonModel, extra='allow'):
5656
The only information needed is the configure preset list for cache variable injection
5757
"""
5858

59+
version: Annotated[int, Field(description='The version of the JSON schema.')] = 9
5960
configurePresets: Annotated[list[ConfigurePreset], Field(description='The list of configure presets')] = []
6061

6162

cppython/plugins/vcpkg/plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ def install(self) -> None:
193193
manifest = generate_manifest(self.core_data, self.data)
194194

195195
# Write out the manifest
196-
serialized = json.loads(manifest.model_dump_json(exclude_none=True, by_alias=True))
196+
serialized = manifest.model_dump_json(exclude_none=True, by_alias=True)
197197
with open(manifest_directory / 'vcpkg.json', 'w', encoding='utf8') as file:
198-
json.dump(serialized, file, ensure_ascii=False, indent=4)
198+
file.write(serialized)
199199

200200
executable = self.core_data.cppython_data.install_path / 'vcpkg'
201201
logger = getLogger('cppython.vcpkg')
@@ -224,9 +224,9 @@ def update(self) -> None:
224224
manifest = generate_manifest(self.core_data, self.data)
225225

226226
# Write out the manifest
227-
serialized = json.loads(manifest.model_dump_json(exclude_none=True, by_alias=True))
227+
serialized = manifest.model_dump_json(exclude_none=True, by_alias=True)
228228
with open(manifest_directory / 'vcpkg.json', 'w', encoding='utf8') as file:
229-
json.dump(serialized, file, ensure_ascii=False, indent=4)
229+
file.write(serialized)
230230

231231
executable = self.core_data.cppython_data.install_path / 'vcpkg'
232232
logger = getLogger('cppython.vcpkg')

tests/unit/plugins/cmake/test_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def test_root_write(tmp_path: Path) -> None:
105105
root_file = tmp_path / 'CMakePresets.json'
106106
presets = CMakePresets()
107107

108-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
108+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False)
109109
with open(root_file, 'w', encoding='utf8') as file:
110-
json.dump(serialized, file, ensure_ascii=False, indent=4)
110+
file.write(serialized)
111111

112112
data = CMakeSyncData(provider_name=TypeName('test-provider'), top_level_includes=includes_file)
113113
builder.write_provider_preset(provider_directory, data)
@@ -140,9 +140,9 @@ def test_relative_root_write(tmp_path: Path) -> None:
140140

141141
root_file = relative_indirection / 'CMakePresets.json'
142142
presets = CMakePresets()
143-
serialized = json.loads(presets.model_dump_json(exclude_none=True, by_alias=False))
143+
serialized = presets.model_dump_json(exclude_none=True, by_alias=False)
144144
with open(root_file, 'w', encoding='utf8') as file:
145-
json.dump(serialized, file, ensure_ascii=False, indent=4)
145+
file.write(serialized)
146146

147147
data = CMakeSyncData(provider_name=TypeName('test-provider'), top_level_includes=includes_file)
148148
builder.write_provider_preset(provider_directory, data)

0 commit comments

Comments
 (0)