Skip to content

Commit

Permalink
do not force ASCII on JSON dump, add json_dumps helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Sep 4, 2024
1 parent 691d839 commit 09f2e2b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
6 changes: 3 additions & 3 deletions pygeometa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# referenced with those assets.
#
# Copyright (c) 2016 Government of Canada
# Copyright (c) 2022 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -63,7 +63,7 @@
import yaml

from pygeometa import cli_options
from pygeometa.helpers import json_serial
from pygeometa.helpers import json_dumps
from pygeometa.schemas import get_supported_schemas, load_schema

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -573,7 +573,7 @@ def validate(ctx, mcf, verbosity):

click.echo(f'Validating {mcf}')

instance = json.loads(json.dumps(read_mcf(mcf), default=json_serial))
instance = json.loads(json_dumps(read_mcf(mcf)))
validate_mcf(instance)

click.echo('Valid MCF document')
Expand Down
20 changes: 17 additions & 3 deletions pygeometa/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2022 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -46,17 +46,31 @@
import base64
from datetime import date, datetime, time
from decimal import Decimal
import json
import logging
from pathlib import Path
from typing import Any

LOGGER = logging.getLogger(__name__)

THISDIR = Path(__file__).resolve().parent


def json_serial(obj):
def json_dumps(obj) -> str:
"""
helper function to convert to JSON non-default
Helper function to dump dict to JSON string
:param obj: `dict` of JSON
:returns: `str` of JSON
"""

return json.dumps(obj, default=json_serial, indent=4, ensure_ascii=False)


def json_serial(obj) -> Any:
"""
Helper function to convert to JSON non-default
types (source: https://stackoverflow.com/a/22238613)
:param obj: `object` to be evaluated
Expand Down
7 changes: 3 additions & 4 deletions pygeometa/schemas/dcat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2020 Tom Kralidis, Paul van Genuchten
# Copyright (c) 2024 Tom Kralidis, Paul van Genuchten
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand All @@ -43,11 +43,10 @@
#
# =================================================================

import json
import os
from typing import Union

from pygeometa.helpers import json_serial
from pygeometa.helpers import json_dumps
from pygeometa.schemas.base import BaseOutputSchema

THISDIR = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -208,6 +207,6 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
dcat[key] = value

if stringify:
return json.dumps(dcat, default=json_serial, indent=4)
return json_dumps(dcat)

return dcat
5 changes: 2 additions & 3 deletions pygeometa/schemas/ogcapi_records/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@
# =================================================================

from datetime import date, datetime
import json
import logging
import os
from typing import Union

from pygeometa.core import get_charstring
from pygeometa.helpers import json_serial
from pygeometa.helpers import json_dumps
from pygeometa.schemas.base import BaseOutputSchema

THISDIR = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -236,7 +235,7 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
record['links'].append(self.generate_link(value))

if stringify:
return json.dumps(record, default=json_serial, indent=4)
return json_dumps(record)

return record

Expand Down
7 changes: 3 additions & 4 deletions pygeometa/schemas/stac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2020 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand All @@ -43,12 +43,11 @@
#
# =================================================================

import json
import os
from typing import Union

from pygeometa.core import get_charstring
from pygeometa.helpers import json_serial
from pygeometa.helpers import json_dumps
from pygeometa.schemas.base import BaseOutputSchema

THISDIR = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -136,6 +135,6 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
stac_item['links'].append(link)

if stringify:
return json.dumps(stac_item, default=json_serial, indent=4)
return json_dumps(stac_item)

return stac_item
5 changes: 2 additions & 3 deletions pygeometa/schemas/wmo_wcmp2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@
# =================================================================

from datetime import datetime
import json
import logging
import os
from typing import Union

from pygeometa.helpers import json_serial
from pygeometa.helpers import json_dumps
from pygeometa.schemas.ogcapi_records import OGCAPIRecordOutputSchema

THISDIR = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -110,6 +109,6 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
record['properties']['created'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # noqa

if stringify:
return json.dumps(record, default=json_serial, indent=4)
return json_dumps(record)
else:
return record
8 changes: 4 additions & 4 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#
# Copyright (c) 2015 Government of Canada
# Copyright (c) 2016 ERT Inc.
# Copyright (c) 2022 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -58,7 +58,7 @@
prune_distribution_formats,
prune_transfer_option, MCFReadError,
MCFValidationError, SCHEMAS, validate_mcf)
from pygeometa.helpers import json_serial
from pygeometa.helpers import json_dumps
from pygeometa.schemas import (get_supported_schemas, InvalidSchemaError,
load_schema)
from pygeometa.schemas.iso19139 import ISO19139OutputSchema
Expand Down Expand Up @@ -381,15 +381,15 @@ def test_validate_mcf(self):

mcf = read_mcf(get_abspath('../sample.yml'))

instance = json.loads(json.dumps(mcf, default=json_serial))
instance = json.loads(json_dumps(mcf))

is_valid = validate_mcf(instance)
assert is_valid

# validated nested MCF
mcf = read_mcf(get_abspath('./sample-child.yml'))

instance = json.loads(json.dumps(mcf, default=json_serial))
instance = json.loads(json_dumps(mcf))

is_valid = validate_mcf(instance)
assert is_valid
Expand Down

0 comments on commit 09f2e2b

Please sign in to comment.