Skip to content

Commit c531915

Browse files
committed
Now handling NaN and Infinity
1 parent 6bffe13 commit c531915

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Json Urley provides a tight standard for converting json objects to and from URL
3434
* The value `false` implies the type is boolean: `?a=false => {"a": false}
3535
* If the value can be parsed as an integer, it is an integer number: `?a=1 => {"a": 1}`
3636
* If the value can be parsed as a float, it is a floating point number: `?a=1.0 => {"a": 1.0}`
37+
* If the value is `NaN`, `Infinity` or `-Infinity` it is one of these numbers.
3738
* The value is a string
3839
8. Duplicate keys mean the presence of an array at the end of the path. *This makes the ordering of elements
3940
significant!*

json_urley/__init__.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from decimal import Decimal
23
from typing import Dict, List, Iterator, Tuple
34
from urllib.parse import urlencode, parse_qsl
@@ -107,7 +108,15 @@ def _generate_query_params(
107108
yield key, json_obj
108109
elif isinstance(json_obj, (int, float, Decimal)):
109110
key = ".".join(current_param)
110-
yield key, str(json_obj)
111+
if math.isnan(json_obj):
112+
value = "NaN"
113+
elif json_obj == math.inf:
114+
value = "Infinity"
115+
elif json_obj == -math.inf:
116+
value = "-Infinity"
117+
else:
118+
value = str(json_obj)
119+
yield key, value
111120
elif isinstance(json_obj, str):
112121
yield from _generate_query_params_for_str(json_obj, current_param)
113122
else:

tests/test_json_urley.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import json
2+
import math
13
from datetime import datetime
24
from unittest import TestCase
35

4-
import tests
56
from json_urley import query_str_to_json_obj, json_obj_to_query_str, JsonUrleyError
67

78

@@ -400,3 +401,32 @@ def test_error_path_3(self):
400401
query_str = "value=1&value.child.grandchild=2"
401402
with self.assertRaises(JsonUrleyError):
402403
query_str_to_json_obj(query_str)
404+
405+
def test_uuid_convert(self):
406+
json_obj = {"id": "1194739a-8e75-47c1-98dd-887055a560c6"}
407+
query_str = json_obj_to_query_str(json_obj)
408+
expected_query_str = "id=1194739a-8e75-47c1-98dd-887055a560c6"
409+
self.assertEqual(query_str, expected_query_str)
410+
result = query_str_to_json_obj(query_str)
411+
self.assertEqual(json_obj, result)
412+
413+
def test_special_number_str(self):
414+
numbers = ["NaN", "Infinity", "-Infinity"]
415+
for n in numbers:
416+
json_obj = {"id": n}
417+
query_str = json_obj_to_query_str(json_obj)
418+
expected_query_str = f"id~s={n}"
419+
self.assertEqual(query_str, expected_query_str)
420+
result = query_str_to_json_obj(query_str)
421+
self.assertEqual(json_obj, result)
422+
423+
def test_special_number(self):
424+
numbers = [math.nan, math.inf, -math.inf]
425+
strings = ["NaN", "Infinity", "-Infinity"]
426+
for n, s in zip(numbers, strings):
427+
json_obj = {"id": n}
428+
query_str = json_obj_to_query_str(json_obj)
429+
expected_query_str = f"id={s}"
430+
self.assertEqual(query_str, expected_query_str)
431+
result = query_str_to_json_obj(query_str)
432+
self.assertEqual(json.dumps(json_obj), json.dumps(result))

0 commit comments

Comments
 (0)