Compare commits
No commits in common. "0a02524d865ad37da078e89e53631095edc311b2" and "35224166222f01fc5c8575b9dc3a84c1fbe8df93" have entirely different histories.
0a02524d86
...
3522416622
11 changed files with 329 additions and 253 deletions
|
|
@ -5,12 +5,12 @@
|
|||
<parent>
|
||||
<groupId>nl.sander</groupId>
|
||||
<artifactId>jsonthingy-pom</artifactId>
|
||||
<version>1.7</version>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>JsonToy-JMH</name>
|
||||
<artifactId>jsonthingy-jmhtests</artifactId>
|
||||
<version>1.7</version>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
|
|
@ -19,6 +19,7 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
|
|
|
|||
|
|
@ -1,79 +0,0 @@
|
|||
package nl.sanderhautvast.json.jmh;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@State(Scope.Thread)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public class ArrayReflectionBenchmarks {
|
||||
|
||||
private static final int ITERATIONS = 10;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new ArrayReflectionBenchmarks().testReflectiveArray();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testReflectiveArray() {
|
||||
int[] r1 = {1};
|
||||
int[] r2 = {1, 2};
|
||||
int[][] table1 = {r1, r2};
|
||||
int[][] table2 = {r1, r2};
|
||||
int[][][] schema = {table1, table2};
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
addArrayElements(schema);
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void testNonReflectiveArray() {
|
||||
int[] r1 = {1};
|
||||
int[] r2 = {1, 2};
|
||||
int[][] table1 = {r1, r2};
|
||||
int[][] table2 = {r1, r2};
|
||||
int[][][] schema = {table1, table2};
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
addIntegerArray(schema);
|
||||
}
|
||||
}
|
||||
|
||||
private int addArrayElements(Object o) {
|
||||
int sum = 0;
|
||||
if (o.getClass().isArray()) {
|
||||
int length = Array.getLength(o);
|
||||
for (int i = 0; i < length; i++) {
|
||||
sum += addArrayElements(Array.get(o, i));
|
||||
}
|
||||
} else {
|
||||
sum += (Integer) o;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private int addIntegerArray(int[][][] o) {
|
||||
int sum = 0;
|
||||
for (int[][] ints : o) {
|
||||
sum += addIntegerArray2(ints);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private int addIntegerArray2(int[][] o) {
|
||||
int sum = 0;
|
||||
for (int[] ints : o) {
|
||||
sum += addIntegerArray3(ints);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private int addIntegerArray3(int[] o) {
|
||||
int sum = 0;
|
||||
for (int j : o) {
|
||||
sum += j;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,14 +8,14 @@ import org.openjdk.jmh.annotations.*;
|
|||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
//@State(Scope.Thread)
|
||||
//@BenchmarkMode(Mode.AverageTime)
|
||||
//@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@State(Scope.Thread)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public class Benchmarks {
|
||||
|
||||
private static final int ITERATIONS = 10;
|
||||
|
||||
// @Benchmark
|
||||
@Benchmark
|
||||
public void testJson() {
|
||||
Bean1 bean1;
|
||||
Bean2 bean2;
|
||||
|
|
@ -30,7 +30,7 @@ public class Benchmarks {
|
|||
}
|
||||
}
|
||||
|
||||
// @Benchmark
|
||||
@Benchmark
|
||||
public void testJackson() throws JsonProcessingException {
|
||||
Bean1 bean1;
|
||||
Bean2 bean2;
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@
|
|||
<parent>
|
||||
<groupId>nl.sander</groupId>
|
||||
<artifactId>jsonthingy-pom</artifactId>
|
||||
<version>1.7</version>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>JsonToy</name>
|
||||
<artifactId>jsonthingy</artifactId>
|
||||
<version>1.7</version>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
|
|
|
|||
|
|
@ -4,32 +4,25 @@ import org.objectweb.asm.ClassReader;
|
|||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.time.temporal.Temporal;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
/**
|
||||
* Mapper.json(StringBuilder b, ...)
|
||||
* TODO write to outputstream
|
||||
*/
|
||||
public class Mapper {
|
||||
private static final Map<Class<?>, BaseMapper<?>> mappers = new ConcurrentHashMap<>();
|
||||
|
||||
private static final ByteClassLoader generatedClassesLoader = new ByteClassLoader();
|
||||
public static final char[] TAB = {'\\', 't'};
|
||||
public static final char[] DOUBLEQUOTE = {'\\', '\"'};
|
||||
public static final char[] SLASH = {'\\', '/'};
|
||||
public static final char[] RETURN = {'\\', 'r'};
|
||||
public static final char[] BACKSLASH = {'\\', '\\'};
|
||||
public static final char[] NEWLINE = {'\\', 'n'};
|
||||
public static final char[] BELL = {'\\', 'b'};
|
||||
public static final char[] FORMFEED = {'\\', 'f'};
|
||||
private static final char[][] MAP = createEscapeMap();
|
||||
private static final StringMapper stringMapper = new StringMapper();
|
||||
private static final BooleanMapper booleanMapper = new BooleanMapper();
|
||||
|
||||
private static final IntegerMapper integerMapper = new IntegerMapper();
|
||||
private static final LongMapper longMapper = new LongMapper();
|
||||
private static final ShortMapper shortMapper = new ShortMapper();
|
||||
private static final ByteMapper byteMapper = new ByteMapper();
|
||||
private static final CharMapper charMapper = new CharMapper();
|
||||
private static final FloatMapper floatMapper = new FloatMapper();
|
||||
private static final DoubleMapper doubleMapper = new DoubleMapper();
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -52,42 +45,56 @@ public class Mapper {
|
|||
return b.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "UnnecessaryToStringCall"})
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static void json(StringBuilder b, Object value) {
|
||||
if (value == null) {
|
||||
b.append("null");
|
||||
} else {
|
||||
Class<?> type = value.getClass();
|
||||
if (type.isArray()) {
|
||||
array(b, value);
|
||||
if (value instanceof byte[]) {
|
||||
array(b, (byte[]) value);
|
||||
} else if (value instanceof int[]) {
|
||||
array(b, (int[]) value);
|
||||
} else if (value instanceof short[]) {
|
||||
array(b, (short[]) value);
|
||||
} else if (value instanceof boolean[]) {
|
||||
array(b, (boolean[]) value);
|
||||
} else if (value instanceof char[]) {
|
||||
array(b, (char[]) value);
|
||||
} else if (value instanceof long[]) {
|
||||
array(b, (long[]) value);
|
||||
} else if (value instanceof float[]) {
|
||||
array(b, (float[]) value);
|
||||
} else if (value instanceof double[]) {
|
||||
array(b, (double[]) value);
|
||||
} else {
|
||||
array(b, (Object[]) value);
|
||||
}
|
||||
} else if (value instanceof Collection) {
|
||||
list(b, (Collection) value);
|
||||
} else if (value instanceof Map) {
|
||||
object(b, (Map) value);
|
||||
} else {
|
||||
if (type == String.class) {
|
||||
b.append("\"");
|
||||
Mapper.escape(b, (String) value);
|
||||
b.append("\"");
|
||||
} else if (type == Character.class) {
|
||||
b.append("\"");
|
||||
Mapper.escape(b, (Character) value);
|
||||
b.append("\"");
|
||||
} else if (type == UUID.class || value instanceof Temporal || type.isEnum()) {
|
||||
b.append("\"");
|
||||
b.append(value.toString());
|
||||
b.append("\"");
|
||||
} else if (type == Boolean.class
|
||||
|| type == Integer.class
|
||||
|| type == Long.class
|
||||
|| type == Float.class
|
||||
|| type == Double.class
|
||||
|| type == Byte.class
|
||||
|| type == Short.class
|
||||
|| type == BigInteger.class
|
||||
|| type == BigDecimal.class
|
||||
) {
|
||||
b.append(value.toString()); // prevents another nullcheck
|
||||
if (type.equals(String.class)) {
|
||||
stringMapper.json(b, (String) value);
|
||||
} else if (type.equals(Boolean.class)) {
|
||||
|
||||
booleanMapper.json(b, (Boolean) value);
|
||||
} else if (type.equals(Integer.class)) {
|
||||
integerMapper.json(b, (Integer) value);
|
||||
} else if (type.equals(Long.class)) {
|
||||
longMapper.json(b, (Long) value);
|
||||
} else if (type.equals(Short.class)) {
|
||||
shortMapper.json(b, (Short) value);
|
||||
} else if (type.equals(Byte.class)) {
|
||||
byteMapper.json(b, (Byte) value);
|
||||
} else if (type.equals(Character.class)) {
|
||||
charMapper.json(b, (Character) value);
|
||||
} else if (type.equals(Float.class)) {
|
||||
floatMapper.json(b, (Float) value);
|
||||
} else if (type.equals(Double.class)) {
|
||||
doubleMapper.json(b, (Double) value);
|
||||
} else {
|
||||
BaseMapper mapper = mappers.computeIfAbsent(type, key -> createObjectMapper(type));
|
||||
mapper.json(b, value);
|
||||
|
|
@ -96,16 +103,140 @@ public class Mapper {
|
|||
}
|
||||
}
|
||||
|
||||
//TODO make this more performant
|
||||
private static void array(StringBuilder b, Object value) {
|
||||
b.append("[");
|
||||
StringJoiner joiner = new StringJoiner(",");
|
||||
for (int i = 0; i < Array.getLength(value); i++) {
|
||||
Object arrayElement = Array.get(value, i);
|
||||
joiner.add(Mapper.json(arrayElement)); // recursie
|
||||
private static void array(StringBuilder b, Object[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
Object first = array[0];
|
||||
b.append("[");
|
||||
Mapper.json(b, first);
|
||||
Arrays.stream(array).skip(1)
|
||||
.forEach(element -> {
|
||||
b.append(",");
|
||||
Mapper.json(b, element);
|
||||
});
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, byte[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
byte first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, short[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
short first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, long[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
long first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, boolean[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
boolean first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, double[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
double first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, char[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
char first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, float[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
float first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void array(StringBuilder b, int[] array) {
|
||||
if (array.length == 0) {
|
||||
b.append("[]");
|
||||
} else {
|
||||
int first = array[0];
|
||||
b.append("[");
|
||||
json(b, first);
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
b.append(",");
|
||||
json(b, array[i]);
|
||||
}
|
||||
b.append("]");
|
||||
}
|
||||
b.append(joiner);
|
||||
b.append("]");
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
|
|
@ -210,64 +341,134 @@ public class Mapper {
|
|||
escape(b, String.valueOf(c));
|
||||
}
|
||||
|
||||
private static char[][] createEscapeMap() {
|
||||
char[][] charmap = new char[0x2100][];
|
||||
for (int i = 0; i < 0x2100; i++) {
|
||||
char c = (char) i;
|
||||
char[] replacement;
|
||||
if (c == '\t') {
|
||||
replacement = TAB;
|
||||
} else if (c == '\"') {
|
||||
replacement = DOUBLEQUOTE;
|
||||
} else if (c == '/') {
|
||||
replacement = SLASH;
|
||||
} else if (c == '\r') {
|
||||
replacement = RETURN;
|
||||
} else if (c == '\\') {
|
||||
replacement = BACKSLASH;
|
||||
} else if (c == '\n') {
|
||||
replacement = NEWLINE;
|
||||
} else if (c == '\b') {
|
||||
replacement = BELL;
|
||||
} else if (c == '\f') {
|
||||
replacement = FORMFEED;
|
||||
} else if ((c <= '\u001F') || (c >= '\u007F' && c <= '\u009F') || (c >= '\u2000')) {
|
||||
replacement = new char[6];
|
||||
replacement[0] = '\\';
|
||||
replacement[1] = 'u';
|
||||
|
||||
String hex = Integer.toHexString(c).toUpperCase();
|
||||
int hexlen = hex.length();
|
||||
for (int k = 0; k < 4 - hexlen; k++) {
|
||||
replacement[k + 2] = '0';
|
||||
}
|
||||
for (int k = 0; k < hexlen; k++) {
|
||||
replacement[6 - hexlen + k] = hex.charAt(k);
|
||||
}
|
||||
} else {
|
||||
replacement = new char[]{c};
|
||||
}
|
||||
charmap[i] = replacement;
|
||||
}
|
||||
|
||||
return charmap;
|
||||
}
|
||||
|
||||
//both methods are equally slow it seems
|
||||
//mainly because we can't do a batch copy into the stringbuilder and have to map every character individually
|
||||
static void escape(StringBuilder b, String value) {
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
int c = value.charAt(i);
|
||||
|
||||
if (c < 0x2100) {
|
||||
b.append(MAP[c]);
|
||||
} else {
|
||||
b.append((char) c);
|
||||
int offset = b.length();
|
||||
b.append(value);
|
||||
int i = offset;
|
||||
while (i < b.length()) {
|
||||
char c = b.charAt(i);
|
||||
switch (c) {
|
||||
case '\t':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(i + 1, "t");
|
||||
break;
|
||||
case '\"':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "\"");
|
||||
break;
|
||||
case '/':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "/");
|
||||
break;
|
||||
case '\r':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "r");
|
||||
break;
|
||||
case '\n':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "n");
|
||||
break;
|
||||
case '\b':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "b");
|
||||
break;
|
||||
case '\f':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "f");
|
||||
break;
|
||||
case '\\':
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "\\");
|
||||
break;
|
||||
case '\'':
|
||||
break;
|
||||
default:
|
||||
if ((c <= '\u001F') || (c >= '\u007F' && c <= '\u009F') || (c >= '\u2000' && c <= '\u20FF')) {
|
||||
String ss = Integer.toHexString(c);
|
||||
b.replace(i, i + 1, "\\");
|
||||
b.insert(++i, "u");
|
||||
for (int k = 0; k < 4 - ss.length(); k++) {
|
||||
b.insert(++i, '0');
|
||||
}
|
||||
b.insert(++i, ss.toUpperCase());
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class BooleanMapper extends BaseMapper<Boolean> {
|
||||
|
||||
@Override
|
||||
public void json(StringBuilder b, Boolean value) {
|
||||
b.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
class ShortMapper extends BaseMapper<Short> {
|
||||
|
||||
@Override
|
||||
public void json(StringBuilder b, Short value) {
|
||||
b.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
class StringMapper extends BaseMapper<String> {
|
||||
@Override
|
||||
public void json(StringBuilder b, String value) {
|
||||
b.append("\"");
|
||||
Mapper.escape(b, value);
|
||||
b.append("\"");
|
||||
}
|
||||
}
|
||||
|
||||
class IntegerMapper extends BaseMapper<Integer> {
|
||||
|
||||
@Override
|
||||
public void json(StringBuilder b, Integer value) {
|
||||
b.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
class LongMapper extends BaseMapper<Long> {
|
||||
|
||||
@Override
|
||||
public void json(StringBuilder b, Long value) {
|
||||
b.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
class ByteMapper extends BaseMapper<Byte> {
|
||||
|
||||
@Override
|
||||
protected void json(StringBuilder b, Byte value) {
|
||||
b.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
class CharMapper extends BaseMapper<Character> {
|
||||
|
||||
@Override
|
||||
protected void json(StringBuilder b, Character value) {
|
||||
b.append("\"");
|
||||
Mapper.escape(b, value);
|
||||
b.append("\"");
|
||||
}
|
||||
}
|
||||
|
||||
class FloatMapper extends BaseMapper<Float> {
|
||||
|
||||
@Override
|
||||
protected void json(StringBuilder b, Float value) {
|
||||
b.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
class DoubleMapper extends BaseMapper<Double> {
|
||||
|
||||
@Override
|
||||
protected void json(StringBuilder b, Double value) {
|
||||
b.append(value);
|
||||
}
|
||||
}
|
||||
|
|
@ -92,7 +92,7 @@ public class MapperFactory extends ClassVisitor {
|
|||
}
|
||||
|
||||
getterInsnList.add(new VarInsnNode(ALOAD, 1));
|
||||
getterInsnList.add(new LdcInsnNode("\"" + correctName(getterMethodName, startIndex) + "\":"));
|
||||
getterInsnList.add(new LdcInsnNode("\"" + getterMethodName.substring(startIndex).toLowerCase() + "\":"));
|
||||
getterInsnList.add(new MethodInsnNode(INVOKEVIRTUAL, STRINGBUILDER, APPEND, APPEND_SIGNATURE));
|
||||
getterInsnList.add(new InsnNode(POP));
|
||||
getterInsnList.add(new VarInsnNode(ALOAD, 1));
|
||||
|
|
@ -101,11 +101,6 @@ public class MapperFactory extends ClassVisitor {
|
|||
getterInsnList.add(new MethodInsnNode(INVOKESTATIC, MAPPER, "json", "(Ljava/lang/StringBuilder;" + genericReturnType(returnType) + ")V"));
|
||||
}
|
||||
|
||||
private String correctName(String getterMethodName, int startIndex) {
|
||||
String tmp = getterMethodName.substring(startIndex);
|
||||
return tmp.substring(0, 1).toLowerCase() + tmp.substring(1);
|
||||
}
|
||||
|
||||
private static String genericReturnType(String returnType) {
|
||||
char firstChar = returnType.charAt(0);
|
||||
if (firstChar == 'L' || firstChar == '[') {
|
||||
|
|
|
|||
|
|
@ -1,17 +0,0 @@
|
|||
package nl.sanderhautvast.json.ser;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class EnumTest {
|
||||
|
||||
enum Answer {
|
||||
YES, NO
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEnums() {
|
||||
assertEquals("\"YES\"", Mapper.json(Answer.YES));
|
||||
}
|
||||
}
|
||||
|
|
@ -22,7 +22,7 @@ public class JacksonComparisonTest {
|
|||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Bean1 bean1 = new Bean1();
|
||||
Bean2 bean2 = new Bean2();
|
||||
bean1.setData1(UUID.randomUUID());
|
||||
bean1.setData1(UUID.randomUUID().toString());
|
||||
bean1.setBean2(bean2);
|
||||
bean2.setData2(UUID.randomUUID().toString());
|
||||
String valueAsString;
|
||||
|
|
@ -34,7 +34,7 @@ public class JacksonComparisonTest {
|
|||
for (int i = 0; i < INNERLOOP_COUNT; i++) {
|
||||
bean1 = new Bean1();
|
||||
bean2 = new Bean2();
|
||||
bean1.setData1(UUID.randomUUID());
|
||||
bean1.setData1(UUID.randomUUID().toString());
|
||||
bean1.setBean2(bean2);
|
||||
bean2.setData2(UUID.randomUUID().toString());
|
||||
valueAsString = objectMapper.writeValueAsString(bean1);
|
||||
|
|
@ -47,7 +47,7 @@ public class JacksonComparisonTest {
|
|||
for (int i = 0; i < INNERLOOP_COUNT; i++) {
|
||||
bean1 = new Bean1();
|
||||
bean2 = new Bean2();
|
||||
bean1.setData1(UUID.randomUUID());
|
||||
bean1.setData1(UUID.randomUUID().toString());
|
||||
bean1.setBean2(bean2);
|
||||
bean2.setData2(UUID.randomUUID().toString());
|
||||
jsonString = Mapper.json(bean1);
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
package nl.sanderhautvast.json.ser;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class LocalDateTest {
|
||||
|
||||
@Test
|
||||
void testLocalDate() {
|
||||
assertEquals("\"2023-10-11\"", Mapper.json(LocalDate.of(2023, 10, 11)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLocalDateTime() {
|
||||
assertEquals("\"2023-10-11T13:15:27\"", Mapper.json(LocalDateTime.of(2023, 10, 11,13,15,27)));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +1,16 @@
|
|||
package nl.sanderhautvast.json.ser.nested;
|
||||
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class Bean1 {
|
||||
private UUID data1;
|
||||
private String data1;
|
||||
private Bean2 bean2;
|
||||
|
||||
public UUID getData1() {
|
||||
public String getData1() {
|
||||
return data1;
|
||||
}
|
||||
|
||||
public void setData1(UUID data1) {
|
||||
public void setData1(String data1) {
|
||||
this.data1 = data1;
|
||||
}
|
||||
|
||||
|
|
|
|||
16
pom.xml
16
pom.xml
|
|
@ -5,22 +5,20 @@
|
|||
<name>JsonToy</name>
|
||||
<groupId>nl.sander</groupId>
|
||||
<artifactId>jsonthingy-pom</artifactId>
|
||||
<version>1.7</version>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>9</maven.compiler.source>
|
||||
<maven.compiler.target>9</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>lib</module>
|
||||
<module>jmh</module>
|
||||
</modules>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>github</id>
|
||||
<name>GitHub OWNER Apache Maven Packages</name>
|
||||
<url>https://maven.pkg.github.com/shautvast/JsonToy</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue