Skip to content

Commit 87c4b1d

Browse files
Season's Greetings
1 parent f284477 commit 87c4b1d

13 files changed

+562
-566
lines changed

Diff for: CDL.java

+38-38
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal
4141
* The names for the elements in the JSONObjects can be taken from the names
4242
* in the first row.
4343
* @author JSON.org
44-
* @version 2009-09-11
44+
* @version 2010-12-24
4545
*/
4646
public class CDL {
4747

@@ -135,6 +135,43 @@ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
135135
}
136136

137137
/**
138+
* Produce a comma delimited text row from a JSONArray. Values containing
139+
* the comma character will be quoted. Troublesome characters may be
140+
* removed.
141+
* @param ja A JSONArray of strings.
142+
* @return A string ending in NEWLINE.
143+
*/
144+
public static String rowToString(JSONArray ja) {
145+
StringBuffer sb = new StringBuffer();
146+
for (int i = 0; i < ja.length(); i += 1) {
147+
if (i > 0) {
148+
sb.append(',');
149+
}
150+
Object object = ja.opt(i);
151+
if (object != null) {
152+
String string = object.toString();
153+
if (string.length() > 0 && (string.indexOf(',') >= 0 ||
154+
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
155+
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
156+
sb.append('"');
157+
int length = string.length();
158+
for (int j = 0; j < length; j += 1) {
159+
char c = string.charAt(j);
160+
if (c >= ' ' && c != '"') {
161+
sb.append(c);
162+
}
163+
}
164+
sb.append('"');
165+
} else {
166+
sb.append(string);
167+
}
168+
}
169+
}
170+
sb.append('\n');
171+
return sb.toString();
172+
}
173+
174+
/**
138175
* Produce a JSONArray of JSONObjects from a comma delimited text string,
139176
* using the first row as a source of names.
140177
* @param string The comma delimited text.
@@ -197,43 +234,6 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
197234
}
198235

199236

200-
/**
201-
* Produce a comma delimited text row from a JSONArray. Values containing
202-
* the comma character will be quoted. Troublesome characters may be
203-
* removed.
204-
* @param ja A JSONArray of strings.
205-
* @return A string ending in NEWLINE.
206-
*/
207-
public static String rowToString(JSONArray ja) {
208-
StringBuffer sb = new StringBuffer();
209-
for (int i = 0; i < ja.length(); i += 1) {
210-
if (i > 0) {
211-
sb.append(',');
212-
}
213-
Object o = ja.opt(i);
214-
if (o != null) {
215-
String s = o.toString();
216-
if (s.length() > 0 && (s.indexOf(',') >= 0 || s.indexOf('\n') >= 0 ||
217-
s.indexOf('\r') >= 0 || s.indexOf(0) >= 0 ||
218-
s.charAt(0) == '"')) {
219-
sb.append('"');
220-
int length = s.length();
221-
for (int j = 0; j < length; j += 1) {
222-
char c = s.charAt(j);
223-
if (c >= ' ' && c != '"') {
224-
sb.append(c);
225-
}
226-
}
227-
sb.append('"');
228-
} else {
229-
sb.append(s);
230-
}
231-
}
232-
}
233-
sb.append('\n');
234-
return sb.toString();
235-
}
236-
237237
/**
238238
* Produce a comma delimited text from a JSONArray of JSONObjects. The
239239
* first row will be a list of names obtained by inspecting the first

Diff for: Cookie.java

+36-36
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
2828
* Convert a web browser cookie specification to a JSONObject and back.
2929
* JSON and Cookies are both notations for name/value pairs.
3030
* @author JSON.org
31-
* @version 2008-09-18
31+
* @version 2010-12-24
3232
*/
3333
public class Cookie {
3434

@@ -48,8 +48,8 @@ public static String escape(String string) {
4848
char c;
4949
String s = string.trim();
5050
StringBuffer sb = new StringBuffer();
51-
int len = s.length();
52-
for (int i = 0; i < len; i += 1) {
51+
int length = s.length();
52+
for (int i = 0; i < length; i += 1) {
5353
c = s.charAt(i);
5454
if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
5555
sb.append('%');
@@ -79,29 +79,29 @@ public static String escape(String string) {
7979
* @throws JSONException
8080
*/
8181
public static JSONObject toJSONObject(String string) throws JSONException {
82-
String n;
83-
JSONObject o = new JSONObject();
84-
Object v;
82+
String name;
83+
JSONObject jo = new JSONObject();
84+
Object value;
8585
JSONTokener x = new JSONTokener(string);
86-
o.put("name", x.nextTo('='));
86+
jo.put("name", x.nextTo('='));
8787
x.next('=');
88-
o.put("value", x.nextTo(';'));
88+
jo.put("value", x.nextTo(';'));
8989
x.next();
9090
while (x.more()) {
91-
n = unescape(x.nextTo("=;"));
91+
name = unescape(x.nextTo("=;"));
9292
if (x.next() != '=') {
93-
if (n.equals("secure")) {
94-
v = Boolean.TRUE;
93+
if (name.equals("secure")) {
94+
value = Boolean.TRUE;
9595
} else {
9696
throw x.syntaxError("Missing '=' in cookie parameter.");
9797
}
9898
} else {
99-
v = unescape(x.nextTo(';'));
99+
value = unescape(x.nextTo(';'));
100100
x.next();
101101
}
102-
o.put(n, v);
102+
jo.put(name, value);
103103
}
104-
return o;
104+
return jo;
105105
}
106106

107107

@@ -111,29 +111,29 @@ public static JSONObject toJSONObject(String string) throws JSONException {
111111
* If the JSONObject contains "expires", "domain", "path", or "secure"
112112
* members, they will be appended to the cookie specification string.
113113
* All other members are ignored.
114-
* @param o A JSONObject
114+
* @param jo A JSONObject
115115
* @return A cookie specification string
116116
* @throws JSONException
117117
*/
118-
public static String toString(JSONObject o) throws JSONException {
118+
public static String toString(JSONObject jo) throws JSONException {
119119
StringBuffer sb = new StringBuffer();
120120

121-
sb.append(escape(o.getString("name")));
121+
sb.append(escape(jo.getString("name")));
122122
sb.append("=");
123-
sb.append(escape(o.getString("value")));
124-
if (o.has("expires")) {
123+
sb.append(escape(jo.getString("value")));
124+
if (jo.has("expires")) {
125125
sb.append(";expires=");
126-
sb.append(o.getString("expires"));
126+
sb.append(jo.getString("expires"));
127127
}
128-
if (o.has("domain")) {
128+
if (jo.has("domain")) {
129129
sb.append(";domain=");
130-
sb.append(escape(o.getString("domain")));
130+
sb.append(escape(jo.getString("domain")));
131131
}
132-
if (o.has("path")) {
132+
if (jo.has("path")) {
133133
sb.append(";path=");
134-
sb.append(escape(o.getString("path")));
134+
sb.append(escape(jo.getString("path")));
135135
}
136-
if (o.optBoolean("secure")) {
136+
if (jo.optBoolean("secure")) {
137137
sb.append(";secure");
138138
}
139139
return sb.toString();
@@ -142,28 +142,28 @@ public static String toString(JSONObject o) throws JSONException {
142142
/**
143143
* Convert <code>%</code><i>hh</i> sequences to single characters, and
144144
* convert plus to space.
145-
* @param s A string that may contain
145+
* @param string A string that may contain
146146
* <code>+</code>&nbsp;<small>(plus)</small> and
147147
* <code>%</code><i>hh</i> sequences.
148148
* @return The unescaped string.
149149
*/
150-
public static String unescape(String s) {
151-
int len = s.length();
152-
StringBuffer b = new StringBuffer();
153-
for (int i = 0; i < len; ++i) {
154-
char c = s.charAt(i);
150+
public static String unescape(String string) {
151+
int length = string.length();
152+
StringBuffer sb = new StringBuffer();
153+
for (int i = 0; i < length; ++i) {
154+
char c = string.charAt(i);
155155
if (c == '+') {
156156
c = ' ';
157-
} else if (c == '%' && i + 2 < len) {
158-
int d = JSONTokener.dehexchar(s.charAt(i + 1));
159-
int e = JSONTokener.dehexchar(s.charAt(i + 2));
157+
} else if (c == '%' && i + 2 < length) {
158+
int d = JSONTokener.dehexchar(string.charAt(i + 1));
159+
int e = JSONTokener.dehexchar(string.charAt(i + 2));
160160
if (d >= 0 && e >= 0) {
161161
c = (char)(d * 16 + e);
162162
i += 2;
163163
}
164164
}
165-
b.append(c);
165+
sb.append(c);
166166
}
167-
return b.toString();
167+
return sb.toString();
168168
}
169169
}

Diff for: CookieList.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
2929
/**
3030
* Convert a web browser cookie list string to a JSONObject and back.
3131
* @author JSON.org
32-
* @version 2008-09-18
32+
* @version 2010-12-24
3333
*/
3434
public class CookieList {
3535

@@ -47,15 +47,15 @@ public class CookieList {
4747
* @throws JSONException
4848
*/
4949
public static JSONObject toJSONObject(String string) throws JSONException {
50-
JSONObject o = new JSONObject();
50+
JSONObject jo = new JSONObject();
5151
JSONTokener x = new JSONTokener(string);
5252
while (x.more()) {
5353
String name = Cookie.unescape(x.nextTo('='));
5454
x.next('=');
55-
o.put(name, Cookie.unescape(x.nextTo(';')));
55+
jo.put(name, Cookie.unescape(x.nextTo(';')));
5656
x.next();
5757
}
58-
return o;
58+
return jo;
5959
}
6060

6161

@@ -64,24 +64,24 @@ public static JSONObject toJSONObject(String string) throws JSONException {
6464
* of name/value pairs. The names are separated from the values by '='.
6565
* The pairs are separated by ';'. The characters '%', '+', '=', and ';'
6666
* in the names and values are replaced by "%hh".
67-
* @param o A JSONObject
67+
* @param jo A JSONObject
6868
* @return A cookie list string
6969
* @throws JSONException
7070
*/
71-
public static String toString(JSONObject o) throws JSONException {
71+
public static String toString(JSONObject jo) throws JSONException {
7272
boolean b = false;
73-
Iterator keys = o.keys();
74-
String s;
73+
Iterator keys = jo.keys();
74+
String string;
7575
StringBuffer sb = new StringBuffer();
7676
while (keys.hasNext()) {
77-
s = keys.next().toString();
78-
if (!o.isNull(s)) {
77+
string = keys.next().toString();
78+
if (!jo.isNull(string)) {
7979
if (b) {
8080
sb.append(';');
8181
}
82-
sb.append(Cookie.escape(s));
82+
sb.append(Cookie.escape(string));
8383
sb.append("=");
84-
sb.append(Cookie.escape(o.getString(s)));
84+
sb.append(Cookie.escape(jo.getString(string)));
8585
b = true;
8686
}
8787
}

0 commit comments

Comments
 (0)