Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/net/roxymc/jserialize/AbstractReader.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.roxymc.jserialize;

import net.roxymc.jserialize.token.TokenType;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Predicate;

import static java.lang.String.format;

@ApiStatus.NonExtendable
public abstract class AbstractReader implements Reader {
protected final void checkToken(TokenType current, TokenType expected) {
if (current != expected) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/net/roxymc/jserialize/AbstractWriter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.roxymc.jserialize;

import net.roxymc.jserialize.token.TokenType;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.NonExtendable
public abstract class AbstractWriter implements Writer {
protected final UnsupportedOperationException notSupported(TokenType tokenType) {
return new UnsupportedOperationException(tokenType + " is not supported");
Expand Down
55 changes: 43 additions & 12 deletions core/src/main/java/net/roxymc/jserialize/Reader.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,66 @@
package net.roxymc.jserialize;

import net.roxymc.jserialize.token.TokenType;
import net.roxymc.jserialize.token.TokenTypes;
import org.jetbrains.annotations.ApiStatus;

import java.io.IOException;

@ApiStatus.NonExtendable
public interface Reader {
TokenType peek() throws IOException;

String readName() throws IOException;
void read(TokenType.NonValued tokenType) throws IOException;

void readObjectStart() throws IOException;
<T> T read(TokenType.Valued<T> tokenType) throws IOException;

void readObjectEnd() throws IOException;
default String readName() throws IOException {
return read(TokenTypes.NAME);
}

void readArrayStart() throws IOException;
default void readObjectStart() throws IOException {
read(TokenTypes.OBJECT_START);
}

void readArrayEnd() throws IOException;
default void readObjectEnd() throws IOException {
read(TokenTypes.OBJECT_END);
}

String readString() throws IOException;
default void readArrayStart() throws IOException {
read(TokenTypes.ARRAY_START);
}

boolean readBoolean() throws IOException;
default void readArrayEnd() throws IOException {
read(TokenTypes.ARRAY_END);
}

int readInt() throws IOException;
default String readString() throws IOException {
return read(TokenTypes.STRING);
}

long readLong() throws IOException;
default boolean readBoolean() throws IOException {
return read(TokenTypes.BOOLEAN);
}

double readDouble() throws IOException;
default int readInt() throws IOException {
return read(TokenTypes.INT);
}

byte[] readBinary() throws IOException;
default long readLong() throws IOException {
return read(TokenTypes.LONG);
}

void readNull() throws IOException;
default double readDouble() throws IOException {
return read(TokenTypes.DOUBLE);
}

default byte[] readBinary() throws IOException {
return read(TokenTypes.BINARY);
}

default void readNull() throws IOException {
read(TokenTypes.NULL);
}

void skipValue() throws IOException;
}
57 changes: 45 additions & 12 deletions core/src/main/java/net/roxymc/jserialize/Writer.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,62 @@
package net.roxymc.jserialize;

import net.roxymc.jserialize.token.TokenType;
import net.roxymc.jserialize.token.TokenTypes;
import org.jetbrains.annotations.ApiStatus;

import java.io.IOException;

@ApiStatus.NonExtendable
public interface Writer {
void writeName(String name) throws IOException;
void write(TokenType.NonValued tokenType) throws IOException;

<T> void write(TokenType.Valued<T> tokenType, T value) throws IOException;

default void writeName(String name) throws IOException {
write(TokenTypes.NAME, name);
}

void writeObjectStart() throws IOException;
default void writeObjectStart() throws IOException {
write(TokenTypes.OBJECT_START);
}

void writeObjectEnd() throws IOException;
default void writeObjectEnd() throws IOException {
write(TokenTypes.OBJECT_END);
}

void writeArrayStart() throws IOException;
default void writeArrayStart() throws IOException {
write(TokenTypes.ARRAY_START);
}

void writeArrayEnd() throws IOException;
default void writeArrayEnd() throws IOException {
write(TokenTypes.ARRAY_END);
}

void writeString(String value) throws IOException;
default void writeString(String value) throws IOException {
write(TokenTypes.STRING, value);
}

void writeBoolean(boolean value) throws IOException;
default void writeBoolean(boolean value) throws IOException {
write(TokenTypes.BOOLEAN, value);
}

void writeInt(int value) throws IOException;
default void writeInt(int value) throws IOException {
write(TokenTypes.INT, value);
}

void writeLong(long value) throws IOException;
default void writeLong(long value) throws IOException {
write(TokenTypes.LONG, value);
}

void writeDouble(double value) throws IOException;
default void writeDouble(double value) throws IOException {
write(TokenTypes.DOUBLE, value);
}

void writeBinary(byte[] value) throws IOException;
default void writeBinary(byte[] value) throws IOException {
write(TokenTypes.BINARY, value);
}

void writeNull() throws IOException;
default void writeNull() throws IOException {
write(TokenTypes.NULL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.roxymc.jserialize.adapter.ReadContext;
import net.roxymc.jserialize.adapter.TypeAdapter;
import net.roxymc.jserialize.adapter.WriteContext;
import net.roxymc.jserialize.token.TokenType;
import net.roxymc.jserialize.token.TokenTypes;
import net.roxymc.jserialize.type.TypeRef;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static TypeAdapter.Factory factory(CollectionProvider... providers) {
private <E extends @Nullable Object> @Nullable Collection<E> mutate0(
Reader reader, TypeRef<? extends Collection<?>> type, @Nullable Collection<E> collection, ReadContext ctx
) throws IOException {
if (reader.peek() == TokenType.NULL) {
if (reader.peek() == TokenTypes.NULL) {
reader.readNull();
return collection;
}
Expand All @@ -67,7 +67,7 @@ public static TypeAdapter.Factory factory(CollectionProvider... providers) {

reader.readArrayStart();

for (int index = 0; reader.peek() != TokenType.ARRAY_END; index++) {
for (int index = 0; reader.peek() != TokenTypes.ARRAY_END; index++) {
E element = elementAdapter.read(reader, collectionType.elementType, ctx);

collection.add(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.roxymc.jserialize.adapter.ReadContext;
import net.roxymc.jserialize.adapter.TypeAdapter;
import net.roxymc.jserialize.adapter.WriteContext;
import net.roxymc.jserialize.token.TokenType;
import net.roxymc.jserialize.token.TokenTypes;
import net.roxymc.jserialize.type.TypeRef;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -52,7 +52,7 @@ private <K, V> MapType<K, V> resolveMapType(TypeRef<? extends Map<?, ?>> type) {
private <K extends @Nullable Object, V extends @Nullable Object> @Nullable Map<K, V> mutate0(
Reader reader, TypeRef<? extends Map<?, ?>> type, @Nullable Map<K, V> map, ReadContext ctx
) throws IOException {
if (reader.peek() == TokenType.NULL) {
if (reader.peek() == TokenTypes.NULL) {
reader.readNull();
return map;
}
Expand All @@ -68,7 +68,7 @@ private <K, V> MapType<K, V> resolveMapType(TypeRef<? extends Map<?, ?>> type) {

reader.readObjectStart();

while (reader.peek() == TokenType.NAME) {
while (reader.peek() == TokenTypes.NAME) {
String name = reader.readName();

K key = keyAdapter.decode(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.roxymc.jserialize.adapter.WriteContext;
import net.roxymc.jserialize.annotation.JSerializable;
import net.roxymc.jserialize.model.ClassModel;
import net.roxymc.jserialize.token.TokenType;
import net.roxymc.jserialize.token.TokenTypes;
import net.roxymc.jserialize.type.TypeRef;
import net.roxymc.jserialize.util.TypeUtils;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -46,7 +46,7 @@ public static TypeAdapter.Factory annotatedFactory(ClassModel.Factory factory) {

@Override
public @Nullable T mutate(Reader reader, TypeRef<? extends T> type, @Nullable T value, ReadContext ctx) throws IOException {
if (reader.peek() == TokenType.NULL) {
if (reader.peek() == TokenTypes.NULL) {
reader.readNull();
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.roxymc.jserialize.model.property.PropertyModel;
import net.roxymc.jserialize.model.property.meta.PropertyKind;
import net.roxymc.jserialize.model.property.meta.PropertyMeta;
import net.roxymc.jserialize.token.TokenType;
import net.roxymc.jserialize.token.TokenTypes;
import net.roxymc.jserialize.type.TypeRef;
import net.roxymc.jserialize.util.TypeUtils;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -74,7 +74,7 @@ T read(Reader reader) throws Throwable {
extrasMap = null;
}

while (reader.peek() == TokenType.NAME) {
while (reader.peek() == TokenTypes.NAME) {
String name = reader.readName();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ private void resolveProperty(T instance, PropertyModel property) throws Throwabl
}

if (value instanceof PropertyValue.Mutable) {
@SuppressWarnings("unchecked")
PropertyValue.Mutable<Object> mutableValue = (PropertyValue.Mutable<Object>) value;

Object currentValue = property.getter().get(instance);

//noinspection unchecked,rawtypes
((PropertyValue.Mutable) value).mutate(instance, currentValue);
mutableValue.mutate(instance, currentValue);
return;
}

Expand Down
46 changes: 46 additions & 0 deletions core/src/main/java/net/roxymc/jserialize/format/TokenTypeInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.roxymc.jserialize.format;

import net.roxymc.jserialize.util.IOBiConsumer;
import net.roxymc.jserialize.util.IOConsumer;
import net.roxymc.jserialize.util.IOFunction;

import java.io.IOException;

public interface TokenTypeInfo<R, W> {
final class NonValued<R, W> implements TokenTypeInfo<R, W> {
private final IOConsumer<R> reader;
private final IOConsumer<W> writer;

NonValued(IOConsumer<R> reader, IOConsumer<W> writer) {
this.reader = reader;
this.writer = writer;
}

public void read(R reader) throws IOException {
this.reader.accept(reader);
}

public void write(W writer) throws IOException {
this.writer.accept(writer);
}
}

final class Valued<R, W, T> implements TokenTypeInfo<R, W> {
private final IOFunction<R, T> reader;
private final IOBiConsumer<W, T> writer;

Valued(IOFunction<R, T> reader, IOBiConsumer<W, T> writer) {
this.reader = reader;
this.writer = writer;
}

@SuppressWarnings("DataFlowIssue") // IntelliJ has existential issues
public T read(R reader) throws IOException {
return this.reader.apply(reader);
}

public void write(W writer, T value) throws IOException {
this.writer.accept(writer, value);
}
}
}
Loading
Loading