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

org.mockserver.serialization.JsonArraySerializer Maven / Gradle / Ivy

There is a newer version: 5.15.0
Show newest version
package org.mockserver.serialization;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.github.fge.jackson.JacksonUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jamesdbloom
 */
public class JsonArraySerializer {
    private static final ObjectMapper objectMapper = ObjectMapperFactory.createObjectMapper();

    public List returnJSONObjects(String jsonArray) {
        List arrayItems = new ArrayList();
        try {
            JsonNode jsonNode = objectMapper.readTree(jsonArray);
            if (jsonNode instanceof ArrayNode) {
                for (JsonNode arrayElement : jsonNode) {
                    arrayItems.add(JacksonUtils.prettyPrint(arrayElement));
                }
            } else {
                arrayItems.add(JacksonUtils.prettyPrint(jsonNode));
            }
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
        return arrayItems;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy