Skip to content
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

Set StreamReadCapability.EXACT_FLOATS=true #307

Merged
merged 1 commit into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@ public int getFormatFeatures() {

@Override // since 2.12
public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
// Defaults are fine
return DEFAULT_READ_CAPABILITIES;
return DEFAULT_READ_CAPABILITIES.with(StreamReadCapability.EXACT_FLOATS);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fasterxml.jackson.dataformat.cbor;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;

import static org.junit.Assert.assertArrayEquals;


// for [jackson-core#730]
public class FloatPrecisionTest extends CBORTestBase
{
// for [jackson-core#730]
public void testFloatRoundtrips() throws Exception
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
CBORGenerator gen = cborGenerator(out);
gen.writeStartArray();

gen.writeNumber(Float.MIN_VALUE);
gen.writeNumber(0.0f);
gen.writeNumber(Float.MAX_VALUE);

gen.writeNumber(Double.MIN_VALUE);
gen.writeNumber(0.0d);
gen.writeNumber(Double.MAX_VALUE);

gen.writeNumber(new BigDecimal("1e999"));
gen.writeEndArray();
gen.close();
byte[] expected = out.toByteArray();

CBORParser parser = cborParser(expected);
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
CBORGenerator gen2 = cborGenerator(out2);
parser.nextToken();
gen2.copyCurrentStructure(parser);
gen2.close();
byte[] actual = out2.toByteArray();
assertArrayEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,12 @@ public final JsonParser overrideFormatFeatures(int values, int mask) {
return this;
}

protected final static JacksonFeatureSet<StreamReadCapability> SMILE_READ_CAPABILITIES
= DEFAULT_READ_CAPABILITIES.with(StreamReadCapability.EXACT_FLOATS);

@Override // since 2.12
public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
// Defaults are fine
return DEFAULT_READ_CAPABILITIES;
return SMILE_READ_CAPABILITIES;
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.dataformat.smile;

import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import com.fasterxml.jackson.dataformat.smile.SmileParser;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;

import static org.junit.Assert.assertArrayEquals;


// for [jackson-core#730]
public class FloatPrecisionTest extends BaseTestForSmile
{
// for [jackson-core#730]
public void testFloatRoundtrips() throws Exception
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
SmileGenerator gen = smileGenerator(out, true);
gen.writeStartArray();

gen.writeNumber(Float.MIN_VALUE);
gen.writeNumber(0.0f);
gen.writeNumber(Float.MAX_VALUE);

gen.writeNumber(Double.MIN_VALUE);
gen.writeNumber(0.0d);
gen.writeNumber(Double.MAX_VALUE);

gen.writeNumber(new BigDecimal("1e999"));
gen.writeEndArray();
gen.close();
byte[] expected = out.toByteArray();

SmileParser parser = _smileParser(expected);
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
SmileGenerator gen2 = smileGenerator(out2, true);
parser.nextToken();
gen2.copyCurrentStructure(parser);
gen2.close();
byte[] actual = out2.toByteArray();
assertArrayEquals(expected, actual);
}
}