Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cs-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<groupId>io.cloudslang.content</groupId>
<artifactId>cs-json</artifactId>
<version>0.0.19-SNAPSHOT</version>
<version>0.0.20-SNAPSHOT</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down Expand Up @@ -111,8 +111,17 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.4.7</version>
</dependency>
<!-- Json provider required for Jayway in order to handle single quoted JSONs-->
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.cloudslang.content.json.actions;

import com.fasterxml.jackson.databind.JsonNode;
import com.hp.oo.sdk.content.annotations.Action;
import com.hp.oo.sdk.content.annotations.Output;
import com.hp.oo.sdk.content.annotations.Param;
Expand All @@ -28,6 +27,7 @@
import io.cloudslang.content.constants.ReturnCodes;
import io.cloudslang.content.json.services.JsonService;
import io.cloudslang.content.json.utils.Constants;
import io.cloudslang.content.json.utils.StringUtils;
import io.cloudslang.content.utils.OutputUtilities;

import java.util.Map;
Expand Down Expand Up @@ -61,8 +61,8 @@ public Map<String, String> execute(
@Param(value = Constants.InputNames.JSON_OBJECT, required = true) String jsonObject,
@Param(value = Constants.InputNames.JSON_PATH, required = true) String jsonPath) {
try {
final JsonNode jsonNode = JsonService.evaluateJsonPathQuery(jsonObject, jsonPath);
if (!jsonNode.isNull()) {
final Object jsonNode = JsonService.evaluateJsonPathQuery(jsonObject, jsonPath);
if (!StringUtils.isEmpty(jsonNode)) {
return OutputUtilities.getSuccessResultsMap(jsonNode.toString());
}
return OutputUtilities.getSuccessResultsMap(NULL_STRING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

package io.cloudslang.content.json.services;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidJsonException;
Expand All @@ -41,7 +37,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import static io.cloudslang.content.json.utils.Constants.InputNames.EMPTY_STRING;

Expand All @@ -51,7 +50,7 @@
public class JsonService {

@NotNull
public static JsonNode evaluateJsonPathQuery(@Nullable final String jsonObject, @Nullable final String jsonPath) {
public static Object evaluateJsonPathQuery(@Nullable final String jsonObject, @Nullable final String jsonPath) {
final JsonContext jsonContext = JsonUtils.getValidJsonContext(jsonObject);
final JsonPath path = JsonUtils.getValidJsonPath(jsonPath);
return jsonContext.read(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ public static JsonContext getValidJsonContext(final String jsonObject) {
final AbstractJsonProvider provider = new JacksonJsonNodeJsonProvider(objectMapper);
final Configuration configuration = Configuration.defaultConfiguration()
.jsonProvider(provider);
final JsonContext jsonContext = new JsonContext(configuration);
jsonContext.parse(jsonObject);


final JsonContext jsonContext = (JsonContext) JsonPath
.using(configuration)
.parse(jsonObject);
return jsonContext;
} catch (IllegalArgumentException iae) {
throw hammerIllegalArgumentExceptionWithMessage(INVALID_JSONOBJECT, iae);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ public void givenJsonWithEmptyElementsThenReturnEmptyJsonString() throws RemoveE

@Test
public void evaluateSimpleJsonPathQuery() throws Exception {
JsonNode jsonNode = JsonService.evaluateJsonPathQuery("{'key1': 'value1','key2': 'value2', 'key3': { 'key31': 'value31'}}", "$.key3.key31");
Object jsonNode = JsonService.evaluateJsonPathQuery("{'key1': 'value1','key2': 'value2', 'key3': { 'key31': 'value31'}}", "$.key3.key31");
assertEquals(jsonNode.toString(), "\"value31\"");
}

@Test
public void evaluateComplexJsonPathQuery() throws Exception {
JsonNode jsonNode = JsonService.evaluateJsonPathQuery("{ \"store\": {\n" +
Object jsonNode = JsonService.evaluateJsonPathQuery("{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
Expand Down