Why is a number casted automatically to double?
<p>I use spring boot for my webservice. Each method returns <code>Map&lt;Object, Object&gt;</code> because it is a general type and the methods are able to return any response, either a primitive <code>int</code> or complex custom object of <code>User</code>. Also I used <code>Map&lt;Object, Object&gt;</code> to eliminate backslash "\" in JSON, instead of using String.</p>

<p>But I got problem with variable casting in client (Android app).</p>

<p>Number variables in Map are automatically casted to double (1.0, 2.0, 3.0, 5.0, ...) while it is <code>long</code> in server.</p>

<p>If I cast number to string at server, then casting at client is right e.g. 1, 2, 3, 5, ...</p>

<pre><code>return String.valueOf(u.getId())
</code></pre>

<p>Server side variable:</p>

<pre><code>long id;
</code></pre>

<p>Server side method:</p>

<pre><code>public final static String SUCCESS = "0";
public final static String NOT_FOUND = "-1";

Map&lt;Object, Object&gt; m = new HashMap&lt;&gt;();

@RequestMapping("/getUser")
Map&lt;Object, Object&gt; getUser(@RequestParam(value = "phoneNumber", defaultValue = "") String phoneNumber,
        @RequestParam(value = "hash", defaultValue = "") String hash) {

    m.clear();

    User user = userRepository.findByPhoneNumberAndHash(phoneNumber, hash);
    if (user != null) {
        m.put(ERROR_JSON, SUCCESS);
        m.put(VALUE_JSON, user);
    } else {
        m.put(ERROR_JSON, NOT_FOUND);
    }
    return m;
}
</code></pre>

<p>JSON:</p>

<pre><code>[{"id":1}] and [{"id":"1"}]
</code></pre>

<p>Android code. Retrofit</p>

<pre><code>userService.getUser(phoneNumber, hash).enqueue(new Callback&lt;Map&lt;Object, Object&gt;&gt;() {

    @Override
    public void onResponse(Call&lt;Map&lt;Object, Object&gt;&gt; call, Response&lt;Map&lt;Object, Object&gt;&gt; response) {
        Map&lt;Object, Object&gt; m = response.body();
    }
    @Override
    public void onFailure(Call&lt;Map&lt;Object, Object&gt;&gt; call, Throwable t) {
        t.printStackTrace();
    }
});
</code></pre>

<p><a href="https://i.stack.imgur.com/Dv91N.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dv91N.jpg" alt="enter image description here"></a></p>