Skip to content

Commit

Permalink
* 完善
Browse files Browse the repository at this point in the history
  • Loading branch information
fjn committed Nov 22, 2022
1 parent bb2ac3e commit 9ce0839
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@

public class JsonNodeNavigator implements Navigator<JsonTreeNode> {
private static final Logger logger = Loggers.getLogger(ObjectNavigator.class);
private String separator;
private String prefix;
private String suffix;

public JsonNodeNavigator() {
this("/");
}

public JsonNodeNavigator(String separator) {
this.separator = Strings.isEmpty(separator) ? "/" : separator;
this(null, separator);
}

public JsonNodeNavigator(String prefix, String suffix) {
this.prefix = prefix;
this.suffix = Strings.isEmpty(suffix) ? "/" : suffix;
}

@Override
public JsonTreeNode get(JsonTreeNode context, String pathExpression) {
if (context == null) {
return null;
}

String[] segments = Strings.split(pathExpression, separator);
String[] segments = Navigators.getPathSegments(pathExpression, this.prefix, this.suffix);
return navigate(context, Collects.asList(segments));
}

Expand All @@ -56,7 +61,7 @@ private JsonTreeNode navigate(JsonTreeNode context, List<String> segments) {
int index = Numbers.createInteger(property);
node = arrayNode.get(index);
} else {
logger.warn("the node which at the path {} is not a object node or array node", Strings.join(this.separator, segments.subList(0, i + 1)));
logger.warn("the node which at the path {} is not a object node or array node", Strings.iterateJoin("", this.prefix, this.suffix, segments.subList(0, i + 1)));
node = null;
break;
}
Expand All @@ -69,7 +74,7 @@ private JsonTreeNode navigate(JsonTreeNode context, List<String> segments) {

@Override
public <E> void set(JsonTreeNode context, String expression, E value) {
String[] segments = Strings.split(expression, separator);
String[] segments = Navigators.getPathSegments(expression, this.prefix, this.suffix);
if (Objs.isEmpty(segments)) {
return;
}
Expand All @@ -87,7 +92,7 @@ public <E> void set(JsonTreeNode context, String expression, E value) {
int index = Numbers.createInteger(property);
arrayNode.set(index, JsonTreeNodes.toJsonTreeNode(value));
} else {
logger.warn("the node which at the path {} is not a object node or array node", Strings.join(this.separator, parentExprSegments));
logger.warn("the node which at the path {} is not a object node or array node", Strings.iterateJoin("", this.prefix, this.suffix, parentExprSegments));
}
} catch (Throwable ex) {
throw Throwables.wrapAsRuntimeException(ex);
Expand Down Expand Up @@ -132,12 +137,12 @@ public <E> Class<E> getType(JsonTreeNode context, String expression) {

@Override
public String getParentPath(String s) {
return Navigators.getParentPath(s, separator);
return Navigators.getParentPath(s, this.prefix, this.suffix);
}

@Override
public String getLeaf(String s) {
return Navigators.getLeaf(s, separator);
return Navigators.getLeaf(s, this.prefix, this.suffix);
}

public static <E extends JsonTreeNode> E getTreeNode(JsonTreeNode tree, String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public static String toJson(Object obj) {
* @since 3.2.27
*/
public static String toJson(Object obj, boolean pretty) {
return prettyJson.toJson(obj);
return pretty ? prettyJson.toJson(obj) : toJson(obj);
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</license>
</licenses>
<properties>
<langx.version>5.0.0</langx.version>
<langx.version>5.1.0</langx.version>
<slf4j.version>1.7.0</slf4j.version>
</properties>

Expand Down

0 comments on commit 9ce0839

Please sign in to comment.