Skip to content

Commit d833c2d

Browse files
committed
added builder class for JSONPointer, and implemented toString() and toURIFragment()
1 parent cbb1546 commit d833c2d

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

JSONPointer.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.UnsupportedEncodingException;
77
import java.net.URLDecoder;
8+
import java.net.URLEncoder;
89
import java.util.ArrayList;
910
import java.util.List;
1011

@@ -37,8 +38,34 @@ of this software and associated documentation files (the "Software"), to deal
3738
* <a href="https://tools.ietf.org/html/rfc6901">RFC 6901</a>.
3839
*/
3940
public class JSONPointer {
41+
42+
private static final String ENCODING = "utf-8";
4043

41-
private List<String> refTokens;
44+
public static class Builder {
45+
46+
private final List<String> refTokens = new ArrayList<String>();
47+
48+
public Builder append(String token) {
49+
refTokens.add(token);
50+
return this;
51+
}
52+
53+
public JSONPointer build() {
54+
return new JSONPointer(refTokens);
55+
}
56+
57+
public Builder append(int arrayIndex) {
58+
refTokens.add(String.valueOf(arrayIndex));
59+
return this;
60+
}
61+
62+
}
63+
64+
public static Builder builder() {
65+
return new Builder();
66+
}
67+
68+
private final List<String> refTokens;
4269

4370
/**
4471
* Pre-parses and initializes a new {@code JSONPointer} instance. If you want to
@@ -59,7 +86,7 @@ public JSONPointer(String pointer) {
5986
if (pointer.startsWith("#/")) {
6087
pointer = pointer.substring(2);
6188
try {
62-
pointer = URLDecoder.decode(pointer, "utf-8");
89+
pointer = URLDecoder.decode(pointer, ENCODING);
6390
} catch (UnsupportedEncodingException e) {
6491
throw new RuntimeException(e);
6592
}
@@ -74,6 +101,10 @@ public JSONPointer(String pointer) {
74101
}
75102
}
76103

104+
public JSONPointer(List<String> refTokens) {
105+
this.refTokens = refTokens;
106+
}
107+
77108
private String unescape(String token) {
78109
return token.replace("~1", "/").replace("~0", "~")
79110
.replace("\\\"", "\"")
@@ -122,4 +153,33 @@ private Object readByIndexToken(Object current, String indexToken) {
122153
throw new JSONPointerException(format("%s is not an array index", indexToken), e);
123154
}
124155
}
156+
157+
@Override
158+
public String toString() {
159+
StringBuilder rval = new StringBuilder("");
160+
for (String token: refTokens) {
161+
rval.append('/').append(escape(token));
162+
}
163+
return rval.toString();
164+
}
165+
166+
private String escape(String token) {
167+
return token.replace("~", "~0")
168+
.replace("/", "~1")
169+
.replace("\\", "\\\\")
170+
.replace("\"", "\\\"");
171+
}
172+
173+
public String toURIFragment() {
174+
try {
175+
StringBuilder rval = new StringBuilder("#");
176+
for (String token : refTokens) {
177+
rval.append('/').append(URLEncoder.encode(token, ENCODING));
178+
}
179+
return rval.toString();
180+
} catch (UnsupportedEncodingException e) {
181+
throw new RuntimeException(e);
182+
}
183+
}
184+
125185
}

0 commit comments

Comments
 (0)