Skip to content

Commit

Permalink
fix: Handle raw string as JSON (#24541)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias authored Aug 23, 2024
1 parent 4c4ece6 commit dfc67cc
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions posthog/temporal/batch_exports/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import collections.abc
import contextlib
import functools
import json
import typing
import uuid

Expand Down Expand Up @@ -123,6 +122,15 @@ class JsonScalar(pa.ExtensionScalar):
"""Represents a JSON binary string."""

def as_py(self) -> dict | None:
"""Try to convert value to Python representation.
We attempt to decode the value returned by `as_py` as JSON 3 times:
1. As returned by `as_py`, without changes.
2. By replacing any encoding errors.
3. By treating the value as a string and surrouding it with quotes.
If all else fails, we will log the offending value and re-raise the decoding error.
"""
if self.value:
value = self.value.as_py()

Expand All @@ -131,13 +139,23 @@ def as_py(self) -> dict | None:

try:
return orjson.loads(value.encode("utf-8"))
except:
# Fallback if it's something orjson can't handle
try:
return json.loads(value)
except json.JSONDecodeError:
logger.exception("Failed to decode: %s", value)
raise
except orjson.JSONEncodeError:
pass

try:
return orjson.loads(value.encode("utf-8", "replace"))
except orjson.JSONDecodeError:
pass

if isinstance(value, str) and len(value) > 0:
# Handles `"$set": "Something"`
value = f'"{value}"'

try:
return orjson.loads(value.encode("utf-8", "replace"))
except orjson.JSONDecodeError:
logger.exception("Failed to decode: %s", value)
raise

else:
return None
Expand Down

0 comments on commit dfc67cc

Please sign in to comment.