• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #447

23 May 2026 01:52AM UTC coverage: 88.921% (-0.4%) from 89.31%
#447

push

areyouok
feat: allow customize DecodeFilter for kryo decoder

54 of 59 new or added lines in 8 files covered. (91.53%)

24 existing lines in 8 files now uncovered.

4976 of 5596 relevant lines covered (88.92%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.81
/jetcache-core/src/main/java/com/alicp/jetcache/support/AbstractJsonEncoder.java
1
/**
2
 * Created on 2022/07/27.
3
 */
4
package com.alicp.jetcache.support;
5

6
import com.alicp.jetcache.CacheValueHolder;
7

8
import java.nio.charset.StandardCharsets;
9

10
/**
11
 * @author huangli
12
 */
13
public abstract class AbstractJsonEncoder extends AbstractValueEncoder {
14
    private final int identityNumber;
15

16
    public AbstractJsonEncoder(boolean useIdentityNumber, int identityNumber) {
17
        super(useIdentityNumber);
1✔
18
        this.identityNumber = identityNumber;
1✔
19
    }
1✔
20

21
    protected abstract byte[] encodeSingleValue(Object value);
22

23
    @Override
24
    public byte[] apply(Object value) {
25
        try {
26
            JsonData[] data = encode(value);
1✔
27
            int len = len(data);
1✔
28
            byte[] buffer = useIdentityNumber ? new byte[len + 4] : new byte[len];
1✔
29
            int index = 0;
1✔
30
            if (useIdentityNumber) {
1✔
31
                index = writeInt(buffer, index, identityNumber);
1✔
32
            }
33
            if (data == null) {
1✔
34
                writeShort(buffer, index, -1);
1✔
35
            } else {
36
                index = writeShort(buffer, index, data.length);
1✔
37
                for (JsonData d : data) {
1✔
38
                    if (d == null) {
1✔
39
                        index = writeShort(buffer, index, -1);
1✔
40
                    } else {
41
                        index = writeShort(buffer, index, d.getClassName().length);
1✔
42
                        index = writeBytes(buffer, index, d.getClassName());
1✔
43
                        index = writeInt(buffer, index, d.getData().length);
1✔
44
                        index = writeBytes(buffer, index, d.getData());
1✔
45
                    }
46
                }
47
            }
48
            return buffer;
1✔
49
        } catch (Throwable e) {
×
50
            StringBuilder sb = new StringBuilder("Json Encode error. ");
×
UNCOV
51
            sb.append("msg=").append(e.getMessage());
×
UNCOV
52
            throw new CacheEncodeException(sb.toString(), e);
×
53
        }
54
    }
55

56
    private int len(JsonData[] data) {
57
        if (data == null) {
1✔
58
            return 2;
1✔
59
        }
60
        int x = 2;
1✔
61
        for (JsonData d : data) {
1✔
62
            if (d == null) {
1✔
63
                x += 2;
1✔
64
            } else {
65
                x += 2 + d.getClassName().length + 4 + d.getData().length;
1✔
66
            }
67
        }
68
        return x;
1✔
69
    }
70

71
    private int writeInt(byte[] buf, int index, int value) {
72
        buf[index] = (byte) (value >> 24 & 0xFF);
1✔
73
        buf[index + 1] = (byte) (value >> 16 & 0xFF);
1✔
74
        buf[index + 2] = (byte) (value >> 8 & 0xFF);
1✔
75
        buf[index + 3] = (byte) (value & 0xFF);
1✔
76
        return index + 4;
1✔
77
    }
78

79
    private int writeShort(byte[] buf, int index, int value) {
80
        buf[index] = (byte) (value >> 8 & 0xFF);
1✔
81
        buf[index + 1] = (byte) (value & 0xFF);
1✔
82
        return index + 2;
1✔
83
    }
84

85
    private int writeBytes(byte[] buf, int index, byte[] data) {
86
        System.arraycopy(data, 0, buf, index, data.length);
1✔
87
        return index + data.length;
1✔
88
    }
89

90
    private JsonData[] encode(Object value) {
91
        if (value == null) {
1✔
92
            return null;
1✔
93
        }
94
        if (value instanceof CacheValueHolder) {
1✔
95
            CacheValueHolder h = (CacheValueHolder) value;
1✔
96
            Object bizObject = h.getValue();
1✔
97
            h.setValue(null);
1✔
98
            JsonData[] result = new JsonData[2];
1✔
99
            result[0] = encodeJsonData(h);
1✔
100
            result[1] = encodeJsonData(bizObject);
1✔
101
            h.setValue(bizObject);
1✔
102
            return result;
1✔
103
        } else if (value instanceof CacheMessage) {
1✔
104
            CacheMessage cm = (CacheMessage) value;
1✔
105
            Object[] keys = cm.getKeys();
1✔
106
            cm.setKeys(null);
1✔
107
            JsonData[] result = keys == null ? new JsonData[1] : new JsonData[keys.length + 1];
1✔
108
            result[0] = encodeJsonData(cm);
1✔
109
            if (keys != null) {
1✔
110
                for (int i = 0; i < keys.length; i++) {
1✔
111
                    result[i + 1] = encodeJsonData(keys[i]);
1✔
112
                }
113
            }
114
            cm.setKeys(keys);
1✔
115
            return result;
1✔
116
        } else {
117
            return new JsonData[]{encodeJsonData(value)};
1✔
118
        }
119
    }
120

121
    private JsonData encodeJsonData(Object value) {
122
        if (value == null) {
1✔
123
            return null;
1✔
124
        }
125
        JsonData jsonData = new JsonData();
1✔
126
        jsonData.setClassName(value.getClass().getName().getBytes(StandardCharsets.UTF_8));
1✔
127
        jsonData.setData(encodeSingleValue(value));
1✔
128
        return jsonData;
1✔
129
    }
130

131
    private static class JsonData {
132
        private byte[] className;
133
        private byte[] data;
134

135
        public byte[] getClassName() {
136
            return className;
1✔
137
        }
138

139
        public void setClassName(byte[] className) {
140
            this.className = className;
1✔
141
        }
1✔
142

143
        public byte[] getData() {
144
            return data;
1✔
145
        }
146

147
        public void setData(byte[] data) {
148
            this.data = data;
1✔
149
        }
1✔
150
    }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc