altoslib: Hide members from json by prefixing with __
authorKeith Packard <keithp@keithp.com>
Tue, 9 May 2017 07:13:16 +0000 (00:13 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 9 May 2017 07:14:12 +0000 (00:14 -0700)
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 <keithp@keithp.com>
altoslib/AltosConfigData.java
altoslib/AltosJson.java

index 6b55cc6fdc9c8dc7c7177b0e5e7bf4030ec2e49b..bd85c2c208373b8a6f7c48ad5b6661d3fbcbc110 100644 (file)
@@ -35,7 +35,7 @@ public class AltosConfigData implements Iterable<String> {
        public int      altitude_32;
 
        /* Strings returned */
-       public LinkedList<String>       lines;
+       public LinkedList<String>       __lines;
 
        /* Config information */
        /* HAS_FLIGHT*/
@@ -143,7 +143,7 @@ public class AltosConfigData implements Iterable<String> {
        }
 
        public Iterator<String> iterator() {
-               return lines.iterator();
+               return __lines.iterator();
        }
 
        public int log_space() {
@@ -233,7 +233,7 @@ public class AltosConfigData implements Iterable<String> {
        }
 
        public void reset() {
-               lines = new LinkedList<String>();
+               __lines = new LinkedList<String>();
 
                manufacturer = "unknown";
                product = "unknown";
@@ -289,7 +289,7 @@ public class AltosConfigData implements Iterable<String> {
        }
 
        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<String> {
                        break;
                }
        }
-
 }
index 22f81d037c06ef43907fb633e8fa3a5c5bf46877..94ec2aee6029d245f7b87a888412760f43250787 100644 (file)
@@ -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<String,AltosJson> 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<AltosJson> 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;