Search in sources :

Example 11 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project pom-manipulation-ext by release-engineering.

the class ConfigIO method parse.

public Properties parse(final File workingDir) throws ManipulationException {
    Properties result = new Properties();
    File propertyFile = new File(workingDir, propertyFileString);
    File yamlPMEFile = new File(workingDir, yamlPMEFileString);
    File yamlPNCFile = new File(workingDir, yamlPNCFileString);
    File jsonPNCFile = new File(workingDir, jsonPNCFileString);
    if (propertyFile.exists() && (yamlPMEFile.exists() || yamlPNCFile.exists() || jsonPNCFile.exists())) {
        throw new ManipulationException("Cannot have both yaml, json and property configuration files.");
    } else if (yamlPMEFile.exists() && yamlPNCFile.exists()) {
        throw new ManipulationException("Cannot have both yaml configuration file formats.");
    } else if ((yamlPMEFile.exists() || yamlPNCFile.exists()) && jsonPNCFile.exists()) {
        throw new ManipulationException("Cannot have yaml and json configuration file formats.");
    }
    if (yamlPMEFile.exists()) {
        result = loadYamlFile(yamlPMEFile);
        logger.debug("Read yaml file containing {}.", result);
    } else if (yamlPNCFile.exists()) {
        result = loadYamlFile(yamlPNCFile);
        logger.debug("Read yaml file containing {}.", result);
    } else if (jsonPNCFile.exists()) {
        try {
            result.putAll(JsonPath.parse(jsonPNCFile).read("$.pme", Map.class));
        } catch (IOException | JsonPathException e) {
            throw new ManipulationException("Caught exception processing JSON file.", e);
        }
        logger.debug("Read json file containing {}.", result);
    } else if (propertyFile.exists()) {
        result = loadPropertiesFile(propertyFile);
        logger.debug("Read properties file containing {}.", result);
    }
    return result;
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) JsonPathException(com.jayway.jsonpath.JsonPathException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) YamlFile(org.commonjava.maven.ext.common.model.YamlFile) Map(java.util.Map)

Example 12 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.

the class JsonOrgJsonProvider method getPropertyKeys.

@Override
public Collection<String> getPropertyKeys(Object obj) {
    JSONObject jsonObject = toJsonObject(obj);
    List<String> keys = new ArrayList<String>();
    try {
        for (int i = 0; i < jsonObject.names().length(); i++) {
            String key = (String) jsonObject.names().get(i);
            keys.add(key);
        }
        return keys;
    } catch (JSONException e) {
        throw new JsonPathException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException)

Example 13 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.

the class JsonOrgJsonProvider method toIterable.

@Override
public Iterable<?> toIterable(Object obj) {
    try {
        if (isArray(obj)) {
            JSONArray arr = toJsonArray(obj);
            List<Object> values = new ArrayList<Object>(arr.length());
            for (int i = 0; i < arr.length(); i++) {
                values.add(unwrap(arr.get(i)));
            }
            return values;
        } else {
            JSONObject jsonObject = toJsonObject(obj);
            List<Object> values = new ArrayList<Object>();
            for (int i = 0; i < jsonObject.names().length(); i++) {
                String key = (String) jsonObject.names().get(i);
                Object val = jsonObject.get(key);
                values.add(unwrap(val));
            }
            return values;
        }
    } catch (JSONException e) {
        throw new JsonPathException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException) JSONObject(org.json.JSONObject)

Example 14 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by json-path.

the class IsJson method describeMismatchSafely.

@Override
protected void describeMismatchSafely(T json, Description mismatchDescription) {
    try {
        ReadContext context = parse(json);
        jsonMatcher.describeMismatch(context, mismatchDescription);
    } catch (JsonPathException e) {
        buildMismatchDescription(json, mismatchDescription, e);
    } catch (IOException e) {
        buildMismatchDescription(json, mismatchDescription, e);
    }
}
Also used : ReadContext(com.jayway.jsonpath.ReadContext) JsonPathException(com.jayway.jsonpath.JsonPathException) IOException(java.io.IOException)

Example 15 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by jayway.

the class JsonOrgJsonProvider method setProperty.

@Override
public void setProperty(Object obj, Object key, Object value) {
    try {
        if (isMap(obj))
            toJsonObject(obj).put(key.toString(), createJsonElement(value));
        else {
            JSONArray array = toJsonArray(obj);
            int index;
            if (key != null) {
                index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
            } else {
                index = array.length();
            }
            if (index == array.length()) {
                array.put(createJsonElement(value));
            } else {
                array.put(index, createJsonElement(value));
            }
        }
    } catch (JSONException e) {
        throw new JsonPathException(e);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException)

Aggregations

JsonPathException (com.jayway.jsonpath.JsonPathException)18 JSONException (org.json.JSONException)8 JSONObject (org.json.JSONObject)6 ArrayList (java.util.ArrayList)4 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)4 JSONArray (org.json.JSONArray)4 DocumentContext (com.jayway.jsonpath.DocumentContext)3 IOException (java.io.IOException)3 ReadContext (com.jayway.jsonpath.ReadContext)2 File (java.io.File)2 StringWriter (java.io.StringWriter)2 List (java.util.List)2 Test (org.junit.Test)2 Parameter (com.jayway.jsonpath.internal.function.Parameter)1 AllErrors (gov.cms.qpp.conversion.model.error.AllErrors)1 Error (gov.cms.qpp.conversion.model.error.Error)1 Map (java.util.Map)1 Properties (java.util.Properties)1 YamlFile (org.commonjava.maven.ext.common.model.YamlFile)1