Skip to content

Commit

Permalink
world class json api
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Nov 15, 2020
1 parent c4e83d1 commit a16eb25
Show file tree
Hide file tree
Showing 29 changed files with 178 additions and 186 deletions.
6 changes: 3 additions & 3 deletions karate-core/src/main/java/com/intuit/karate/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public Response get() {
return method("get");
}

public Response post(String body) {
Json json = new Json(body);
return post(json.asMapOrList());
public Response postJson(String body) {
Json json = Json.of(body);
return post(json.value());
}

public Response post(Object body) {
Expand Down
104 changes: 50 additions & 54 deletions karate-core/src/main/java/com/intuit/karate/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.intuit.karate;

import com.intuit.karate.StringUtils;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
Expand All @@ -39,37 +38,38 @@
* @author pthomas3
*/
public class Json {

private static final Logger logger = LoggerFactory.getLogger(Json.class);

private final DocumentContext doc;
private final boolean array;
private final String prefix;

private String prefix(String path) {
return path.charAt(0) == '$' ? path : prefix + path;
}

public static Json of(String json) {
return new Json(json);
}

public Json() {
this("{}");
}

public Json(String json) {
this(JsonPath.parse(json));

public static Json object() {
return Json.of("{}");
}
public Json(Map o) {
this(JsonPath.parse(o));

public static Json array() {
return Json.of("[]");
}

public Json(List o) {
this(JsonPath.parse(o));

public static Json of(Object o) {
if (o instanceof String) {
return new Json(JsonPath.parse((String) o));
} else if (o instanceof List) {
return new Json(JsonPath.parse((List) o));
} else if (o instanceof Map) {
return new Json(JsonPath.parse((Map) o));
} else {
String json = toJsonString(o);
return new Json(JsonPath.parse(json));
}
}

private static String toJsonString(Object o) {
try {
return JsonUtils.toJson(o);
Expand All @@ -78,61 +78,57 @@ private static String toJsonString(Object o) {
return JsonUtils.toJsonSafe(o, false);
}
}

public Json(Object o) {
this(toJsonString(o));
}


private Json(DocumentContext doc) {
this.doc = doc;
array = (doc.json() instanceof List);
prefix = array ? "$" : "$.";
}

public Json getJson(String path) {
return new Json(get(path, String.class));
return Json.of(get(path, String.class));
}

public <T> T get(String path) {
return (T) doc.read(prefix(path));
}

public <T> T getFirst(String path) {
List<T> list = get(path);
if (list == null || list.isEmpty()) {
return null;
}
return list.get(0);
}

public <T> T get(String path, Class<T> clazz) {
return doc.read(prefix(path), clazz);
}

@Override
public String toString() {
return doc.jsonString();
}

public boolean isArray() {
return array;
}

public Object asMapOrList() {
return doc.read("$");
}

public Map<String, Object> asMap() {

public <T> T value() {
return doc.read("$");
}

public List asList() {
return doc.read("$");
return value();
}


public Map<String, Object> asMap() {
return value();
}

public Json set(String path, String s) {
if (JsonUtils.isJson(s)) {
setInternal(path, new Json(s).asMapOrList());
setInternal(path, Json.of(s).value());
} else {
if (s != null && s.charAt(0) == '\\') {
s = s.substring(1);
Expand All @@ -141,26 +137,26 @@ public Json set(String path, String s) {
}
return this;
}

public Json remove(String path) {
doc.delete(path);
return this;
}

public Json set(String path, Object o) {
setInternal(path, o);
return this;
}

private boolean isArrayPath(String s) {
return s.endsWith("]") && !s.endsWith("']");
}

private String arrayKey(String s) {
int pos = s.lastIndexOf('[');
return s.substring(0, pos);
}

private int arrayIndex(String s) {
int leftPos = s.lastIndexOf('[');
if (leftPos == -1) {
Expand All @@ -180,7 +176,7 @@ private int arrayIndex(String s) {
return -1;
}
}

private void setInternal(String path, Object o) {
path = prefix(path);
if ("$".equals(path)) {
Expand All @@ -202,7 +198,7 @@ private void setInternal(String path, Object o) {
doc.put(pair.left, pair.right, o);
}
}

public boolean pathExists(String path) {
if (path.endsWith("[]")) {
path = path.substring(0, path.length() - 2);
Expand All @@ -214,7 +210,7 @@ public boolean pathExists(String path) {
return false;
}
}

private void createPath(String path, boolean array) {
if (isArrayPath(path)) {
String parentPath = arrayKey(path);
Expand Down Expand Up @@ -251,7 +247,7 @@ private void createPath(String path, boolean array) {
}
}
}

public static StringUtils.Pair toParentAndLeaf(String path) {
int pos = path.lastIndexOf('.');
int temp = path.lastIndexOf("['");
Expand All @@ -265,5 +261,5 @@ public static StringUtils.Pair toParentAndLeaf(String path) {
String left = path.substring(0, pos == -1 ? 0 : pos);
return StringUtils.pair(left, right);
}

}
3 changes: 1 addition & 2 deletions karate-core/src/main/java/com/intuit/karate/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ public static Document toNewDocument(Node in) {
}

public static Document toXmlDoc(Object o) {
Json json = new Json(o);
return fromObject("root", json.asMapOrList()); // keep it simple for people to write generic xpath starting with /root
return fromObject("root", Json.of(o).value()); // keep it simple for people to write generic xpath starting with /root
}

public static String toXml(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static DapMessage encode(ByteBuf in, int length) {
if (logger.isTraceEnabled()) {
logger.trace(">> {}", msg);
}
Map<String, Object> map = new Json(msg).asMap();
Map<String, Object> map = Json.of(msg).value();
return new DapMessage(map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean isBreakpoint(int line) {
}

public SourceBreakpoints(Map<String, Object> map) {
Json json = new Json(map);
Json json = Json.of(map);
name = json.get("source.name");
path = json.get("source.path");
List<Map<String, Object>> list = json.get("breakpoints");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected DevToolsDriver(DriverOptions options, Command command, String webSocke
// to avoid swamping the console when large base64 encoded binary responses happen
logger.debug("<< {}", StringUtils.truncate(text, 1024, true));
}
Map<String, Object> map = new Json(text).asMap();
Map<String, Object> map = Json.of(text).value();
DevToolsMessage dtm = new DevToolsMessage(this, map);
receive(dtm);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public <T> T getResult(String path, Class<T> clazz) {
if (result == null) {
return null;
}
Json json = new Json(result.<Object>getValue());
Json json = Json.of(result.getValue());
return json.get(path, clazz);
}

Expand Down Expand Up @@ -154,7 +154,7 @@ public DevToolsMessage(DevToolsDriver driver, Map<String, Object> map) {
method = (String) map.get("method");
Map temp = (Map) map.get("params");
if (temp != null) {
params = new Json(temp);
params = Json.of(temp);
}
temp = (Map) map.get("result");
if (temp != null) {
Expand All @@ -180,14 +180,14 @@ public DevToolsMessage(DevToolsDriver driver, Map<String, Object> map) {

public DevToolsMessage param(String path, Object value) {
if (params == null) {
params = new Json();
params = Json.object();
}
params.set(path, value);
return this;
}

public DevToolsMessage params(Map<String, Object> map) {
this.params = new Json(map);
this.params = Json.of(map);
return this;
}

Expand All @@ -199,7 +199,7 @@ public Map<String, Object> toMap() {
}
map.put("method", method);
if (params != null) {
map.put("params", params.asMap());
map.put("params", params.value());
}
return map;
}
Expand Down
Loading

0 comments on commit a16eb25

Please sign in to comment.