Skip to content

Commit 9a81b40

Browse files
committed
added some JavaDocs
1 parent 4a458a9 commit 9a81b40

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

JSONPointer.java

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.json;
22

33
import static java.lang.String.format;
4-
import static java.util.Collections.emptyList;
54

65
import java.io.UnsupportedEncodingException;
76
import java.net.URLDecoder;
87
import java.net.URLEncoder;
9-
import java.util.ArrayList;
10-
import java.util.List;
8+
import java.util.*;
119

1210
/*
1311
Copyright (c) 2002 JSON.org
@@ -36,18 +34,35 @@ of this software and associated documentation files (the "Software"), to deal
3634
/**
3735
* A JSON Pointer is a simple query language defined for JSON documents by
3836
* <a href="https://tools.ietf.org/html/rfc6901">RFC 6901</a>.
39-
*
37+
*
38+
* In a nutshell, JSONPointer allows the user to navigate into a JSON document
39+
* using strings, and retrieve targeted objects, like a simple form of XPATH.
40+
* Path segments are separated by the '/' char, which signifies the root of
41+
* the document when it appears as the first char of the string. Array
42+
* elements are navigated using ordinals, counting from 0. JSONPointer strings
43+
* may be extended to any arbitrary number of segments. If the navigation
44+
* is successful, the matched item is returned. A matched item may be a
45+
* JSONObject, a JSONArray, or a JSON value. If the JSONPointer string building
46+
* fails, an appropriate exception is thrown. If the navigation fails to find
47+
* a match, a JSONPointerException is thrown.
48+
*
4049
* @author JSON.org
41-
* @version 2016-05-13
50+
* @version 2016-05-14
4251
*/
4352
public class JSONPointer {
44-
53+
54+
// used for URL encoding and decoding
4555
private static final String ENCODING = "utf-8";
4656

57+
/**
58+
* This class allows the user to build a JSONPointer in steps, using
59+
* exactly one segment in each step.
60+
*/
4761
public static class Builder {
48-
62+
63+
// Segments for the eventual JSONPointer string
4964
private final List<String> refTokens = new ArrayList<String>();
50-
65+
5166
/**
5267
* Creates a {@code JSONPointer} instance using the tokens previously set using the
5368
* {@link #append(String)} method calls.
@@ -87,9 +102,8 @@ public Builder append(int arrayIndex) {
87102
refTokens.add(String.valueOf(arrayIndex));
88103
return this;
89104
}
90-
91105
}
92-
106+
93107
/**
94108
* Static factory method for {@link Builder}. Example usage:
95109
*
@@ -109,6 +123,7 @@ public static Builder builder() {
109123
return new Builder();
110124
}
111125

126+
// Segments for the JSONPointer string
112127
private final List<String> refTokens;
113128

114129
/**
@@ -124,7 +139,7 @@ public JSONPointer(String pointer) {
124139
throw new NullPointerException("pointer cannot be null");
125140
}
126141
if (pointer.isEmpty()) {
127-
refTokens = emptyList();
142+
refTokens = Collections.emptyList();
128143
return;
129144
}
130145
if (pointer.startsWith("#/")) {
@@ -184,6 +199,13 @@ public Object queryFrom(Object document) {
184199
return current;
185200
}
186201

202+
/**
203+
* Matches a JSONArray element by ordinal position
204+
* @param current the JSONArray to be evaluated
205+
* @param indexToken the array index in string form
206+
* @return the matched object. If no matching item is found a
207+
* JSONPointerException is thrown
208+
*/
187209
private Object readByIndexToken(Object current, String indexToken) {
188210
try {
189211
int index = Integer.parseInt(indexToken);
@@ -197,7 +219,11 @@ private Object readByIndexToken(Object current, String indexToken) {
197219
throw new JSONPointerException(format("%s is not an array index", indexToken), e);
198220
}
199221
}
200-
222+
223+
/**
224+
* Returns a string representing the JSONPointer path value using string
225+
* representation
226+
*/
201227
@Override
202228
public String toString() {
203229
StringBuilder rval = new StringBuilder("");
@@ -207,13 +233,25 @@ public String toString() {
207233
return rval.toString();
208234
}
209235

236+
/**
237+
* Escapes path segment values to an unambiguous form.
238+
* The escape char to be inserted is '~'. The chars to be escaped
239+
* are ~, which maps to ~0, and /, which maps to ~1. Backslashes
240+
* and double quote chars are also escaped.
241+
* @param token the JSONPointer segment value to be escaped
242+
* @return the escaped value for the token
243+
*/
210244
private String escape(String token) {
211245
return token.replace("~", "~0")
212246
.replace("/", "~1")
213247
.replace("\\", "\\\\")
214248
.replace("\"", "\\\"");
215249
}
216250

251+
/**
252+
* Returns a string representing the JSONPointer path value using URI
253+
* fragment identifier representation
254+
*/
217255
public String toURIFragment() {
218256
try {
219257
StringBuilder rval = new StringBuilder("#");

0 commit comments

Comments
 (0)