json_context.py 934 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from enum import Enum, auto
  2. class ContextValues(Enum):
  3. OBJECT_KEY = auto()
  4. OBJECT_VALUE = auto()
  5. ARRAY = auto()
  6. class JsonContext:
  7. def __init__(self) -> None:
  8. self.context: list[ContextValues] = []
  9. self.current: ContextValues | None = None
  10. self.empty: bool = True
  11. def set(self, value: ContextValues) -> None:
  12. """
  13. Set a new context value.
  14. Args:
  15. value (ContextValues): The context value to be added.
  16. Returns:
  17. None
  18. """
  19. self.context.append(value)
  20. self.current = value
  21. self.empty = False
  22. def reset(self) -> None:
  23. """
  24. Remove the most recent context value.
  25. Returns:
  26. None
  27. """
  28. try:
  29. self.context.pop()
  30. self.current = self.context[-1]
  31. except IndexError:
  32. self.current = None
  33. self.empty = True