diff --git a/src/main/java/it/cavallium/dbengine/client/IntOpenHashSetJsonAdapter.java b/src/main/java/it/cavallium/dbengine/client/IntOpenHashSetJsonAdapter.java new file mode 100644 index 0000000..8ac66f8 --- /dev/null +++ b/src/main/java/it/cavallium/dbengine/client/IntOpenHashSetJsonAdapter.java @@ -0,0 +1,35 @@ +package it.cavallium.dbengine.client; + +import com.squareup.moshi.JsonReader; +import com.squareup.moshi.JsonWriter; +import it.cavallium.data.generator.nativedata.Int52; +import it.unimi.dsi.fastutil.ints.IntOpenHashSet; +import java.io.IOException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class IntOpenHashSetJsonAdapter extends com.squareup.moshi.JsonAdapter { + + @Nullable + @Override + public IntOpenHashSet fromJson(@NotNull JsonReader reader) throws IOException { + var intOpenHashSet = new IntOpenHashSet(); + while (reader.hasNext()) { + intOpenHashSet.add(reader.nextInt()); + } + return intOpenHashSet; + } + + @Override + public void toJson(@NotNull JsonWriter writer, @Nullable IntOpenHashSet value) throws IOException { + if (value == null) { + writer.nullValue(); + } else { + writer.beginArray(); + for (int integer : value) { + writer.value(integer); + } + writer.endArray(); + } + } +} diff --git a/src/main/java/it/cavallium/dbengine/client/query/QueryMoshi.java b/src/main/java/it/cavallium/dbengine/client/query/QueryMoshi.java index 5da3da0..9ecfa7d 100644 --- a/src/main/java/it/cavallium/dbengine/client/query/QueryMoshi.java +++ b/src/main/java/it/cavallium/dbengine/client/query/QueryMoshi.java @@ -1,21 +1,43 @@ package it.cavallium.dbengine.client.query; +import com.squareup.moshi.JsonAdapter; +import it.cavallium.dbengine.client.IntOpenHashSetJsonAdapter; import it.cavallium.dbengine.client.query.current.CurrentVersion; import it.cavallium.dbengine.client.query.current.data.IBasicType; import it.cavallium.dbengine.client.query.current.data.IType; +import it.unimi.dsi.fastutil.booleans.BooleanList; +import it.unimi.dsi.fastutil.bytes.ByteList; +import it.unimi.dsi.fastutil.chars.CharList; +import it.unimi.dsi.fastutil.ints.IntList; +import it.unimi.dsi.fastutil.ints.IntOpenHashSet; +import it.unimi.dsi.fastutil.longs.LongList; +import it.unimi.dsi.fastutil.shorts.ShortList; +import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; +import org.warp.commonutils.moshi.BooleanListJsonAdapter; +import org.warp.commonutils.moshi.ByteListJsonAdapter; +import org.warp.commonutils.moshi.CharListJsonAdapter; +import org.warp.commonutils.moshi.IntListJsonAdapter; +import org.warp.commonutils.moshi.LongListJsonAdapter; import org.warp.commonutils.moshi.MoshiPolymorphic; +import org.warp.commonutils.moshi.ShortListJsonAdapter; public class QueryMoshi extends MoshiPolymorphic { private final Set> abstractClasses; private final Set> concreteClasses; + private final Map, JsonAdapter> extraAdapters; @SuppressWarnings({"unchecked", "RedundantCast", "rawtypes"}) public QueryMoshi() { + super(true, true); HashSet> abstractClasses = new HashSet<>(); HashSet> concreteClasses = new HashSet<>(); + + // Add all super types with their implementations for (var superTypeClass : CurrentVersion.getSuperTypeClasses()) { for (Class superTypeSubtypesClass : CurrentVersion.getSuperTypeSubtypesClasses( superTypeClass)) { @@ -23,8 +45,29 @@ public class QueryMoshi extends MoshiPolymorphic { } abstractClasses.add((Class) (Class) superTypeClass); } + + // Add IBasicType with all basic types + abstractClasses.add((Class) (Class) IBasicType.class); + for (BasicType basicType : BasicType.values()) { + concreteClasses.add((Class) (Class) CurrentVersion.VERSION.getClass(basicType)); + } + this.abstractClasses = abstractClasses; this.concreteClasses = concreteClasses; + Map, JsonAdapter> extraAdapters = new HashMap<>(); + extraAdapters.put(BooleanList.class, new BooleanListJsonAdapter()); + extraAdapters.put(ByteList.class, new ByteListJsonAdapter()); + extraAdapters.put(ShortList.class, new ShortListJsonAdapter()); + extraAdapters.put(CharList.class, new CharListJsonAdapter()); + extraAdapters.put(IntList.class, new IntListJsonAdapter()); + extraAdapters.put(LongList.class, new LongListJsonAdapter()); + extraAdapters.put(IntOpenHashSet.class, new IntOpenHashSetJsonAdapter()); + this.extraAdapters = Collections.unmodifiableMap(extraAdapters); + } + + @Override + public Map, JsonAdapter> getExtraAdapters() { + return extraAdapters; } @Override @@ -39,6 +82,6 @@ public class QueryMoshi extends MoshiPolymorphic { @Override protected boolean shouldIgnoreField(String fieldName) { - return false; + return fieldName.contains("$"); } } \ No newline at end of file