@SuppressWarnings("unchecked") public EitherTypeArgument(Gson gson, Type type) { this.typeToken = (TypeToken<T>) TypeToken.get(type); this.adapter = gson.getAdapter(this.typeToken); this.expectedTokens = new HashSet<>(); for (Type expectedType : TypeUtils.getExpectedTypes(type)) { Class<?> rawType = TypeToken.get(expectedType).getRawType(); JsonToken expectedToken = getExpectedToken(rawType); expectedTokens.add(expectedToken); } }
protected Either<L, R> create(JsonToken nextToken, JsonReader in) throws IOException { boolean matchesLeft = left.isAssignable(nextToken); boolean matchesRight = right.isAssignable(nextToken); if (matchesLeft && matchesRight) { if (leftChecker != null || rightChecker != null) { JsonElement element = new JsonParser().parse(in); if (leftChecker != null && leftChecker.test(element)) // Parse the left alternative from the JSON element tree return createLeft(left.read(element)); if (rightChecker != null && rightChecker.test(element)) // Parse the right alternative from the JSON element tree return createRight(right.read(element)); } throw new JsonParseException("Ambiguous Either type: token " + nextToken + " matches both alternatives."); } else if (matchesLeft) { // Parse the left alternative from the JSON stream return createLeft(left.read(in)); } else if (matchesRight) { // Parse the right alternative from the JSON stream return createRight(right.read(in)); } else { throw new JsonParseException("Unexpected token " + nextToken + ": expected " + left + " | " + right + " tokens."); } }
protected Either<L, R> create(JsonToken nextToken, JsonReader in) throws IOException { boolean matchesLeft = left.isAssignable(nextToken); boolean matchesRight = right.isAssignable(nextToken); if (matchesLeft && matchesRight) { if (leftChecker != null || rightChecker != null) { JsonElement element = new JsonParser().parse(in); if (leftChecker != null && leftChecker.test(element)) // Parse the left alternative from the JSON element tree return createLeft(left.read(element)); if (rightChecker != null && rightChecker.test(element)) // Parse the right alternative from the JSON element tree return createRight(right.read(element)); } throw new JsonParseException("Ambiguous Either type: token " + nextToken + " matches both alternatives."); } else if (matchesLeft) { // Parse the left alternative from the JSON stream return createLeft(left.read(in)); } else if (matchesRight) { // Parse the right alternative from the JSON stream return createRight(right.read(in)); } else { throw new JsonParseException("Unexpected token " + nextToken + ": expected " + left + " | " + right + " tokens."); } }
public EitherTypeAdapter(Gson gson, TypeToken<Either<L, R>> typeToken, Predicate<JsonElement> leftChecker, Predicate<JsonElement> rightChecker) { this.typeToken = typeToken; Type[] elementTypes = TypeUtils.getElementTypes(typeToken, Either.class); this.left = new EitherTypeArgument<L>(gson, elementTypes[0]); this.right = new EitherTypeArgument<R>(gson, elementTypes[1]); this.leftChecker = leftChecker; this.rightChecker = rightChecker; }
@SuppressWarnings("unchecked") public EitherTypeArgument(Gson gson, Type type) { this.typeToken = (TypeToken<T>) TypeToken.get(type); this.adapter = gson.getAdapter(this.typeToken); this.expectedTokens = new HashSet<>(); for (Type expectedType : TypeUtils.getExpectedTypes(type)) { Class<?> rawType = TypeToken.get(expectedType).getRawType(); JsonToken expectedToken = getExpectedToken(rawType); expectedTokens.add(expectedToken); } }
public EitherTypeAdapter(Gson gson, TypeToken<Either<L, R>> typeToken, Predicate<JsonElement> leftChecker, Predicate<JsonElement> rightChecker) { this.typeToken = typeToken; Type[] elementTypes = TypeUtils.getElementTypes(typeToken, Either.class); this.left = new EitherTypeArgument<L>(gson, elementTypes[0]); this.right = new EitherTypeArgument<R>(gson, elementTypes[1]); this.leftChecker = leftChecker; this.rightChecker = rightChecker; }