Skip to content

Commit

Permalink
feat: implement SensorReference.to_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 14, 2024
1 parent 43db3fc commit 5e6e4c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 10 additions & 1 deletion raillabel/format/sensor_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass
from decimal import Decimal

from raillabel.json_format import JSONStreamSync
from raillabel.json_format import JSONStreamSync, JSONStreamSyncProperties, JSONStreamSyncTimestamp


@dataclass
Expand All @@ -27,3 +27,12 @@ def from_json(cls, json: JSONStreamSync) -> SensorReference:
timestamp=Decimal(json.stream_properties.sync.timestamp),
uri=json.uri,
)

def to_json(self) -> JSONStreamSync:
"""Export this object into the RailLabel JSON format."""
return JSONStreamSync(
stream_properties=JSONStreamSyncProperties(
sync=JSONStreamSyncTimestamp(timestamp=str(self.timestamp)),
),
uri=self.uri,
)
12 changes: 11 additions & 1 deletion tests/test_raillabel/format/test_sensor_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,20 @@ def test_from_json(sensor_reference, sensor_reference_json):
assert actual == sensor_reference


def test_from_json(another_sensor_reference, another_sensor_reference_json):
def test_from_json__another(another_sensor_reference, another_sensor_reference_json):
actual = SensorReference.from_json(another_sensor_reference_json)
assert actual == another_sensor_reference


def test_to_json(sensor_reference, sensor_reference_json):
actual = sensor_reference.to_json()
assert actual == sensor_reference_json


def test_to_json__another(another_sensor_reference, another_sensor_reference_json):
actual = another_sensor_reference.to_json()
assert actual == another_sensor_reference_json


if __name__ == "__main__":
pytest.main([__file__, "-v"])

0 comments on commit 5e6e4c7

Please sign in to comment.