-
Notifications
You must be signed in to change notification settings - Fork 113
json convenience functions #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
07d9400
json convenience functions
tomvanmele 4100ff2
base transformations on base
tomvanmele 1512b33
use dtype when available
tomvanmele dbb1778
any object as parameter
tomvanmele 26b5581
tests
tomvanmele 7dea801
log
tomvanmele a0a4c68
clean up leftovers
tomvanmele d2006e7
version in data structure data
tomvanmele 8396feb
ignore numpy test on ipy
tomvanmele fd42658
tests for ipy
tomvanmele f0fdada
"fixing" ipy tests
tomvanmele f91b6fd
beverly says faking tests is not allowed
tomvanmele 4306ee7
losing patience here
tomvanmele 9d09420
last try and then i am merging without the approval of ironpython
tomvanmele File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| from __future__ import print_function | ||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
|
|
||
| import json | ||
| from compas.utilities import DataEncoder | ||
| from compas.utilities import DataDecoder | ||
|
|
||
|
|
||
| __all__ = [ | ||
| 'json_dump', | ||
| 'json_dumps', | ||
| 'json_load', | ||
| 'json_loads' | ||
| ] | ||
|
|
||
|
|
||
| def json_dump(data, fp): | ||
| """Write a collection of COMPAS object data to a JSON file. | ||
| Parameters | ||
| ---------- | ||
| data : any | ||
| Any JSON serializable object. | ||
| This includes any (combination of) COMPAS object(s). | ||
| fp : file-like object or path | ||
| A writeable file-like object or the path to a file. | ||
| Returns | ||
| ------- | ||
| None | ||
| Examples | ||
| -------- | ||
| >>> import compas | ||
| >>> from compas.geometry import Point, Vector | ||
| >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] | ||
| >>> compas.json_dump(data1, 'data.json') | ||
| >>> data2 = compas.json_load('data.json') | ||
| >>> data1 == data2 | ||
| True | ||
| """ | ||
| if hasattr(fp, 'write'): | ||
| return json.dump(data, fp, cls=DataEncoder) | ||
| with open(fp, 'w') as fp: | ||
| return json.dump(data, fp, cls=DataEncoder) | ||
|
|
||
|
|
||
| def json_dumps(data): | ||
| """Write a collection of COMPAS objects to a JSON string. | ||
| Parameters | ||
| ---------- | ||
| data : any | ||
| Any JSON serializable object. | ||
| This includes any (combination of) COMPAS object(s). | ||
| Returns | ||
| ------- | ||
| str | ||
| Examples | ||
| -------- | ||
| >>> import compas | ||
| >>> from compas.geometry import Point, Vector | ||
| >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] | ||
| >>> s = compas.json_dumps(data1) | ||
| >>> data2 compas.json_loads(s) | ||
| >>> data1 == data2 | ||
| True | ||
| """ | ||
| return json.dumps(data, cls=DataEncoder) | ||
|
|
||
|
|
||
| def json_load(fp): | ||
| """Read COMPAS object data from a JSON file. | ||
| Parameters | ||
| ---------- | ||
| fp : file-like object or path | ||
| A writeable file-like object or the path to a file. | ||
| Returns | ||
| ------- | ||
| data | ||
| The (COMPAS) data contained in the file. | ||
| Examples | ||
| -------- | ||
| >>> import compas | ||
| >>> from compas.geometry import Point, Vector | ||
| >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] | ||
| >>> compas.json_dump(data1, 'data.json') | ||
| >>> data2 = compas.json_load('data.json') | ||
| >>> data1 == data2 | ||
| True | ||
| """ | ||
| if hasattr(fp, 'read'): | ||
| return json.load(fp, cls=DataDecoder) | ||
| with open(fp, 'r') as fp: | ||
| return json.load(fp, cls=DataDecoder) | ||
|
|
||
|
|
||
| def json_loads(s): | ||
| """Read COMPAS object data from a JSON string. | ||
| Parameters | ||
| ---------- | ||
| s : str | ||
| A JSON data string. | ||
| Returns | ||
| ------- | ||
| data | ||
| The (COMPAS) data contained in the string. | ||
| Examples | ||
| -------- | ||
| >>> import compas | ||
| >>> from compas.geometry import Point, Vector | ||
| >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] | ||
| >>> s = compas.json_dumps(data1) | ||
| >>> data2 = compas.json_loads() | ||
| >>> data1 == data2 | ||
| True | ||
| """ | ||
| return json.loads(s, cls=DataDecoder) | ||
|
|
||
|
|
||
| # ============================================================================== | ||
| # Main | ||
| # ============================================================================== | ||
|
|
||
| if __name__ == '__main__': | ||
| import doctest | ||
|
|
||
| doctest.testmod(globs=globals()) | ||
tomvanmele marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import compas | ||
|
|
||
| from compas.geometry import Point, Vector, Frame | ||
| from compas.geometry import Box | ||
| from compas.geometry import Transformation | ||
|
|
||
| from compas.datastructures import Mesh | ||
|
|
||
|
|
||
| def test_json_native(): | ||
| before = [[], (), {}, '', 1, 1.0, True, None] | ||
| after = compas.json_loads(compas.json_dumps(before)) | ||
| assert after == [[], [], {}, '', 1, 1.0, True, None] | ||
|
|
||
|
|
||
| if not compas.IPY: | ||
| import numpy as np | ||
|
|
||
| def test_json_numpy(): | ||
| before = [np.array([1, 2, 3]), np.array([1.0, 2.0, 3.0]), np.float64(1.0), np.int32(1)] | ||
| after = compas.json_loads(compas.json_dumps(before)) | ||
| assert after == [[1, 2, 3], [1.0, 2.0, 3.0], 1.0, 1] | ||
|
|
||
|
|
||
| def test_json_primitive(): | ||
| before = Point(0, 0, 0) | ||
| after = compas.json_loads(compas.json_dumps(before)) | ||
| assert before.dtype == after.dtype | ||
| assert all(a == b for a, b in zip(before, after)) | ||
|
|
||
|
|
||
| def test_json_shape(): | ||
| before = Box(Frame(Point(0, 0, 0), Vector(1, 0, 0), Vector(0, 1, 0)), 1, 1, 1) | ||
| after = compas.json_loads(compas.json_dumps(before)) | ||
| assert before.dtype == after.dtype | ||
| assert all(a == b for a, b in zip(before.vertices, after.vertices)) | ||
|
|
||
|
|
||
| def test_json_xform(): | ||
| before = Transformation.from_frame_to_frame(Frame.worldXY(), Frame.worldXY()) | ||
| after = compas.json_loads(compas.json_dumps(before)) | ||
| assert before.dtype == after.dtype | ||
| assert all(a == b for a, b in zip(before, after)) | ||
|
|
||
|
|
||
| def test_json_mesh(): | ||
| before = Mesh.from_vertices_and_faces([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], [[0, 1, 2, 3]]) | ||
| after = compas.json_loads(compas.json_dumps(before)) | ||
| assert before.dtype == after.dtype | ||
| assert all(before.has_vertex(vertex) for vertex in after.vertices()) | ||
| assert all(after.has_vertex(vertex) for vertex in before.vertices()) | ||
| assert all(before.has_face(face) for face in after.faces()) | ||
| assert all(after.has_face(face) for face in before.faces()) | ||
| assert all(before.has_edge(edge) for edge in after.edges()) | ||
| assert all(after.has_edge(edge) for edge in before.edges()) | ||
| assert all(before.face_vertices(a) == after.face_vertices(b) for a, b in zip(before.faces(), after.faces())) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the plan is to fix up all of the file readers so that they take file-like objects and paths, I think these lines of code will be used quite a few times. Maybe there should be a utility function which does this. Some sort of context manager like
And I suppose this could be generalized to handle reading as well.