Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -202,76 +200,157 @@ private String sanitizeKey(@Nonnull final String key) {
}

@Nullable public <T> List<T> getCollectionOfPrimitiveValues(@Nonnull final Class<T> targetClass) {
final List<String> primitiveStringCollection = Arrays.asList(getStringValue().split(","));
final Iterator<String> sourceIterator = primitiveStringCollection.iterator();
final FormParseNode _this = this;
final List<T> result = new ArrayList<>();
final Iterable<T> iterable =
new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
@Override
public boolean hasNext() {
return sourceIterator.hasNext();
}

@Override
@SuppressWarnings("unchecked")
public T next() {
final String item = sourceIterator.next();
final Consumer<Parsable> onBefore =
_this.getOnBeforeAssignFieldValues();
final Consumer<Parsable> onAfter =
_this.getOnAfterAssignFieldValues();
final FormParseNode itemNode =
new FormParseNode(item) {
{
this.setOnBeforeAssignFieldValues(onBefore);
this.setOnAfterAssignFieldValues(onAfter);
}
};
if (targetClass == Boolean.class) {
return (T) itemNode.getBooleanValue();
} else if (targetClass == Short.class) {
return (T) itemNode.getShortValue();
} else if (targetClass == Byte.class) {
return (T) itemNode.getByteValue();
} else if (targetClass == BigDecimal.class) {
return (T) itemNode.getBigDecimalValue();
} else if (targetClass == String.class) {
return (T) itemNode.getStringValue();
} else if (targetClass == Integer.class) {
return (T) itemNode.getIntegerValue();
} else if (targetClass == Float.class) {
return (T) itemNode.getFloatValue();
} else if (targetClass == Long.class) {
return (T) itemNode.getLongValue();
} else if (targetClass == UUID.class) {
return (T) itemNode.getUUIDValue();
} else if (targetClass == OffsetDateTime.class) {
return (T) itemNode.getOffsetDateTimeValue();
} else if (targetClass == LocalDate.class) {
return (T) itemNode.getLocalDateValue();
} else if (targetClass == LocalTime.class) {
return (T) itemNode.getLocalTimeValue();
} else if (targetClass == PeriodAndDuration.class) {
return (T) itemNode.getPeriodAndDurationValue();
} else {
throw new RuntimeException(
"unknown type to deserialize " + targetClass.getName());
}
}
};
}
};

for (T elem : iterable) {
result.add(elem);
final String[] primitiveStringCollection = getStringValue().split(",");
final List<T> result = new ArrayList<>(primitiveStringCollection.length);
for (final String item : primitiveStringCollection) {
String decodedItem;
try {
decodedItem = URLDecoder.decode(item, encoding);
} catch (UnsupportedEncodingException e) {
decodedItem = item;
}
result.add(convertPrimitiveValue(decodedItem, targetClass));
}
return result;
}

@SuppressWarnings("unchecked")
@Nonnull private static <T> T convertPrimitiveValue(
@Nullable final String value, @Nonnull final Class<T> targetClass) {
if (targetClass == Boolean.class) {
return (T) parseBooleanValue(value);
} else if (targetClass == Short.class) {
return (T) parseShortValue(value);
} else if (targetClass == Byte.class) {
return (T) parseByteValue(value);
} else if (targetClass == BigDecimal.class) {
return (T) parseBigDecimalValue(value);
} else if (targetClass == String.class) {
return (T) (value != null && value.equalsIgnoreCase("null") ? null : value);
} else if (targetClass == Integer.class) {
return (T) parseIntegerValue(value);
} else if (targetClass == Float.class) {
return (T) parseFloatValue(value);
} else if (targetClass == Long.class) {
return (T) parseLongValue(value);
} else if (targetClass == UUID.class) {
return (T) parseUUIDValue(value);
} else if (targetClass == OffsetDateTime.class) {
return (T) parseOffsetDateTimeValue(value);
} else if (targetClass == LocalDate.class) {
return (T) parseLocalDateValue(value);
} else if (targetClass == LocalTime.class) {
return (T) parseLocalTimeValue(value);
} else if (targetClass == PeriodAndDuration.class) {
return (T) parsePeriodAndDurationValue(value);
} else {
throw new RuntimeException("unknown type to deserialize " + targetClass.getName());
}
}

@Nullable private static Boolean parseBooleanValue(@Nullable final String value) {
Comment thread
baywet marked this conversation as resolved.
if (value == null) return null;
switch (value.toLowerCase(Locale.ROOT)) {
case "true":
case "1":
return true;
case "false":
case "0":
return false;
default:
return null;
}
}

@Nullable private static Byte parseByteValue(@Nullable final String value) {
if (value == null) return null;
try {
return Byte.parseByte(value);
} catch (final NumberFormatException ex) {
return null;
}
}

@Nullable private static Short parseShortValue(@Nullable final String value) {
if (value == null) return null;
try {
return Short.parseShort(value);
} catch (final NumberFormatException ex) {
return null;
}
}

@Nullable private static BigDecimal parseBigDecimalValue(@Nullable final String value) {
if (value == null) return null;
try {
return new BigDecimal(value);
} catch (final NumberFormatException ex) {
return null;
}
}

@Nullable private static Integer parseIntegerValue(@Nullable final String value) {
if (value == null) return null;
try {
return Integer.parseInt(value);
} catch (final NumberFormatException ex) {
return null;
}
}

@Nullable private static Float parseFloatValue(@Nullable final String value) {
if (value == null) return null;
try {
return Float.parseFloat(value);
} catch (final NumberFormatException ex) {
return null;
}
}

@Nullable private static Long parseLongValue(@Nullable final String value) {
if (value == null) return null;
try {
return Long.parseLong(value);
} catch (final NumberFormatException ex) {
return null;
}
}

@Nullable private static UUID parseUUIDValue(@Nullable final String value) {
if (value == null) return null;
return UUID.fromString(value);
}

@Nullable private static OffsetDateTime parseOffsetDateTimeValue(@Nullable final String value) {
if (value == null) return null;
try {
return OffsetDateTime.parse(value);
} catch (DateTimeParseException ex) {
try {
LocalDateTime localDateTime = LocalDateTime.parse(value);
return localDateTime.atOffset(ZoneOffset.UTC);
} catch (DateTimeParseException ex2) {
throw ex;
}
}
}

@Nullable private static LocalDate parseLocalDateValue(@Nullable final String value) {
if (value == null) return null;
return LocalDate.parse(value);
}

@Nullable private static LocalTime parseLocalTimeValue(@Nullable final String value) {
if (value == null) return null;
return LocalTime.parse(value);
}

@Nullable private static PeriodAndDuration parsePeriodAndDurationValue(
@Nullable final String value) {
if (value == null) return null;
return PeriodAndDuration.parse(value);
}

@Nullable public <T extends Parsable> List<T> getCollectionOfObjectValues(
@Nonnull final ParsableFactory<T> factory) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,10 @@ public JsonParseNode(@Nonnull final JsonElement node, @Nonnull final Gson gson)
return gson.fromJson(currentNode, PeriodAndDuration.class);
}

@Nullable private <T> T getPrimitiveValue(
@Nonnull final Class<T> targetClass, @Nonnull final JsonParseNode itemNode) {
return gson.fromJson(itemNode.currentNode, targetClass);
}

private <T> List<T> iterateOnArray(JsonElement jsonElement, Function<JsonParseNode, T> fn) {
JsonArray array = jsonElement.getAsJsonArray();
final Iterator<JsonElement> sourceIterator = array.iterator();
final List<T> result = new ArrayList<>();
final List<T> result = new ArrayList<>(array.size());
while (sourceIterator.hasNext()) {
final JsonElement item = sourceIterator.next();
final JsonParseNode itemNode = createNewNode(item);
Expand All @@ -151,8 +146,12 @@ private <T> List<T> iterateOnArray(JsonElement jsonElement, Function<JsonParseNo
if (currentNode.isJsonNull()) {
return null;
} else if (currentNode.isJsonArray()) {
return iterateOnArray(
currentNode, itemNode -> getPrimitiveValue(targetClass, itemNode));
final JsonArray array = currentNode.getAsJsonArray();
final List<T> result = new ArrayList<>(array.size());
for (final JsonElement item : array) {
result.add(gson.fromJson(item, targetClass));
}
return result;
} else throw new RuntimeException("invalid state expected to have an array node");
}

Expand All @@ -172,7 +171,20 @@ private <T> List<T> iterateOnArray(JsonElement jsonElement, Function<JsonParseNo
if (currentNode.isJsonNull()) {
return null;
} else if (currentNode.isJsonArray()) {
return iterateOnArray(currentNode, itemNode -> itemNode.getEnumValue(enumParser));
final JsonArray array = currentNode.getAsJsonArray();
final List<T> result = new ArrayList<>(array.size());
for (final JsonElement item : array) {
if (!item.isJsonNull()) {
final String rawValue = gson.fromJson(item, String.class);
if (rawValue != null && !rawValue.isEmpty()) {
final T value = enumParser.forValue(rawValue);
if (value != null) {
result.add(value);
}
}
}
Comment thread
baywet marked this conversation as resolved.
}
return result;
} else throw new RuntimeException("invalid state expected to have an array node");
}

Expand Down Expand Up @@ -202,17 +214,17 @@ else if (element.isJsonPrimitive()) {
HashMap<String, UntypedNode> propertiesMap = new HashMap<>();
for (final Map.Entry<String, JsonElement> fieldEntry :
element.getAsJsonObject().entrySet()) {
final String fieldKey = fieldEntry.getKey();
final JsonElement fieldValue = fieldEntry.getValue();
final JsonParseNode childNode = createNewNode(fieldValue);
childNode.setOnBeforeAssignFieldValues(this.getOnBeforeAssignFieldValues());
childNode.setOnAfterAssignFieldValues(this.getOnAfterAssignFieldValues());
propertiesMap.put(fieldKey, childNode.getUntypedValue());
propertiesMap.put(fieldEntry.getKey(), getUntypedValue(fieldEntry.getValue()));
}
return new UntypedObject(propertiesMap);

} else if (element.isJsonArray()) {
return new UntypedArray(iterateOnArray(element, JsonParseNode::getUntypedValue));
final JsonArray array = element.getAsJsonArray();
final List<UntypedNode> result = new ArrayList<>(array.size());
for (final JsonElement item : array) {
result.add(getUntypedValue(item));
}
return new UntypedArray(result);
}

throw new RuntimeException(
Expand Down
Loading