forked from materialsvirtuallab/monty
/
test_json.py
643 lines (536 loc) · 22.1 KB
/
test_json.py
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
__version__ = "0.1"
import datetime
import json
import os
import unittest
from enum import Enum
import numpy as np
import pandas as pd
from bson.objectid import ObjectId
from monty.json import MontyDecoder, MontyEncoder, MSONable, _load_redirect, jsanitize
from . import __version__ as tests_version
test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_files")
class GoodMSONClass(MSONable):
def __init__(self, a, b, c, d=1, *values, **kwargs):
self.a = a
self.b = b
self._c = c
self._d = d
self.values = values
self.kwargs = kwargs
def __eq__(self, other):
return (
self.a == other.a
and self.b == other.b
and self._c == other._c
and self._d == other._d
and self.kwargs == other.kwargs
and self.values == other.values
)
class GoodNestedMSONClass(MSONable):
def __init__(self, a_list, b_dict, c_list_dict_list, **kwargs):
assert isinstance(a_list, list)
assert isinstance(b_dict, dict)
assert isinstance(c_list_dict_list, list)
assert isinstance(c_list_dict_list[0], dict)
first_key = list(c_list_dict_list[0].keys())[0]
assert isinstance(c_list_dict_list[0][first_key], list)
self.a_list = a_list
self.b_dict = b_dict
self._c_list_dict_list = c_list_dict_list
self.kwargs = kwargs
class MethodSerializationClass(MSONable):
def __init__(self, a):
self.a = a
def method(self):
pass
@staticmethod
def staticmethod(self):
pass
@classmethod
def classmethod(cls):
pass
def __call__(self, b):
# override call for instances
return self.__class__(b)
class NestedClass:
def inner_method(self):
pass
class MethodNonSerializationClass:
def __init__(self, a):
self.a = a
def method(self):
pass
def my_callable(a, b):
return a + b
class EnumTest(MSONable, Enum):
a = 1
b = 2
class ClassContainingDataFrame(MSONable):
def __init__(self, df):
self.df = df
class ClassContainingSeries(MSONable):
def __init__(self, s):
self.s = s
class ClassContainingNumpyArray(MSONable):
def __init__(self, np_a):
self.np_a = np_a
class MSONableTest(unittest.TestCase):
def setUp(self):
self.good_cls = GoodMSONClass
class BadMSONClass(MSONable):
def __init__(self, a, b):
self.a = a
self.b = b
def as_dict(self):
d = {"init": {"a": self.a, "b": self.b}}
return d
self.bad_cls = BadMSONClass
class BadMSONClass2(MSONable):
def __init__(self, a, b):
self.a = a
self.c = b
self.bad_cls2 = BadMSONClass2
class AutoMSON(MSONable):
def __init__(self, a, b):
self.a = a
self.b = b
self.auto_mson = AutoMSON
def test_to_from_dict(self):
obj = self.good_cls("Hello", "World", "Python")
d = obj.as_dict()
self.assertIsNotNone(d)
self.good_cls.from_dict(d)
jsonstr = obj.to_json()
d = json.loads(jsonstr)
self.assertTrue(d["@class"], "GoodMSONClass")
obj = self.bad_cls("Hello", "World")
d = obj.as_dict()
self.assertIsNotNone(d)
self.assertRaises(TypeError, self.bad_cls.from_dict, d)
obj = self.bad_cls2("Hello", "World")
self.assertRaises(NotImplementedError, obj.as_dict)
obj = self.auto_mson(2, 3)
d = obj.as_dict()
self.auto_mson.from_dict(d)
def test_unsafe_hash(self):
GMC = GoodMSONClass
a_list = [GMC(1, 1.0, "one"), GMC(2, 2.0, "two")]
b_dict = {"first": GMC(3, 3.0, "three"), "second": GMC(4, 4.0, "four")}
c_list_dict_list = [
{
"list1": [
GMC(5, 5.0, "five"),
GMC(6, 6.0, "six"),
GMC(7, 7.0, "seven"),
],
"list2": [GMC(8, 8.0, "eight")],
},
{
"list3": [
GMC(9, 9.0, "nine"),
GMC(10, 10.0, "ten"),
GMC(11, 11.0, "eleven"),
GMC(12, 12.0, "twelve"),
],
"list4": [GMC(13, 13.0, "thirteen"), GMC(14, 14.0, "fourteen")],
"list5": [GMC(15, 15.0, "fifteen")],
},
]
obj = GoodNestedMSONClass(a_list=a_list, b_dict=b_dict, c_list_dict_list=c_list_dict_list)
self.assertEqual(
a_list[0].unsafe_hash().hexdigest(),
"ea44de0e2ef627be582282c02c48e94de0d58ec6",
)
self.assertEqual(obj.unsafe_hash().hexdigest(), "44204c8da394e878f7562c9aa2e37c2177f28b81")
def test_version(self):
obj = self.good_cls("Hello", "World", "Python")
d = obj.as_dict()
self.assertEqual(d["@version"], tests_version)
def test_nested_to_from_dict(self):
GMC = GoodMSONClass
a_list = [GMC(1, 1.0, "one"), GMC(2, 2.0, "two")]
b_dict = {"first": GMC(3, 3.0, "three"), "second": GMC(4, 4.0, "four")}
c_list_dict_list = [
{
"list1": [
GMC(5, 5.0, "five"),
GMC(6, 6.0, "six"),
GMC(7, 7.0, "seven"),
],
"list2": [GMC(8, 8.0, "eight")],
},
{
"list3": [
GMC(9, 9.0, "nine"),
GMC(10, 10.0, "ten"),
GMC(11, 11.0, "eleven"),
GMC(12, 12.0, "twelve"),
],
"list4": [GMC(13, 13.0, "thirteen"), GMC(14, 14.0, "fourteen")],
"list5": [GMC(15, 15.0, "fifteen")],
},
]
obj = GoodNestedMSONClass(a_list=a_list, b_dict=b_dict, c_list_dict_list=c_list_dict_list)
obj_dict = obj.as_dict()
obj2 = GoodNestedMSONClass.from_dict(obj_dict)
self.assertTrue([obj2.a_list[ii] == aa for ii, aa in enumerate(obj.a_list)])
self.assertTrue([obj2.b_dict[kk] == val for kk, val in obj.b_dict.items()])
self.assertEqual(len(obj.a_list), len(obj2.a_list))
self.assertEqual(len(obj.b_dict), len(obj2.b_dict))
s = json.dumps(obj_dict)
obj3 = json.loads(s, cls=MontyDecoder)
self.assertTrue([obj2.a_list[ii] == aa for ii, aa in enumerate(obj3.a_list)])
self.assertTrue([obj2.b_dict[kk] == val for kk, val in obj3.b_dict.items()])
self.assertEqual(len(obj3.a_list), len(obj2.a_list))
self.assertEqual(len(obj3.b_dict), len(obj2.b_dict))
s = json.dumps(obj, cls=MontyEncoder)
obj4 = json.loads(s, cls=MontyDecoder)
self.assertTrue([obj4.a_list[ii] == aa for ii, aa in enumerate(obj.a_list)])
self.assertTrue([obj4.b_dict[kk] == val for kk, val in obj.b_dict.items()])
self.assertEqual(len(obj.a_list), len(obj4.a_list))
self.assertEqual(len(obj.b_dict), len(obj4.b_dict))
def test_enum_serialization(self):
e = EnumTest.a
d = e.as_dict()
e_new = EnumTest.from_dict(d)
self.assertEqual(e_new.name, e.name)
self.assertEqual(e_new.value, e.value)
d = {"123": EnumTest.a}
f = jsanitize(d)
self.assertEqual(f["123"], "EnumTest.a")
f = jsanitize(d, strict=True)
self.assertEqual(f["123"]["@module"], "tests.test_json")
self.assertEqual(f["123"]["@class"], "EnumTest")
self.assertEqual(f["123"]["value"], 1)
f = jsanitize(d, strict=True, enum_values=True)
self.assertEqual(f["123"], 1)
f = jsanitize(d, enum_values=True)
self.assertEqual(f["123"], 1)
class JsonTest(unittest.TestCase):
def test_as_from_dict(self):
obj = GoodMSONClass(1, 2, 3, hello="world")
s = json.dumps(obj, cls=MontyEncoder)
obj2 = json.loads(s, cls=MontyDecoder)
self.assertEqual(obj2.a, 1)
self.assertEqual(obj2.b, 2)
self.assertEqual(obj2._c, 3)
self.assertEqual(obj2._d, 1)
self.assertEqual(obj2.kwargs, {"hello": "world", "values": []})
obj = GoodMSONClass(obj, 2, 3)
s = json.dumps(obj, cls=MontyEncoder)
obj2 = json.loads(s, cls=MontyDecoder)
self.assertEqual(obj2.a.a, 1)
self.assertEqual(obj2.b, 2)
self.assertEqual(obj2._c, 3)
self.assertEqual(obj2._d, 1)
listobj = [obj, obj2]
s = json.dumps(listobj, cls=MontyEncoder)
listobj2 = json.loads(s, cls=MontyDecoder)
self.assertEqual(listobj2[0].a.a, 1)
def test_datetime(self):
dt = datetime.datetime.now()
jsonstr = json.dumps(dt, cls=MontyEncoder)
d = json.loads(jsonstr, cls=MontyDecoder)
self.assertEqual(type(d), datetime.datetime)
self.assertEqual(dt, d)
# Test a nested datetime.
a = {"dt": dt, "a": 1}
jsonstr = json.dumps(a, cls=MontyEncoder)
d = json.loads(jsonstr, cls=MontyDecoder)
self.assertEqual(type(d["dt"]), datetime.datetime)
def test_uuid(self):
from uuid import UUID, uuid4
uuid = uuid4()
jsonstr = json.dumps(uuid, cls=MontyEncoder)
d = json.loads(jsonstr, cls=MontyDecoder)
self.assertEqual(type(d), UUID)
self.assertEqual(uuid, d)
# Test a nested UUID.
a = {"uuid": uuid, "a": 1}
jsonstr = json.dumps(a, cls=MontyEncoder)
d = json.loads(jsonstr, cls=MontyDecoder)
self.assertEqual(type(d["uuid"]), UUID)
def test_numpy(self):
x = np.array([1, 2, 3], dtype="int64")
self.assertRaises(TypeError, json.dumps, x)
djson = json.dumps(x, cls=MontyEncoder)
d = json.loads(djson)
self.assertEqual(d["@class"], "array")
self.assertEqual(d["@module"], "numpy")
self.assertEqual(d["data"], [1, 2, 3])
self.assertEqual(d["dtype"], "int64")
x = json.loads(djson, cls=MontyDecoder)
self.assertEqual(type(x), np.ndarray)
x = np.min([1, 2, 3]) > 2
self.assertRaises(TypeError, json.dumps, x)
x = np.array([1 + 1j, 2 + 1j, 3 + 1j], dtype="complex64")
self.assertRaises(TypeError, json.dumps, x)
djson = json.dumps(x, cls=MontyEncoder)
d = json.loads(djson)
self.assertEqual(d["@class"], "array")
self.assertEqual(d["@module"], "numpy")
self.assertEqual(d["data"], [[1.0, 2.0, 3.0], [1.0, 1.0, 1.0]])
self.assertEqual(d["dtype"], "complex64")
x = json.loads(djson, cls=MontyDecoder)
self.assertEqual(type(x), np.ndarray)
self.assertEqual(x.dtype, "complex64")
x = np.array([[1 + 1j, 2 + 1j], [3 + 1j, 4 + 1j]], dtype="complex64")
self.assertRaises(TypeError, json.dumps, x)
djson = json.dumps(x, cls=MontyEncoder)
d = json.loads(djson)
self.assertEqual(d["@class"], "array")
self.assertEqual(d["@module"], "numpy")
self.assertEqual(d["data"], [[[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]])
self.assertEqual(d["dtype"], "complex64")
x = json.loads(djson, cls=MontyDecoder)
self.assertEqual(type(x), np.ndarray)
self.assertEqual(x.dtype, "complex64")
x = {"energies": [np.float64(1234.5)]}
d = jsanitize(x, strict=True)
assert type(d["energies"][0]) == float
# Test data nested in a class
x = np.array([[1 + 1j, 2 + 1j], [3 + 1j, 4 + 1j]], dtype="complex64")
cls = ClassContainingNumpyArray(np_a={"a": [{"b": x}]})
d = json.loads(json.dumps(cls, cls=MontyEncoder))
self.assertEqual(d["np_a"]["a"][0]["b"]["@module"], "numpy")
self.assertEqual(d["np_a"]["a"][0]["b"]["@class"], "array")
self.assertEqual(d["np_a"]["a"][0]["b"]["data"], [[[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]])
self.assertEqual(d["np_a"]["a"][0]["b"]["dtype"], "complex64")
obj = ClassContainingNumpyArray.from_dict(d)
self.assertIsInstance(obj, ClassContainingNumpyArray)
self.assertIsInstance(obj.np_a["a"][0]["b"], np.ndarray)
self.assertEqual(obj.np_a["a"][0]["b"][0][1], 2 + 1j)
def test_pandas(self):
cls = ClassContainingDataFrame(df=pd.DataFrame([{"a": 1, "b": 1}, {"a": 1, "b": 2}]))
d = json.loads(MontyEncoder().encode(cls))
self.assertEqual(d["df"]["@module"], "pandas")
self.assertEqual(d["df"]["@class"], "DataFrame")
obj = ClassContainingDataFrame.from_dict(d)
self.assertIsInstance(obj, ClassContainingDataFrame)
self.assertIsInstance(obj.df, pd.DataFrame)
self.assertEqual(list(obj.df.a), [1, 1])
cls = ClassContainingSeries(s=pd.Series({"a": [1, 2, 3], "b": [4, 5, 6]}))
d = json.loads(MontyEncoder().encode(cls))
self.assertEqual(d["s"]["@module"], "pandas")
self.assertEqual(d["s"]["@class"], "Series")
obj = ClassContainingSeries.from_dict(d)
self.assertIsInstance(obj, ClassContainingSeries)
self.assertIsInstance(obj.s, pd.Series)
self.assertEqual(list(obj.s.a), [1, 2, 3])
cls = ClassContainingSeries(s={"df": [pd.Series({"a": [1, 2, 3], "b": [4, 5, 6]})]})
d = json.loads(MontyEncoder().encode(cls))
self.assertEqual(d["s"]["df"][0]["@module"], "pandas")
self.assertEqual(d["s"]["df"][0]["@class"], "Series")
obj = ClassContainingSeries.from_dict(d)
self.assertIsInstance(obj, ClassContainingSeries)
self.assertIsInstance(obj.s["df"][0], pd.Series)
self.assertEqual(list(obj.s["df"][0].a), [1, 2, 3])
def test_callable(self):
instance = MethodSerializationClass(a=1)
for function in [
# builtins
str,
list,
sum,
open,
# functions
os.path.join,
my_callable,
# unbound methods
MethodSerializationClass.NestedClass.inner_method,
MethodSerializationClass.staticmethod,
instance.staticmethod,
# methods bound to classes
MethodSerializationClass.classmethod,
instance.classmethod,
# classes
MethodSerializationClass,
Enum,
]:
self.assertRaises(TypeError, json.dumps, function)
djson = json.dumps(function, cls=MontyEncoder)
d = json.loads(djson)
self.assertTrue("@callable" in d)
self.assertTrue("@module" in d)
x = json.loads(djson, cls=MontyDecoder)
self.assertEqual(x, function)
# test method bound to instance
for function in [instance.method]:
self.assertRaises(TypeError, json.dumps, function)
djson = json.dumps(function, cls=MontyEncoder)
d = json.loads(djson)
self.assertTrue("@callable" in d)
self.assertTrue("@module" in d)
x = json.loads(djson, cls=MontyDecoder)
# can't just check functions are equal as the instance the function is bound
# to will be different. Instead, we check that the serialized instance
# is the same, and that the function qualname is the same
self.assertEqual(x.__qualname__, function.__qualname__)
self.assertEqual(x.__self__.as_dict(), function.__self__.as_dict())
# test method bound to object that is not serializable
for function in [MethodNonSerializationClass(1).method]:
self.assertRaises(TypeError, json.dumps, function, cls=MontyEncoder)
# test that callable MSONable objects still get serialized as the objects
# rather than as a callable
djson = json.dumps(instance, cls=MontyEncoder)
self.assertTrue("@class" in djson)
def test_objectid(self):
oid = ObjectId("562e8301218dcbbc3d7d91ce")
self.assertRaises(TypeError, json.dumps, oid)
djson = json.dumps(oid, cls=MontyEncoder)
x = json.loads(djson, cls=MontyDecoder)
self.assertEqual(type(x), ObjectId)
def test_jsanitize(self):
# clean_json should have no effect on None types.
d = {"hello": 1, "world": None}
clean = jsanitize(d)
self.assertIsNone(clean["world"])
self.assertEqual(json.loads(json.dumps(d)), json.loads(json.dumps(clean)))
d = {"hello": GoodMSONClass(1, 2, 3)}
self.assertRaises(TypeError, json.dumps, d)
clean = jsanitize(d)
self.assertIsInstance(clean["hello"], str)
clean_strict = jsanitize(d, strict=True)
self.assertEqual(clean_strict["hello"]["a"], 1)
self.assertEqual(clean_strict["hello"]["b"], 2)
clean_recursive_msonable = jsanitize(d, recursive_msonable=True)
self.assertEqual(clean_recursive_msonable["hello"]["a"], 1)
self.assertEqual(clean_recursive_msonable["hello"]["b"], 2)
d = {"dt": datetime.datetime.now()}
clean = jsanitize(d)
self.assertIsInstance(clean["dt"], str)
clean = jsanitize(d, allow_bson=True)
self.assertIsInstance(clean["dt"], datetime.datetime)
d = {
"a": ["b", np.array([1, 2, 3])],
"b": ObjectId.from_datetime(datetime.datetime.now()),
}
clean = jsanitize(d)
self.assertEqual(clean["a"], ["b", [1, 2, 3]])
self.assertIsInstance(clean["b"], str)
rnd_bin = bytes(np.random.rand(10))
d = {"a": bytes(rnd_bin)}
clean = jsanitize(d, allow_bson=True)
self.assertEqual(clean["a"], bytes(rnd_bin))
self.assertIsInstance(clean["a"], bytes)
# test jsanitizing callables (including classes)
instance = MethodSerializationClass(a=1)
for function in [
# builtins
str,
list,
sum,
open,
# functions
os.path.join,
my_callable,
# unbound methods
MethodSerializationClass.NestedClass.inner_method,
MethodSerializationClass.staticmethod,
instance.staticmethod,
# methods bound to classes
MethodSerializationClass.classmethod,
instance.classmethod,
# classes
MethodSerializationClass,
Enum,
]:
d = {"f": function}
clean = jsanitize(d)
self.assertTrue("@module" in clean["f"])
self.assertTrue("@callable" in clean["f"])
# test method bound to instance
for function in [instance.method]:
d = {"f": function}
clean = jsanitize(d)
self.assertTrue("@module" in clean["f"])
self.assertTrue("@callable" in clean["f"])
self.assertTrue(clean["f"].get("@bound", None) is not None)
self.assertTrue("@class" in clean["f"]["@bound"])
# test method bound to object that is not serializable
for function in [MethodNonSerializationClass(1).method]:
d = {"f": function}
clean = jsanitize(d)
self.assertTrue(isinstance(clean["f"], str))
# test that strict checking gives an error
self.assertRaises(AttributeError, jsanitize, d, strict=True)
# test that callable MSONable objects still get serialized as the objects
# rather than as a callable
d = {"c": instance}
clean = jsanitize(d, strict=True)
self.assertTrue("@class" in clean["c"])
# test on pandas
df = pd.DataFrame([{"a": 1, "b": 1}, {"a": 1, "b": 2}])
clean = jsanitize(df)
self.assertEqual(clean, df.to_dict())
s = pd.Series({"a": [1, 2, 3], "b": [4, 5, 6]})
clean = jsanitize(s)
self.assertEqual(clean, s.to_dict())
def test_redirect(self):
MSONable.REDIRECT["tests.test_json"] = {"test_class": {"@class": "GoodMSONClass", "@module": "tests.test_json"}}
d = {
"@class": "test_class",
"@module": "tests.test_json",
"a": 1,
"b": 1,
"c": 1,
}
obj = json.loads(json.dumps(d), cls=MontyDecoder)
self.assertEqual(type(obj), GoodMSONClass)
d["@class"] = "not_there"
obj = json.loads(json.dumps(d), cls=MontyDecoder)
self.assertEqual(type(obj), dict)
def test_redirect_settings_file(self):
data = _load_redirect(os.path.join(test_dir, "test_settings.yaml"))
self.assertEqual(
data,
{"old_module": {"old_class": {"@class": "new_class", "@module": "new_module"}}},
)
def test_pydantic_integrations(self):
from pydantic import BaseModel
global ModelWithMSONable # allow model to be deserialized in test
class ModelWithMSONable(BaseModel):
a: GoodMSONClass
test_object = ModelWithMSONable(a=GoodMSONClass(1, 1, 1))
test_dict_object = ModelWithMSONable(a=test_object.a.as_dict())
assert test_dict_object.a.a == test_object.a.a
assert test_object.schema() == {
"title": "ModelWithMSONable",
"type": "object",
"properties": {
"a": {
"title": "A",
"type": "object",
"properties": {
"@class": {"enum": ["GoodMSONClass"], "type": "string"},
"@module": {"enum": ["tests.test_json"], "type": "string"},
"@version": {"type": "string"},
},
"required": ["@class", "@module"],
}
},
"required": ["a"],
}
d = jsanitize(test_object, strict=True)
assert d == {
"a": {
"@module": "tests.test_json",
"@class": "GoodMSONClass",
"@version": "0.1",
"a": 1,
"b": 1,
"c": 1,
"d": 1,
"values": [],
},
"@module": "tests.test_json",
"@class": "ModelWithMSONable",
"@version": "0.1",
}
obj = MontyDecoder().process_decoded(d)
assert isinstance(obj, BaseModel)
assert isinstance(obj.a, GoodMSONClass)
assert obj.a.b == 1
if __name__ == "__main__":
unittest.main()