Create a dataclass field with Fory-specific serialization metadata. This wraps dataclasses.field() and stores Fory configuration in field.metadata. Args: id: Field tag ID (optional). - omitted: Use field name with meta string encoding - >=0: Use numeric
(
id: int = _FIELD_ID_UNSET,
*,
nullable: bool = False,
ref: bool = False,
ignore: bool = False,
dynamic: Optional[bool] = None,
# Standard dataclass.field() options (passthrough)
default: Any = MISSING,
default_factory: Optional[Callable[[], Any]] = MISSING,
init: bool = True,
repr: bool = True,
hash: Optional[bool] = None,
compare: bool = True,
metadata: Optional[Mapping[str, Any]] = None,
**kwargs,
)
| 104 | |
| 105 | |
| 106 | def field( |
| 107 | id: int = _FIELD_ID_UNSET, |
| 108 | *, |
| 109 | nullable: bool = False, |
| 110 | ref: bool = False, |
| 111 | ignore: bool = False, |
| 112 | dynamic: Optional[bool] = None, |
| 113 | # Standard dataclass.field() options (passthrough) |
| 114 | default: Any = MISSING, |
| 115 | default_factory: Optional[Callable[[], Any]] = MISSING, |
| 116 | init: bool = True, |
| 117 | repr: bool = True, |
| 118 | hash: Optional[bool] = None, |
| 119 | compare: bool = True, |
| 120 | metadata: Optional[Mapping[str, Any]] = None, |
| 121 | **kwargs, |
| 122 | ) -> Any: |
| 123 | """ |
| 124 | Create a dataclass field with Fory-specific serialization metadata. |
| 125 | |
| 126 | This wraps dataclasses.field() and stores Fory configuration in field.metadata. |
| 127 | |
| 128 | Args: |
| 129 | id: Field tag ID (optional). |
| 130 | - omitted: Use field name with meta string encoding |
| 131 | - >=0: Use numeric tag ID (more compact, stable across renames) |
| 132 | Must be unique within the class. Negative configured IDs are invalid. |
| 133 | |
| 134 | nullable: Whether to write null flag for this field. |
| 135 | - False (default): Skip null flag, field cannot be None |
| 136 | - True: Write null flag (1 byte overhead), field can be None |
| 137 | Note: For Optional[T] fields, nullable=True is required. |
| 138 | Setting nullable=False on Optional[T] raises ValueError. |
| 139 | |
| 140 | ref: Whether to enable reference tracking for this field. |
| 141 | - False (default): No tracking, skip IdentityMap overhead |
| 142 | - True: Track references (handles circular refs, shared objects) |
| 143 | Note: If Fory(ref=False), all fields use ref=False |
| 144 | regardless of this setting. |
| 145 | |
| 146 | ignore: Whether to ignore this field during serialization. |
| 147 | - False (default): Field is serialized |
| 148 | - True: Field is excluded from serialization |
| 149 | |
| 150 | dynamic: Whether to write type info for this field. |
| 151 | - None (default): Auto-detect based on type and mode |
| 152 | - Abstract classes: always True (type info must be written) |
| 153 | - Native mode: True for all types |
| 154 | - Xlang mode: False for concrete types |
| 155 | - True: Always write type info (support runtime subtypes) |
| 156 | - False: Never write type info (use declared type's serializer) |
| 157 | |
| 158 | default, default_factory, init, repr, hash, compare, metadata: |
| 159 | Standard dataclass.field() parameters, passed through. |
| 160 | |
| 161 | **kwargs: Additional arguments forwarded to dataclasses.field(). |
| 162 | |
| 163 | Returns: |