1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import dataclasses
import datetime
import uuid
import pytest
import orjson
try:
import pytz
except ImportError:
pytz = None # type: ignore
try:
import numpy
except ImportError:
numpy = None # type: ignore
class SubStr(str):
pass
class TestNonStrKeyTests:
def test_dict_keys_duplicate(self):
"""
OPT_NON_STR_KEYS serializes duplicate keys
"""
assert (
orjson.dumps({"1": True, 1: False}, option=orjson.OPT_NON_STR_KEYS)
== b'{"1":true,"1":false}'
)
def test_dict_keys_int(self):
assert (
orjson.dumps({1: True, 2: False}, option=orjson.OPT_NON_STR_KEYS)
== b'{"1":true,"2":false}'
)
def test_dict_keys_substr(self):
assert (
orjson.dumps({SubStr("aaa"): True}, option=orjson.OPT_NON_STR_KEYS)
== b'{"aaa":true}'
)
def test_dict_keys_substr_passthrough(self):
"""
OPT_PASSTHROUGH_SUBCLASS does not affect OPT_NON_STR_KEYS
"""
assert (
orjson.dumps(
{SubStr("aaa"): True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_PASSTHROUGH_SUBCLASS,
)
== b'{"aaa":true}'
)
def test_dict_keys_substr_invalid(self):
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps({SubStr("\ud800"): True}, option=orjson.OPT_NON_STR_KEYS)
def test_dict_keys_strict(self):
"""
OPT_NON_STR_KEYS does not respect OPT_STRICT_INTEGER
"""
assert (
orjson.dumps(
{9223372036854775807: True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER,
)
== b'{"9223372036854775807":true}'
)
def test_dict_keys_int_range_valid_i64(self):
"""
OPT_NON_STR_KEYS has a i64 range for int, valid
"""
assert (
orjson.dumps(
{9223372036854775807: True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER,
)
== b'{"9223372036854775807":true}'
)
assert (
orjson.dumps(
{-9223372036854775807: True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER,
)
== b'{"-9223372036854775807":true}'
)
assert (
orjson.dumps(
{9223372036854775809: True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER,
)
== b'{"9223372036854775809":true}'
)
def test_dict_keys_int_range_valid_u64(self):
"""
OPT_NON_STR_KEYS has a u64 range for int, valid
"""
assert (
orjson.dumps(
{0: True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER,
)
== b'{"0":true}'
)
assert (
orjson.dumps(
{18446744073709551615: True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER,
)
== b'{"18446744073709551615":true}'
)
def test_dict_keys_int_range_invalid(self):
"""
OPT_NON_STR_KEYS has a range of i64::MIN to u64::MAX
"""
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps({-9223372036854775809: True}, option=orjson.OPT_NON_STR_KEYS)
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps({18446744073709551616: True}, option=orjson.OPT_NON_STR_KEYS)
def test_dict_keys_float(self):
assert (
orjson.dumps({1.1: True, 2.2: False}, option=orjson.OPT_NON_STR_KEYS)
== b'{"1.1":true,"2.2":false}'
)
def test_dict_keys_inf(self):
assert (
orjson.dumps({float("Infinity"): True}, option=orjson.OPT_NON_STR_KEYS)
== b'{"null":true}'
)
assert (
orjson.dumps({float("-Infinity"): True}, option=orjson.OPT_NON_STR_KEYS)
== b'{"null":true}'
)
def test_dict_keys_nan(self):
assert (
orjson.dumps({float("NaN"): True}, option=orjson.OPT_NON_STR_KEYS)
== b'{"null":true}'
)
def test_dict_keys_bool(self):
assert (
orjson.dumps({True: True, False: False}, option=orjson.OPT_NON_STR_KEYS)
== b'{"true":true,"false":false}'
)
def test_dict_keys_datetime(self):
assert (
orjson.dumps(
{datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True},
option=orjson.OPT_NON_STR_KEYS,
)
== b'{"2000-01-01T02:03:04.000123":true}'
)
def test_dict_keys_datetime_opt(self):
assert (
orjson.dumps(
{datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True},
option=orjson.OPT_NON_STR_KEYS
| orjson.OPT_OMIT_MICROSECONDS
| orjson.OPT_NAIVE_UTC
| orjson.OPT_UTC_Z,
)
== b'{"2000-01-01T02:03:04Z":true}'
)
def test_dict_keys_datetime_passthrough(self):
"""
OPT_PASSTHROUGH_DATETIME does not affect OPT_NON_STR_KEYS
"""
assert (
orjson.dumps(
{datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_PASSTHROUGH_DATETIME,
)
== b'{"2000-01-01T02:03:04.000123":true}'
)
def test_dict_keys_uuid(self):
"""
OPT_NON_STR_KEYS always serializes UUID as keys
"""
assert (
orjson.dumps(
{uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece"): True},
option=orjson.OPT_NON_STR_KEYS,
)
== b'{"7202d115-7ff3-4c81-a7c1-2a1f067b1ece":true}'
)
def test_dict_keys_date(self):
assert (
orjson.dumps(
{datetime.date(1970, 1, 1): True}, option=orjson.OPT_NON_STR_KEYS
)
== b'{"1970-01-01":true}'
)
def test_dict_keys_time(self):
assert (
orjson.dumps(
{datetime.time(12, 15, 59, 111): True},
option=orjson.OPT_NON_STR_KEYS,
)
== b'{"12:15:59.000111":true}'
)
def test_dict_non_str_and_sort_keys(self):
assert (
orjson.dumps(
{
"other": 1,
datetime.date(1970, 1, 5): 2,
datetime.date(1970, 1, 3): 3,
},
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS,
)
== b'{"1970-01-03":3,"1970-01-05":2,"other":1}'
)
@pytest.mark.skipif(pytz is None, reason="pytz optional")
def test_dict_keys_time_err(self):
"""
OPT_NON_STR_KEYS propagates errors in types
"""
val = datetime.time(12, 15, 59, 111, tzinfo=pytz.timezone("Asia/Shanghai"))
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS)
def test_dict_keys_str(self):
assert (
orjson.dumps({"1": True}, option=orjson.OPT_NON_STR_KEYS) == b'{"1":true}'
)
def test_dict_keys_type(self):
class Obj:
a: str
val = Obj()
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS)
@pytest.mark.skipif(numpy is None, reason="numpy is not installed")
def test_dict_keys_array(self):
with pytest.raises(TypeError):
{numpy.array([1, 2]): True}
def test_dict_keys_dataclass(self):
@dataclasses.dataclass
class Dataclass:
a: str
with pytest.raises(TypeError):
{Dataclass("a"): True}
def test_dict_keys_dataclass_hash(self):
@dataclasses.dataclass
class Dataclass:
a: str
def __hash__(self):
return 1
obj = {Dataclass("a"): True}
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS)
def test_dict_keys_list(self):
with pytest.raises(TypeError):
{[]: True}
def test_dict_keys_dict(self):
with pytest.raises(TypeError):
{{}: True}
def test_dict_keys_tuple(self):
obj = {(): True}
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS)
def test_dict_keys_unknown(self):
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps({frozenset(): True}, option=orjson.OPT_NON_STR_KEYS)
def test_dict_keys_no_str_call(self):
class Obj:
a: str
def __str__(self):
return "Obj"
val = Obj()
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS)
|