国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - gson ajax 數字精度丟失問題的解決方法

gson ajax 數字精度丟失問題的解決方法

2020-08-24 10:40jingxian Java教程

下面小編就為大家帶來一篇gson ajax 數字精度丟失問題的解決方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

ajax傳輸的json,gson會發生丟失,long > 15的時候會丟失0

解決方案:直接把屬性為long的屬性自動加上雙引號成為js的字符串,這樣就不會發生丟失了,ajax自動識別為字符串。

用法:

ajaxResult("",0,new Object()); //隨便一個對象就可以,List 之類的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
   * 以Ajax方式輸出常規操作結果
   *
   * @param status
   *      返回狀態,200表示成功, 500表示錯誤
   * @param message
   *      操作結果描述
   * @param tag
   *      附加數據
   * @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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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方法轉換json串到實體類出錯", e);
    }
    return obj;
  }
 
  /**
   * 如果 Long 的數字超過15位,轉換為String,在json中數字兩邊有引號
   * @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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

?
1
2
3
4
5
6
7
8
9
10
11
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 數字精度丟失問題的解決方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄频免费在线观看 | 91免费在线视频 | 在线日韩中文字幕 | 亚洲成人激情在线观看 | 午夜视频免费 | 亚洲精品不卡 | 精品av| 一呦二呦三呦国产精品 | 四虎影视免费看电影 | 国产91久久久久蜜臀青青天草二 | 日本久久精品视频 | 欧美午夜精品久久久久久浪潮 | 国产精品久久久久久吹潮 | 久久精品a一级国产免视看成人 | 特黄一级| 国产精品美女久久久久久久久久久 | 高清视频一区 | 亚洲精品久久久 | 中文字幕日韩欧美 | 欧美日韩国产精品一区 | 久久国产精品免费一区二区三区 | 久久精品亚洲精品国产欧美kt∨ | 欧美精品一区二区三区在线播放 | 日韩国产欧美亚洲 | 国内精品一区二区 | 国产高潮失禁喷水爽网站 | 欧美日韩免费看 | 天天摸天天做天天爽 | 亚洲精品www久久久久久广东 | 夜夜av| 免费黄色大片网址 | 在线日韩成人 | 精品一区二区在线观看 | 色视频www在线播放国产人成 | 久久国产精品一区二区 | 久久久久久成人 | 欧州一级片 | 亚洲综合精品 | 色偷偷888欧美精品久久久 | 午夜在线观看视频网站 | 黄色毛片在线 |