Comments (5)
Can you please provide a reproducer that demonstrates the issue?
from bson4jackson.
Here is the test case.
public class Person {
private String name;
private BigDecimal wage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getWage() {
return wage;
}
public void setWage(BigDecimal wage) {
this.wage = wage;
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import de.undercouch.bson4jackson.BsonFactory;
import de.undercouch.bson4jackson.BsonGenerator;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import static org.junit.Assert.*;
/**
* Created by van on 5/12/15.
*/
public class BigDecimalTest {
@Test
public void testNewBigDecimalWithDouble() {
Person p = new Person();
p.setName("Van Fan");
p.setWage(new BigDecimal(140001.12d));
try {
ObjectMapper mapper = new ObjectMapper(new BsonFactory());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mapper.writeValue(baos, p);
ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray());
Person person = mapper.readValue(bais, Person.class);
assertEquals(p.getName(), person.getName());
assertEquals(p.getWage(), person.getWage());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testBigDecimalValueOfDouble() {
Person p = new Person();
p.setName("Van Fan");
p.setWage(BigDecimal.valueOf(140001.12d));
try {
ObjectMapper mapper = new ObjectMapper(new BsonFactory());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mapper.writeValue(baos, p);
ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray());
Person person = mapper.readValue(bais, Person.class);
assertEquals(p.getName(), person.getName());
assertEquals(p.getWage(), person.getWage());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testNewBigDecimalWithDoubleWithFeatureWRITE_BIGDECIMALS_AS_STRINGS() {
Person p = new Person();
p.setName("Van Fan");
p.setWage(new BigDecimal(140001.12d));
try {
ObjectMapper mapper = new ObjectMapper(
new BsonFactory().configure(BsonGenerator.Feature.WRITE_BIGDECIMALS_AS_STRINGS, true));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mapper.writeValue(baos, p);
ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray());
Person person = mapper.readValue(bais, Person.class);
assertEquals(p.getName(), person.getName());
assertEquals(p.getWage(), person.getWage());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testBigDecimalValueOfDoubleWithFeatureWRITE_BIGDECIMALS_AS_STRINGS() {
Person p = new Person();
p.setName("Van Fan");
p.setWage(BigDecimal.valueOf(140001.12d));
try {
ObjectMapper mapper = new ObjectMapper(
new BsonFactory().configure(BsonGenerator.Feature.WRITE_BIGDECIMALS_AS_STRINGS, true));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mapper.writeValue(baos, p);
ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray());
Person person = mapper.readValue(bais, Person.class);
assertEquals(p.getName(), person.getName());
assertEquals(p.getWage(), person.getWage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
from bson4jackson.
Vanco,
I don't have the time to test your code right now. I probably have some tomorrow.
From what I can see by just looking at it, there might be an issue with the way you're creating your BigDecimal numbers. new BigDecimal(140001.12d)
doesn't work AFAIK. You need to use new BigDecimal("140001.12")
. Please try again and see if it helps. As I said, I can probably have a look at it tomorrow too.
Cheers,
Michel
from bson4jackson.
Here's what I get when I enter the code in a Java REPL:
java> new BigDecimal(140001.12d)
java.math.BigDecimal res0 = 140001.119999999995343387126922607421875
from bson4jackson.
I tested your code and it behaves exactly as expected. The tests using BsonGenerator.Feature.WRITE_BIGDECIMALS_AS_STRINGS
work correctly while the others fail. The reason for this is that the BSON specification does not support BigDecimals
. It only supports single precision and double precision floats (see http://bsonspec.org/spec.html). bson4jackson makes best effort to use the right precision when BsonGenerator.Feature.WRITE_BIGDECIMALS_AS_STRINGS
is not enabled, but oftentimes it's just not possible to represent a certain decimal number in IEEE floating point format. That's why WRITE_BIGDECIMALS_AS_STRINGS
exists.
I hope this helps. Let me know if you need anything else.
Cheers,
Michel
from bson4jackson.
Related Issues (20)
- Version 2.13 does not contain OSGi metadata anymore HOT 2
- UTF-8 conversion is significantly slower with BSON than regular JSON HOT 4
- Jackson 2.15.0 - Error deserializing an @JsonUnwrapped object with a field of type float HOT 6
- empty String error reading from array within sealed class with @JsonTypeInfo rules HOT 2
- Leading 4 bytes are all zeroes HOT 16
- How to enable a feature in BsonGenerator.java ? HOT 8
- unexpected block in ObjectMapper.readValue() in BSON streaming situation HOT 1
- BigDecimal precision HOT 1
- BsonGenerator does not handle null values correctly HOT 3
- BsonFactory doesn't override 'createParser(DataInput in)' method HOT 2
- UnsafeByteArrayInputStream.read for bytes >= 0x80 returns negative integer, caller thinks "Oh, EOF" HOT 1
- Buffer caching for heavy multithreaded load HOT 6
- Release 2.10.x version (+ test and document compatibility with Jackson 2.10.x)
- Failed to deserialize object HOT 2
- com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input HOT 2
- Support for Jackson 2.12 HOT 2
- ObjectId no longer compatible with mongodb ObjectId
- Decimal128 should extend Number & implement Comparable
- Serialization performance with large objects HOT 1
- IllegalStateException found when fuzzing bson4jackson HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bson4jackson.