|
| 1 | +package com.jsoniter.extra; |
| 2 | + |
| 3 | +import com.jsoniter.CodegenAccess; |
| 4 | +import com.jsoniter.JsonIterator; |
| 5 | +import com.jsoniter.Slice; |
| 6 | +import com.jsoniter.any.Any; |
| 7 | +import com.jsoniter.output.JsonStream; |
| 8 | +import com.jsoniter.spi.Decoder; |
| 9 | +import com.jsoniter.spi.Encoder; |
| 10 | +import com.jsoniter.spi.JsonException; |
| 11 | +import com.jsoniter.spi.JsoniterSpi; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +/** |
| 16 | + * encode float/double as base64, faster than PreciseFloatSupport |
| 17 | + */ |
| 18 | +public class Base64FloatSupport { |
| 19 | + |
| 20 | + final static int[] DIGITS = new int[256]; |
| 21 | + final static int[] HEX = new int[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 22 | + final static int[] DEC = new int[127]; |
| 23 | + |
| 24 | + static { |
| 25 | + for (int i = 0; i < 256; i++) { |
| 26 | + DIGITS[i] = HEX[i >> 4] << 8 | HEX[i & 0xf]; |
| 27 | + } |
| 28 | + DEC['0'] = 0; |
| 29 | + DEC['1'] = 1; |
| 30 | + DEC['2'] = 2; |
| 31 | + DEC['3'] = 3; |
| 32 | + DEC['4'] = 4; |
| 33 | + DEC['5'] = 5; |
| 34 | + DEC['6'] = 6; |
| 35 | + DEC['7'] = 7; |
| 36 | + DEC['8'] = 8; |
| 37 | + DEC['9'] = 9; |
| 38 | + DEC['a'] = 10; |
| 39 | + DEC['b'] = 11; |
| 40 | + DEC['c'] = 12; |
| 41 | + DEC['d'] = 13; |
| 42 | + DEC['e'] = 14; |
| 43 | + DEC['f'] = 15; |
| 44 | + } |
| 45 | + |
| 46 | + private static boolean enabled; |
| 47 | + |
| 48 | + public static synchronized void enableEncodersAndDecoders() { |
| 49 | + if (enabled) { |
| 50 | + throw new JsonException("BinaryFloatSupport.enable can only be called once"); |
| 51 | + } |
| 52 | + enabled = true; |
| 53 | + enableDecoders(); |
| 54 | + JsoniterSpi.registerTypeEncoder(Double.class, new Encoder() { |
| 55 | + @Override |
| 56 | + public void encode(Object obj, JsonStream stream) throws IOException { |
| 57 | + Double number = (Double) obj; |
| 58 | + long bits = Double.doubleToRawLongBits(number.doubleValue()); |
| 59 | + Base64.encodeLongBits(bits, stream); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Any wrap(Object obj) { |
| 64 | + Double number = (Double) obj; |
| 65 | + return Any.wrap(number.doubleValue()); |
| 66 | + } |
| 67 | + }); |
| 68 | + JsoniterSpi.registerTypeEncoder(double.class, new Encoder.DoubleEncoder() { |
| 69 | + @Override |
| 70 | + public void encodeDouble(double obj, JsonStream stream) throws IOException { |
| 71 | + long bits = Double.doubleToRawLongBits(obj); |
| 72 | + Base64.encodeLongBits(bits, stream); |
| 73 | + } |
| 74 | + }); |
| 75 | + JsoniterSpi.registerTypeEncoder(Float.class, new Encoder() { |
| 76 | + @Override |
| 77 | + public void encode(Object obj, JsonStream stream) throws IOException { |
| 78 | + Float number = (Float) obj; |
| 79 | + long bits = Double.doubleToRawLongBits(number.doubleValue()); |
| 80 | + Base64.encodeLongBits(bits, stream); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Any wrap(Object obj) { |
| 85 | + Float number = (Float) obj; |
| 86 | + return Any.wrap(number.floatValue()); |
| 87 | + } |
| 88 | + }); |
| 89 | + JsoniterSpi.registerTypeEncoder(float.class, new Encoder.FloatEncoder() { |
| 90 | + @Override |
| 91 | + public void encodeFloat(float obj, JsonStream stream) throws IOException { |
| 92 | + long bits = Double.doubleToRawLongBits(obj); |
| 93 | + Base64.encodeLongBits(bits, stream); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + public static void enableDecoders() { |
| 99 | + JsoniterSpi.registerTypeDecoder(Double.class, new Decoder() { |
| 100 | + @Override |
| 101 | + public Object decode(JsonIterator iter) throws IOException { |
| 102 | + byte token = CodegenAccess.nextToken(iter); |
| 103 | + CodegenAccess.unreadByte(iter); |
| 104 | + if (token == '"') { |
| 105 | + return Double.longBitsToDouble(Base64.decodeLongBits(iter)); |
| 106 | + } else { |
| 107 | + return iter.readDouble(); |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + JsoniterSpi.registerTypeDecoder(double.class, new Decoder.DoubleDecoder() { |
| 112 | + @Override |
| 113 | + public double decodeDouble(JsonIterator iter) throws IOException { |
| 114 | + byte token = CodegenAccess.nextToken(iter); |
| 115 | + CodegenAccess.unreadByte(iter); |
| 116 | + if (token == '"') { |
| 117 | + return Double.longBitsToDouble(Base64.decodeLongBits(iter)); |
| 118 | + }else { |
| 119 | + return iter.readDouble(); |
| 120 | + } |
| 121 | + } |
| 122 | + }); |
| 123 | + JsoniterSpi.registerTypeDecoder(Float.class, new Decoder() { |
| 124 | + @Override |
| 125 | + public Object decode(JsonIterator iter) throws IOException { |
| 126 | + byte token = CodegenAccess.nextToken(iter); |
| 127 | + CodegenAccess.unreadByte(iter); |
| 128 | + if (token == '"') { |
| 129 | + return (float)Double.longBitsToDouble(Base64.decodeLongBits(iter)); |
| 130 | + }else { |
| 131 | + return (float)iter.readDouble(); |
| 132 | + } |
| 133 | + } |
| 134 | + }); |
| 135 | + JsoniterSpi.registerTypeDecoder(float.class, new Decoder.FloatDecoder() { |
| 136 | + @Override |
| 137 | + public float decodeFloat(JsonIterator iter) throws IOException { |
| 138 | + byte token = CodegenAccess.nextToken(iter); |
| 139 | + CodegenAccess.unreadByte(iter); |
| 140 | + if (token == '"') { |
| 141 | + return (float)Double.longBitsToDouble(Base64.decodeLongBits(iter)); |
| 142 | + }else { |
| 143 | + return (float)iter.readDouble(); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + private static long readLongBits(JsonIterator iter) throws IOException { |
| 150 | + Slice slice = iter.readStringAsSlice(); |
| 151 | + byte[] data = slice.data(); |
| 152 | + long val = 0; |
| 153 | + for (int i = slice.head(); i < slice.tail(); i++) { |
| 154 | + byte b = data[i]; |
| 155 | + val = val << 4 | DEC[b]; |
| 156 | + } |
| 157 | + return val; |
| 158 | + } |
| 159 | + |
| 160 | + private static void writeLongBits(long bits, JsonStream stream) throws IOException { |
| 161 | + int digit = DIGITS[(int) (bits & 0xff)]; |
| 162 | + byte b2 = (byte) (digit >> 8); |
| 163 | + byte b1 = (byte) digit; |
| 164 | + bits = bits >> 8; |
| 165 | + if (bits == 0) { |
| 166 | + stream.write((byte) '"', b2, b1, (byte) '"'); |
| 167 | + } |
| 168 | + digit = DIGITS[(int) (bits & 0xff)]; |
| 169 | + byte b4 = (byte) (digit >> 8); |
| 170 | + byte b3 = (byte) digit; |
| 171 | + bits = bits >> 8; |
| 172 | + if (bits == 0) { |
| 173 | + stream.write((byte) '"', b4, b3, b2, b1, (byte) '"'); |
| 174 | + } |
| 175 | + digit = DIGITS[(int) (bits & 0xff)]; |
| 176 | + byte b6 = (byte) (digit >> 8); |
| 177 | + byte b5 = (byte) digit; |
| 178 | + bits = bits >> 8; |
| 179 | + if (bits == 0) { |
| 180 | + stream.write((byte) '"', b6, b5, b4, b3); |
| 181 | + stream.write(b2, b1, (byte) '"'); |
| 182 | + } |
| 183 | + digit = DIGITS[(int) (bits & 0xff)]; |
| 184 | + byte b8 = (byte) (digit >> 8); |
| 185 | + byte b7 = (byte) digit; |
| 186 | + bits = bits >> 8; |
| 187 | + if (bits == 0) { |
| 188 | + stream.write((byte) '"', b8, b7, b6, b5, b4); |
| 189 | + stream.write(b3, b2, b1, (byte) '"'); |
| 190 | + } |
| 191 | + digit = DIGITS[(int) (bits & 0xff)]; |
| 192 | + byte b10 = (byte) (digit >> 8); |
| 193 | + byte b9 = (byte) digit; |
| 194 | + bits = bits >> 8; |
| 195 | + if (bits == 0) { |
| 196 | + stream.write((byte) '"', b10, b9, b8, b7, b6); |
| 197 | + stream.write(b5, b4, b3, b2, b1, (byte) '"'); |
| 198 | + } |
| 199 | + digit = DIGITS[(int) (bits & 0xff)]; |
| 200 | + byte b12 = (byte) (digit >> 8); |
| 201 | + byte b11 = (byte) digit; |
| 202 | + bits = bits >> 8; |
| 203 | + if (bits == 0) { |
| 204 | + stream.write((byte) '"', b12, b11, b10, b9, b8); |
| 205 | + stream.write(b7, b6, b5, b4, b3, b2); |
| 206 | + stream.write(b1, (byte) '"'); |
| 207 | + } |
| 208 | + digit = DIGITS[(int) (bits & 0xff)]; |
| 209 | + byte b14 = (byte) (digit >> 8); |
| 210 | + byte b13 = (byte) digit; |
| 211 | + bits = bits >> 8; |
| 212 | + if (bits == 0) { |
| 213 | + stream.write((byte) '"', b14, b13, b12, b11, b10); |
| 214 | + stream.write(b9, b8, b7, b6, b5, b4); |
| 215 | + stream.write(b3, b2, b1, (byte) '"'); |
| 216 | + } |
| 217 | + digit = DIGITS[(int) (bits & 0xff)]; |
| 218 | + byte b16 = (byte) (digit >> 8); |
| 219 | + byte b15 = (byte) digit; |
| 220 | + stream.write((byte) '"', b16, b15, b14, b13, b12); |
| 221 | + stream.write(b11, b10, b9, b8, b7, b6); |
| 222 | + stream.write(b5, b4, b3, b2, b1, (byte) '"'); |
| 223 | + } |
| 224 | +} |
0 commit comments