Skip to content

Commit

Permalink
Adds option to omit the Ion version marker from text output (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Cornell committed Oct 17, 2019
1 parent dc05bfb commit 3908afc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions amazon/ion/simpleion.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def dump(obj, fp, imports=None, binary=True, sequence_as_stream=False, skipkeys=
check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None,
use_decimal=True, namedtuple_as_object=True, tuple_as_array=True, bigint_as_string=False, sort_keys=False,
item_sort_key=None, for_json=None, ignore_nan=False, int_as_string_bitcount=None, iterable_as_array=False,
tuple_as_sexp=False, **kw):
tuple_as_sexp=False, omit_version_marker=False, **kw):
"""Serialize ``obj`` as an Ion-formatted stream to ``fp`` (a file-like object), using the following conversion
table::
+-------------------+-------------------+
Expand Down Expand Up @@ -138,14 +138,17 @@ def dump(obj, fp, imports=None, binary=True, sequence_as_stream=False, skipkeys=
iterable_as_array: NOT IMPLEMENTED
tuple_as_sexp (Optional[True|False]): When True, all tuple values will be written as Ion s-expressions.
When False, all tuple values will be written as Ion lists. Default: False.
omit_version_marker (Optional|True|False): If binary is False and omit_version_marker is True, omits the
Ion Version Marker ($ion_1_0) from the output. Default: False.
**kw: NOT IMPLEMENTED
"""

raw_writer = binary_writer(imports) if binary else text_writer(indent=indent)
writer = blocking_writer(raw_writer, fp)
from_type = _FROM_TYPE_TUPLE_AS_SEXP if tuple_as_sexp else _FROM_TYPE
writer.send(ION_VERSION_MARKER_EVENT) # The IVM is emitted automatically in binary; it's optional in text.
if not binary and not omit_version_marker:
writer.send(ION_VERSION_MARKER_EVENT) # The IVM is emitted automatically in binary; it's optional in text.
if sequence_as_stream and isinstance(obj, (list, tuple)):
# Treat this top-level sequence as a stream; serialize its elements as top-level values, but don't serialize the
# sequence itself.
Expand Down Expand Up @@ -232,7 +235,7 @@ def dumps(obj, imports=None, binary=True, sequence_as_stream=False, skipkeys=Fal
allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, use_decimal=True,
namedtuple_as_object=True, tuple_as_array=True, bigint_as_string=False, sort_keys=False, item_sort_key=None,
for_json=None, ignore_nan=False, int_as_string_bitcount=None, iterable_as_array=False, tuple_as_sexp=False,
**kw):
omit_version_marker=False, **kw):
"""Serialize ``obj`` as Python ``string`` or ``bytes`` object, using the conversion table used by ``dump`` (above).
Args:
Expand Down Expand Up @@ -267,6 +270,8 @@ def dumps(obj, imports=None, binary=True, sequence_as_stream=False, skipkeys=Fal
iterable_as_array: NOT IMPLEMENTED
tuple_as_sexp (Optional[True|False]): When True, all tuple values will be written as Ion s-expressions.
When False, all tuple values will be written as Ion lists. Default: False.
omit_version_marker (Optional|True|False): If binary is False and omit_version_marker is True, omits the
Ion Version Marker ($ion_1_0) from the output. Default: False.
**kw: NOT IMPLEMENTED
Returns:
Expand All @@ -281,7 +286,7 @@ def dumps(obj, imports=None, binary=True, sequence_as_stream=False, skipkeys=Fal
use_decimal=use_decimal, namedtuple_as_object=namedtuple_as_object, tuple_as_array=tuple_as_array,
bigint_as_string=bigint_as_string, sort_keys=sort_keys, item_sort_key=item_sort_key, for_json=for_json,
ignore_nan=ignore_nan, int_as_string_bitcount=int_as_string_bitcount, iterable_as_array=iterable_as_array,
tuple_as_sexp=tuple_as_sexp, **kw)
tuple_as_sexp=tuple_as_sexp, omit_version_marker=omit_version_marker, **kw)

ret_val = ion_buffer.getvalue()
ion_buffer.close()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_simpleion.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,14 @@ def test_struct_field():
# verify this no longer happens
assert u'dont_remember_my_name' not in struct_c
assert u'new_name' in struct_c


def test_dumps_omit_version_marker():
v = loads('5')
assert dumps(v, binary=False) == '$ion_1_0 5'
assert dumps(v, binary=False, omit_version_marker=True) == '5'

# verify no impact on binary output
assert dumps(v) == b'\xe0\x01\x00\xea\x21\x05'
assert dumps(v, omit_version_marker=True) == b'\xe0\x01\x00\xea\x21\x05'

0 comments on commit 3908afc

Please sign in to comment.