mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.9.1.1
This commit is contained in:
@@ -39,7 +39,7 @@ public class Json_ary extends Json_itm_base implements Json_grp {
|
||||
if (new_len > subs_max) { // ary too small >>> expand
|
||||
subs_max = new_len * 2;
|
||||
Json_itm[] new_subs = new Json_itm[subs_max];
|
||||
Array_.CopyTo(subs, 0, new_subs, 0, subs_len);
|
||||
Array_.Copy_to(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = itm;
|
||||
|
||||
@@ -18,34 +18,44 @@ 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 {
|
||||
private final byte[][] tmp_qry_bry = new byte[1][];
|
||||
public void Ctor(byte[] src, Json_nde root) {this.src = src; this.root = root;}
|
||||
public void Ctor(byte[] src, Json_grp new_root) {
|
||||
this.src = src;
|
||||
this.root_grp = new_root;
|
||||
switch (root_grp.Tid()) {
|
||||
case Json_itm_.Tid__nde: this.root_ary = null; this.root_nde = (Json_nde)root_grp; break;
|
||||
case Json_itm_.Tid__ary: this.root_nde = null; this.root_ary = (Json_ary)root_grp; break;
|
||||
default: throw Err_.new_unhandled(root_grp.Tid());
|
||||
}
|
||||
}
|
||||
public byte[] Src() {return src;} private byte[] src;
|
||||
public Json_nde Root_nde() {return root;} private Json_nde root;
|
||||
public Json_grp Root_grp() {return root_grp;} private Json_grp root_grp;
|
||||
public Json_nde Root_nde() {return root_nde;} private Json_nde root_nde;
|
||||
public Json_ary Root_ary() {return root_ary;} private Json_ary root_ary;
|
||||
public Bry_bfr Bfr() {return bfr;} private final Bry_bfr bfr = Bry_bfr.new_();
|
||||
public Number_parser Utl_num_parser() {return utl_num_parser;} private final Number_parser utl_num_parser = new Number_parser();
|
||||
public byte[] Tmp_u8_bry() {return tmp_u8_bry;} private final byte[] tmp_u8_bry = new byte[6]; // tmp bry[] for decoding sequences like \u0008
|
||||
public byte[] Get_val_as_bry_or(byte[] qry_bry, byte[] or) {tmp_qry_bry[0] = qry_bry; return Get_val_as_bry_or(tmp_qry_bry, or);}
|
||||
public byte[] Get_val_as_bry_or(byte[][] qry_bry, byte[] or) {
|
||||
Json_itm nde = Find_nde(root, qry_bry, qry_bry.length - 1, 0);
|
||||
Json_itm nde = Find_nde(root_nde, qry_bry, qry_bry.length - 1, 0);
|
||||
return nde == null || nde.Tid() != Json_itm_.Tid__str ? or : nde.Data_bry();
|
||||
}
|
||||
public String Get_val_as_str_or(byte[] qry_bry, String or) {tmp_qry_bry[0] = qry_bry; return Get_val_as_str_or(tmp_qry_bry, or);}
|
||||
public String Get_val_as_str_or(byte[][] qry_bry, String or) {
|
||||
Json_itm nde = Find_nde(root, qry_bry, qry_bry.length - 1, 0);
|
||||
Json_itm nde = Find_nde(root_nde, qry_bry, qry_bry.length - 1, 0);
|
||||
return nde == null || nde.Tid() != Json_itm_.Tid__str ? or : (String)nde.Data();
|
||||
}
|
||||
public Json_grp Get_grp(byte[] qry_bry) {
|
||||
tmp_qry_bry[0] = qry_bry;
|
||||
Json_itm rv = Find_nde(root, tmp_qry_bry, 0, 0); if (rv == null) return null;
|
||||
Json_itm rv = Find_nde(root_nde, tmp_qry_bry, 0, 0); if (rv == null) return null;
|
||||
return (Json_grp)rv;
|
||||
}
|
||||
public Json_grp Get_grp(byte[][] qry_bry) {
|
||||
Json_itm rv = Find_nde(root, qry_bry, qry_bry.length - 1, 0); if (rv == null) return null;
|
||||
Json_itm rv = Find_nde(root_nde, qry_bry, qry_bry.length - 1, 0); if (rv == null) return null;
|
||||
return (Json_grp)rv;
|
||||
}
|
||||
public Json_itm Find_nde(byte[] key) {
|
||||
tmp_qry_bry[0] = key;
|
||||
return Find_nde(root, tmp_qry_bry, 0, 0);
|
||||
return Find_nde(root_nde, tmp_qry_bry, 0, 0);
|
||||
}
|
||||
private Json_itm Find_nde(Json_nde owner, byte[][] paths, int paths_last, int paths_idx) {
|
||||
byte[] path = paths[paths_idx];
|
||||
@@ -59,4 +69,5 @@ public class Json_doc {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String Make_str_by_apos(String... ary) {return String_.Replace(String_.Concat_lines_nl_skip_last(ary), "'", "\"");}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public class Json_nde extends Json_itm_base implements Json_grp {
|
||||
if (new_len > subs_max) { // ary too small >>> expand
|
||||
subs_max = new_len * 2;
|
||||
Json_itm[] new_subs = new Json_itm[subs_max];
|
||||
Array_.CopyTo(subs, 0, new_subs, 0, subs_len);
|
||||
Array_.Copy_to(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = (Json_itm)itm;
|
||||
|
||||
@@ -36,8 +36,11 @@ public class Json_parser {
|
||||
}
|
||||
Skip_ws();
|
||||
Json_doc doc = new Json_doc();
|
||||
Json_nde root = Make_nde(doc);
|
||||
if (root_is_nde) {}
|
||||
Json_grp root = null;
|
||||
if (root_is_nde)
|
||||
root = Make_nde(doc);
|
||||
else
|
||||
root = Make_ary(doc);
|
||||
doc.Ctor(src, root);
|
||||
return doc;
|
||||
}
|
||||
|
||||
74
400_xowa/src/gplx/core/json/Json_parser__itm__base.java
Normal file
74
400_xowa/src/gplx/core/json/Json_parser__itm__base.java
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
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.*;
|
||||
import gplx.core.primitives.*;
|
||||
public abstract class Json_parser__itm__base {
|
||||
protected String context;
|
||||
protected final Hash_adp_bry hash = Hash_adp_bry.cs();
|
||||
protected final Bry_bfr tmp_bfr = Bry_bfr.new_(255);
|
||||
protected String[] keys;
|
||||
protected Json_kv[] atrs;
|
||||
protected Json_itm cur_itm;
|
||||
protected int keys_len;
|
||||
public void Ctor(String... keys) {
|
||||
this.keys = keys;
|
||||
this.keys_len = keys.length;
|
||||
for (int i = 0; i < keys_len; ++i)
|
||||
hash.Add(Bry_.new_u8(keys[i]), Int_obj_val.new_(i));
|
||||
this.atrs = new Json_kv[keys_len];
|
||||
}
|
||||
public int Kv__int(Json_kv[] ary, int i) {return Bry_.To_int(ary[i].Val_as_bry());}
|
||||
public long Kv__long(Json_kv[] ary, int i) {return Bry_.To_long_or(ary[i].Val_as_bry(), 0);}
|
||||
public long Kv__long_or_0(Json_kv[] ary, int i) {
|
||||
Json_kv kv = ary[i]; if (kv == null) return 0;
|
||||
return Bry_.To_long_or(kv.Val_as_bry(), 0);
|
||||
}
|
||||
public byte[] Kv__bry(Json_kv[] ary, int i) {
|
||||
byte[] rv = Kv__bry_or_null(ary, i); if (rv == null) throw Err_.new_("json.parser", "missing val", "key", context + "." + keys[i], "excerpt", Json_itm_.To_bry(tmp_bfr, cur_itm));
|
||||
return rv;
|
||||
}
|
||||
public byte[][] Kv__bry_ary(Json_kv[] ary, int i) {
|
||||
return ary[i].Val_as_ary().Xto_bry_ary();
|
||||
}
|
||||
public byte[] Kv__bry_or_empty(Json_kv[] ary, int i) {
|
||||
byte[] rv = Kv__bry_or_null(ary, i);
|
||||
return rv == null ? Bry_.Empty : rv;
|
||||
}
|
||||
public byte[] Kv__bry_or_null(Json_kv[] ary, int i) {
|
||||
Json_kv kv = ary[i]; if (kv == null) return null;
|
||||
Json_itm val = kv.Val();
|
||||
return kv == null ? null : val.Data_bry();
|
||||
}
|
||||
public boolean Kv__mw_bool(Json_kv[] ary, int i) {
|
||||
Json_kv kv = ary[i]; if (kv == null) return false;
|
||||
Json_itm val = kv.Val();
|
||||
if ( val.Tid() == Json_itm_.Tid__str
|
||||
&& Bry_.Len_eq_0(val.Data_bry())) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
Warn("unknown val: val=" + String_.new_u8(kv.Data_bry()) + " excerpt=" + String_.new_u8(Json_itm_.To_bry(tmp_bfr, cur_itm)), kv);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public boolean Kv__has(Json_kv[] ary, int i) {return Kv__bry_or_empty(ary, i) != null;}
|
||||
protected abstract void Parse_hook_nde(Json_nde sub, Json_kv[] atrs);
|
||||
protected void Warn(String msg, Json_kv kv) {
|
||||
Gfo_usr_dlg_.I.Warn_many("", "", msg + ": path=~{0}.~{1} excerpt=~{2}", context, kv.Key_as_bry(), Json_itm_.To_bry(tmp_bfr, cur_itm));
|
||||
}
|
||||
}
|
||||
70
400_xowa/src/gplx/core/json/Json_parser__list_nde__base.java
Normal file
70
400_xowa/src/gplx/core/json/Json_parser__list_nde__base.java
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
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.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Json_parser__list_nde__base extends Json_parser__itm__base {
|
||||
public void Parse_grp(String context, Json_grp grp) {
|
||||
this.context = context;
|
||||
int len = grp.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_nde sub = null;
|
||||
if (grp.Tid() == Json_itm_.Tid__nde) {
|
||||
Json_kv kv = Json_nde.cast(grp).Get_at_as_kv(i);
|
||||
sub = kv.Val_as_nde();
|
||||
}
|
||||
else {
|
||||
sub = Json_nde.cast(grp.Get_at(i));
|
||||
}
|
||||
Parse_nde(context, sub);
|
||||
}
|
||||
}
|
||||
public void Parse_nde(String context, Json_nde nde) {
|
||||
this.cur_itm = nde;
|
||||
for (int j = 0; j < keys_len; ++j)
|
||||
atrs[j] = null;
|
||||
int atr_len = nde.Len();
|
||||
for (int j = 0; j < atr_len; ++j) {
|
||||
Json_kv atr = nde.Get_at_as_kv(j);
|
||||
Object idx_obj = hash.Get_by_bry(atr.Key_as_bry());
|
||||
if (idx_obj == null) {Warn("unknown key", atr); continue;}
|
||||
int idx_int = ((Int_obj_val)idx_obj).Val();
|
||||
atrs[idx_int] = atr;
|
||||
}
|
||||
Parse_hook_nde(nde, atrs);
|
||||
}
|
||||
public void Parse_to_list_as_bry(String context, Json_ary ary, Ordered_hash list) {
|
||||
this.cur_itm = ary;
|
||||
int len = ary.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
byte[] val = ary.Get_at(i).Data_bry();
|
||||
list.Add(val, val);
|
||||
}
|
||||
}
|
||||
public void Parse_to_list_as_kv(String context, Json_nde nde, Ordered_hash list) {
|
||||
this.cur_itm = nde;
|
||||
int len = nde.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_kv sub = nde.Get_at_as_kv(i);
|
||||
byte[] key = sub.Key_as_bry();
|
||||
byte[] val = Parse_to_list_as_kv__get_val(sub, key);
|
||||
list.Add(key, KeyVal_.new_(String_.new_u8(key), String_.new_u8(val)));
|
||||
}
|
||||
}
|
||||
@gplx.Virtual protected byte[] Parse_to_list_as_kv__get_val(Json_kv sub, byte[] key) {return sub.Val_as_bry();}
|
||||
@Override protected void Parse_hook_nde(Json_nde sub, Json_kv[] atrs) {}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ public class Json_parser_tst {
|
||||
@Test public void Subs_empty() {fxt.Test_parse("{'k0':{}}", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", fxt.itm_nde_())));}
|
||||
@Test public void Subs_ws() {fxt.Test_parse("{'k0': { 'k00' : 1 } }", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", fxt.itm_nde_().Add_many(fxt.itm_kv_("k00", 1)))));}
|
||||
@Test public void Ws() {fxt.Test_parse(" { 'k0' : 'v0' } ", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", "v0")));}
|
||||
@Test public void Root_is_ary() {fxt.Test_parse("[ 1 , 2 , 3 ]", fxt.itm_ary_().Add_many(fxt.itm_int_(1), fxt.itm_int_(2), fxt.itm_int_(3)));}
|
||||
public static String Replace_apos_as_str(String v) {return String_.new_u8(Replace_apos(Bry_.new_u8(v)));}
|
||||
public static byte[] Replace_apos(byte[] v) {return Bry_.Replace(v, Byte_ascii.Apos, Byte_ascii.Quote);}
|
||||
}
|
||||
@@ -50,10 +51,10 @@ class Json_parser_fxt {
|
||||
factory = parser.Factory();
|
||||
}
|
||||
} 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 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_ary itm_ary_() {return factory.Ary(-1, -1);}
|
||||
public Json_nde itm_nde_() {return factory.Nde(null, -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));}
|
||||
@@ -77,7 +78,7 @@ class Json_parser_fxt {
|
||||
public void Test_parse(String raw_str, Json_itm... expd_ary) {
|
||||
byte[] raw = Json_parser_tst.Replace_apos(Bry_.new_u8(raw_str));
|
||||
Json_doc doc = parser.Parse(raw);
|
||||
doc.Root_nde().Print_as_json(tmp_bfr, 0);
|
||||
doc.Root_grp().Print_as_json(tmp_bfr, 0);
|
||||
String actl = tmp_bfr.Xto_str_and_clear();
|
||||
String expd = Xto_str(raw, doc, expd_ary, 0, expd_ary.length);
|
||||
Tfds.Eq_str_lines(expd, actl, actl);
|
||||
|
||||
@@ -18,9 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.core.json; import gplx.*; import gplx.core.*;
|
||||
public class Json_wtr {
|
||||
private final Bry_bfr bfr = Bry_bfr.new_(255);
|
||||
private final Int_ary idx_stack = new Int_ary(4);
|
||||
private int idx = 0;
|
||||
private int indent;
|
||||
private boolean nde_itm_is_first;
|
||||
private boolean ary_itm_is_first;
|
||||
public Bry_bfr Bfr() {return bfr;}
|
||||
public void Indent_(int v) {this.indent = v;}
|
||||
public byte Opt_quote_byte() {return opt_quote_byte;} public Json_wtr Opt_quote_byte_(byte v) {opt_quote_byte = v; return this;} private byte opt_quote_byte = Byte_ascii.Quote;
|
||||
@@ -28,38 +28,49 @@ public class Json_wtr {
|
||||
public byte[] To_bry_and_clear() {return bfr.Xto_bry_and_clear();}
|
||||
public String To_str_and_clear() {return bfr.Xto_str_and_clear();}
|
||||
public Json_wtr Clear() {
|
||||
indent = 0;
|
||||
nde_itm_is_first = ary_itm_is_first = true;
|
||||
indent = -1;
|
||||
idx_stack.Clear();
|
||||
idx = 0;
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Doc_bgn() {return Add_grp_bgn(Sym_nde_bgn);}
|
||||
public Json_wtr Doc_end() {Add_grp_end(Bool_.Y, Sym_nde_end); return Add_nl();}
|
||||
public Json_wtr Doc_nde_bgn() {return Write_grp_bgn(Sym_nde_bgn);}
|
||||
public Json_wtr Doc_nde_end() {Write_grp_end(Bool_.Y, Sym_nde_end); return Write_nl();}
|
||||
public Json_wtr Doc_ary_bgn() {return Write_grp_bgn(Sym_ary_bgn);}
|
||||
public Json_wtr Doc_ary_end() {Write_grp_end(Bool_.N, Sym_ary_end); return Write_nl();}
|
||||
public Json_wtr Nde_bgn(String key) {return Nde_bgn(Bry_.new_u8(key));}
|
||||
public Json_wtr Nde_bgn(byte[] key) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(key);
|
||||
Add_nl();
|
||||
return Add_grp_bgn(Sym_nde_bgn);
|
||||
Write_indent_itm();
|
||||
Write_key(key);
|
||||
Write_nl();
|
||||
return Write_grp_bgn(Sym_nde_bgn);
|
||||
}
|
||||
public Json_wtr Nde_end() {
|
||||
Write_grp_end(Bool_.Y, Sym_nde_end);
|
||||
return Write_nl();
|
||||
}
|
||||
public Json_wtr Nde_end() {Add_grp_end(Bool_.Y, Sym_nde_end); return Add_nl();}
|
||||
public Json_wtr Ary_bgn(String nde) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(Bry_.new_u8(nde));
|
||||
Add_nl();
|
||||
ary_itm_is_first = true;
|
||||
return Add_grp_bgn(Sym_ary_bgn);
|
||||
Write_indent_itm();
|
||||
Write_key(Bry_.new_u8(nde));
|
||||
return Ary_bgn_keyless();
|
||||
}
|
||||
private Json_wtr Ary_bgn_keyless() {
|
||||
Write_nl();
|
||||
return Write_grp_bgn(Sym_ary_bgn);
|
||||
}
|
||||
public Json_wtr Ary_itm_str(String itm) {return Ary_itm_by_type_tid(Type_adp_.Tid__str, itm);}
|
||||
public Json_wtr Ary_itm_bry(byte[] itm) {return Ary_itm_by_type_tid(Type_adp_.Tid__bry, itm);}
|
||||
public Json_wtr Ary_itm_obj(Object itm) {return Ary_itm_by_type_tid(Type_adp_.To_tid_obj(itm), itm);}
|
||||
public Json_wtr Ary_itm_by_type_tid(int itm_type_tid, Object itm) {
|
||||
Add_indent_itm(ary_itm_is_first);
|
||||
Add_itm_by_tid(itm_type_tid, itm);
|
||||
Add_nl();
|
||||
ary_itm_is_first = false;
|
||||
Write_indent_itm();
|
||||
Write_val_obj(itm_type_tid, itm);
|
||||
Write_nl();
|
||||
++idx;
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Ary_end() {Add_grp_end(Bool_.N, Sym_ary_end); return Add_nl();}
|
||||
public Json_wtr Ary_end() {
|
||||
Write_grp_end(Bool_.N, Sym_ary_end);
|
||||
return Write_nl();
|
||||
}
|
||||
public Json_wtr Kv_bool(String key, boolean val) {return Kv_bool(Bry_.new_u8(key), val);}
|
||||
public Json_wtr Kv_bool(byte[] key, boolean val) {return Kv_raw(key, val ? Bool_.True_bry : Bool_.False_bry);}
|
||||
public Json_wtr Kv_int(String key, int val) {return Kv_raw(Bry_.new_u8(key), Int_.Xto_bry(val));}
|
||||
@@ -67,83 +78,111 @@ public class Json_wtr {
|
||||
public Json_wtr Kv_float(String key, float val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Float_.Xto_str(val)));}
|
||||
public Json_wtr Kv_double(String key, double val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Double_.Xto_str(val)));}
|
||||
private Json_wtr Kv_raw(byte[] key, byte[] val) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(key);
|
||||
Write_indent_itm();
|
||||
Write_key(key);
|
||||
bfr.Add(val);
|
||||
Add_nl();
|
||||
nde_itm_is_first = false;
|
||||
Write_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Kv_str(String key, String val) {return Kv_bry(Bry_.new_u8(key), Bry_.new_u8(val));}
|
||||
public Json_wtr Kv_str(byte[] key, String val) {return Kv_bry(key, Bry_.new_u8(val));}
|
||||
public Json_wtr Kv_bry(String key, byte[] val) {return Kv_bry(Bry_.new_u8(key), val);}
|
||||
public Json_wtr Kv_bry(byte[] key, byte[] val) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(key);
|
||||
Add_itm_bry(val);
|
||||
Add_nl();
|
||||
nde_itm_is_first = false;
|
||||
Write_indent_itm();
|
||||
Write_key(key);
|
||||
Write_str(val);
|
||||
Write_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Kv_obj(byte[] key, Object val, int val_tid) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(key);
|
||||
Add_itm_by_tid(val_tid, val);
|
||||
Add_nl();
|
||||
nde_itm_is_first = false;
|
||||
Write_indent_itm();
|
||||
Write_key(key);
|
||||
Write_val_obj(val_tid, val);
|
||||
Write_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Kv_bfr(String key, Bry_bfr val) {
|
||||
Add_indent_itm(nde_itm_is_first);
|
||||
Add_key(Bry_.new_u8(key));
|
||||
Add_itm_bry(val.Bfr(), 0, val.Len());
|
||||
Add_nl();
|
||||
nde_itm_is_first = false;
|
||||
val.Clear();
|
||||
return this;
|
||||
}
|
||||
private Json_wtr Add_grp_bgn(byte[] grp_sym) {
|
||||
Add_indent(0);
|
||||
bfr.Add(grp_sym);
|
||||
private Json_wtr Write_grp_bgn(byte[] grp_sym) {return Write_grp_bgn(grp_sym, Bool_.Y);}
|
||||
private Json_wtr Write_grp_bgn(byte[] grp_sym, boolean write_indent) {
|
||||
idx_stack.Add(idx);
|
||||
idx = 0;
|
||||
++indent;
|
||||
nde_itm_is_first = true;
|
||||
return this;
|
||||
}
|
||||
private Json_wtr Add_grp_end(boolean grp_is_nde, byte[] grp_sym) {
|
||||
--indent;
|
||||
if ((grp_is_nde && nde_itm_is_first) || (!grp_is_nde && ary_itm_is_first))
|
||||
Add_nl();
|
||||
Add_indent(0);
|
||||
nde_itm_is_first = false;
|
||||
if (write_indent) Write_indent();
|
||||
bfr.Add(grp_sym);
|
||||
return this;
|
||||
}
|
||||
private Json_wtr Add_key(byte[] bry) {
|
||||
Add_itm_bry(bry);
|
||||
bfr.Add_byte_colon();
|
||||
private Json_wtr Write_grp_end(boolean grp_is_nde, byte[] grp_sym) {
|
||||
if ((grp_is_nde && idx == 0) || (!grp_is_nde && idx == 0))
|
||||
Write_nl();
|
||||
Write_indent();
|
||||
--indent;
|
||||
bfr.Add(grp_sym);
|
||||
this.idx = idx_stack.Pop_or(0);
|
||||
return this;
|
||||
}
|
||||
private void Add_itm_by_tid(int type_tid, Object obj) {
|
||||
private Json_wtr Write_key(byte[] bry) {
|
||||
Write_str(bry); // "key"
|
||||
bfr.Add_byte_colon(); // ":"
|
||||
++idx;
|
||||
return this;
|
||||
}
|
||||
private void Write_val_obj(int type_tid, Object obj) {
|
||||
switch (type_tid) {
|
||||
case Type_adp_.Tid__null: bfr.Add(Object_.Bry__null); break;
|
||||
case Type_adp_.Tid__bool: bfr.Add_bool(Bool_.cast(obj)); break;
|
||||
case Type_adp_.Tid__byte: bfr.Add_byte(Byte_.cast(obj)); break;
|
||||
case Type_adp_.Tid__int: bfr.Add_int_variable(Int_.cast(obj)); break;
|
||||
case Type_adp_.Tid__long: bfr.Add_long_variable(Long_.cast(obj)); break;
|
||||
case Type_adp_.Tid__float: bfr.Add_float(Float_.cast(obj)); break;
|
||||
case Type_adp_.Tid__double: bfr.Add_double(Double_.cast(obj)); break;
|
||||
case Type_adp_.Tid__str: Add_itm_bry(Bry_.new_u8((String)obj)); break;
|
||||
case Type_adp_.Tid__bry: Add_itm_bry((byte[])obj); break;
|
||||
case Type_adp_.Tid__str: Write_str(Bry_.new_u8((String)obj)); break;
|
||||
case Type_adp_.Tid__bry: Write_str((byte[])obj); break;
|
||||
case Type_adp_.Tid__char:
|
||||
case Type_adp_.Tid__date:
|
||||
case Type_adp_.Tid__decimal: Add_itm_bry(Bry_.new_u8(Object_.Xto_str_strict_or_empty(obj))); break;
|
||||
case Type_adp_.Tid__null:
|
||||
case Type_adp_.Tid__decimal: Write_str(Bry_.new_u8(Object_.Xto_str_strict_or_empty(obj))); break;
|
||||
case Type_adp_.Tid__obj:
|
||||
Class<?> type = obj.getClass();
|
||||
if (Type_adp_.Eq(type, KeyVal[].class)) {
|
||||
if (idx == 0) { // if nde, and first item, then put on new line
|
||||
bfr.Del_by_1();
|
||||
if (opt_ws) {
|
||||
bfr.Add_byte_nl();
|
||||
++indent;
|
||||
Write_indent();
|
||||
--indent;
|
||||
}
|
||||
}
|
||||
KeyVal[] kvy = (KeyVal[])obj;
|
||||
Write_grp_bgn(Sym_nde_bgn, Bool_.N);
|
||||
int kvy_len = kvy.length;
|
||||
for (int i = 0; i < kvy_len; ++i) {
|
||||
KeyVal kv = kvy[i];
|
||||
Object kv_val = kv.Val();
|
||||
Kv_obj(Bry_.new_u8(kv.Key()), kv_val, Type_adp_.To_tid_obj(kv_val));
|
||||
}
|
||||
Write_grp_end(Bool_.Y, Sym_nde_end);
|
||||
}
|
||||
else if (Type_adp_.Is_array(type))
|
||||
Write_val_ary(obj);
|
||||
else
|
||||
throw Err_.new_unhandled(type);
|
||||
break;
|
||||
default: throw Err_.new_unhandled(type_tid);
|
||||
}
|
||||
}
|
||||
private void Add_itm_bry(byte[] bry) {Add_itm_bry(bry, 0, bry.length);}
|
||||
private void Add_itm_bry(byte[] bry, int bgn, int end) {
|
||||
private void Write_val_ary(Object ary_obj) {
|
||||
Ary_bgn_keyless();
|
||||
Object ary = Array_.cast(ary_obj);
|
||||
int len = Array_.Len(ary);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Object itm = Array_.Get_at(ary, i);
|
||||
Ary_itm_obj(itm);
|
||||
}
|
||||
Write_grp_end(Bool_.N, Sym_ary_end);
|
||||
}
|
||||
private void Write_str(byte[] bry) {
|
||||
int len = bry.length;
|
||||
bfr.Add_byte(opt_quote_byte);
|
||||
for (int i = bgn; i < end; i++) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
byte b = bry[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash: bfr.Add_byte(Byte_ascii.Backslash).Add_byte(b); break; // "\" -> "\\"; needed else js will usurp \ as escape; EX: "\&" -> "&"; DATE:2014-06-24
|
||||
@@ -156,30 +195,30 @@ public class Json_wtr {
|
||||
}
|
||||
bfr.Add_byte(opt_quote_byte);
|
||||
}
|
||||
private void Add_indent_itm(boolean v) {
|
||||
if (v) {
|
||||
bfr.Add_byte_space();
|
||||
private void Write_indent_itm() {
|
||||
if (idx == 0) {
|
||||
if (opt_ws)
|
||||
bfr.Add_byte_space();
|
||||
}
|
||||
else {
|
||||
Add_indent(-1);
|
||||
Write_indent();
|
||||
bfr.Add(Sym_itm_spr);
|
||||
if (opt_ws) bfr.Add_byte_space();
|
||||
}
|
||||
}
|
||||
private Json_wtr Add_nl() {
|
||||
if (opt_ws)
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
private void Write_indent() {
|
||||
if (opt_ws && indent > 0)
|
||||
bfr.Add_byte_repeat(Byte_ascii.Space, indent * 2);
|
||||
}
|
||||
private void Add_indent(int adj) {
|
||||
int level = indent + adj;
|
||||
if (opt_ws && level > 0)
|
||||
bfr.Add_byte_repeat(Byte_ascii.Space, level * 2);
|
||||
private Json_wtr Write_nl() {
|
||||
if (opt_ws) bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
private static final byte[]
|
||||
Sym_nde_bgn = Bry_.new_a7("{")
|
||||
, Sym_nde_end = Bry_.new_a7("}")
|
||||
, Sym_ary_bgn = Bry_.new_a7("[")
|
||||
, Sym_ary_end = Bry_.new_a7("]")
|
||||
, Sym_itm_spr = Bry_.new_a7(", ")
|
||||
, Sym_itm_spr = Bry_.new_a7(",")
|
||||
;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.junit.*;
|
||||
public class Json_wtr_tst {
|
||||
@Before public void init() {fxt.Clear();} private final Json_wtr_fxt fxt = new Json_wtr_fxt();
|
||||
@Test public void Root() {
|
||||
fxt.Wtr().Doc_bgn().Doc_end();
|
||||
fxt.Wtr().Doc_nde_bgn().Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{"
|
||||
, "}"
|
||||
@@ -28,10 +28,10 @@ public class Json_wtr_tst {
|
||||
}
|
||||
@Test public void Kv() {
|
||||
fxt.Wtr()
|
||||
.Doc_bgn()
|
||||
.Doc_nde_bgn()
|
||||
.Kv_str("k0", "v0")
|
||||
.Kv_str("k1", "v1")
|
||||
.Doc_end();
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 'k0':'v0'"
|
||||
, ", 'k1':'v1'"
|
||||
@@ -40,7 +40,7 @@ public class Json_wtr_tst {
|
||||
}
|
||||
@Test public void Nde() {
|
||||
fxt.Wtr()
|
||||
.Doc_bgn()
|
||||
.Doc_nde_bgn()
|
||||
.Nde_bgn("s0")
|
||||
.Nde_bgn("s00")
|
||||
.Nde_end()
|
||||
@@ -49,7 +49,7 @@ public class Json_wtr_tst {
|
||||
.Nde_bgn("s10")
|
||||
.Nde_end()
|
||||
.Nde_end()
|
||||
.Doc_end();
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 's0':"
|
||||
, " { 's00':"
|
||||
@@ -66,12 +66,12 @@ public class Json_wtr_tst {
|
||||
}
|
||||
@Test public void Ary() {
|
||||
fxt.Wtr()
|
||||
.Doc_bgn()
|
||||
.Doc_nde_bgn()
|
||||
.Ary_bgn("a0")
|
||||
.Ary_itm_str("v0")
|
||||
.Ary_itm_str("v1")
|
||||
.Ary_end()
|
||||
.Doc_end();
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 'a0':"
|
||||
, " [ 'v0'"
|
||||
@@ -80,6 +80,26 @@ public class Json_wtr_tst {
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Nde__nested() {
|
||||
fxt.Wtr()
|
||||
.Doc_nde_bgn()
|
||||
.Ary_bgn("a0")
|
||||
.Ary_itm_obj(KeyVal_.Ary
|
||||
( KeyVal_.new_("k1", "v1")
|
||||
, KeyVal_.new_("k2", "v2")
|
||||
))
|
||||
.Ary_end()
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 'a0':"
|
||||
, " ["
|
||||
, " { 'k1':'v1'"
|
||||
, " , 'k2':'v2'"
|
||||
, " }"
|
||||
, " ]"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Json_wtr_fxt {
|
||||
private final Json_wtr wtr = new Json_wtr().Opt_quote_byte_(Byte_ascii.Apos);
|
||||
|
||||
@@ -17,12 +17,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.net; import gplx.*; import gplx.core.*;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
public class Server_socket_adp__base implements Server_socket_adp {
|
||||
private ServerSocket server_socket;
|
||||
public Server_socket_adp Ctor(int port) {
|
||||
try {this.server_socket = new ServerSocket(port);}
|
||||
try {
|
||||
this.server_socket = new ServerSocket();
|
||||
server_socket.setReuseAddress(true);
|
||||
server_socket.bind(new InetSocketAddress(port));
|
||||
}
|
||||
catch (IOException e) {throw Err_.new_exc(e, "net", "Get_input_stream failed");}
|
||||
return this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user