title: Retrofit & Gson - prevent gson from converting Long to scientific notation
body: <h2>Summary</h2>
  
  <p>I've been using <strong><em>Retrofit and Gson</em></strong> in my android app for a while but the <code>IDs</code> I get from the server are <strong>13 digit numbers</strong>. Gson automatically converts those numbers to scientific notation, then casts it to <code>String/Long</code>, whatever I specify.</p>
  
  <h2>Here is a sample Retrofit class with an <code>ID</code></h2>
  
  <pre><code>public class User implements Serializable {
  
      @SerializedName("accessToken")
      @Expose
      public String accessToken;
  
      @SerializedName("userId")
      @Expose
      public String userId; //This is where it casts it into a Scientific notation
  
      @SerializedName("userRole")
      @Expose
      public int userRole;
  
      @SerializedName("name")
      @Expose
      public String name;
  
      @SerializedName("mobile")
      @Expose
      public String mobile;
  
      @SerializedName("countryId")
      @Expose
      public String countryId; //Here too
  
      @SerializedName("email")
      @Expose
      public String email;
  
      @SerializedName("username")
      @Expose
      public String username;
  
  
  }
  </code></pre>
  
  <h2>This is the Screenshot I got from the debugger</h2>
  
  <p><a href="https://i.stack.imgur.com/16gtw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/16gtw.png" alt="User implements Serializable Response"></a></p>
  
  <h2>Solutions I've tried</h2>
  
  <p><strong>This doesn't work for some reason</strong></p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/11586299/how-to-prevent-gson-from-converting-a-long-number-a-json-string-to-scientific">How to prevent Gson from converting a long number (a json string ) to scientific notation format?</a></p>
  </blockquote>
  
  <p><strong>As I've checked the debugger, my control flow never goes into</strong> <code>serialize(Double src, Type typeOfSrc, JsonSerializationContext context)</code></p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/11119094/switch-off-scientific-notation-in-gson-double-serialization">switch off scientific notation in Gson double serialization</a> </p>
  </blockquote>
  
  <pre><code>    @Override
      public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
          if(src == src.longValue())
              return new JsonPrimitive(src.longValue());          
          return new JsonPrimitive(src);
      }
  </code></pre>
  
  <p><strong>Another solution I've tried</strong></p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/15507997/how-to-prevent-gson-from-expressing-integers-as-floats">How to prevent Gson from expressing integers as floats</a></p>
  </blockquote>
  
  <p><strong>And this is my current RetrofitBuilder Method</strong></p>
  
  <pre><code>        Retrofit.Builder rBuilder = new Builder();
          rBuilder.baseUrl(API_URL);
          GsonBuilder gsonBuilder = new GsonBuilder();
          gsonBuilder.registerTypeAdapter(Double.class,  new JsonSerializer&lt;Double&gt;() {
                  @Override
                  public JsonElement serialize(final Double src, final Type typeOfSrc, final JsonSerializationContext context) {
                      BigDecimal value = BigDecimal.valueOf(src);
                      return new JsonPrimitive(value);
                  }
              });
          gsonBuilder.registerTypeAdapter(Long.class,  new JsonSerializer&lt;Long&gt;() {
                  @Override
                  public JsonElement serialize(final Long src, final Type typeOfSrc, final JsonSerializationContext context) {
                      BigDecimal value = BigDecimal.valueOf(src);
                      return new JsonPrimitive(value);
                  }
              });
  
          rBuilder.addConverterFactory(GsonConverterFactory.create(gsonBuilder.create()));
  </code></pre>
  
  <p>Any kind of help anywhere would be highly appreciated.</p>