Add unimi adapters

This commit is contained in:
Andrea Cavalli 2021-06-08 19:14:31 +02:00
parent 379326fd63
commit 9f2feb1195
6 changed files with 235 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package org.warp.commonutils.moshi;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import it.unimi.dsi.fastutil.booleans.BooleanList;
import it.unimi.dsi.fastutil.booleans.BooleanLists;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class BooleanListJsonAdapter extends JsonAdapter<BooleanList> {
@Override
public @NotNull BooleanList fromJson(@NotNull JsonReader reader) throws IOException {
reader.beginArray();
BooleanArrayList modifiableOutput = new BooleanArrayList();
while (reader.hasNext()) {
modifiableOutput.add(reader.nextBoolean());
}
reader.endArray();
return BooleanLists.unmodifiable(modifiableOutput);
}
@Override
public void toJson(@NotNull JsonWriter writer, @Nullable BooleanList value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.beginArray();
for (int i = 0; i < value.size(); i++) {
writer.value(value.getBoolean(i));
}
writer.endArray();
}
}

View File

@ -0,0 +1,39 @@
package org.warp.commonutils.moshi;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.bytes.ByteList;
import it.unimi.dsi.fastutil.bytes.ByteLists;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class ByteListJsonAdapter extends JsonAdapter<ByteList> {
@Override
public @NotNull ByteList fromJson(@NotNull JsonReader reader) throws IOException {
reader.beginArray();
ByteArrayList modifiableOutput = new ByteArrayList();
while (reader.hasNext()) {
modifiableOutput.add((byte) reader.nextInt());
}
reader.endArray();
return ByteLists.unmodifiable(modifiableOutput);
}
@Override
public void toJson(@NotNull JsonWriter writer, @Nullable ByteList value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.beginArray();
for (int i = 0; i < value.size(); i++) {
writer.value((long) value.getByte(i));
}
writer.endArray();
}
}

View File

@ -0,0 +1,39 @@
package org.warp.commonutils.moshi;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.unimi.dsi.fastutil.chars.CharArrayList;
import it.unimi.dsi.fastutil.chars.CharList;
import it.unimi.dsi.fastutil.chars.CharLists;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class CharListJsonAdapter extends JsonAdapter<CharList> {
@Override
public @NotNull CharList fromJson(@NotNull JsonReader reader) throws IOException {
reader.beginArray();
CharArrayList modifiableOutput = new CharArrayList();
while (reader.hasNext()) {
modifiableOutput.add((char) reader.nextInt());
}
reader.endArray();
return CharLists.unmodifiable(modifiableOutput);
}
@Override
public void toJson(@NotNull JsonWriter writer, @Nullable CharList value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.beginArray();
for (int i = 0; i < value.size(); i++) {
writer.value((long) value.getChar(i));
}
writer.endArray();
}
}

View File

@ -0,0 +1,39 @@
package org.warp.commonutils.moshi;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntLists;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class IntListJsonAdapter extends JsonAdapter<IntList> {
@Override
public @NotNull IntList fromJson(@NotNull JsonReader reader) throws IOException {
reader.beginArray();
IntArrayList modifiableOutput = new IntArrayList();
while (reader.hasNext()) {
modifiableOutput.add(reader.nextInt());
}
reader.endArray();
return IntLists.unmodifiable(modifiableOutput);
}
@Override
public void toJson(@NotNull JsonWriter writer, @Nullable IntList value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.beginArray();
for (int i = 0; i < value.size(); i++) {
writer.value((long) value.getInt(i));
}
writer.endArray();
}
}

View File

@ -0,0 +1,39 @@
package org.warp.commonutils.moshi;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import it.unimi.dsi.fastutil.longs.LongLists;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class LongListJsonAdapter extends JsonAdapter<LongList> {
@Override
public @NotNull LongList fromJson(@NotNull JsonReader reader) throws IOException {
reader.beginArray();
LongArrayList modifiableOutput = new LongArrayList();
while (reader.hasNext()) {
modifiableOutput.add(reader.nextLong());
}
reader.endArray();
return LongLists.unmodifiable(modifiableOutput);
}
@Override
public void toJson(@NotNull JsonWriter writer, @Nullable LongList value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.beginArray();
for (int i = 0; i < value.size(); i++) {
writer.value(value.getLong(i));
}
writer.endArray();
}
}

View File

@ -0,0 +1,39 @@
package org.warp.commonutils.moshi;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import it.unimi.dsi.fastutil.shorts.ShortList;
import it.unimi.dsi.fastutil.shorts.ShortLists;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class ShortListJsonAdapter extends JsonAdapter<ShortList> {
@Override
public @NotNull ShortList fromJson(@NotNull JsonReader reader) throws IOException {
reader.beginArray();
ShortArrayList modifiableOutput = new ShortArrayList();
while (reader.hasNext()) {
modifiableOutput.add((short) reader.nextInt());
}
reader.endArray();
return ShortLists.unmodifiable(modifiableOutput);
}
@Override
public void toJson(@NotNull JsonWriter writer, @Nullable ShortList value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.beginArray();
for (int i = 0; i < value.size(); i++) {
writer.value((long) value.getShort(i));
}
writer.endArray();
}
}