From 975751b604784e86b2ddb944a72b55ca3f14cf63 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 9 May 2017 00:13:16 -0700 Subject: [PATCH] altoslib: Hide members from json by prefixing with __ This lets some structures which would otherwise recurse (and crash) get converted to json, assuming the hidden members aren't relevant. Signed-off-by: Keith Packard --- altoslib/AltosConfigData.java | 9 ++--- altoslib/AltosJson.java | 76 ++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/altoslib/AltosConfigData.java b/altoslib/AltosConfigData.java index 6b55cc6f..bd85c2c2 100644 --- a/altoslib/AltosConfigData.java +++ b/altoslib/AltosConfigData.java @@ -35,7 +35,7 @@ public class AltosConfigData implements Iterable { public int altitude_32; /* Strings returned */ - public LinkedList lines; + public LinkedList __lines; /* Config information */ /* HAS_FLIGHT*/ @@ -143,7 +143,7 @@ public class AltosConfigData implements Iterable { } public Iterator iterator() { - return lines.iterator(); + return __lines.iterator(); } public int log_space() { @@ -233,7 +233,7 @@ public class AltosConfigData implements Iterable { } public void reset() { - lines = new LinkedList(); + __lines = new LinkedList(); manufacturer = "unknown"; product = "unknown"; @@ -289,7 +289,7 @@ public class AltosConfigData implements Iterable { } public void parse_line(String line) { - lines.add(line); + __lines.add(line); /* Version replies */ try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {} try { product = get_string(line, "product"); } catch (Exception e) {} @@ -719,5 +719,4 @@ public class AltosConfigData implements Iterable { break; } } - } diff --git a/altoslib/AltosJson.java b/altoslib/AltosJson.java index 22f81d03..94ec2aee 100644 --- a/altoslib/AltosJson.java +++ b/altoslib/AltosJson.java @@ -25,39 +25,39 @@ import java.lang.*; import java.lang.reflect.*; class JsonUtil { - StringBuffer quote(StringBuffer result, String a) { - result.append("\""); + Writer quote(Writer writer, String a) throws IOException { + writer.append("\""); for (int i = 0; i < a.length(); i++) { char c = a.charAt(i); switch (c) { case '"': case '\\': - result.append('\\').append(c); + writer.append('\\').append(c); break; case '\n': - result.append("\\n"); + writer.append("\\n"); break; default: - result.append(c); + writer.append(c); break; } } - result.append("\""); - return result; + writer.append("\""); + return writer; } - StringBuffer append(StringBuffer result, AltosJson value, int indent, boolean pretty) { + Writer append(Writer result, AltosJson value, int indent, boolean pretty) throws IOException { value.append(result, indent, pretty); return result; } - StringBuffer append(StringBuffer result, String string) { + Writer append(Writer result, String string) throws IOException { result.append(string); return result; } - StringBuffer indent(StringBuffer result, int indent) { + Writer indent(Writer result, int indent) throws IOException { result.append("\n"); for (int i = 0; i < indent; i++) result.append("\t"); @@ -80,7 +80,7 @@ class JsonUtil { class JsonHash extends JsonUtil { Hashtable hash; - void append_hash(StringBuffer result, int indent, boolean pretty) { + void append_hash(Writer result, int indent, boolean pretty) throws IOException { boolean first = true; result.append("{"); @@ -125,7 +125,7 @@ class JsonHash extends JsonUtil { class JsonArray extends JsonUtil { ArrayList array; - void append_array(StringBuffer result, int indent, boolean pretty) { + void append_array(Writer result, int indent, boolean pretty) throws IOException { boolean first = true; append(result, "["); @@ -245,7 +245,7 @@ class JsonToken { this.sval = sval; } - JsonToken(int token, StringBuffer bval) { + JsonToken(int token, Writer bval) { this(token, bval.toString()); } } @@ -254,7 +254,7 @@ class JsonToken { * Lexer for json */ class JsonLexer extends JsonUtil { - StringReader f; + Reader f; int line; int ungot = -2; StringBuffer pending_token; @@ -382,7 +382,7 @@ class JsonLexer extends JsonUtil { return new JsonToken(JsonToken._long, lval); } case '"': - StringBuffer bval = new StringBuffer(); + Writer bval = new StringWriter(); for (;;) { c = ch(); if (c == '"') @@ -400,7 +400,7 @@ class JsonLexer extends JsonUtil { break; } } - bval.appendCodePoint(c); + bval.write(c); } return new JsonToken(JsonToken._string, bval); default: @@ -442,6 +442,12 @@ class JsonLexer extends JsonUtil { line = 1; token = null; } + + JsonLexer(Reader f) { + this.f = f; + line = 1; + token = null; + } } /* @@ -556,6 +562,10 @@ class JsonParse { JsonParse(String s) { lexer = new JsonLexer(s); } + + JsonParse(Reader f) { + lexer = new JsonLexer(f); + } } public class AltosJson extends JsonUtil { @@ -578,7 +588,7 @@ public class AltosJson extends JsonUtil { /* Generate string representation of the value */ - StringBuffer append(StringBuffer result, int indent, boolean pretty) { + Writer append(Writer result, int indent, boolean pretty) throws IOException { switch (type) { case type_hash: hash.append_hash(result, indent, pretty); @@ -615,9 +625,13 @@ public class AltosJson extends JsonUtil { } private String toString(int indent, boolean pretty) { - StringBuffer result = new StringBuffer(); - append(result, indent, pretty); - return result.toString(); + try { + Writer result = new StringWriter(); + append(result, indent, pretty); + return result.toString(); + } catch (Exception e) { + return null; + } } public String toString() { @@ -628,6 +642,14 @@ public class AltosJson extends JsonUtil { return toString(0, true); } + public void write(Writer w, int indent, boolean pretty) throws IOException { + append(w, indent, pretty); + } + + public void write(Writer w) throws IOException { + write(w, 0, true); + } + /* Parse string representation to a value */ @@ -641,6 +663,16 @@ public class AltosJson extends JsonUtil { } } + public static AltosJson fromReader(Reader f) { + JsonParse parse = new JsonParse(f); + try { + return parse.parse(); + } catch (IllegalArgumentException ie) { + System.out.printf("json:\n%s\n", ie.getMessage()); + return null; + } + } + /* Accessor functions */ private boolean assert_type(boolean setting, int type, int other_type, String error) { @@ -1213,6 +1245,10 @@ public class AltosJson extends JsonUtil { for (Field field : c.getDeclaredFields()) { String fieldName = field.getName(); + /* XXX hack to allow fields to be not converted */ + if (fieldName.startsWith("__")) + continue; + /* Skip static fields */ if (Modifier.isStatic(field.getModifiers())) continue; -- 2.30.2