Skip to content

Commit bde65a8

Browse files
committed
GH #32: Allow for non-string keys in dicts.
This fixes #32, where json5.dumps({1: "foo"}) didn't work correctly. It will now match the `json` module's output by casting 1 to a string, i.e., producing '{"1": "foo"}'. Note that this means that by default, you may produce ill-formed JSON in some caes, with duplicate keys if you mix numbers and strings. For example, {1: "foo", "1": "bar"} -> '{"1": "foo", "1": "bar"}'. This matches the `json` module's behavior, for better or worse.
1 parent c530c74 commit bde65a8

3 files changed

Lines changed: 137 additions & 78 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ and is 200x slower (or more) than the pure Python JSON module.
5252

5353
## Version History / Release Notes
5454

55+
* v0.9.0 (2020-01-30)
56+
* [GitHub issue #25](https://github.com/dpranke/pyjson5/issues/32):
57+
Allow for non-string keys in dicts.
58+
5559
* v0.8.5 (2019-07-04)
5660
* [GitHub issue #25](https://github.com/dpranke/pyjson5/issues/25):
5761
Add LICENSE and README.md to the dist.

‎json5/lib.py‎

Lines changed: 127 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -140,72 +140,109 @@ def _walk_ast(el, dictify, parse_float, parse_int, parse_constant):
140140
raise Exception('unknown el: ' + el) # pragma: no cover
141141

142142

143-
def dumps(obj, quote_keys=False, trailing_commas=True, **kwargs):
144-
"""Serialize ``obj`` to a JSON5-formatted ``str``.
143+
def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
144+
allow_nan=True, cls=None, indent=None, separators=None,
145+
default=None, sort_keys=False,
146+
quote_keys=False, trailing_commas=True,
147+
**kwargs):
148+
"""Serialize ``obj`` to a JSON5-formatted stream to ``fp`` (a ``.write()``-
149+
supporting file-like object).
145150
146151
Supports the same arguments as ``json.dumps()``, except that:
147152
148153
- The ``cls`` keyword is not supported.
149-
- The ``encoding`` keyword is ignored; Unicode strings are always returned.
154+
- The ``encoding`` keyword is ignored; Unicode strings are always written.
150155
- By default, object keys that are legal identifiers are not quoted;
151156
if you pass quote_keys=True, they will be.
152157
- By default, if lists and objects span multiple lines of output (i.e.,
153158
when ``indent`` >=0), the last item will have a trailing comma
154159
after it. If you pass ``trailing_commas=False, it will not.
155160
156-
Calling ``dumps(obj, quote_keys=True, trailing_commas=False)`` should
157-
produce exactly the same output as ``json.dumps(obj).``
161+
Calling ``dumps(obj, fp, quote_keys=True, trailing_commas=False)`` should
162+
produce exactly the same output as ``json.dumps(obj, fp).``
158163
"""
159164

160-
assert kwargs.get('cls', None) is None, 'Custom encoders are not supported'
165+
fp.write(str(dumps(obj, skipkeys, ensure_ascii, check_circular,
166+
allow_nan, indent, separators, default, sort_keys,
167+
quote_keys, trailing_commas)))
161168

162-
if kwargs.get('check_circular', True):
163-
seen = set()
164-
else:
165-
seen = None
166-
return _dumps(obj, seen, quote_keys=quote_keys,
167-
trailing_commas=trailing_commas,
168-
**kwargs)
169169

170-
171-
def dump(obj, fp, quote_keys=False, trailing_commas=True, **kwargs):
172-
"""Serialize ``obj`` to a JSON5-formatted stream to ``fp`` (a ``.write()``-
173-
supporting file-like object).
170+
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
171+
allow_nan=True, cls=None, indent=None, separators=None,
172+
default=None, sort_keys=False,
173+
quote_keys=False, trailing_commas=True,
174+
**kwargs):
175+
"""Serialize ``obj`` to a JSON5-formatted ``str``.
174176
175177
Supports the same arguments as ``json.dumps()``, except that:
176178
177179
- The ``cls`` keyword is not supported.
178-
- The ``encoding`` keyword is ignored; Unicode strings are always written.
180+
- The ``encoding`` keyword is ignored; Unicode strings are always returned.
179181
- By default, object keys that are legal identifiers are not quoted;
180182
if you pass quote_keys=True, they will be.
181183
- By default, if lists and objects span multiple lines of output (i.e.,
182184
when ``indent`` >=0), the last item will have a trailing comma
183185
after it. If you pass ``trailing_commas=False, it will not.
184186
185-
Calling ``dumps(obj, fp, quote_keys=True, trailing_commas=False)`` should
186-
produce exactly the same output as ``json.dumps(obj, fp).``
187+
Calling ``dumps(obj, quote_keys=True, trailing_commas=False)`` should
188+
produce exactly the same output as ``json.dumps(obj).``
187189
"""
188190

189-
s = dumps(obj, quote_keys=quote_keys, trailing_commas=trailing_commas,
190-
**kwargs)
191-
fp.write(str(s))
191+
assert kwargs.get('cls', None) is None, 'Custom encoders are not supported'
192192

193+
if separators is None:
194+
if indent is None:
195+
separators = (u', ', u': ')
196+
else:
197+
separators = (u',', u': ')
193198

194-
def _dumps(obj, seen, **kwargs):
199+
default = default or _raise_type_error
200+
201+
if check_circular:
202+
seen = set()
203+
else:
204+
seen = None
205+
206+
level = 1
207+
is_key = False
208+
209+
_, v = _dumps(obj, skipkeys, ensure_ascii, check_circular,
210+
allow_nan, indent, separators, default, sort_keys,
211+
quote_keys, trailing_commas, seen, level, is_key)
212+
return v
213+
214+
215+
def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent,
216+
separators, default, sort_keys,
217+
quote_keys, trailing_commas, seen, level, is_key):
218+
s = None
195219
if obj is True:
196-
return u'true'
220+
s = u'true'
197221
if obj is False:
198-
return u'false'
222+
s = u'false'
199223
if obj is None:
200-
return u'null'
224+
s = u'null'
201225

202226
t = type(obj)
203227
if t == type('') or t == type(u''):
204-
return _dump_str(obj, kwargs.get('ensure_ascii', True))
228+
if (is_key and _is_ident(obj) and not quote_keys
229+
and not _is_reserved_word(obj)):
230+
return True, obj
231+
return True, _dump_str(obj, ensure_ascii)
205232
if t is float:
206-
return _dump_float(obj, kwargs.get('allow_nan', True))
233+
s = _dump_float(obj,allow_nan)
207234
if t is int:
208-
return str(obj)
235+
s = str(obj)
236+
237+
if is_key:
238+
if s is not None:
239+
return True, '"%s"' % s
240+
if skipkeys:
241+
return False, None
242+
raise TypeError('invalid key %s' % repr(obj))
243+
244+
if s is not None:
245+
return True, s
209246

210247
if seen is not None:
211248
i = id(obj)
@@ -214,82 +251,101 @@ def _dumps(obj, seen, **kwargs):
214251
else:
215252
seen.add(i)
216253

217-
indent = kwargs.get('indent', None)
218-
if indent is None:
219-
separators = kwargs.get('separators', (u', ', u': '))
220-
else:
221-
separators = kwargs.get('separators', (u',', u': '))
222254
if indent is not None:
223-
level = kwargs.get('level', 1)
224-
nl = '\n'
255+
end_str = ''
256+
if trailing_commas:
257+
end_str = ','
225258
if type(indent) == int:
226259
if indent > 0:
227-
indent = ' ' * indent
260+
indent_str = '\n' + ' ' * indent * level
228261
else:
229-
indent = ''
262+
indent_str = '\n'
263+
else:
264+
indent_str = '\n' + indent * level
265+
end_str += '\n'
230266
else:
231-
indent = ''
232-
level = 0
233-
nl = ''
267+
indent_str = ''
268+
end_str = ''
234269

235270
item_sep, kv_sep = separators
236-
indent_str = nl + indent * level
237-
if nl and kwargs.get('trailing_commas', True):
238-
end_str = ',' + nl + indent * (level - 1)
239-
else:
240-
end_str = nl + indent * (level - 1)
241-
242271
item_sep += indent_str
243-
kwargs['level'] = level + 1
272+
level += 1
244273

245274
# In Python3, we'd check if this was an abc.Mapping.
246275
# For now, just check for the attrs we need to iterate over the object.
247276
if hasattr(t, 'keys') and hasattr(t, '__getitem__'):
248-
return _dump_dict(obj, seen, item_sep, kv_sep, indent_str, end_str,
249-
**kwargs)
277+
return False, _dump_dict(obj, skipkeys, ensure_ascii,
278+
check_circular, allow_nan, indent,
279+
separators, default, sort_keys,
280+
quote_keys, trailing_commas, seen, level,
281+
item_sep, kv_sep, indent_str, end_str)
282+
250283

251284
# In Python3, we'd check if this was an abc.Sequence.
252285
# For now, just check for the attrs we need to iterate over the object.
253286
if hasattr(t, '__getitem__') and hasattr(t, '__iter__'):
254-
if not obj:
255-
return u'[]'
256-
return (u'[' + indent_str +
257-
item_sep.join([_dumps(el, seen, **kwargs) for el in obj]) +
258-
end_str + u']')
287+
return False, _dump_array(obj, skipkeys, ensure_ascii,
288+
check_circular, allow_nan, indent,
289+
separators, default, sort_keys,
290+
quote_keys, trailing_commas, seen, level,
291+
item_sep, indent_str, end_str)
259292

260-
return kwargs.get('default', _raise_type_error)(obj)
293+
return False, default(obj)
261294

262295

263-
def _dump_dict(obj, seen, item_sep, kv_sep, indent_str, end_str, **kwargs):
296+
def _dump_dict(obj, skipkeys, ensure_ascii, check_circular, allow_nan,
297+
indent, separators, default, sort_keys,
298+
quote_keys, trailing_commas, seen, level,
299+
item_sep, kv_sep, indent_str, end_str):
264300
if not obj:
265301
return u'{}'
266302

267-
if kwargs.get('sort_keys', False):
303+
if sort_keys:
268304
keys = sorted(obj.keys())
269305
else:
270306
keys = obj.keys()
271307

272308
s = u'{' + indent_str
273309

274-
skipkeys = kwargs.get('skipkeys', False)
275-
ensure_ascii = kwargs.get('ensure_ascii', True)
276-
quote_keys = kwargs.get('quote_keys', False)
277-
first_item = True
310+
l = len(keys)
278311
for key in keys:
279-
valid_key, key_str = _dumpkey(key, ensure_ascii, quote_keys)
312+
valid_key, key_str = _dumps(key, skipkeys, ensure_ascii, check_circular,
313+
allow_nan, indent, separators, default,
314+
sort_keys,
315+
quote_keys, trailing_commas, seen, level,
316+
is_key=True)
280317
if valid_key:
281-
if not first_item:
318+
s += key_str + kv_sep + _dumps(obj[key], skipkeys, ensure_ascii,
319+
check_circular, allow_nan, indent,
320+
separators, default, sort_keys,
321+
quote_keys, trailing_commas,
322+
seen, level, is_key=False)[1]
323+
l -= 1
324+
if l:
282325
s += item_sep
283-
s += key_str + kv_sep + _dumps(obj[key], seen, **kwargs)
284-
first_item = False
285-
elif skipkeys:
286-
continue
287-
else:
326+
elif not skipkeys:
288327
raise TypeError('invalid key %s' % repr(key))
328+
else:
329+
l -= 1
330+
289331
s += end_str + u'}'
290332
return s
291333

292334

335+
def _dump_array(obj, skipkeys, ensure_ascii, check_circular, allow_nan,
336+
indent, separators, default, sort_keys,
337+
quote_keys, trailing_commas, seen, level,
338+
item_sep, indent_str, end_str):
339+
if not obj:
340+
return u'[]'
341+
return (u'[' + indent_str +
342+
item_sep.join([_dumps(el, skipkeys, ensure_ascii, check_circular,
343+
allow_nan, indent, separators, default,
344+
sort_keys, quote_keys, trailing_commas,
345+
seen, level, False)[1] for el in obj]) +
346+
end_str + u']')
347+
348+
293349
def _dump_float(obj, allow_nan):
294350
if allow_nan:
295351
if math.isnan(obj):
@@ -304,14 +360,6 @@ def _dump_float(obj, allow_nan):
304360
return str(obj)
305361

306362

307-
def _dumpkey(k, ensure_ascii, quote_keys):
308-
if type(k) in (int, float, type(''), long, type(u'')) or k is None:
309-
if not quote_keys and _is_ident(k) and not _is_reserved_word(k):
310-
return True, k
311-
return True, _dump_str(k, ensure_ascii)
312-
return False, ''
313-
314-
315363
def _dump_str(obj, ensure_ascii):
316364
ret = ['"']
317365
for ch in obj:
@@ -362,6 +410,7 @@ def _is_ident(k):
362410
return False
363411
return True
364412

413+
365414
def _is_id_start(ch):
366415
return unicodedata.category(ch) in (
367416
'Lu', 'Ll', 'Li', 'Lt', 'Lm', 'Lo', 'Nl')

‎tests/lib_test.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ def check(self, obj, s):
248248
def test_arrays(self):
249249
self.check([], '[]')
250250
self.check([1, 2, 3], '[1, 2, 3]')
251+
self.check([{'foo': 'bar'}, {'baz': 'quux'}],
252+
'[{foo: "bar"}, {baz: "quux"}]')
251253

252254
def test_bools(self):
253255
self.check(True, 'true')
@@ -320,6 +322,10 @@ def test_objects(self):
320322
def test_reserved_words_in_object_keys_are_quoted(self):
321323
self.check({'new': 1}, '{"new": 1}')
322324

325+
def test_non_string_keys(self):
326+
self.assertEqual(json5.dumps({False: 'a', 1: 'b', 2.0: 'c', None: 'd'}),
327+
'{"false": "a", "1": "b", "2.0": "c", "null": "d"}')
328+
323329
def test_quote_keys(self):
324330
self.assertEqual(json5.dumps({"foo": 1}, quote_keys=True),
325331
'{"foo": 1}')

0 commit comments

Comments
 (0)