mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.7.3.2
This commit is contained in:
@@ -16,15 +16,19 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_itm_ary extends Json_itm_base implements Json_grp {
|
||||
public Json_itm_ary(int src_bgn, int src_end) {this.Ctor(src_bgn, src_end);}
|
||||
public class Json_ary extends Json_itm_base implements Json_grp {
|
||||
public Json_ary(int src_bgn, int src_end) {this.Ctor(src_bgn, src_end);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid_array;}
|
||||
public void Src_end_(int v) {this.src_end = v;}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public byte[] Data_bry() {return null;}
|
||||
public int Len() {return subs_len;} private int subs_len = 0, subs_max = 0;
|
||||
public Json_nde Get_at_as_nde(int i) {
|
||||
Json_itm rv = subs[i]; if (rv.Tid() != Json_itm_.Tid_nde) throw Err_.new_("json", "itm is not nde", "type", rv.Tid(), "i", i);
|
||||
return (Json_nde)rv;
|
||||
}
|
||||
public Json_itm Get_at(int i) {return subs[i];}
|
||||
public Json_itm_ary Add_many(Json_itm... ary) {
|
||||
public Json_ary Add_many(Json_itm... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
Add(ary[i]);
|
||||
@@ -67,5 +71,9 @@ public class Json_itm_ary extends Json_itm_base implements Json_grp {
|
||||
return rv;
|
||||
}
|
||||
private Json_itm[] subs = Json_itm_.Ary_empty;
|
||||
public static Json_itm_ary cast_(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_array ? null : (Json_itm_ary)v;}
|
||||
public static Json_ary cast_or_null(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_array ? null : (Json_ary)v;}
|
||||
public static Json_ary cast(Json_itm v) {
|
||||
if (v == null || v.Tid() != Json_itm_.Tid_array) throw Err_.new_("json", "itm is not array");
|
||||
return (Json_ary)v;
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public class Json_doc {
|
||||
byte[] path = paths[paths_idx];
|
||||
int subs_len = owner.Len();
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm_kv itm = Json_itm_kv.cast_(owner.Get_at(i)); if (itm == null) continue; // ignore simple props, arrays, ndes
|
||||
Json_kv itm = Json_kv.cast_(owner.Get_at(i)); if (itm == null) continue; // ignore simple props, arrays, ndes
|
||||
if (!itm.Key_eq(path)) continue;
|
||||
if (paths_idx == paths_last) return itm.Val();
|
||||
Json_nde sub_nde = Json_nde.cast_(itm.Val()); if (sub_nde == null) return null; // match, but has not a nde; exit
|
||||
|
||||
@@ -17,21 +17,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_doc_bldr {
|
||||
public Json_nde Nde() {return factory.Nde(-1);}
|
||||
public Json_nde Nde(Json_grp owner) {
|
||||
Json_nde rv = factory.Nde(-1);
|
||||
public Json_nde Nde(Json_doc jdoc) {return factory.Nde(jdoc, -1);}
|
||||
public Json_nde Nde(Json_doc jdoc, Json_grp owner) {
|
||||
Json_nde rv = factory.Nde(jdoc, -1);
|
||||
owner.Add(rv);
|
||||
return rv;
|
||||
}
|
||||
public Json_itm Str(byte[] v) {return Str(String_.new_u8(v));}
|
||||
public Json_itm Str(String v) {return Json_itm_tmp.new_str_(v);}
|
||||
public Json_itm Int(int v) {return Json_itm_tmp.new_int_(v);}
|
||||
public Json_itm_kv Kv_int(Json_grp owner, String key, int val) {Json_itm_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_int_(val)); owner.Add(rv); return rv;}
|
||||
public Json_itm_kv Kv_str(Json_grp owner, String key, String val) {Json_itm_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_str_(val)); owner.Add(rv); return rv;}
|
||||
public Json_itm_ary Kv_ary(Json_grp owner, String key, Json_itm... subs) {
|
||||
public Json_kv Kv_int(Json_grp owner, String key, int val) {Json_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_int_(val)); owner.Add(rv); return rv;}
|
||||
public Json_kv Kv_str(Json_grp owner, String key, String val) {Json_kv rv = factory.Kv(Json_itm_tmp.new_str_(key), Json_itm_tmp.new_str_(val)); owner.Add(rv); return rv;}
|
||||
public Json_ary Kv_ary(Json_grp owner, String key, Json_itm... subs) {
|
||||
Json_itm key_itm = Json_itm_tmp.new_str_(key);
|
||||
Json_itm_ary val_ary = factory.Ary(-1, -1);
|
||||
Json_itm_kv kv = factory.Kv(key_itm, val_ary);
|
||||
Json_ary val_ary = factory.Ary(-1, -1);
|
||||
Json_kv kv = factory.Kv(key_itm, val_ary);
|
||||
owner.Add(kv);
|
||||
int len = subs.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
|
||||
@@ -23,7 +23,7 @@ public class Json_factory {
|
||||
public Json_itm_int Int(Json_doc doc, int bgn, int end) {return new Json_itm_int(doc, bgn, end);}
|
||||
public Json_itm Decimal(Json_doc doc, int bgn, int end) {return new Json_itm_decimal(doc, bgn, end);}
|
||||
public Json_itm Str(Json_doc doc, int bgn, int end, boolean exact) {return new Json_itm_str(doc, bgn, end, exact);}
|
||||
public Json_itm_kv Kv(Json_itm key, Json_itm val) {return new Json_itm_kv(key, val);}
|
||||
public Json_itm_ary Ary(int bgn, int end) {return new Json_itm_ary(bgn, end);}
|
||||
public Json_nde Nde(int bgn) {return new Json_nde(bgn);}
|
||||
public Json_kv Kv(Json_itm key, Json_itm val) {return new Json_kv(key, val);}
|
||||
public Json_ary Ary(int bgn, int end) {return new Json_ary(bgn, end);}
|
||||
public Json_nde Nde(Json_doc doc, int bgn) {return new Json_nde(doc, bgn);}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,14 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_itm_kv extends Json_itm_base {
|
||||
public Json_itm_kv(Json_itm key, Json_itm val) {this.key = key; this.val = val;}
|
||||
public class Json_kv extends Json_itm_base {
|
||||
public Json_kv(Json_itm key, Json_itm val) {this.key = key; this.val = val;}
|
||||
@Override public byte Tid() {return Json_itm_.Tid_kv;}
|
||||
public Json_itm Key() {return key;} Json_itm key;
|
||||
public Json_itm Val() {return val;} Json_itm val;
|
||||
public byte[] Key_as_bry() {return key.Data_bry();}
|
||||
public String Key_as_str() {return (String)key.Data();}
|
||||
public byte[] Val_as_bry() {return val.Data_bry();}
|
||||
public boolean Key_eq(byte[] comp) {return ((Json_itm_str)key).Data_eq(comp);}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public byte[] Data_bry() {return null;}
|
||||
@@ -30,6 +32,6 @@ public class Json_itm_kv extends Json_itm_base {
|
||||
bfr.Add_byte(Byte_ascii.Colon);
|
||||
val.Print_as_json(bfr, depth);
|
||||
}
|
||||
public static final Json_itm_kv[] Ary_empty = new Json_itm_kv[0];
|
||||
public static Json_itm_kv cast_(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_kv ? null : (Json_itm_kv)v;}
|
||||
public static final Json_kv[] Ary_empty = new Json_kv[0];
|
||||
public static Json_kv cast_(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid_kv ? null : (Json_kv)v;}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class Json_kv_ary_srl {
|
||||
public static KeyVal Kv_by_itm(Json_itm itm) {
|
||||
switch (itm.Tid()) {
|
||||
case Json_itm_.Tid_kv:
|
||||
Json_itm_kv kv = (Json_itm_kv)itm;
|
||||
Json_kv kv = (Json_kv)itm;
|
||||
return KeyVal_.new_(kv.Key_as_str(), Val_by_itm(kv.Val()));
|
||||
default:
|
||||
throw Err_.new_unhandled(itm.Tid());
|
||||
@@ -33,13 +33,13 @@ public class Json_kv_ary_srl {
|
||||
case Json_itm_.Tid_null:
|
||||
case Json_itm_.Tid_string:
|
||||
case Json_itm_.Tid_decimal: return itm.Data();
|
||||
case Json_itm_.Tid_array: return Val_by_itm_ary((Json_itm_ary)itm);
|
||||
case Json_itm_.Tid_array: return Val_by_itm_ary((Json_ary)itm);
|
||||
case Json_itm_.Tid_nde: return Val_by_itm_nde((Json_nde)itm);
|
||||
case Json_itm_.Tid_kv: // kv should never be val; EX: "a":"b":c; not possible
|
||||
default: throw Err_.new_unhandled(itm.Tid());
|
||||
}
|
||||
}
|
||||
private static KeyVal[] Val_by_itm_ary(Json_itm_ary itm) {
|
||||
private static KeyVal[] Val_by_itm_ary(Json_ary itm) {
|
||||
int subs_len = itm.Len();
|
||||
KeyVal[] rv = new KeyVal[subs_len];
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
|
||||
@@ -16,18 +16,24 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_nde extends Json_itm_base implements Json_grp {
|
||||
public class Json_nde extends Json_itm_base implements Json_grp {
|
||||
private Json_itm[] subs = Json_itm_.Ary_empty; private int subs_len = 0, subs_max = 0;
|
||||
public Json_nde(int src_bgn) {this.Ctor(src_bgn, -1);}
|
||||
public Json_nde(Json_doc jdoc, int src_bgn) {this.jdoc = jdoc; this.Ctor(src_bgn, -1);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid_nde;}
|
||||
public Json_doc Doc() {return jdoc;} private final Json_doc jdoc;
|
||||
public void Src_end_(int v) {this.src_end = v;}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public byte[] Data_bry() {return null;}
|
||||
public int Len() {return subs_len;}
|
||||
public Json_kv Get_at_as_kv(int i) {
|
||||
Json_itm rv_itm = Get_at(i);
|
||||
Json_kv rv = Json_kv.cast_(rv_itm); if (rv == null) throw Err_.new_("json", "sub is not kv", "i", i, "src", Bry_.Mid(jdoc.Src(), this.Src_bgn(), src_end));
|
||||
return rv;
|
||||
}
|
||||
public Json_itm Get_at(int i) {return subs[i];}
|
||||
public Json_nde Get(String key) {return Get(Bry_.new_u8(key));}
|
||||
public Json_nde Get(byte[] key) {
|
||||
Json_itm_kv kv = Json_itm_kv.cast_(this.Get_itm(key)); if (kv == null) throw Err_.new_("json", "kv not found", "key", key);
|
||||
Json_kv kv = Json_kv.cast_(this.Get_itm(key)); if (kv == null) throw Err_.new_("json", "kv not found", "key", key);
|
||||
Json_nde rv = Json_nde.cast_(kv.Val()); if (rv == null) throw Err_.new_("json", "nde not found", "key", key);
|
||||
return rv;
|
||||
}
|
||||
@@ -35,7 +41,7 @@ public class Json_nde extends Json_itm_base implements Json_grp {
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm itm = subs[i];
|
||||
if (itm.Tid() == Json_itm_.Tid_kv) {
|
||||
Json_itm_kv itm_as_kv = (Json_itm_kv)itm;
|
||||
Json_kv itm_as_kv = (Json_kv)itm;
|
||||
if (Bry_.Eq(key, itm_as_kv.Key().Data_bry()))
|
||||
return itm;
|
||||
}
|
||||
@@ -52,7 +58,7 @@ public class Json_nde extends Json_itm_base implements Json_grp {
|
||||
Json_itm kv_obj = Get_itm(key);
|
||||
if (kv_obj == null) return or; // key not found;
|
||||
if (kv_obj.Tid() != Json_itm_.Tid_kv) return or; // key is not a key_val
|
||||
Json_itm_kv kv = (Json_itm_kv)kv_obj;
|
||||
Json_kv kv = (Json_kv)kv_obj;
|
||||
Json_itm val = kv.Val();
|
||||
if (val == null) return or;
|
||||
return val.Data_bry();
|
||||
|
||||
@@ -33,9 +33,9 @@ public class Json_parser {
|
||||
doc.Ctor(src, root);
|
||||
return doc;
|
||||
}
|
||||
Json_nde Make_nde(Json_doc doc) {
|
||||
private Json_nde Make_nde(Json_doc doc) {
|
||||
++pos; // brack_bgn
|
||||
Json_nde nde = new Json_nde(pos);
|
||||
Json_nde nde = new Json_nde(doc, pos);
|
||||
while (pos < src_len) {
|
||||
Skip_ws();
|
||||
if (src[pos] == Byte_ascii.Curly_end) {++pos; return nde;}
|
||||
@@ -55,7 +55,7 @@ public class Json_parser {
|
||||
Chk(Byte_ascii.Colon);
|
||||
Skip_ws();
|
||||
Json_itm val = Make_val(doc);
|
||||
return new Json_itm_kv(key, val);
|
||||
return new Json_kv(key, val);
|
||||
}
|
||||
Json_itm Make_val(Json_doc doc) {
|
||||
while (pos < src_len) {
|
||||
@@ -131,8 +131,8 @@ public class Json_parser {
|
||||
? factory.Decimal(doc, num_bgn, pos)
|
||||
: factory.Int(doc, num_bgn, pos);
|
||||
}
|
||||
Json_itm_ary Make_ary(Json_doc doc) {
|
||||
Json_itm_ary rv = factory.Ary(pos++, pos); // brack_bgn
|
||||
Json_ary Make_ary(Json_doc doc) {
|
||||
Json_ary rv = factory.Ary(pos++, pos); // brack_bgn
|
||||
while (pos < src_len) {
|
||||
Skip_ws();
|
||||
if (src[pos] == Byte_ascii.Brack_end) {++pos; return rv;}
|
||||
|
||||
@@ -52,23 +52,23 @@ class Json_parser_fxt {
|
||||
} Json_parser parser; Json_factory factory; Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
|
||||
Json_itm itm_int_(int v) {return Json_itm_tmp.new_int_(v);}
|
||||
Json_itm itm_str_(String v) {return Json_itm_tmp.new_str_(v);}
|
||||
public Json_itm_ary itm_ary_() {return factory.Ary(-1, -1);}
|
||||
public Json_nde itm_nde_() {return factory.Nde(-1);}
|
||||
public Json_itm_kv itm_kv_null_(String k) {return factory.Kv(itm_str_(k), factory.Null());}
|
||||
public Json_itm_kv itm_kv_(String k, String v) {return factory.Kv(itm_str_(k), itm_str_(v));}
|
||||
public Json_itm_kv itm_kv_(String k, int v) {return factory.Kv(itm_str_(k), itm_int_(v));}
|
||||
public Json_itm_kv itm_kv_(String k, boolean v) {return factory.Kv(itm_str_(k), v ? factory.Bool_y() : factory.Bool_n());}
|
||||
public Json_itm_kv itm_kv_dec_(String k, String v) {return factory.Kv(itm_str_(k), new Json_itm_tmp(Json_itm_.Tid_decimal, v));}
|
||||
public Json_itm_kv itm_kv_(String k, Json_nde v) {return factory.Kv(itm_str_(k), v);}
|
||||
public Json_itm_kv itm_kv_ary_int_(String k, int... v) {
|
||||
Json_itm_ary ary = factory.Ary(-1, -1);
|
||||
public Json_ary itm_ary_() {return factory.Ary(-1, -1);}
|
||||
public Json_nde itm_nde_() {return factory.Nde(null, -1);}
|
||||
public Json_kv itm_kv_null_(String k) {return factory.Kv(itm_str_(k), factory.Null());}
|
||||
public Json_kv itm_kv_(String k, String v) {return factory.Kv(itm_str_(k), itm_str_(v));}
|
||||
public Json_kv itm_kv_(String k, int v) {return factory.Kv(itm_str_(k), itm_int_(v));}
|
||||
public Json_kv itm_kv_(String k, boolean v) {return factory.Kv(itm_str_(k), v ? factory.Bool_y() : factory.Bool_n());}
|
||||
public Json_kv itm_kv_dec_(String k, String v) {return factory.Kv(itm_str_(k), new Json_itm_tmp(Json_itm_.Tid_decimal, v));}
|
||||
public Json_kv itm_kv_(String k, Json_nde v) {return factory.Kv(itm_str_(k), v);}
|
||||
public Json_kv itm_kv_ary_int_(String k, int... v) {
|
||||
Json_ary ary = factory.Ary(-1, -1);
|
||||
int len = v.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
ary.Add(itm_int_(v[i]));
|
||||
return factory.Kv(itm_str_(k), ary);
|
||||
}
|
||||
public Json_itm_kv itm_kv_ary_str_(String k, String... v) {
|
||||
Json_itm_ary ary = factory.Ary(-1, -1);
|
||||
public Json_kv itm_kv_ary_str_(String k, String... v) {
|
||||
Json_ary ary = factory.Ary(-1, -1);
|
||||
int len = v.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
ary.Add(itm_str_(v[i]));
|
||||
@@ -85,7 +85,7 @@ class Json_parser_fxt {
|
||||
public void Test_parse_val0(String raw_str, Object expd) {
|
||||
byte[] raw = Json_parser_tst.Replace_apos(Bry_.new_u8(raw_str));
|
||||
Json_doc doc = parser.Parse(raw);
|
||||
Json_itm_kv kv = Json_itm_kv.cast_(doc.Root().Get_at(0)); // assume root has kv as first sub; EX: {"a":"b"}
|
||||
Json_kv kv = Json_kv.cast_(doc.Root().Get_at(0)); // assume root has kv as first sub; EX: {"a":"b"}
|
||||
Object actl = kv.Val().Data(); // NOTE: Data_bry is escaped val; EX: a\"b has DataBry of a"b
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user