Giter Club home page Giter Club logo

Comments (5)

michel-kraemer avatar michel-kraemer commented on September 25, 2024

Can you please provide a reproducer that demonstrates the issue?

from bson4jackson.

Vanco avatar Vanco commented on September 25, 2024

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.

michel-kraemer avatar michel-kraemer commented on September 25, 2024

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.

michel-kraemer avatar michel-kraemer commented on September 25, 2024

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.

michel-kraemer avatar michel-kraemer commented on September 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.