Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GsonReader: Rethrow exceptions as Gson types #2

Merged
merged 3 commits into from
Mar 7, 2024
Merged
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
69 changes: 54 additions & 15 deletions gson/src/main/java/org/quiltmc/parsers/json/gson/GsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

package org.quiltmc.parsers.json.gson;

import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import org.quiltmc.parsers.json.FormatViolationException;
import org.quiltmc.parsers.json.MalformedSyntaxException;
import org.quiltmc.parsers.json.ParseException;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -27,6 +32,7 @@
*/
public class GsonReader extends JsonReader {
private final org.quiltmc.parsers.json.JsonReader delegate;

/**
* Creates a new instance that reads a JSON-encoded stream from {@code in}.
*
Expand All @@ -40,34 +46,67 @@ public org.quiltmc.parsers.json.JsonReader getDelegate() {
return delegate;
}

@FunctionalInterface
private interface DelegateFunction<T> {
T call() throws IOException;
}

@FunctionalInterface
private interface DelegateAction extends DelegateFunction<Void> {
void run() throws IOException;

@Override
default Void call() throws IOException
{
run();
return null;
}
}

private <T> T rethrowGsonExceptions(DelegateFunction<T> func) throws IOException {
try {
return func.call();
} catch (MalformedSyntaxException | FormatViolationException e) {
throw new JsonSyntaxException(e.getMessage(), e);
} catch (ParseException e) {
throw new JsonParseException(e.getMessage(), e);
}
}

private void rethrowGsonExceptionsVoid(DelegateAction action) throws IOException {
rethrowGsonExceptions(action);
}

@Override
public void beginArray() throws IOException {
delegate.beginArray();
rethrowGsonExceptionsVoid(delegate::beginArray);
}

@Override
public void endArray() throws IOException {
delegate.endArray();
rethrowGsonExceptionsVoid(delegate::endArray);
}

@Override
public void beginObject() throws IOException {
delegate.beginObject();
rethrowGsonExceptionsVoid(delegate::beginObject);
}

@Override
public void endObject() throws IOException {
delegate.endObject();
rethrowGsonExceptionsVoid(delegate::endObject);
}

@Override
public boolean hasNext() throws IOException {
return delegate.hasNext();
return rethrowGsonExceptions(delegate::hasNext);
}

@Override
public JsonToken peek() throws IOException {
switch (delegate.peek()) {
var quiltToken = rethrowGsonExceptions(delegate::peek);

switch (quiltToken) {
case BEGIN_ARRAY:
return JsonToken.BEGIN_ARRAY;
case END_ARRAY:
Expand All @@ -89,43 +128,43 @@ public JsonToken peek() throws IOException {
case END_DOCUMENT:
return JsonToken.END_DOCUMENT;
default:
throw new IllegalArgumentException();
throw new IllegalStateException("Delegate returned unrecognized token " + quiltToken);
}
}

@Override
public String nextName() throws IOException {
return delegate.nextName();
return rethrowGsonExceptions(delegate::nextName);
}

@Override
public String nextString() throws IOException {
return delegate.nextString();
return rethrowGsonExceptions(delegate::nextString);
}

@Override
public boolean nextBoolean() throws IOException {
return delegate.nextBoolean();
return rethrowGsonExceptions(delegate::nextBoolean);
}

@Override
public void nextNull() throws IOException {
delegate.nextNull();
rethrowGsonExceptionsVoid(delegate::nextNull);
}

@Override
public double nextDouble() throws IOException {
return delegate.nextDouble();
return rethrowGsonExceptions(delegate::nextDouble);
}

@Override
public long nextLong() throws IOException {
return delegate.nextLong();
return rethrowGsonExceptions(delegate::nextLong);
}

@Override
public int nextInt() throws IOException {
return delegate.nextInt();
return rethrowGsonExceptions(delegate::nextInt);
}

@Override
Expand All @@ -136,7 +175,7 @@ public void close() throws IOException {

@Override
public void skipValue() throws IOException {
delegate.skipValue();
rethrowGsonExceptionsVoid(delegate::skipValue);
}

@Override
Expand Down
Loading