Changeset 4a79b76 in flexoentity


Ignore:
Timestamp:
11/28/25 18:13:58 (7 weeks ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
master
Children:
df459f7
Parents:
100c1d2
Message:

move from_json_file and to_json_file to the base class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • flexoentity/flexo_entity.py

    r100c1d2 r4a79b76  
    5656from dataclasses import dataclass, field, replace
    5757from typing import Optional
     58from pathlib import Path
    5859from abc import ABC, abstractmethod
    5960import hashlib
    6061
    6162
    62 from flexoentity.id_factory import FlexOID, canonical_seed
     63from flexoentity.id_factory import FlexOID
    6364from flexoentity.logger import logger
    6465
     
    114115        return self.name
    115116
     117
    116118TRANSITIONS = {
    117119    EntityState.DRAFT: [EntityState.APPROVED],
     
    122124    EntityState.OBSOLETE: [],
    123125}
    124 
    125126
    126127
     
    306307        return obj
    307308
    308     def to_json(self, *, indent: int | None = None) -> str:
    309         """I serialize myself (and my FlexOID) into JSON."""
    310         return json.dumps(self.to_dict(), indent=indent, ensure_ascii=False)
     309    def to_json(self, indent: int = 2) -> str:
     310        """Return a JSON representation including all FlexO metadata."""
     311        return json.dumps(self.to_dict(), ensure_ascii=False, indent=indent)
     312
     313    def to_json_file(self, path):
     314        Path(path).write_text(self.to_json(), encoding="utf-8")
     315        return path
    311316
    312317    @classmethod
     
    315320        data = json.loads(data_str)
    316321        return cls.from_dict(data)
     322
     323    @classmethod
     324    def from_json_file(cls, filename: str):
     325        with open(filename, "r", encoding="utf-8") as f:
     326            data = json.load(f)
     327        return cls.from_dict(data)
     328
    317329
    318330    def _deserialize_content(self, content_dict):
Note: See TracChangeset for help on using the changeset viewer.