|
| 1 | +from __future__ import print_function |
| 2 | +from __future__ import absolute_import |
| 3 | +from __future__ import division |
| 4 | + |
| 5 | +import json |
| 6 | +from compas.utilities import DataEncoder |
| 7 | +from compas.utilities import DataDecoder |
| 8 | + |
| 9 | + |
| 10 | +__all__ = [ |
| 11 | + 'json_dump', |
| 12 | + 'json_dumps', |
| 13 | + 'json_load', |
| 14 | + 'json_loads' |
| 15 | +] |
| 16 | + |
| 17 | + |
| 18 | +def json_dump(data, fp): |
| 19 | + """Write a collection of COMPAS object data to a JSON file. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + data : any |
| 24 | + Any JSON serializable object. |
| 25 | + This includes any (combination of) COMPAS object(s). |
| 26 | + fp : file-like object or path |
| 27 | + A writeable file-like object or the path to a file. |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + None |
| 32 | +
|
| 33 | + Examples |
| 34 | + -------- |
| 35 | + >>> import compas |
| 36 | + >>> from compas.geometry import Point, Vector |
| 37 | + >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] |
| 38 | + >>> compas.json_dump(data1, 'data.json') |
| 39 | + >>> data2 = compas.json_load('data.json') |
| 40 | + >>> data1 == data2 |
| 41 | + True |
| 42 | + """ |
| 43 | + if hasattr(fp, 'write'): |
| 44 | + return json.dump(data, fp, cls=DataEncoder) |
| 45 | + with open(fp, 'w') as fp: |
| 46 | + return json.dump(data, fp, cls=DataEncoder) |
| 47 | + |
| 48 | + |
| 49 | +def json_dumps(data): |
| 50 | + """Write a collection of COMPAS objects to a JSON string. |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + data : any |
| 55 | + Any JSON serializable object. |
| 56 | + This includes any (combination of) COMPAS object(s). |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + str |
| 61 | +
|
| 62 | + Examples |
| 63 | + -------- |
| 64 | + >>> import compas |
| 65 | + >>> from compas.geometry import Point, Vector |
| 66 | + >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] |
| 67 | + >>> s = compas.json_dumps(data1) |
| 68 | + >>> data2 compas.json_loads(s) |
| 69 | + >>> data1 == data2 |
| 70 | + True |
| 71 | + """ |
| 72 | + return json.dumps(data, cls=DataEncoder) |
| 73 | + |
| 74 | + |
| 75 | +def json_load(fp): |
| 76 | + """Read COMPAS object data from a JSON file. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + fp : file-like object or path |
| 81 | + A writeable file-like object or the path to a file. |
| 82 | +
|
| 83 | + Returns |
| 84 | + ------- |
| 85 | + data |
| 86 | + The (COMPAS) data contained in the file. |
| 87 | +
|
| 88 | + Examples |
| 89 | + -------- |
| 90 | + >>> import compas |
| 91 | + >>> from compas.geometry import Point, Vector |
| 92 | + >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] |
| 93 | + >>> compas.json_dump(data1, 'data.json') |
| 94 | + >>> data2 = compas.json_load('data.json') |
| 95 | + >>> data1 == data2 |
| 96 | + True |
| 97 | + """ |
| 98 | + if hasattr(fp, 'read'): |
| 99 | + return json.load(fp, cls=DataDecoder) |
| 100 | + with open(fp, 'r') as fp: |
| 101 | + return json.load(fp, cls=DataDecoder) |
| 102 | + |
| 103 | + |
| 104 | +def json_loads(s): |
| 105 | + """Read COMPAS object data from a JSON string. |
| 106 | +
|
| 107 | + Parameters |
| 108 | + ---------- |
| 109 | + s : str |
| 110 | + A JSON data string. |
| 111 | +
|
| 112 | + Returns |
| 113 | + ------- |
| 114 | + data |
| 115 | + The (COMPAS) data contained in the string. |
| 116 | +
|
| 117 | + Examples |
| 118 | + -------- |
| 119 | + >>> import compas |
| 120 | + >>> from compas.geometry import Point, Vector |
| 121 | + >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] |
| 122 | + >>> s = compas.json_dumps(data1) |
| 123 | + >>> data2 = compas.json_loads() |
| 124 | + >>> data1 == data2 |
| 125 | + True |
| 126 | + """ |
| 127 | + return json.loads(s, cls=DataDecoder) |
| 128 | + |
| 129 | + |
| 130 | +# ============================================================================== |
| 131 | +# Main |
| 132 | +# ============================================================================== |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + import doctest |
| 136 | + |
| 137 | + doctest.testmod(globs=globals()) |
0 commit comments