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

io.split.api.client.utils.EncodingUtil Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package io.split.api.client.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.split.api.client.exceptions.SplitJsonException;
import io.split.api.dtos.result.FailureDTO;
import io.split.api.dtos.result.ListResultDTO;
import io.split.api.dtos.result.ResultDTO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class EncodingUtil {
    private static final ObjectMapper _mapper = new ObjectMapper();

    public static String encode(Object object) throws SplitJsonException {
        try {
            return _mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new SplitJsonException(e);
        }
    }

    public static  T parse(String json, Class objectType) throws SplitJsonException {
        try {
            return _mapper.readValue(json, objectType);
        } catch (IOException e) {
            throw new SplitJsonException(e);
        }
    }

    public static  List parseList(String jsonString, Class clazz) throws SplitJsonException {
        try {
            Iterator nodes = _mapper.readValue(jsonString, JsonNode.class).elements();
            List list = new ArrayList<>();
            while (nodes.hasNext()) {
                JsonNode ni = nodes.next();
                if (ni.size() > 0) {
                    list.add(_mapper.readValue(ni.toString(), clazz));
                }
            }
            return list;
        } catch (IOException e) {
            throw new SplitJsonException(e);
        }
    }

    public static  ResultDTO parseResult(String jsonString, Class objectType) {
        try {
            JsonNode node = _mapper.readValue(jsonString, JsonNode.class);


            List successful = new ArrayList<>();
            List objectsNode = node.findValues("objects");
            if (objectsNode != null && !objectsNode.isEmpty()) {
                Iterator successfulNodes = node.findValues("objects").get(0).elements();
                while (successfulNodes.hasNext()) {
                    JsonNode ni = successfulNodes.next();
                    if (ni.size() > 0) {
                        successful.add(_mapper.readValue(ni.toString(), objectType));
                    }
                }
            }

            List> failed = new ArrayList<>();
            List failedNode = node.findValues("failed");
            if (failedNode != null && !failedNode.isEmpty()) {
                Iterator failedNodes = failedNode.get(0).elements();
                while (failedNodes.hasNext()) {
                    JsonNode ni = failedNodes.next();
                    if (ni.size() > 0) {
                        int status = ni.findValue("status").asInt();
                        String message = ni.findValue("message").asText();
                        T object = _mapper.readValue(ni.findValues("object").get(0).toString(), objectType);
                        failed.add(FailureDTO.builder()
                                .status(status)
                                .message(message)
                                .object(object)
                                .build()
                        );
                    }
                }
            }

            Map metadata = new HashMap<>();
            List metadataNode = node.findValues("metadata");
            if (metadataNode != null && !metadata.isEmpty()) {
                _mapper.readValue(
                        metadataNode.get(0).toString(),
                        new TypeReference>() {
                        }
                );
            }

            ResultDTO.Builder builder = ResultDTO.builder();
            JsonNode offset = node.findValue("offset");
            if (offset != null) {
                builder.offset(offset.asInt());
            }
            JsonNode limit = node.findValue("limit");
            if (limit != null) {
                builder.limit(limit.asInt());
            }

            JsonNode count = node.findValue("count");
            if (count != null) {
                builder.count(count.asInt());
            }

            JsonNode total = node.findValue("total");
            if (total != null) {
                builder.total(total.asInt());
            }

            return builder
                    .objects(successful)
                    .failed(failed)
                    .metadata(metadata)
                    .build();
        } catch (IOException e) {
            throw new SplitJsonException(e);
        }
    }

    public static  ListResultDTO parseListResult(String jsonString, Class objectType) {
        try {
            JsonNode node = _mapper.readValue(jsonString, JsonNode.class);

            List successful = new ArrayList<>();
            List objectsNode = node.findValues("objects");
            if (objectsNode != null && !objectsNode.isEmpty()) {
                Iterator successfulNodes = node.findValues("objects").get(0).elements();
                while (successfulNodes.hasNext()) {
                    JsonNode ni = successfulNodes.next();
                    if (ni.size() > 0) {
                        successful.add(_mapper.readValue(ni.toString(), objectType));
                    }
                }
            }

            ListResultDTO.Builder builder = ListResultDTO.builder();
            JsonNode offset = node.findValue("offset");
            if (offset != null) {
                builder.offset(offset.asInt());
            }
            JsonNode limit = node.findValue("limit");
            if (limit != null) {
                builder.limit(limit.asInt());
            }

            JsonNode totalCount = node.findValue("totalCount");
            if (totalCount != null) {
                builder.totalCount(totalCount.asLong());
            }

            return builder
                    .objects(successful)
                    .build();
        } catch (IOException e) {
            throw new SplitJsonException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy