最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

gson ajax 數(shù)字精度丟失問(wèn)題的解決方法

 更新時(shí)間:2017年03月05日 10:44:47   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇gson ajax 數(shù)字精度丟失問(wèn)題的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

ajax傳輸?shù)膉son,gson會(huì)發(fā)生丟失,long > 15的時(shí)候會(huì)丟失0

解決方案:直接把屬性為long的屬性自動(dòng)加上雙引號(hào)成為js的字符串,這樣就不會(huì)發(fā)生丟失了,ajax自動(dòng)識(shí)別為字符串。

用法:

ajaxResult("",0,new Object()); //隨便一個(gè)對(duì)象就可以,List 之類(lèi)的

/**
   * 以Ajax方式輸出常規(guī)操作結(jié)果
   *
   * @param status
   *      返回狀態(tài),200表示成功, 500表示錯(cuò)誤
   * @param message
   *      操作結(jié)果描述
   * @param tag
   *      附加數(shù)據(jù)
   * @return
   */
  protected ActionResult ajaxResult(int status, final String message, Object tag) {
    JsonObject json = new JsonObject();
    json.addProperty("status", status);
    json.addProperty("message", message);

    String strJson = json.toString();

    if (tag != null) {
      StringBuffer sb = new StringBuffer();
      sb.append(strJson.substring(0, strJson.length() - 1));
      sb.append(",\"tag\":");
      sb.append(GsonUtils.toJsonWithGson(tag));
      sb.append("}");
      strJson = sb.toString();
    }

    return writeJson(strJson);
  }

/**
   * 向客戶端輸出文本信息
   *
   * @param message
   * @return
   */
  protected ActionResult write(final String message) {
    return new ActionResult() {
      @Override
      public void render(BeatContext arg0) throws Exception {
        beat.getResponse().setCharacterEncoding("UTF-8");
        beat.getResponse().setContentType("text/json;charset=UTF-8");
        PrintWriter out = beat.getResponse().getWriter();
        out.print(message);
        out.close();
      }

    };
  }

  /**
   * 向客戶端輸出文本信息
   *
   * @param message
   * @return
   */
  protected ActionResult writeText(final String message) {
    return new ActionResult() {
      @Override
      public void render(BeatContext arg0) throws Exception {
        beat.getResponse().setCharacterEncoding("UTF-8");
        beat.getResponse().setContentType("application/text");
        PrintWriter out = beat.getResponse().getWriter();
        out.print(message);
        out.close();
      }

    };
  }

GsonUtils.java

package com.xxx.xxx.common.util.gson;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GsonUtils {
  //private static Log logger = LogFactory.getLog(GsonUtils.class);
  public static String toJsonWithGson(Object obj) {
    Gson gson = createGson();  //new Gson();
    return gson.toJson(obj);
  }

  public static String toJsonWithGson(Object obj, Type type) {
    Gson gson = createGson();  //new Gson();
    return gson.toJson(obj, type);
  }

  @SuppressWarnings("unchecked")
  public static String toJsonWithGson(List list) {
    Gson gson = createGson();  //new Gson();
    return gson.toJson(list);
  }

  @SuppressWarnings("unchecked")
  public static String toJsonWithGson(List list, Type type) {
    Gson gson = createGson();  //new Gson();
    return gson.toJson(list, type);
  }

  public static String toJsonWithGsonBuilder(Object obj) {
    Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
    return gson.toJson(obj);
  }

  public static String toJsonWithGsonBuilder(Object obj, Type type) {
    Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
    return gson.toJson(obj, type);
  }

  @SuppressWarnings("unchecked")
  public static String toJsonWithGsonBuilder(List list) {
    Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
    return gson.toJson(list);
  }

  @SuppressWarnings("unchecked")
  public static String toJsonWithGsonBuilder(List list, Type type) {
    Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
    return gson.toJson(list, type);
  }

  public static <T> Object fromJson(String json, Class<T> clazz) {
    Object obj = null;
    try {
      Gson gson = new Gson();
      obj = gson.fromJson(json, clazz);
    } catch (Exception e) {
      //logger.error("fromJson方法轉(zhuǎn)換json串到實(shí)體類(lèi)出錯(cuò)", e);
    }
    return obj;
  }

  /**
   * 如果 Long 的數(shù)字超過(guò)15位,轉(zhuǎn)換為String,在json中數(shù)字兩邊有引號(hào)
   * @return
   */
  private static Gson createGson(){
    GsonBuilder gsonBuilder = new GsonBuilder();
    LongSerializer serializer = new LongSerializer();
    gsonBuilder.registerTypeAdapter(Long.class, serializer);
    gsonBuilder.registerTypeAdapter(long.class, serializer);
    Gson gson = gsonBuilder.create();
    return gson;
  }

  public static void main(String... args) throws Exception{
//    long a = 12345678901234578L;
//
//    GsonBuilder builder = new GsonBuilder();
//    builder.registerTypeAdapter(Long.class, new LongSerializer());
//    Gson gson2 = builder.create();
//    System.out.println(gson2.toJson(a));
//
//    Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
//    String str = gson.toJson(a);
//    System.out.println(str);

    TestVO vo = new TestVO();
    vo.setId(618708732263538688L);
    vo.setId2(918708732263538688L);
    System.out.println(toJsonWithGson(vo));

  }

  static class LongSerializer implements JsonSerializer<Long> {
    public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
      if(src!=null){
        String strSrc = src.toString();
        if(strSrc.length()>15){
          return new JsonPrimitive(strSrc);
        }
      }
      return new JsonPrimitive(src);
    }
  }

  static class TestVO {
    public long getId() {
      return id;
    }

    public void setId(long id) {
      this.id = id;
    }

    private long id;

    public Long getId2() {
      return id2;
    }

    public void setId2(Long id2) {
      this.id2 = id2;
    }

    private Long id2;
  }
}

MyExclusionStrategy.java

package com.xxx.xxx.common.util.gson;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class MyExclusionStrategy implements ExclusionStrategy { 
 
  private final Class<?> typeToSkip; 
   
  public MyExclusionStrategy(){ 
    this.typeToSkip=null; 
  } 
   
  public MyExclusionStrategy(Class<?> typeToSkip) { 
    this.typeToSkip = typeToSkip; 
  } 
   
  public boolean shouldSkipClass(Class<?> clazz) { 
    return (clazz == typeToSkip); 
  } 
 
  public boolean shouldSkipField(FieldAttributes f) { 
    return f.getAnnotation(NotSerialize.class) != null; 
  } 
 
} 

NotSerialize

package com.xxx.xxx.common.util.gson;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.FIELD}) 
public @interface NotSerialize { 
}

以上這篇gson ajax 數(shù)字精度丟失問(wèn)題的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合RabbitMQ實(shí)戰(zhàn)教程附死信交換機(jī)

    SpringBoot整合RabbitMQ實(shí)戰(zhàn)教程附死信交換機(jī)

    這篇文章主要介紹了SpringBoot整合RabbitMQ實(shí)戰(zhàn)附加死信交換機(jī),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • intellij idea 將模塊打jar包的步驟詳解

    intellij idea 將模塊打jar包的步驟詳解

    這篇文章主要介紹了intellij idea 將模塊打jar包的步驟,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • jvm排查工具箱jvm-tools下載使用詳解

    jvm排查工具箱jvm-tools下載使用詳解

    這篇文章主要為大家介紹了jvm排查工具箱jvm-tools下載使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例

    Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例

    這篇文章主要介紹了Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • springboot多租戶設(shè)計(jì)過(guò)程圖解

    springboot多租戶設(shè)計(jì)過(guò)程圖解

    這篇文章主要介紹了springboot多租戶設(shè)計(jì)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Spring?Boot?使用觀察者模式實(shí)現(xiàn)實(shí)時(shí)庫(kù)存管理的步驟

    Spring?Boot?使用觀察者模式實(shí)現(xiàn)實(shí)時(shí)庫(kù)存管理的步驟

    在現(xiàn)代軟件開(kāi)發(fā)中,實(shí)時(shí)數(shù)據(jù)處理非常關(guān)鍵,本文提供了一個(gè)使用SpringBoot和觀察者模式開(kāi)發(fā)實(shí)時(shí)庫(kù)存管理系統(tǒng)的詳細(xì)教程,步驟包括創(chuàng)建項(xiàng)目、定義實(shí)體類(lèi)、實(shí)現(xiàn)觀察者模式、集成Spring框架、創(chuàng)建RESTful?API端點(diǎn)和測(cè)試應(yīng)用等,這將有助于開(kāi)發(fā)者構(gòu)建能夠即時(shí)響應(yīng)庫(kù)存變化的系統(tǒng)
    2024-09-09
  • Java?SpringBoot?中的操作事務(wù)

    Java?SpringBoot?中的操作事務(wù)

    這篇文章主要介紹了Java?SpringBoot?中的操作事務(wù),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Java代碼執(zhí)行順序——類(lèi)的初始化場(chǎng)景

    Java代碼執(zhí)行順序——類(lèi)的初始化場(chǎng)景

    這篇文章主要為大家介紹了Java代碼執(zhí)行順序類(lèi)的初始化場(chǎng)景實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 解密Spring?Boot深入理解條件裝配與條件注解

    解密Spring?Boot深入理解條件裝配與條件注解

    條件注解是一種特殊的注解,用于標(biāo)記在配置類(lèi)、組件類(lèi)或方法上,它們根據(jù)某些條件的結(jié)果來(lái)決定是否應(yīng)用相應(yīng)的配置或組件,這篇文章主要介紹了解密Spring?Boot深入理解條件裝配與條件注解,需要的朋友可以參考下
    2024-06-06
  • 聊聊單線程的Redis為何會(huì)快到飛起

    聊聊單線程的Redis為何會(huì)快到飛起

    Redis想必大家都或多或少聽(tīng)過(guò)吧,我們?cè)诠ぷ鲗W(xué)習(xí)中通常用它來(lái)作為緩存使用,既然是作為緩存,大家的第一反應(yīng)肯定是:這家伙很快
    2022-02-02

最新評(píng)論

邳州市| 洮南市| 榆林市| 麦盖提县| 潢川县| 甘谷县| 烟台市| 苍山县| 波密县| 娄烦县| 南昌县| 汝阳县| 新郑市| 宝丰县| 彩票| 文水县| 英吉沙县| 玛沁县| 理塘县| 怀宁县| 陆良县| 喜德县| 张掖市| 阳朔县| 辽宁省| 时尚| 南溪县| 会宁县| 常德市| 横峰县| 三原县| 绿春县| 海伦市| 古蔺县| 连江县| 喀喇| 长顺县| 荆门市| 镇宁| 乐昌市| 会泽县|