2024-04-12 21:19:36 +02:00
|
|
|
package io.gitlab.jfronny.muscript.json.impl.typed;
|
|
|
|
|
|
|
|
import io.gitlab.jfronny.commons.serialize.MalformedDataException;
|
|
|
|
import io.gitlab.jfronny.commons.serialize.SerializeReader;
|
|
|
|
import io.gitlab.jfronny.commons.serialize.SerializeWriter;
|
|
|
|
import io.gitlab.jfronny.commons.serialize.Token;
|
2024-04-13 13:49:00 +02:00
|
|
|
import io.gitlab.jfronny.commons.serialize.databind.api.SerializerFor;
|
|
|
|
import io.gitlab.jfronny.commons.serialize.databind.api.TypeAdapter;
|
2024-04-12 21:19:36 +02:00
|
|
|
import io.gitlab.jfronny.muscript.data.additional.DFinal;
|
|
|
|
import io.gitlab.jfronny.muscript.data.dynamic.DList;
|
|
|
|
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
|
2024-04-13 12:27:57 +02:00
|
|
|
import io.gitlab.jfronny.muscript.json.impl.DynamicTypeAdapterHolder;
|
2024-04-12 21:19:36 +02:00
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@SerializerFor(targets = DList.class, hierarchical = true)
|
2024-04-13 12:27:57 +02:00
|
|
|
public class DListTypeAdapter extends TypeAdapter<DList> {
|
2024-04-12 21:19:36 +02:00
|
|
|
@Override
|
2024-04-18 21:42:57 +02:00
|
|
|
public <TEx extends Exception, Writer extends SerializeWriter<TEx, Writer>> void serialize(DList value, Writer writer) throws TEx, MalformedDataException {
|
2024-04-12 21:19:36 +02:00
|
|
|
writer.beginArray();
|
|
|
|
for (Dynamic v : value.getValue()) {
|
2024-04-13 12:27:57 +02:00
|
|
|
DynamicTypeAdapterHolder.getInstance().serialize(v, writer);
|
2024-04-12 21:19:36 +02:00
|
|
|
}
|
|
|
|
writer.endArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-18 21:42:57 +02:00
|
|
|
public <TEx extends Exception, Reader extends SerializeReader<TEx, Reader>> DList deserialize(Reader reader) throws TEx, MalformedDataException {
|
2024-04-12 21:19:36 +02:00
|
|
|
List<Dynamic> elements = new LinkedList<>();
|
|
|
|
reader.beginArray();
|
|
|
|
while (reader.peek() != Token.END_ARRAY) {
|
2024-04-13 12:27:57 +02:00
|
|
|
elements.add(DynamicTypeAdapterHolder.getInstance().deserialize(reader));
|
2024-04-12 21:19:36 +02:00
|
|
|
}
|
|
|
|
reader.endArray();
|
|
|
|
return DFinal.of(elements);
|
|
|
|
}
|
|
|
|
}
|