Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid rounding of a long value on client side?

Using Play Framework.

Controller

@BodyParser.Of(BodyParser.Json.class)
public static Result getResponseJson(Long id){
    List<DataField> entities = new Model.Finder<>(Long.class, DataField.class)
      .where("response_id = " + id).findList();
    Response response = new Response(entities, id);
    return ok(toJson(response));
}

The Response entity is:

public class Response {
  private Long uuid;
  private Map<TableField, String> map = new HashMap<>();
  ...constructors, getters and setters...
}

I check the response with POSTMAN. On the tab "Raw" I got:

{"uuid":3942015886343226874,"map":{"Work":"Contract","locale":"Mogilev"}}

On the tab "Pretty":

{
  "uuid": 3942015886343227000,
  "map": {
    "Work": "Contract",
    "locale": "Mogilev"
  }
}

Why the uuid is got rounded?

like image 521
barbariania Avatar asked Oct 30 '22 23:10

barbariania


1 Answers

It is not related to Playframework it is a browser issue. Open the developer console in your browser and try this -

var c = 3942015886343226874;
console.log(c);

This will print - 3942015886343227000

because of number limit in JavaScript.

Its better to use String in case of UUID.

like image 98
Rakesh Chouhan Avatar answered Nov 11 '22 04:11

Rakesh Chouhan