Skip to content

Commit

Permalink
unflatten: Allow datetimes in json output (convert them to str)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Jul 4, 2024
1 parent 1d8b9e5 commit fed1789
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Fixed

- Don't error when there's a datetime in the header https://github.com/OpenDataServices/flatten-tool/issues/438

- Ignore null characters in the input CSV file when reading configuration from the header rows
https://github.com/OpenDataServices/flatten-tool/pull/446

Expand Down
19 changes: 14 additions & 5 deletions flattentool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import codecs
import datetime
import json
import sys
from collections import OrderedDict
Expand Down Expand Up @@ -192,12 +193,14 @@ def __float__(self):
return self


def decimal_default(o):
def decimal_datetime_default(o):
if isinstance(o, Decimal):
if int(o) == o:
return int(o)
else:
return NumberStr(o)
if isinstance(o, datetime.datetime):
return str(o)
raise TypeError(repr(o) + " is not JSON serializable")


Expand Down Expand Up @@ -372,20 +375,26 @@ def unflatten(
else:
if output_name is None:
print(
json.dumps(base, indent=4, default=decimal_default, ensure_ascii=False)
json.dumps(
base, indent=4, default=decimal_datetime_default, ensure_ascii=False
)
)
else:
with codecs.open(output_name, "w", encoding="utf-8") as fp:
json.dump(
base, fp, indent=4, default=decimal_default, ensure_ascii=False
base,
fp,
indent=4,
default=decimal_datetime_default,
ensure_ascii=False,
)
if cell_source_map:
with codecs.open(cell_source_map, "w", encoding="utf-8") as fp:
json.dump(
cell_source_map_data,
fp,
indent=4,
default=decimal_default,
default=decimal_datetime_default,
ensure_ascii=False,
)
if heading_source_map:
Expand All @@ -394,6 +403,6 @@ def unflatten(
heading_source_map_data,
fp,
indent=4,
default=decimal_default,
default=decimal_datetime_default,
ensure_ascii=False,
)
13 changes: 9 additions & 4 deletions flattentool/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime
import json
from decimal import Decimal

import pytest

from flattentool import decimal_default, unflatten
from flattentool import decimal_datetime_default, unflatten


def original_cell_and_row_locations(data):
Expand Down Expand Up @@ -51,9 +52,13 @@ def original_headings(heading_data):
return headings


def test_decimal_default():
assert json.dumps(Decimal("1.2"), default=decimal_default) == "1.2"
assert json.dumps(Decimal("42"), default=decimal_default) == "42"
def test_decimal_datetime_default():
assert json.dumps(Decimal("1.2"), default=decimal_datetime_default) == "1.2"
assert json.dumps(Decimal("42"), default=decimal_datetime_default) == "42"
assert (
json.dumps(datetime.datetime(2024, 1, 1), default=decimal_datetime_default)
== '"2024-01-01 00:00:00"'
)


def lines_strip_whitespace(text):
Expand Down

0 comments on commit fed1789

Please sign in to comment.