JsonDocument
Base classes
| Name |
Children |
Inherits |
QJsonDocument PySide6.QtCore QJsonDocument(self, /) -> None |
|
|
⋔ Inheritance diagram
graph TD
2848389598["core.JsonDocument"]
1279635541["QtCore.QJsonDocument"]
37984305["Shiboken.Object"]
2751680898["builtins.object"]
1279635541 --> 2848389598
37984305 --> 1279635541
2751680898 --> 37984305
🛈 DocStrings
Bases: QJsonDocument
Way to read and write JSON documents.
Source code in prettyqt/core/jsondocument.py
| class JsonDocument(core.QJsonDocument):
"""Way to read and write JSON documents."""
def __str__(self):
return str(self.toVariant())
def __format__(self, fmt):
if fmt in JSON_FORMAT:
return self.to_string(fmt == "indented")
return super().__format__(fmt)
def __repr__(self):
return get_repr(self, self.toVariant())
def __getitem__(self, index: int | str):
val = self.array() if self.isArray() else self.object()
return core.JsonValue(val[index]) # type: ignore
def __setitem__(self, index: int | str, value):
if self.isArray():
if not isinstance(index, int):
raise TypeError
array = self.array()
if isinstance(array, list): # PyQt6 returns a list
array[index] = value
else: # PySide6 returns a QJsonArray, which has no item assignment
array.replace(index, core.QJsonValue(value))
self.setArray(array)
elif self.isObject():
if not isinstance(index, str):
raise TypeError
obj = self.object()
obj[index] = value
self.setObject(obj)
def to_string(self, indented: bool = False) -> str:
flag = self.JsonFormat.Indented if indented else self.JsonFormat.Compact
return bytes(self.toJson(flag).data()).decode()
@classmethod
def from_variant(cls, obj) -> Self:
doc = cls.fromVariant(obj)
new = cls()
if doc.isArray():
new.setArray(doc.array())
else:
new.setObject(doc.object())
return new
|
:fa-brands-github: Show source on GitHub