X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altoslib%2FAltosJson.java;h=8f6897bb8efabad56da8a3007b4a081969d75ea6;hp=94ec2aee6029d245f7b87a888412760f43250787;hb=HEAD;hpb=975751b604784e86b2ddb944a72b55ca3f14cf63 diff --git a/altoslib/AltosJson.java b/altoslib/AltosJson.java index 94ec2aee..1c3a342d 100644 --- a/altoslib/AltosJson.java +++ b/altoslib/AltosJson.java @@ -16,7 +16,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_11; +package org.altusmetrum.altoslib_14; import java.io.*; import java.util.*; @@ -63,18 +63,22 @@ class JsonUtil { result.append("\t"); return result; } - static NumberFormat get_nf_json() { - DecimalFormat nf = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ROOT); - nf.setParseIntegerOnly(false); - nf.setGroupingUsed(false); - nf.setMaximumFractionDigits(17); - nf.setMinimumFractionDigits(0); - nf.setMinimumIntegerDigits(1); - nf.setDecimalSeparatorAlwaysShown(false); - return nf; - } - static NumberFormat nf_json = get_nf_json(); + NumberFormat _nf_json; + + NumberFormat nf_json() { + if (_nf_json == null) { + DecimalFormat nf = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ROOT); + nf.setParseIntegerOnly(false); + nf.setGroupingUsed(false); + nf.setMaximumFractionDigits(17); + nf.setMinimumFractionDigits(0); + nf.setMinimumIntegerDigits(1); + nf.setDecimalSeparatorAlwaysShown(false); + _nf_json = nf; + } + return _nf_json; + } } class JsonHash extends JsonUtil { @@ -185,6 +189,7 @@ class JsonToken { static final int _colon = 9; static final int _end = 10; static final int _error = 11; + static final int _none = 12; static String token_name(int token) { switch (token) { @@ -254,11 +259,11 @@ class JsonToken { * Lexer for json */ class JsonLexer extends JsonUtil { - Reader f; - int line; - int ungot = -2; - StringBuffer pending_token; - JsonToken token; + InputStream f; + int line; + int ungot = -2; + StringBuffer pending_token; + private JsonToken token; static class keyword { String word; @@ -371,7 +376,7 @@ class JsonLexer extends JsonUtil { String dstr = dbuf.toString(); double dval; try { - dval = nf_json.parse(dstr).doubleValue(); + dval = nf_json().parse(dstr).doubleValue(); } catch (ParseException pe) { return new JsonToken(JsonToken._error, dstr); } @@ -424,11 +429,17 @@ class JsonLexer extends JsonUtil { } void next() { - token = lex(); + token = null; + } + + JsonToken token() { + if (token == null) + token = lex(); + return token; } JsonToken expect(int e) { - JsonToken t = token; + JsonToken t = token(); if (t.token != e) throw new IllegalArgumentException(String.format("got \"%s\" while expecting \"%s\"", token.token_name(), @@ -438,12 +449,12 @@ class JsonLexer extends JsonUtil { } JsonLexer(String s) { - f = new StringReader(s); + f = new AltosStringInputStream(s); line = 1; token = null; } - JsonLexer(Reader f) { + JsonLexer(InputStream f) { this.f = f; line = 1; token = null; @@ -470,7 +481,7 @@ class JsonParse { lexer.next(); for (;;) { /* Allow for empty hashes */ - if (lexer.token.token == JsonToken._cc) { + if (lexer.token().token == JsonToken._cc) { lexer.next(); return hash; } @@ -481,7 +492,7 @@ class JsonParse { AltosJson value = value(); hash.put(key, value); - switch (lexer.token.token) { + switch (lexer.token().token) { case JsonToken._comma: lexer.next(); break; @@ -489,7 +500,7 @@ class JsonParse { lexer.next(); return hash; default: - parse_error("got %s expect \",\" or \"}\"", lexer.token.token_name()); + parse_error("got %s expect \",\" or \"}\"", lexer.token().token_name()); return null; } } @@ -502,14 +513,14 @@ class JsonParse { lexer.next(); for (int i = 0;; i++) { /* Allow for empty arrays */ - if (lexer.token.token == JsonToken._cs) { + if (lexer.token().token == JsonToken._cs) { lexer.next(); return array; } AltosJson value = value(); array.put(i, value); - switch (lexer.token.token) { + switch (lexer.token().token) { case JsonToken._comma: lexer.next(); break; @@ -517,7 +528,7 @@ class JsonParse { lexer.next(); return array; default: - parse_error("got %s expect \",\" or \"]\"", lexer.token.token_name()); + parse_error("got %s expect \",\" or \"]\"", lexer.token().token_name()); return null; } } @@ -527,29 +538,29 @@ class JsonParse { * identify the next object in the input */ AltosJson value() { - switch (lexer.token.token) { + switch (lexer.token().token) { case JsonToken._oc: return new AltosJson(hash()); case JsonToken._os: return new AltosJson(array()); case JsonToken._double: - double dval = lexer.token.dval; + double dval = lexer.token().dval; lexer.next(); return new AltosJson(dval); case JsonToken._long: - long lval = lexer.token.lval; + long lval = lexer.token().lval; lexer.next(); return new AltosJson(lval); case JsonToken._string: - String sval = lexer.token.sval; + String sval = lexer.token().sval; lexer.next(); return new AltosJson(sval); case JsonToken._boolean: - boolean bval = lexer.token.bval; + boolean bval = lexer.token().bval; lexer.next(); return new AltosJson(bval); default: - parse_error("Unexpected token \"%s\"", lexer.token.token_name()); + parse_error("Unexpected token \"%s\"", lexer.token().token_name()); } return null; } @@ -563,7 +574,7 @@ class JsonParse { lexer = new JsonLexer(s); } - JsonParse(Reader f) { + JsonParse(InputStream f) { lexer = new JsonLexer(f); } } @@ -605,14 +616,14 @@ public class AltosJson extends JsonUtil { } else if (Double.isNaN(d_number)) { result.append("NaN"); } else { - String dval = nf_json.format(d_number); + String dval = nf_json().format(d_number); if (dval.equals("-0")) dval = "0"; result.append(dval); } break; case type_long: - result.append(new Long(l_number).toString()); + result.append(Long.valueOf(l_number).toString()); break; case type_string: quote(result, string); @@ -663,7 +674,7 @@ public class AltosJson extends JsonUtil { } } - public static AltosJson fromReader(Reader f) { + public static AltosJson fromInputStream(InputStream f) { JsonParse parse = new JsonParse(f); try { return parse.parse(); @@ -1025,6 +1036,7 @@ public class AltosJson extends JsonUtil { * all inner classes are only members of their immediate outer * class */ + @SuppressWarnings("unchecked") private Object make(Class c, Class enclosing_class, Object enclosing_object) { Object ret; if (c == Boolean.TYPE) { @@ -1094,7 +1106,7 @@ public class AltosJson extends JsonUtil { Constructor ctor = ((Class)c).getDeclaredConstructor((Class) enclosing_class); object = ctor.newInstance(enclosing_object); } else { - object = c.newInstance(); + object = c.getDeclaredConstructor().newInstance(); } for (; c != Object.class; c = c.getSuperclass()) { for (Field field : c.getDeclaredFields()) { @@ -1206,6 +1218,10 @@ public class AltosJson extends JsonUtil { } else if (object instanceof String) { type = type_string; string = (String) object; + } else if (object == null) { + System.out.printf("unexpected null object\n"); + } else if (object.getClass() == null) { + System.out.printf("unexpected null object class\n"); } else if (object.getClass().isArray()) { assert_array(true);