Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
Change the way JSONs are parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Kulas committed Sep 19, 2023
1 parent 86dedaf commit 62365dc
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions odd_collector_sdk/utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class DefinitionType(Enum):


def extract_metadata(
datasource: str,
entity: HasMetadata,
definition: DefinitionType,
flatten: Optional[bool] = False,
jsonify: Optional[bool] = False,
json_encoder: Optional[Type[json.JSONEncoder]] = None,
datasource: str,
entity: HasMetadata,
definition: DefinitionType,
flatten: Optional[bool] = False,
jsonify: Optional[bool] = False,
json_encoder: Optional[Type[json.JSONEncoder]] = None,
) -> MetadataExtension:
"""
:param datasource: name of datasource.
Expand All @@ -48,13 +48,13 @@ def extract_metadata(
if not isinstance(data, dict):
raise TypeError(f"Metadata must be a dict, got {type(data)}")

if flatten:
data = flat_dict(data)

if jsonify:
encode = partial(to_json, encoder=json_encoder)
data = walk_values(encode, data)

if flatten:
data = flat_dict(data)

not_none = select_values(complement(is_none), data)

return MetadataExtension(schema_url=schema_url, metadata=not_none)
Expand All @@ -68,8 +68,10 @@ def is_none(value) -> bool:


def to_json(value, encoder: Optional[Type[json.JSONEncoder]]) -> Optional[str]:
if isinstance(value, (str, int, float, bool, type(None))):
return value # Return primitives as-is
try:
return json.dumps(value, cls=encoder or CustomJSONEncoder)
return json.loads(json.dumps(value, cls=encoder or CustomJSONEncoder)) # Deserialize back to Python objects
except Exception as error:
logger.error(f"Could not jsonfy {value=}. {error}.\n SKIP.")
return None
Expand Down

0 comments on commit 62365dc

Please sign in to comment.