Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

BigDecimal in a client-side widget?

Dave Ball
7 years ago Aug 06, 2015 12:44am
Dave Ball
7 years ago Aug 06, 2015 10:49am
Artur Signell
7 years ago Aug 06, 2015 11:00am
Johannes Dahlström
7 years ago Aug 06, 2015 11:03am
Dave Ball
7 years ago Aug 06, 2015 11:44am
Dave Ball
7 years ago Aug 06, 2015 2:53pm

For anyone else stumbling where I was, I found two interfaces:

  • com.vaadin.server.communication.JSONSerializer;
  • com.vaadin.client.communication.JSONSerializer;

It appears I can teach Vaadin how to serialise a BigDecimal by implementing both of these:

Client side:

public class BigDecimal_Serializer implements JSONSerializer<BigDecimal> {

    @Override
    public BigDecimal deserialize(Type type, JsonValue jsonValue, ApplicationConnection connection) {
        return new BigDecimal(jsonValue.asString());
    }

    @Override
    public JsonValue serialize(BigDecimal value, ApplicationConnection connection) {
        return Json.create(value.toString());
    }
}

Server side:

public class BigDecimal_Serializer implements JSONSerializer<BigDecimal> {

    @Override
    public BigDecimal deserialize(Type type, JsonValue jsonValue, ConnectorTracker connectorTracker) {
        return new BigDecimal(jsonValue.asString());
    }

    @Override
    public JsonValue serialize(BigDecimal value, ConnectorTracker connectorTracker) {
        return Json.create(value.toString());
    }
}

And all my troubles have dissapeared!

Thanks for your help Artur & Johannes.

Dave

Dave Ball
7 years ago Aug 14, 2015 8:12pm
Jens Offenbach
7 years ago Aug 28, 2015 7:46am
Dave Ball
7 years ago Aug 28, 2015 9:10am
Jens Offenbach
7 years ago Aug 28, 2015 9:22am
Dave Ball
7 years ago Aug 28, 2015 9:32am
Bogdan Vasile
6 years ago Jan 05, 2017 8:47am