enhanced json encoding of strings

pull/93/head
Michael Peter Christen 9 years ago
parent 6139bd85a8
commit c716648c78

@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Set;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.util.JSONObject;
import net.yacy.peers.Seed;
import net.yacy.search.Switchboard;
import net.yacy.server.serverCore;
@ -91,14 +92,14 @@ public final class seedlist {
Set<String> ips = seed.getIPs();
if (ips == null || ips.size() == 0) continue;
prop.putJSON("peers_" + count + "_map_0_k", Seed.HASH);
prop.put("peers_" + count + "_map_0_v", '"' + serverObjects.toJSON(seed.hash) + '"');
prop.put("peers_" + count + "_map_0_v", JSONObject.quote(seed.hash));
prop.put("peers_" + count + "_map_0_c", 1);
Map<String, String> map = seed.getMap();
int c = 1;
if (!addressonly) {
for (Map.Entry<String, String> m: map.entrySet()) {
prop.putJSON("peers_" + count + "_map_" + c + "_k", m.getKey());
prop.put("peers_" + count + "_map_" + c + "_v", '"' + serverObjects.toJSON(m.getValue()) + '"');
prop.put("peers_" + count + "_map_" + c + "_v", JSONObject.quote(m.getValue()));
prop.put("peers_" + count + "_map_" + c + "_c", 1);
c++;
}
@ -106,7 +107,7 @@ public final class seedlist {
// construct a list of ips
StringBuilder a = new StringBuilder();
a.append('[');
for (String ip: ips) a.append('"').append(serverObjects.toJSON(seed.getPublicAddress(ip))).append('"').append(',');
for (String ip: ips) a.append(JSONObject.quote(seed.getPublicAddress(ip))).append(',');
a.setCharAt(a.length()-1, ']');
prop.putJSON("peers_" + count + "_map_" + c + "_k", "Address");
prop.put("peers_" + count + "_map_" + c + "_v", a.toString());

@ -34,9 +34,9 @@ import net.yacy.cora.document.analysis.Classification;
import net.yacy.cora.document.id.MultiProtocolURL;
import net.yacy.cora.federate.solr.responsewriter.OpensearchResponseWriter.ResHead;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.util.JSONObject;
import net.yacy.data.URLLicense;
import net.yacy.search.schema.CollectionSchema;
import net.yacy.server.serverObjects;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexableField;
@ -213,7 +213,7 @@ public class YJsonResponseWriter implements QueryResponseWriter {
int sp = snippetstring.lastIndexOf(' ');
if (sp >= 0) snippetstring = snippetstring.substring(0, sp) + " ..."; else snippetstring = snippetstring + "...";
}
writer.write("\"description\":\""); writer.write(serverObjects.toJSON(snippetstring)); writer.write("\"\n}\n");
writer.write("\"description\":"); writer.write(JSONObject.quote(snippetstring)); writer.write("\n}\n");
if (i < responseCount - 1) {
writer.write(",\n".toCharArray());
}
@ -307,16 +307,16 @@ public class YJsonResponseWriter implements QueryResponseWriter {
public static void solitaireTag(final Writer writer, final String tagname, String value) throws IOException {
if (value == null) return;
writer.write('"'); writer.write(tagname); writer.write("\":\""); writer.write(serverObjects.toJSON(value)); writer.write("\","); writer.write('\n');
writer.write('"'); writer.write(tagname); writer.write("\":"); writer.write(JSONObject.quote(value)); writer.write(','); writer.write('\n');
}
private static void facetEntry(final Writer writer, String modifier, String propname, String value) throws IOException {
private static void facetEntry(final Writer writer, String modifier, String propname, final String value) throws IOException {
modifier = modifier.replaceAll("\"", "'").trim();
propname = propname.replaceAll("\"", "'").trim();
writer.write("{\"name\": \""); writer.write(propname);
writer.write("\", \"count\": \""); writer.write(value);
writer.write("\", \"modifier\": \""); writer.write(modifier); writer.write("%3A"); writer.write(propname);
writer.write("\"}");
writer.write("{\"name\":"); writer.write(JSONObject.quote(propname));
writer.write(",\"count\":"); writer.write(JSONObject.quote(value.replaceAll("\"", "'").trim()));
writer.write(",\"modifier\":"); writer.write(JSONObject.quote(modifier+"%3A"+propname));
writer.write("}");
}
}
/**

@ -63,6 +63,7 @@ import net.yacy.cora.document.encoding.UTF8;
import net.yacy.cora.document.id.MultiProtocolURL;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.protocol.RequestHeader.FileType;
import net.yacy.cora.util.JSONObject;
import net.yacy.document.parser.html.CharacterCoding;
import net.yacy.kelondro.util.Formatter;
import net.yacy.search.Switchboard;
@ -81,12 +82,6 @@ public class serverObjects implements Serializable, Cloneable {
public final static String ADMIN_AUTHENTICATE_MSG = "admin log-in. If you don't know the password, set it with {yacyhome}/bin/passwd.sh {newpassword}";
private final static Pattern patternNewline = Pattern.compile("\n");
private final static Pattern patternDoublequote = Pattern.compile("\"");
private final static Pattern patternSlash = Pattern.compile("/");
private final static Pattern patternB = Pattern.compile("\b");
private final static Pattern patternF = Pattern.compile("\f");
private final static Pattern patternR = Pattern.compile("\r");
private final static Pattern patternT = Pattern.compile("\t");
private boolean localized = true;
@ -284,20 +279,10 @@ public class serverObjects implements Serializable, Cloneable {
* @param key key name as String.
* @param value a String that will be reencoded for JSON output.
*/
public void putJSON(final String key, final String value) {
put(key, toJSON(value));
}
public static String toJSON(String value) {
// value = value.replaceAll("\\", "\\\\");
value = patternDoublequote.matcher(value).replaceAll("'");
value = patternSlash.matcher(value).replaceAll("\\/");
value = patternB.matcher(value).replaceAll("\\b");
value = patternF.matcher(value).replaceAll("\\f");
value = patternNewline.matcher(value).replaceAll("\\r");
value = patternR.matcher(value).replaceAll("\\r");
value = patternT.matcher(value).replaceAll("\\t");
return value;
public void putJSON(final String key, String value) {
value = JSONObject.quote(value);
value = value.substring(1, value.length() - 1);
put(key, value);
}
/**
@ -558,9 +543,4 @@ public class serverObjects implements Serializable, Cloneable {
return this.map;
}
public static void main(final String[] args) {
final String v = "ein \"zitat\"";
System.out.println(toJSON(v));
}
}

Loading…
Cancel
Save