Skip to content

Commit

Permalink
#168 - Experimental JSON CAS support
Browse files Browse the repository at this point in the history
- Better check whether adding a TextIOWrapper is necessary during serialization
- Fixed bad access to element type name
- Formatting
  • Loading branch information
reckart committed Sep 10, 2021
1 parent 1bea06c commit 95da404
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cassis/json.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import json
from collections import OrderedDict
from io import TextIOWrapper
from io import TextIOBase, TextIOWrapper

from cassis.cas import NAME_DEFAULT_SOFA, Cas, IdGenerator, Sofa, View
from cassis.typesystem import *
Expand Down Expand Up @@ -268,7 +268,7 @@ def serialize(
json_fs = self._serialize_feature_structure(fs)
feature_structures.append(json_fs)

if isinstance(sink, BytesIO):
if not isinstance(sink, TextIOBase):
sink = TextIOWrapper(sink, encoding="utf-8", write_through=True)

if sink:
Expand Down Expand Up @@ -316,7 +316,7 @@ def _serialize_feature(self, json_type, feature: Feature):
json_feature[MULTIPLE_REFERENCES_ALLOWED_FIELD] = feature.multipleReferencesAllowed

if feature.elementType is not None:
json_feature[ELEMENT_TYPE_FIELD] = self._to_external_type_name(feature.elementType)
json_feature[ELEMENT_TYPE_FIELD] = self._to_external_type_name(feature.elementType.name)

return json_feature

Expand Down
20 changes: 16 additions & 4 deletions tests/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

typesystem = generator.generate_type_system()
randomized_cas = generator.generate_cas(typesystem)

randomized_cas_xmi = randomized_cas.to_xmi()
randomized_cas_xmi_bytes = randomized_cas_xmi.encode("utf-8")

randomized_cas_json = randomized_cas.to_json()
randomized_cas_json_bytes = randomized_cas_json.encode("utf-8")


@pytest.mark.performance
Expand All @@ -24,7 +28,9 @@ def test_xmi_serialization_performance():
randomized_cas.to_xmi()
end = timer()

print(f"XMI: Serializing {iterations} CASes took {end - start} seconds")
print(
f"XMI: Serializing {iterations} CASes with {generator.size} each took {end - start} seconds ({len(randomized_cas_xmi_bytes)} bytes each)"
)


@pytest.mark.performance
Expand All @@ -34,7 +40,9 @@ def test_json_serialization_performance():
randomized_cas.to_json()
end = timer()

print(f"JSON: Serializing {iterations} CASes took {end - start} seconds")
print(
f"JSON: Serializing {iterations} CASes with {generator.size} each took {end - start} seconds ({len(randomized_cas_json_bytes)} bytes each)"
)


@pytest.mark.performance
Expand All @@ -44,7 +52,9 @@ def test_xmi_deserialization_performance():
load_cas_from_xmi(randomized_cas_xmi, typesystem)
end = timer()

print(f"XMI: Deserializing {iterations} CASes took {end - start} seconds")
print(
f"XMI: Deserializing {iterations} CASes with {generator.size} each took {end - start} seconds ({len(randomized_cas_xmi_bytes)} bytes each)"
)


@pytest.mark.performance
Expand All @@ -54,4 +64,6 @@ def test_json_deserialization_performance():
load_cas_from_json(randomized_cas_json, typesystem)
end = timer()

print(f"JSON: Deserializing {iterations} CASes took {end - start} seconds")
print(
f"JSON: Deserializing {iterations} CASes with {generator.size} each took {end - start} seconds ({len(randomized_cas_json_bytes)} bytes each)"
)

0 comments on commit 95da404

Please sign in to comment.