Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unflatten: Allow datetimes in json output (convert them to str) #441

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.24.3]

### 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
Loading