pl.jalokim.propertiestojson.resolvers.primitives.string.TextToElementsResolver Maven / Gradle / Ivy
package pl.jalokim.propertiestojson.resolvers.primitives.string;
import pl.jalokim.propertiestojson.resolvers.PrimitiveJsonTypesResolver;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static java.lang.String.join;
import static pl.jalokim.propertiestojson.Constants.EMPTY_STRING;
import static pl.jalokim.propertiestojson.Constants.SIMPLE_ARRAY_DELIMITER;
import static pl.jalokim.propertiestojson.resolvers.primitives.utils.JsonObjectHelper.hasJsonArraySignature;
import static pl.jalokim.propertiestojson.resolvers.primitives.utils.JsonObjectHelper.isValidJsonObjectOrArray;
public class TextToElementsResolver implements TextToConcreteObjectResolver> {
private final String arrayElementSeparator;
private final boolean resolveTypeOfEachElement;
public TextToElementsResolver() {
this(true);
}
public TextToElementsResolver(boolean resolveTypeOfEachElement) {
this(resolveTypeOfEachElement, SIMPLE_ARRAY_DELIMITER);
}
public TextToElementsResolver(boolean resolveTypeOfEachElement, String arrayElementSeparator) {
this.resolveTypeOfEachElement = resolveTypeOfEachElement;
this.arrayElementSeparator = arrayElementSeparator;
}
@Override
public Optional> returnObjectWhenCanBeResolved(PrimitiveJsonTypesResolver primitiveJsonTypesResolver, String propertyValue, String propertyKey) {
if(isSimpleArray(propertyValue) && !isValidJsonObjectOrArray(propertyValue)) {
if(hasJsonArraySignature(propertyValue)) {
propertyValue = propertyValue
.replaceAll("]\\s*$", EMPTY_STRING)
.replaceAll("^\\s*\\[\\s*", EMPTY_STRING);
String[] elements = propertyValue.split(arrayElementSeparator);
List clearedElements = new ArrayList<>();
for(String element : elements) {
element = element.trim();
clearedElements.add(element);
}
propertyValue = join(arrayElementSeparator, clearedElements);
}
List