Skip to content

Commit

Permalink
Merge pull request #696 from wireservice/jsonkey
Browse files Browse the repository at this point in the history
Fix for "TypeError: key Decimal(…) is not a string"
  • Loading branch information
nbedi committed Nov 26, 2017
2 parents c58e129 + 24bfe88 commit 63bcc45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion agate/table/to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def dump_json(data):
if key_is_row_function:
k = key(row)
else:
k = row[key]
k = str(row[key]) if six.PY3 else unicode(row[key])

if k in output:
raise ValueError('Value %s is not unique in the key column.' % six.text_type(k))
Expand Down
26 changes: 26 additions & 0 deletions examples/test_non_string_keyed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"1": {
"number": 1.0,
"text": "a",
"boolean": true,
"date": "2015-11-04",
"datetime": "2015-11-04T12:22:00",
"timedelta": "0:04:15"
},
"2": {
"number": 2.0,
"text": "👍",
"boolean": false,
"date": "2015-11-05",
"datetime": "2015-11-04T12:45:00",
"timedelta": "0:06:18"
},
"None": {
"number": null,
"text": "b",
"boolean": null,
"date": null,
"datetime": null,
"timedelta": null
}
}
13 changes: 13 additions & 0 deletions tests/test_table/test_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ def test_to_json_key(self):

self.assertEqual(js1, js2)

def test_to_json_non_string_key(self):
table = Table(self.rows, self.column_names, self.column_types)

output = six.StringIO()
table.to_json(output, key='number', indent=4)

js1 = json.loads(output.getvalue())

with open('examples/test_non_string_keyed.json') as f:
js2 = json.load(f)

self.assertEqual(js1, js2)

def test_to_json_key_func(self):
table = Table(self.rows, self.column_names, self.column_types)

Expand Down

0 comments on commit 63bcc45

Please sign in to comment.