Skip to content

Commit

Permalink
Merge branch 'master' into flat-writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton authored Jun 16, 2021
2 parents aaff53a + 64a5b8e commit b102d59
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/com/amazon/ion/impl/IonWriterSystemText.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ public void writeFloat(double value)
throws IOException
{
startValue();
_output.printFloat(value);
_output.printFloat(_options, value);
closeValue();
}

Expand Down
24 changes: 18 additions & 6 deletions src/com/amazon/ion/impl/_Private_IonTextAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ else if (adjustedExponent >= -6)
}


public void printFloat(double value)
public void printFloat(_Private_IonTextWriterBuilder _options, double value)
throws IOException
{
// shortcut zero cases
Expand All @@ -815,15 +815,27 @@ public void printFloat(double value)
}
else if (Double.isNaN(value))
{
appendAscii("nan");
if (_options._float_nan_and_inf_as_null) {
appendAscii("null");
} else {
appendAscii("nan");
}
}
else if (value == Double.POSITIVE_INFINITY)
{
appendAscii("+inf");
if (_options._float_nan_and_inf_as_null) {
appendAscii("null");
} else {
appendAscii("+inf");
}
}
else if (value == Double.NEGATIVE_INFINITY)
{
appendAscii("-inf");
if (_options._float_nan_and_inf_as_null) {
appendAscii("null");
} else {
appendAscii("-inf");
}
}
else
{
Expand All @@ -846,7 +858,7 @@ else if (value == Double.NEGATIVE_INFINITY)
}
}

public void printFloat(Double value)
public void printFloat(_Private_IonTextWriterBuilder _options, Double value)
throws IOException
{
if (value == null)
Expand All @@ -855,7 +867,7 @@ public void printFloat(Double value)
}
else
{
printFloat(value.doubleValue());
printFloat(_options, value.doubleValue());
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/com/amazon/ion/impl/_Private_IonTextWriterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,20 @@ public static _Private_IonTextWriterBuilder standard()
//=========================================================================

private boolean _pretty_print;

// These options control whether the IonTextWriter will write standard ion or ion that is down-converted json.
public boolean _blob_as_string;
public boolean _clob_as_string;
public boolean _decimal_as_float;
public boolean _float_nan_and_inf_as_null;
public boolean _sexp_as_list;
public boolean _skip_annotations;
public boolean _string_as_json;
public boolean _symbol_as_string;
public boolean _timestamp_as_millis;
public boolean _timestamp_as_string;
public boolean _untyped_nulls;

private _Private_CallbackBuilder _callback_builder;


Expand All @@ -73,6 +77,7 @@ private _Private_IonTextWriterBuilder(_Private_IonTextWriterBuilder that)
this._blob_as_string = that._blob_as_string ;
this._clob_as_string = that._clob_as_string ;
this._decimal_as_float = that._decimal_as_float ;
this._float_nan_and_inf_as_null = that._float_nan_and_inf_as_null;
this._sexp_as_list = that._sexp_as_list ;
this._skip_annotations = that._skip_annotations ;
this._string_as_json = that._string_as_json ;
Expand Down Expand Up @@ -123,6 +128,7 @@ public final IonTextWriterBuilder withJsonDowngrade()
_clob_as_string = true;
// datagramAsList = true; // TODO
_decimal_as_float = true;
_float_nan_and_inf_as_null = true;
_sexp_as_list = true;
_skip_annotations = true;
// skipSystemValues = true; // TODO
Expand Down
4 changes: 2 additions & 2 deletions src/com/amazon/ion/util/IonTextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ public static void printFloat(Appendable out, double value)
{
_Private_IonTextAppender appender =
_Private_IonTextAppender.forAppendable(out);
appender.printFloat(value);
appender.printFloat(STANDARD, value);
}

public static String printFloat(double value)
Expand All @@ -900,7 +900,7 @@ public static void printFloat(Appendable out, Double value)
{
_Private_IonTextAppender appender =
_Private_IonTextAppender.forAppendable(out);
appender.printFloat(value);
appender.printFloat(STANDARD, value);
}

public static String printFloat(Double value)
Expand Down
51 changes: 51 additions & 0 deletions test/com/amazon/ion/impl/TextWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,57 @@ public void testWritingJsonLongClobs()
assertEquals("\"a\\\"'\\nc\\u007f\"", actual);
}

@Test
public void testWritingFloatNanToJson()
throws Exception
{
options = IonTextWriterBuilder.json();
options.setInitialIvmHandling(SUPPRESS);

IonDatagram dg = system().newDatagram();
dg.add().newFloat(Double.NaN);

iw = makeWriter();
dg.writeTo(iw);

String actual = outputString();
assertEquals("null", actual);
}

@Test
public void testWritingFloatPositiveInfinityToJson()
throws Exception
{
options = IonTextWriterBuilder.json();
options.setInitialIvmHandling(SUPPRESS);

IonDatagram dg = system().newDatagram();
dg.add().newFloat(Double.POSITIVE_INFINITY);

iw = makeWriter();
dg.writeTo(iw);

String actual = outputString();
assertEquals("null", actual);
}

@Test
public void testWritingFloatNegativeInfinityToJson()
throws Exception
{
options = IonTextWriterBuilder.json();
options.setInitialIvmHandling(SUPPRESS);

IonDatagram dg = system().newDatagram();
dg.add().newFloat(Double.NEGATIVE_INFINITY);

iw = makeWriter();
dg.writeTo(iw);

String actual = outputString();
assertEquals("null", actual);
}

@Test
public void testSuppressInitialIvm()
throws Exception
Expand Down

0 comments on commit b102d59

Please sign in to comment.