Normalize a JSONField-backed custom_properties value into a dict. Historical rows (TextField era and early JSONField migration) may store a JSON-encoded string instead of an object. API clients can also submit a string value because JSONField accepts any JSON type. Call this before
(value)
| 73 | |
| 74 | |
| 75 | def custom_properties_as_dict(value): |
| 76 | """ |
| 77 | Normalize a JSONField-backed custom_properties value into a dict. |
| 78 | |
| 79 | Historical rows (TextField era and early JSONField migration) may store a |
| 80 | JSON-encoded string instead of an object. API clients can also submit a |
| 81 | string value because JSONField accepts any JSON type. Call this before |
| 82 | reading or merging custom_properties. |
| 83 | """ |
| 84 | if isinstance(value, dict): |
| 85 | return value |
| 86 | if isinstance(value, str): |
| 87 | try: |
| 88 | parsed = json.loads(value) |
| 89 | except (ValueError, TypeError): |
| 90 | logger.warning( |
| 91 | "custom_properties stored as non-JSON string; ignoring: %r", |
| 92 | value[:100], |
| 93 | ) |
| 94 | return {} |
| 95 | return parsed if isinstance(parsed, dict) else {} |
| 96 | if value is None: |
| 97 | return {} |
| 98 | return {} |
| 99 | |
| 100 | |
| 101 | def ensure_custom_properties_dict(value): |
no outgoing calls