All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.jalokim.propertiestojson.JsonObjectsTraverseResolver Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package pl.jalokim.propertiestojson;

import java.util.Map;
import pl.jalokim.propertiestojson.object.AbstractJsonType;
import pl.jalokim.propertiestojson.object.ObjectJsonType;
import pl.jalokim.propertiestojson.object.SkipJsonField;
import pl.jalokim.propertiestojson.path.PathMetadata;
import pl.jalokim.propertiestojson.resolvers.JsonTypeResolver;
import pl.jalokim.propertiestojson.resolvers.PrimitiveJsonTypesResolver;
import pl.jalokim.propertiestojson.resolvers.transfer.DataForResolve;

public class JsonObjectsTraverseResolver {

    private final Map algorithms;
    private final PrimitiveJsonTypesResolver primitiveJsonTypesResolver;
    private Map properties;
    private String propertyKey;
    private PathMetadata rootPathMetaData;
    private ObjectJsonType currentObjectJsonType;

    public JsonObjectsTraverseResolver(Map algorithms,
        Map properties, String propertyKey,
        PathMetadata rootPathMetaData, ObjectJsonType coreObjectJsonType) {
        this.properties = properties;
        this.propertyKey = propertyKey;
        this.rootPathMetaData = rootPathMetaData;
        this.currentObjectJsonType = coreObjectJsonType;
        this.algorithms = algorithms;
        this.primitiveJsonTypesResolver = (PrimitiveJsonTypesResolver) algorithms.get(AlgorithmType.PRIMITIVE);
    }

    public void initializeFieldsInJson() {
        PathMetadata currentPathMetaData = rootPathMetaData;
        Object valueFromProperties = properties.get(currentPathMetaData.getOriginalPropertyKey());
        if (valueFromProperties instanceof SkipJsonField) {
            return;

        }
        AbstractJsonType resolverJsonObject = primitiveJsonTypesResolver
            .resolvePrimitiveTypeAndReturn(valueFromProperties, currentPathMetaData.getOriginalPropertyKey());
        if (resolverJsonObject instanceof SkipJsonField && !rootPathMetaData.getLeaf().isArrayField()) {
            return;
        } else {
            rootPathMetaData.getLeaf().setJsonValue(resolverJsonObject);
        }
        rootPathMetaData.getLeaf().setRawValue(properties.get(propertyKey));

        while (currentPathMetaData != null) {
            DataForResolve dataForResolve = new DataForResolve(properties, propertyKey, currentObjectJsonType, currentPathMetaData);
            currentObjectJsonType = algorithms.get(resolveAlgorithm(currentPathMetaData))
                .traverseOnObjectAndInitByField(dataForResolve);
            currentPathMetaData = currentPathMetaData.getChild();
        }
    }

    private AlgorithmType resolveAlgorithm(PathMetadata currentPathMetaData) {
        if (isPrimitiveField(currentPathMetaData)) {
            return AlgorithmType.PRIMITIVE;
        }
        if (currentPathMetaData.isArrayField()) {
            return AlgorithmType.ARRAY;
        }
        return AlgorithmType.OBJECT;
    }


    private boolean isPrimitiveField(PathMetadata currentPathMetaData) {
        return currentPathMetaData.isLeaf();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy