|
| 1 | +package com.fasterxml.jackson.databind.ser.jdk; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests for serialization (and deserialization) of {@code float[]} |
| 13 | + * as "packed binary" data, as per [databind#5242]. |
| 14 | + */ |
| 15 | +public class VectorsAsBinarySerTest extends DatabindTestUtil |
| 16 | +{ |
| 17 | + private final static float[] FLOAT_VECTOR = new float[] { 1.0f, 0.5f, -1.25f }; |
| 18 | + private final static String FLOAT_VECTOR_STR = "[1.0,0.5,-1.25]"; |
| 19 | + |
| 20 | + private final static double[] DOUBLE_VECTOR = new double[] { -1.0, 1.5, 0.0125 }; |
| 21 | + private final static String DOUBLE_VECTOR_STR = "[-1.0,1.5,0.0125]"; |
| 22 | + |
| 23 | + private final ObjectMapper MAPPER = sharedMapper(); |
| 24 | + |
| 25 | + // // // Float Vector tests |
| 26 | + |
| 27 | + @Test |
| 28 | + public void defaultFloatVectorSerialization() throws Exception { |
| 29 | + String json = MAPPER.writeValueAsString(FLOAT_VECTOR); |
| 30 | + assertEquals(FLOAT_VECTOR_STR, json); |
| 31 | + |
| 32 | + float[] result = MAPPER.readValue(json, float[].class); |
| 33 | + assertArrayEquals(FLOAT_VECTOR, result); |
| 34 | + } |
| 35 | + |
| 36 | + // // // Double Vector tests |
| 37 | + |
| 38 | + @Test |
| 39 | + public void defaultDoubleVectorSerialization() throws Exception { |
| 40 | + String json = MAPPER.writeValueAsString(DOUBLE_VECTOR); |
| 41 | + assertEquals(DOUBLE_VECTOR_STR, json); |
| 42 | + |
| 43 | + double[] result = MAPPER.readValue(json, double[].class); |
| 44 | + assertArrayEquals(DOUBLE_VECTOR, result); |
| 45 | + } |
| 46 | +} |
0 commit comments