Skip to content

Commit 035bcea

Browse files
Merge pull request #374 from dalito/issue2557-jsonDumper-dates
Handle date & datetime in JSONDumper.dumps
2 parents f8e79f9 + 05d61a5 commit 035bcea

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

linkml_runtime/dumpers/json_dumper.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from datetime import datetime, date
23
from decimal import Decimal
34
from typing import Dict, Union
45
from pydantic import BaseModel
@@ -56,6 +57,8 @@ def default(o):
5657
elif isinstance(o, Decimal):
5758
# https://stackoverflow.com/questions/1960516/python-json-serialize-a-decimal-object
5859
return str(o)
60+
elif isinstance(o, (datetime, date)):
61+
return str(o)
5962
else:
6063
return json.JSONDecoder().decode(o)
6164
if isinstance(element, BaseModel):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"aliases": [],
3+
"id": "P01",
4+
"name": "John Doe",
5+
"has_birth_event": {
6+
"started_at_time": "2021-01-01",
7+
"ended_at_time": "2021-01-02"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id: P01
2+
name: John Doe
3+
has_birth_event:
4+
started_at_time: 2021-01-01
5+
ended_at_time: 2021-01-02

tests/test_loaders_dumpers/test_dumpers_pydantic.py

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from linkml_runtime.dumpers import yaml_dumper, json_dumper, csv_dumper
66
from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase
77
from tests.test_loaders_dumpers.models.books_normalized_pydantic import Book, BookSeries, Author
8+
from tests.test_loaders_dumpers.models.kitchen_sink_pydantic import BirthEvent, Person
89
from linkml_runtime.utils.formatutils import remove_empty_items
910

1011

@@ -51,3 +52,35 @@ def test_json_dumper(self):
5152
'inStock' not in data['books'][i].keys()
5253
'creator' not in data['books'][i].keys()
5354
self.assertEqual(data, remove_empty_items(self.bookseries.dict()))
55+
56+
57+
class PydanticDumpersDateTestCase(LoaderDumperTestCase):
58+
59+
@classmethod
60+
def setUpClass(cls) -> None:
61+
""" Generate an example with a date for testing purposes """
62+
LoaderDumperTestCase.setUpClass()
63+
b1 = BirthEvent(started_at_time="2021-01-01", ended_at_time="2021-01-02")
64+
65+
cls.person = Person(
66+
id="P01",
67+
name='John Doe',
68+
has_birth_event=b1
69+
)
70+
71+
def test_yaml_dumper(self):
72+
""" Test the yaml emitter """
73+
# with open(self.env.input_path('kitchen_sink_person_01.yaml'), 'w', encoding='utf-8') as f:
74+
# # write the yaml file
75+
# f.write(yaml_dumper.dumps(self.person))
76+
# with open(self.env.input_path('kitchen_sink_person_01.json'), 'w', encoding='utf-8') as f:
77+
# # write the json file
78+
# f.write(json_dumper.dumps(self.person, inject_type=False))
79+
80+
self.dump_test('kitchen_sink_person_01.yaml', lambda out_fname: yaml_dumper.dump(self.person, out_fname))
81+
self.dumps_test('kitchen_sink_person_01.yaml', lambda: yaml_dumper.dumps(self.person))
82+
83+
def test_json_dumper(self):
84+
""" Test the json emitter """
85+
self.dump_test('kitchen_sink_person_01.json', lambda out_fname: json_dumper.dump(self.person, out_fname))
86+
self.dumps_test('kitchen_sink_person_01.json', lambda: json_dumper.dumps(self.person))

0 commit comments

Comments
 (0)