Polygraphy's custom JSON Decoder implementation.
| 154 | |
| 155 | @mod.export() |
| 156 | class Decoder(BaseCustomImpl): |
| 157 | """ |
| 158 | Polygraphy's custom JSON Decoder implementation. |
| 159 | """ |
| 160 | |
| 161 | polygraphy_registered = {} |
| 162 | |
| 163 | def __call__(self, pairs): |
| 164 | dct = OrderedDict(pairs) |
| 165 | |
| 166 | if config.INTERNAL_CORRECTNESS_CHECKS: |
| 167 | custom_type_keys = [key for key in dct if key.startswith(TYPE_STRING_PREFIX)] |
| 168 | if custom_type_keys and custom_type_keys[0] not in self.polygraphy_registered: |
| 169 | G_LOGGER.internal_error( |
| 170 | f"Custom type has no decode function registered! Note: Encoded object is:\n{dct}" |
| 171 | ) |
| 172 | |
| 173 | # The encoder will insert special key-value pairs into dictionaries encoded from |
| 174 | # custom types. If we find one, then we know to decode using the corresponding custom |
| 175 | # type function. |
| 176 | type_name = dct.get(constants.TYPE_MARKER) |
| 177 | func = self.polygraphy_registered.get(type_name) |
| 178 | if func: |
| 179 | return func(dct) |
| 180 | |
| 181 | for type_str, func in self.polygraphy_registered.items(): |
| 182 | if type_str in dct and dct[type_str] == constants.LEGACY_TYPE_MARKER: # Found a custom type! |
| 183 | return func(dct) |
| 184 | return dct |
| 185 | |
| 186 | |
| 187 | NUMPY_REGISTRATION_SUCCESS = False |