File: test_jsonio.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (37 lines) | stat: -rw-r--r-- 752 bytes parent folder | download | duplicates (2)
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
from datetime import datetime
import io

import numpy as np

from ase.io.jsonio import encode, decode, read_json, write_json


def test_jsonio():
    """Test serialization of ndarrays and other stuff."""
    assert decode(encode(np.int64(42))) == 42

    c = np.array([0.1j])
    assert (decode(encode(c)) == c).all()

    fd = io.StringIO()

    obj1 = {'hello': 'world'}
    write_json(fd, obj1)
    fd.seek(0)
    obj2 = read_json(fd)

    print(obj1)
    print(obj2)

    for obj in [0.5 + 1.5j,
                datetime.now()]:
        s = encode(obj)
        o = decode(s)
        print(obj)
        print(s)
        print(obj)
        assert obj == o, (obj, o, s)


def test_dict_with_int_key():
    assert decode(encode({1: 2}), False)[1] == 2