mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Embeddable: Create core dbs in proper subdirectory
This commit is contained in:
@@ -13,66 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
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__ary;}
|
||||
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_nde Get_as_nde(int i) {return Json_nde.cast(subs[i]);}
|
||||
public Json_ary Add_many(Json_itm... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
Add(ary[i]);
|
||||
return this;
|
||||
}
|
||||
public void Add(Json_itm itm) {
|
||||
int new_len = subs_len + 1;
|
||||
if (new_len > subs_max) { // ary too small >>> expand
|
||||
subs_max = new_len * 2;
|
||||
Json_itm[] new_subs = new Json_itm[subs_max];
|
||||
Array_.Copy_to(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = itm;
|
||||
subs_len = new_len;
|
||||
}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
if (subs_len == 0) { // empty grp; print on one line (rather than printing across 3)
|
||||
bfr.Add_byte(Byte_ascii.Brack_bgn).Add_byte(Byte_ascii.Brack_end);
|
||||
return;
|
||||
}
|
||||
bfr.Add_byte_nl();
|
||||
Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Brack_bgn).Add_byte(Byte_ascii.Space);
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
if (i != 0) {
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Comma).Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
subs[i].Print_as_json(bfr, depth + 1);
|
||||
}
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Brack_end).Add_byte_nl();
|
||||
}
|
||||
public byte[][] Xto_bry_ary() {
|
||||
if (subs_len == 0) return Bry_.Ary_empty;
|
||||
byte[][] rv = new byte[subs_len][];
|
||||
for (int i = 0; i < subs_len; ++i)
|
||||
rv[i] = subs[i].Data_bry();
|
||||
return rv;
|
||||
}
|
||||
private Json_itm[] subs = Json_itm_.Ary_empty;
|
||||
public static Json_ary cast_or_null(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid__ary ? null : (Json_ary)v;}
|
||||
public static Json_ary cast(Json_itm v) {
|
||||
if (v == null || v.Tid() != Json_itm_.Tid__ary) throw Err_.new_("json", "itm is not array");
|
||||
return (Json_ary)v;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,70 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Json_doc {
|
||||
private final byte[][] tmp_qry_bry = new byte[1][];
|
||||
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_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 Gfo_number_parser Utl_num_parser() {return utl_num_parser;} private final Gfo_number_parser utl_num_parser = new Gfo_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_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_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_nde, tmp_qry_bry, 0, 0); if (rv == null) return null;
|
||||
return (Json_grp)rv;
|
||||
}
|
||||
public Json_grp Get_grp_many(String... qry_ary) {return Get_grp_many(Bry_.Ary(qry_ary));}
|
||||
public Json_grp Get_grp_many(byte[]... qry_bry) {
|
||||
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_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];
|
||||
int subs_len = owner.Len();
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
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
|
||||
return Find_nde(sub_nde, paths, paths_last, paths_idx + 1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String Make_str_by_apos(String... ary) {return String_.Replace(String_.Concat_lines_nl_skip_last(ary), "'", "\"");}
|
||||
public static String[] Make_str_ary_by_apos(String... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String itm = ary[i];
|
||||
if (String_.Has(itm, "'"))
|
||||
ary[i] = String_.Replace(itm, "'", "\"");
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,28 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_doc_bldr {
|
||||
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_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_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++)
|
||||
val_ary.Add(subs[i]);
|
||||
return val_ary;
|
||||
}
|
||||
Json_doc doc = new Json_doc(); Json_factory factory = new Json_factory();
|
||||
}
|
||||
|
||||
@@ -13,75 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_doc_srl {
|
||||
private int indent = -1;
|
||||
private Bry_bfr bfr = Bry_bfr_.Reset(255);
|
||||
public boolean Ws_enabled() {return ws_enabled;} public void Ws_enabled_(boolean v) {ws_enabled = v;} private boolean ws_enabled = false;
|
||||
public byte[] Bld() {return bfr.To_bry_and_clear();}
|
||||
public String Bld_as_str() {return bfr.To_str_and_clear();}
|
||||
public Json_doc_srl Write_root(byte[] key, Object val) {
|
||||
Write_nde_bgn();
|
||||
Write_obj(false, key, val);
|
||||
Write_nde_end();
|
||||
return this;
|
||||
}
|
||||
public void Write_obj(boolean comma, byte[] key, Object val) {
|
||||
Class<?> t = Type_.Type_by_obj(val);
|
||||
if (Type_.Is_array(t))
|
||||
Write_kv_ary(comma, key, (Object[])val);
|
||||
else
|
||||
Write_kv_str(comma, key, Object_.Xto_str_strict_or_empty(val));
|
||||
}
|
||||
private void Write_kv_ary(boolean comma, byte[] key, Object[] val) {
|
||||
Write_key(comma, key); Write_new_line(); // '"key":\n'
|
||||
Write_ary_bgn(); // '[\n'
|
||||
Indent_add(); // -->
|
||||
int len = val.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Write_itm_hdr(i != 0); // ', '
|
||||
Write_str(Bry_.new_u8(Object_.Xto_str_strict_or_null(val[i])));
|
||||
Write_new_line();
|
||||
}
|
||||
Indent_del();
|
||||
Write_ary_end();
|
||||
}
|
||||
private void Write_kv_str(boolean comma, byte[] key, String val) {
|
||||
Write_key(comma, key); // "key":
|
||||
Write_str(Bry_.new_u8(val)); // "val"
|
||||
Write_new_line(); // \n
|
||||
}
|
||||
private void Write_key(boolean comma, byte[] key) { // "key":
|
||||
Write_indent();
|
||||
Write_str(key);
|
||||
bfr.Add_byte(Byte_ascii.Colon);
|
||||
}
|
||||
private void Write_indent() {if (ws_enabled && indent > 0) bfr.Add_byte_repeat(Byte_ascii.Space, indent);}
|
||||
private void Write_str(byte[] v) {
|
||||
if (v == null)
|
||||
bfr.Add(Object_.Bry__null);
|
||||
else
|
||||
bfr.Add_byte(Byte_ascii.Quote).Add(v).Add_byte(Byte_ascii.Quote);
|
||||
}
|
||||
private void Write_comma(boolean comma) {
|
||||
if (comma)
|
||||
bfr.Add_byte(Byte_ascii.Comma);
|
||||
else {
|
||||
if (ws_enabled)
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
if (ws_enabled)
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
private void Write_ary_bgn() {Indent_add(); Write_indent(); bfr.Add_byte(Byte_ascii.Brack_bgn); Write_new_line();}
|
||||
private void Write_ary_end() { Write_indent(); bfr.Add_byte(Byte_ascii.Brack_end); Write_new_line(); Indent_del();}
|
||||
private void Write_nde_bgn() {Indent_add(); Write_indent(); bfr.Add_byte(Byte_ascii.Curly_bgn); Write_new_line();}
|
||||
private void Write_nde_end() { Write_indent(); bfr.Add_byte(Byte_ascii.Curly_end); Write_new_line(); Indent_del();}
|
||||
private void Write_itm_hdr(boolean comma) {
|
||||
Write_indent();
|
||||
Write_comma(comma);
|
||||
}
|
||||
private void Indent_add() {indent += 2;}
|
||||
private void Indent_del() {indent -= 2;}
|
||||
private void Write_new_line() {if (ws_enabled) bfr.Add_byte_nl();}
|
||||
}
|
||||
|
||||
@@ -13,32 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import org.junit.*;
|
||||
public class Json_doc_tst {
|
||||
private final Json_qry_mgr_fxt fxt = new Json_qry_mgr_fxt();
|
||||
@Test public void Select() {
|
||||
Json_doc doc = fxt.Make_json
|
||||
( "{'0':"
|
||||
, " {'0_0':"
|
||||
, " {'0_0_0':'000'"
|
||||
, " },"
|
||||
, " '0_1':"
|
||||
, " {'0_1_0':'010'"
|
||||
, " }"
|
||||
, " }"
|
||||
, "}"
|
||||
);
|
||||
fxt.Test_get_val_as_str(doc, "0/0_0/0_0_0", "000");
|
||||
fxt.Test_get_val_as_str(doc, "0/0_1/0_1_0", "010");
|
||||
fxt.Test_get_val_as_str(doc, "x", null);
|
||||
}
|
||||
}
|
||||
class Json_qry_mgr_fxt {
|
||||
private final Json_parser json_parser = new Json_parser();
|
||||
public Json_doc Make_json(String... ary) {return json_parser.Parse_by_apos_ary(ary);}
|
||||
public void Test_get_val_as_str(Json_doc doc, String qry, String expd){
|
||||
byte[][] qry_bry = Bry_split_.Split(Bry_.new_u8(qry), Byte_ascii.Slash);
|
||||
Tfds.Eq(expd, doc.Get_val_as_str_or(qry_bry, null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,84 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_doc_wtr {
|
||||
private int indent = -2;
|
||||
private Bry_bfr bfr = Bry_bfr_.Reset(255);
|
||||
public Json_doc_wtr Indent() {return Indent(indent);}
|
||||
private Json_doc_wtr Indent(int v) {if (v > 0) bfr.Add_byte_repeat(Byte_ascii.Space, v); return this;}
|
||||
public Json_doc_wtr Indent_add() {indent += 2; return this;}
|
||||
public Json_doc_wtr Indent_del() {indent -= 2; return this;}
|
||||
public Json_doc_wtr Nde_bgn() {Indent_add(); Indent(); bfr.Add_byte(Byte_ascii.Curly_bgn).Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Nde_end() { Indent(); bfr.Add_byte(Byte_ascii.Curly_end).Add_byte_nl(); Indent_del(); return this;}
|
||||
public Json_doc_wtr Ary_bgn() {Indent_add(); Indent(); bfr.Add_byte(Byte_ascii.Brack_bgn).Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Ary_end() { Indent(); bfr.Add_byte(Byte_ascii.Brack_end).Add_byte_nl(); Indent_del(); return this;}
|
||||
public Json_doc_wtr New_line() {bfr.Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Str(byte[] v) {
|
||||
if (v == null)
|
||||
bfr.Add(Object_.Bry__null);
|
||||
else
|
||||
bfr.Add_byte(Byte_ascii.Quote).Add(v).Add_byte(Byte_ascii.Quote);
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Int(int v) {bfr.Add_int_variable(v); return this;}
|
||||
public Json_doc_wtr Double(double v) {bfr.Add_double(v); return this;}
|
||||
public Json_doc_wtr Comma() {Indent(); bfr.Add_byte(Byte_ascii.Comma).Add_byte_nl(); return this;}
|
||||
public Json_doc_wtr Kv_ary_empty(boolean comma, byte[] key) {
|
||||
Key_internal(comma, key);
|
||||
bfr.Add_byte(Byte_ascii.Brack_bgn).Add_byte(Byte_ascii.Brack_end);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Kv(boolean comma, byte[] key, byte[] val) {
|
||||
Key_internal(comma, key);
|
||||
Str(val);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Kv_double(boolean comma, byte[] key, double v) {
|
||||
Key_internal(comma, key);
|
||||
Double(v);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Kv(boolean comma, byte[] key, int v) {
|
||||
Key_internal(comma, key);
|
||||
Int(v);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Key(boolean comma, byte[] key) {
|
||||
Key_internal(comma, key);
|
||||
bfr.Add_byte_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Val(boolean comma, int v) {
|
||||
Val_internal(comma);
|
||||
Int(v);
|
||||
New_line();
|
||||
return this;
|
||||
}
|
||||
public Json_doc_wtr Val(boolean comma, byte[] v) {
|
||||
Val_internal(comma);
|
||||
Str(v);
|
||||
New_line();
|
||||
return this;
|
||||
}
|
||||
Json_doc_wtr Val_internal(boolean comma) {
|
||||
Indent();
|
||||
bfr.Add_byte(comma ? Byte_ascii.Comma : Byte_ascii.Space);
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
return this;
|
||||
}
|
||||
Json_doc_wtr Key_internal(boolean comma, byte[] key) {
|
||||
Indent();
|
||||
bfr.Add_byte(comma ? Byte_ascii.Comma : Byte_ascii.Space);
|
||||
bfr.Add_byte(Byte_ascii.Space);
|
||||
Str(key);
|
||||
bfr.Add_byte(Byte_ascii.Colon);
|
||||
return this;
|
||||
}
|
||||
public byte[] Bld() {return bfr.To_bry_and_clear();}
|
||||
public String Bld_as_str() {return bfr.To_str_and_clear();}
|
||||
}
|
||||
|
||||
@@ -13,16 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_factory {
|
||||
public Json_itm Null() {return Json_itm_null.Null;}
|
||||
public Json_itm Bool_n() {return Json_itm_bool.Bool_n;}
|
||||
public Json_itm Bool_y() {return Json_itm_bool.Bool_y;}
|
||||
public Json_itm_int Int(Json_doc doc, int bgn, int end) {return new Json_itm_int(doc, bgn, end);}
|
||||
public Json_itm_long Long(Json_doc doc, int bgn, int end) {return new Json_itm_long(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_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);}
|
||||
}
|
||||
|
||||
@@ -13,21 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public interface Json_grp extends Json_itm {
|
||||
void Src_end_(int v);
|
||||
int Len();
|
||||
Json_itm Get_at(int i);
|
||||
Json_nde Get_as_nde(int i);
|
||||
void Add(Json_itm itm);
|
||||
}
|
||||
class Json_grp_ {
|
||||
public static final Json_grp[] Ary_empty = new Json_grp[0];
|
||||
public static void Print_nl(Bry_bfr bfr) { // \n\n can be caused by nested groups (EX: "[[]]"); only print 1
|
||||
if (bfr.Bfr()[bfr.Len() - 1] != Byte_ascii.Nl)
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
public static void Print_indent(Bry_bfr bfr, int depth) {
|
||||
if (depth > 0) bfr.Add_byte_repeat(Byte_ascii.Space, depth * 2); // indent
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,21 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public interface Json_itm {
|
||||
byte Tid();
|
||||
int Src_bgn();
|
||||
int Src_end();
|
||||
Object Data();
|
||||
byte[] Data_bry();
|
||||
void Print_as_json(Bry_bfr bfr, int depth);
|
||||
boolean Data_eq(byte[] comp);
|
||||
}
|
||||
class Json_itm_null extends Json_itm_base {
|
||||
Json_itm_null() {this.Ctor(-1, -1);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__null;}
|
||||
@Override public Object Data() {return null;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add(Object_.Bry__null);}
|
||||
@Override public byte[] Data_bry() {return Object_.Bry__null;}
|
||||
public static final Json_itm_null Null = new Json_itm_null();
|
||||
}
|
||||
|
||||
@@ -13,14 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_ {
|
||||
public static final Json_itm[] Ary_empty = new Json_itm[0];
|
||||
public static final byte Tid__unknown = 0, Tid__null = 1, Tid__bool = 2, Tid__int = 3, Tid__long = 4, Tid__decimal = 5, Tid__str = 6, Tid__kv = 7, Tid__ary = 8, Tid__nde = 9;
|
||||
public static final byte[] Bry__true = Bool_.True_bry, Bry__false = Bool_.False_bry, Bry__null = Object_.Bry__null;
|
||||
public static byte[] To_bry(Bry_bfr bfr, Json_itm itm) {
|
||||
if (itm == null) return Bry_.Empty;
|
||||
itm.Print_as_json(bfr, 0);
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public abstract class Json_itm_base implements Json_itm {
|
||||
public abstract byte Tid();
|
||||
public void Ctor(int src_bgn, int src_end) {this.src_bgn = src_bgn; this.src_end = src_end;}
|
||||
public int Src_bgn() {return src_bgn;} private int src_bgn;
|
||||
public int Src_end() {return src_end;} protected int src_end;
|
||||
public abstract Object Data();
|
||||
public abstract byte[] Data_bry();
|
||||
public String Print_as_json() {Bry_bfr bfr = Bry_bfr_.New(); Print_as_json(bfr, 0); return bfr.To_str_and_clear();}
|
||||
public abstract void Print_as_json(Bry_bfr bfr, int depth);
|
||||
@gplx.Virtual public boolean Data_eq(byte[] comp) {return false;}
|
||||
}
|
||||
|
||||
@@ -13,14 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_bool extends Json_itm_base {
|
||||
private boolean data;
|
||||
public Json_itm_bool(boolean data) {this.data = data; this.Ctor(-1, -1);}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__bool;}
|
||||
public boolean Data_as_bool() {return data;}
|
||||
@Override public Object Data() {return data;}
|
||||
@Override public byte[] Data_bry() {return data ? Json_itm_.Bry__true : Json_itm_.Bry__false;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add(data ? Json_itm_.Bry__true: Json_itm_.Bry__false);}
|
||||
public static final Json_itm_bool Bool_n = new Json_itm_bool(false), Bool_y = new Json_itm_bool(true);
|
||||
}
|
||||
|
||||
@@ -13,20 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_decimal extends Json_itm_base {
|
||||
private final Json_doc doc; private Decimal_adp data; private byte[] data_bry;
|
||||
public Json_itm_decimal(Json_doc doc, int src_bgn, int src_end) {this.Ctor(src_bgn, src_end); this.doc = doc;}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__decimal;}
|
||||
@Override public Object Data() {return this.Data_as_decimal();}
|
||||
@Override public byte[] Data_bry() {
|
||||
if (data_bry == null) data_bry = Bry_.Mid(doc.Src(), this.Src_bgn(), this.Src_end());
|
||||
return data_bry;
|
||||
}
|
||||
public Decimal_adp Data_as_decimal() {
|
||||
if (data == null)
|
||||
data = Decimal_adp_.parse(String_.new_a7(this.Data_bry()));
|
||||
return data;
|
||||
}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add_mid(doc.Src(), this.Src_bgn(), this.Src_end());}
|
||||
}
|
||||
|
||||
@@ -13,21 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_int extends Json_itm_base {
|
||||
private final Json_doc doc;
|
||||
private byte[] data_bry; private int data; private boolean data_is_null = true;
|
||||
public Json_itm_int(Json_doc doc, int src_bgn, int src_end) {this.Ctor(src_bgn, src_end); this.doc = doc;}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__int;}
|
||||
public int Data_as_int() {
|
||||
if (data_is_null) {
|
||||
data = doc.Utl_num_parser().Parse(doc.Src(), Src_bgn(), Src_end()).Rv_as_int();
|
||||
data_is_null = false;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@Override public Object Data() {return Data_as_int();}
|
||||
@Override public byte[] Data_bry() {if (data_bry == null) data_bry = Bry_.Mid(doc.Src(), this.Src_bgn(), this.Src_end()); return data_bry;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add_mid(doc.Src(), this.Src_bgn(), this.Src_end());}
|
||||
public static Json_itm_int cast(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid__int ? null : (Json_itm_int)v;}
|
||||
}
|
||||
|
||||
@@ -13,21 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_long extends Json_itm_base {
|
||||
private final Json_doc doc;
|
||||
private byte[] data_bry; private long data; private boolean data_is_null = true;
|
||||
public Json_itm_long(Json_doc doc, int src_bgn, int src_end) {this.Ctor(src_bgn, src_end); this.doc = doc;}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__long;}
|
||||
public long Data_as_long() {
|
||||
if (data_is_null) {
|
||||
data = doc.Utl_num_parser().Parse(doc.Src(), Src_bgn(), Src_end()).Rv_as_long();
|
||||
data_is_null = false;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@Override public Object Data() {return Data_as_long();}
|
||||
@Override public byte[] Data_bry() {if (data_bry == null) data_bry = Bry_.Mid(doc.Src(), this.Src_bgn(), this.Src_end()); return data_bry;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add_mid(doc.Src(), this.Src_bgn(), this.Src_end());}
|
||||
public static Json_itm_long cast(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid__long ? null : (Json_itm_long)v;}
|
||||
}
|
||||
|
||||
@@ -13,65 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_str extends Json_itm_base {
|
||||
private final boolean exact; private final Json_doc doc;
|
||||
private String data_str; private byte[] data_bry = null;
|
||||
public Json_itm_str(Json_doc doc, int src_bgn, int src_end, boolean exact) {this.Ctor(src_bgn + 1, src_end - 1); this.doc = doc; this.exact = exact;}
|
||||
@Override public byte Tid() {return Json_itm_.Tid__str;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
bfr.Add_byte(Byte_ascii.Quote);
|
||||
gplx.langs.htmls.Gfh_utl.Escape_html_to_bfr(bfr, doc.Src(), this.Src_bgn(), this.Src_end(), true, true, true, true, false); // false to apos for backwards compatibility
|
||||
bfr.Add_byte(Byte_ascii.Quote);
|
||||
}
|
||||
@Override public Object Data() {return this.Data_as_str();}
|
||||
public String Data_as_str() {
|
||||
if (data_str == null) {
|
||||
if (data_bry == null)
|
||||
data_bry = Data_make_bry();
|
||||
data_str = String_.new_u8(data_bry);
|
||||
}
|
||||
return data_str;
|
||||
}
|
||||
@Override public byte[] Data_bry() {if (data_bry == null) data_bry = Data_make_bry(); return data_bry;}
|
||||
@Override public boolean Data_eq(byte[] comp) {
|
||||
if (exact) return Bry_.Eq(doc.Src(), this.Src_bgn(), this.Src_end(), comp);
|
||||
if (data_bry == null) data_bry = Data_make_bry();
|
||||
return Bry_.Match(data_bry, comp);
|
||||
}
|
||||
private byte[] Data_make_bry() {
|
||||
byte[] src = doc.Src(); int bgn = this.Src_bgn(), end = this.Src_end();
|
||||
if (exact) return Bry_.Mid(src, bgn, end);
|
||||
Bry_bfr bfr = doc.Bfr();
|
||||
byte[] utf8_bry = doc.Tmp_u8_bry();
|
||||
for (int i = bgn; i < end; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash:
|
||||
b = src[++i];
|
||||
switch (b) { // NOTE: must properly unescape chars; EX:wd.q:2; DATE:2014-04-23
|
||||
case Byte_ascii.Ltr_t: bfr.Add_byte(Byte_ascii.Tab); break;
|
||||
case Byte_ascii.Ltr_n: bfr.Add_byte(Byte_ascii.Nl); break;
|
||||
case Byte_ascii.Ltr_r: bfr.Add_byte(Byte_ascii.Cr); break;
|
||||
case Byte_ascii.Ltr_b: bfr.Add_byte(Byte_ascii.Backfeed); break;
|
||||
case Byte_ascii.Ltr_f: bfr.Add_byte(Byte_ascii.Formfeed); break;
|
||||
case Byte_ascii.Ltr_u:
|
||||
int utf8_val = gplx.core.encoders.Hex_utl_.Parse_or(src, i + 1, i + 5, -1);
|
||||
int len = gplx.core.intls.Utf16_.Encode_int(utf8_val, utf8_bry, 0);
|
||||
bfr.Add_mid(utf8_bry, 0, len);
|
||||
i += 4;
|
||||
break; // \uFFFF 4 hex-dec
|
||||
case Byte_ascii.Backslash:
|
||||
case Byte_ascii.Slash:
|
||||
default:
|
||||
bfr.Add_byte(b); break; // \? " \ / b f n r t
|
||||
}
|
||||
break;
|
||||
default:
|
||||
bfr.Add_byte(b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,17 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_itm_tmp implements Json_itm { // TEST:
|
||||
public Json_itm_tmp(byte tid, String data) {this.tid = tid; this.data = data;}
|
||||
public byte Tid() {return tid;} private byte tid;
|
||||
public byte[] Data_bry() {return Bry_.new_u8(Object_.Xto_str_strict_or_empty(data));}
|
||||
public int Src_bgn() {return -1;}
|
||||
public int Src_end() {return -1;}
|
||||
public Object Data() {return data;} private String data;
|
||||
public void Print_as_json(Bry_bfr bfr, int depth) {bfr.Add_str_u8(data);}
|
||||
public boolean Data_eq(byte[] comp) {return false;}
|
||||
public void Clear() {}
|
||||
public static Json_itm new_str_(String v) {return new Json_itm_tmp(Json_itm_.Tid__str, "\"" + v + "\"");}
|
||||
public static Json_itm new_int_(int v) {return new Json_itm_tmp(Json_itm_.Tid__int, Int_.To_str(v));}
|
||||
}
|
||||
|
||||
@@ -13,25 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
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;} private final Json_itm key;
|
||||
public Json_itm Val() {return val;} private final 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 Json_nde Val_as_nde() {return Json_nde.cast(val);}
|
||||
public Json_ary Val_as_ary() {return Json_ary.cast(val);}
|
||||
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;}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
key.Print_as_json(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Colon);
|
||||
val.Print_as_json(bfr, depth);
|
||||
}
|
||||
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;}
|
||||
}
|
||||
|
||||
@@ -13,47 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_kv_ary_srl {
|
||||
public static Keyval Kv_by_itm(Json_itm itm) {
|
||||
switch (itm.Tid()) {
|
||||
case Json_itm_.Tid__kv:
|
||||
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());
|
||||
}
|
||||
}
|
||||
private static Object Val_by_itm(Json_itm itm) {
|
||||
switch (itm.Tid()) {
|
||||
case Json_itm_.Tid__bool: return Bool_.To_str_lower(Bool_.Cast(itm.Data()));
|
||||
case Json_itm_.Tid__int:
|
||||
case Json_itm_.Tid__null:
|
||||
case Json_itm_.Tid__str:
|
||||
case Json_itm_.Tid__decimal: return itm.Data();
|
||||
case Json_itm_.Tid__ary: 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_ary itm) {
|
||||
int subs_len = itm.Len();
|
||||
Keyval[] rv = new Keyval[subs_len];
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm sub = itm.Get_at(i);
|
||||
Keyval kv = Keyval_.new_(Int_.To_str(i + Int_.Base1), Val_by_itm(sub));
|
||||
rv[i] = kv;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Keyval[] Val_by_itm_nde(Json_nde itm) {
|
||||
int subs_len = itm.Len();
|
||||
Keyval[] rv = new Keyval[subs_len];
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm sub = itm.Get_at(i);
|
||||
rv[i] = Kv_by_itm(sub);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,36 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import org.junit.*;
|
||||
public class Json_kv_ary_srl_tst {
|
||||
@Before public void init() {fxt.Clear();} private Json_kv_ary_srl_fxt fxt = new Json_kv_ary_srl_fxt();
|
||||
@Test public void Null() {fxt.Test_parse("{'k0':null}" , fxt.ary_(fxt.kv_str_("k0", null)));}
|
||||
@Test public void Bool_n() {fxt.Test_parse("{'k0':false}" , fxt.ary_(fxt.kv_bool_("k0", false)));}
|
||||
@Test public void Num() {fxt.Test_parse("{'k0':123}" , fxt.ary_(fxt.kv_int_("k0", 123)));}
|
||||
@Test public void Str() {fxt.Test_parse("{'k0':'v0'}" , fxt.ary_(fxt.kv_str_("k0", "v0")));}
|
||||
@Test public void Num_dec() {fxt.Test_parse("{'k0':1.23}" , fxt.ary_(fxt.kv_dec_("k0", Decimal_adp_.parse("1.23"))));}
|
||||
@Test public void Ary_int() {fxt.Test_parse("{'k0':[1,2,3]}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_(fxt.kv_int_("1", 1), fxt.kv_int_("2", 2), fxt.kv_int_("3", 3)))));}
|
||||
@Test public void Ary_empty() {fxt.Test_parse("{'k0':[]}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_())));}
|
||||
@Test public void Subs_int() {fxt.Test_parse("{'k0':{'k00':1,'k01':2}}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_(fxt.kv_int_("k00", 1), fxt.kv_int_("k01", 2)))));}
|
||||
@Test public void Subs_empty() {fxt.Test_parse("{'k0':{}}" , fxt.ary_(fxt.kv_obj_("k0", fxt.ary_())));}
|
||||
}
|
||||
class Json_kv_ary_srl_fxt {
|
||||
public void Clear() {
|
||||
if (parser == null) {
|
||||
parser = new Json_parser();
|
||||
}
|
||||
} private Json_parser parser;
|
||||
public void Test_parse(String raw_str, Keyval[] expd) {
|
||||
byte[] raw_bry = Json_parser_tst.Replace_apos(Bry_.new_u8(raw_str));
|
||||
Json_doc doc = parser.Parse(raw_bry);
|
||||
Keyval[] actl = Json_kv_ary_srl.Val_by_itm_nde(doc.Root_nde());
|
||||
Tfds.Eq_str_lines(Keyval_.Ary_to_str(expd), Keyval_.Ary_to_str(actl));
|
||||
}
|
||||
public Keyval[] ary_(Keyval... ary) {return ary;}
|
||||
public Keyval kv_obj_(String key, Object val) {return Keyval_.new_(key, val);}
|
||||
public Keyval kv_str_(String key, String val) {return Keyval_.new_(key, val);}
|
||||
public Keyval kv_int_(String key, int val) {return Keyval_.new_(key, val);}
|
||||
public Keyval kv_bool_(String key, boolean val) {return Keyval_.new_(key, Bool_.To_str_lower(val));}
|
||||
public Keyval kv_dec_(String key, Decimal_adp val) {return Keyval_.new_(key, val.To_str());}
|
||||
}
|
||||
|
||||
@@ -13,157 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
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;
|
||||
private Hash_adp_bry subs_hash;
|
||||
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_itm Get_at(int i) {return subs[i];}
|
||||
public Json_itm Get_as_itm_or_null(byte[] key) {if (subs_hash == null) subs_hash = subs_hash_init(); return (Json_itm)subs_hash.Get_by_bry(key);}
|
||||
public Json_ary Get_as_ary(int idx) {return Json_ary.cast(Get_at(idx));}
|
||||
public Json_nde Get_as_nde(String key) {return Json_nde.cast(Get_as_itm_or_null(Bry_.new_u8(key)));}
|
||||
public Json_nde Get_as_nde(int idx) {return Json_nde.cast(Get_at(idx));}
|
||||
public Json_ary Get_as_ary(String key) {return Get_as_ary(Bry_.new_u8(key));}
|
||||
public Json_ary Get_as_ary(byte[] key) {
|
||||
Json_itm rv = Get_as_itm_or_null(key); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return Json_ary.cast(rv);
|
||||
}
|
||||
public byte[] Get_as_bry(String key) {
|
||||
byte[] rv = Get_as_bry_or(Bry_.new_u8(key), null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public byte[] Get_as_bry_or(byte[] key, byte[] or) {
|
||||
Json_itm rv = Get_as_itm_or_null(key);
|
||||
return rv == null ? or : rv.Data_bry();
|
||||
}
|
||||
public String Get_as_str(String key) {
|
||||
String rv = Get_as_str_or(key, null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public String Get_as_str_or(String key, String or) {return Get_as_str_or(Bry_.new_u8(key), or);}
|
||||
public String Get_as_str_or(byte[] key, String or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : String_.new_u8(rv);
|
||||
}
|
||||
public int Get_as_int(String key) {
|
||||
int rv = Get_as_int_or(key, Int_.Min_value); if (rv == Int_.Min_value) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public int Get_as_int_or(String key, int or) {return Get_as_int_or(Bry_.new_u8(key), or);}
|
||||
public int Get_as_int_or(byte[] key, int or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.To_int(rv);
|
||||
}
|
||||
public long Get_as_long(String key) {
|
||||
long rv = Get_as_long_or(key, Long_.Min_value); if (rv == Long_.Min_value) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public long Get_as_long_or(String key, long or) {return Get_as_long_or(Bry_.new_u8(key), or);}
|
||||
public long Get_as_long_or(byte[] key, long or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.To_long_or(rv, or);
|
||||
}
|
||||
public boolean Get_as_bool_or(String key, boolean or) {return Get_as_bool_or(Bry_.new_u8(key), or);}
|
||||
public boolean Get_as_bool_or(byte[] key, boolean or) {
|
||||
byte[] rv = Get_as_bry_or(key, null);
|
||||
return rv == null ? or : Bry_.Eq(rv, Bool_.True_bry);
|
||||
}
|
||||
public DateAdp Get_as_date_by_utc(String key) {
|
||||
byte[] rv = Get_as_bry_or(Bry_.new_u8(key), null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return DateAdp_.parse_gplx(String_.new_u8(rv));
|
||||
}
|
||||
|
||||
// to convert
|
||||
public boolean Has(byte[] key) {return Get_bry(key, null) != null;}
|
||||
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_kv Get_kv(byte[] key) {return Json_kv.cast(Get_itm(key));}
|
||||
public Json_nde Get(String key) {return Get(Bry_.new_u8(key));}
|
||||
public Json_nde Get(byte[] 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;
|
||||
}
|
||||
public Json_itm Get_itm(byte[] key) {
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
Json_itm itm = subs[i];
|
||||
if (itm.Tid() == Json_itm_.Tid__kv) {
|
||||
Json_kv itm_as_kv = (Json_kv)itm;
|
||||
if (Bry_.Eq(key, itm_as_kv.Key().Data_bry()))
|
||||
return itm;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Json_ary Get_ary(String key) {return Get_ary(Bry_.new_u8(key));}
|
||||
public Json_ary Get_ary(byte[] key) {return Json_ary.cast(Get_kv(key).Val_as_ary());}
|
||||
public String Get_str(String key) {return String_.new_u8(Get_bry(Bry_.new_u8(key)));}
|
||||
public byte[] Get_bry(byte[] key) {
|
||||
byte[] rv = Get_bry(key, null); if (rv == null) throw Err_.new_("json", "key missing", "key", key);
|
||||
return rv;
|
||||
}
|
||||
public byte[] Get_bry_or_null(String key) {return Get_bry(Bry_.new_u8(key), null);}
|
||||
public byte[] Get_bry_or_null(byte[] key) {return Get_bry(key, null);}
|
||||
public byte[] Get_bry(byte[] key, byte[] or) {
|
||||
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_kv kv = (Json_kv)kv_obj;
|
||||
Json_itm val = kv.Val();
|
||||
return (val == null) ? or : val.Data_bry();
|
||||
}
|
||||
public Json_nde Add_many(Json_itm... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
Add(ary[i]);
|
||||
return this;
|
||||
}
|
||||
public void Add(Json_itm itm) {
|
||||
int new_len = subs_len + 1;
|
||||
if (new_len > subs_max) { // ary too small >>> expand
|
||||
subs_max = new_len * 2;
|
||||
Json_itm[] new_subs = new Json_itm[subs_max];
|
||||
Array_.Copy_to(subs, 0, new_subs, 0, subs_len);
|
||||
subs = new_subs;
|
||||
}
|
||||
subs[subs_len] = (Json_itm)itm;
|
||||
subs_len = new_len;
|
||||
subs_hash = null;
|
||||
}
|
||||
@Override public void Print_as_json(Bry_bfr bfr, int depth) {
|
||||
if (bfr.Len() != 0) bfr.Add_byte_nl();
|
||||
Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Curly_bgn).Add_byte(Byte_ascii.Space);
|
||||
for (int i = 0; i < subs_len; i++) {
|
||||
if (i != 0) {
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Comma).Add_byte(Byte_ascii.Space);
|
||||
}
|
||||
subs[i].Print_as_json(bfr, depth + 1);
|
||||
}
|
||||
Json_grp_.Print_nl(bfr); Json_grp_.Print_indent(bfr, depth);
|
||||
bfr.Add_byte(Byte_ascii.Curly_end).Add_byte_nl();
|
||||
}
|
||||
private Hash_adp_bry subs_hash_init() {
|
||||
Hash_adp_bry rv = Hash_adp_bry.cs();
|
||||
for (int i = 0; i < subs_len; ++i) {
|
||||
Json_itm itm = subs[i];
|
||||
if (itm.Tid() == Json_itm_.Tid__kv) {
|
||||
Json_kv itm_as_kv = (Json_kv)itm;
|
||||
rv.Add(itm_as_kv.Key().Data_bry(), itm_as_kv.Val());
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Json_nde cast(Json_itm v) {return v == null || v.Tid() != Json_itm_.Tid__nde ? null : (Json_nde)v;}
|
||||
}
|
||||
|
||||
@@ -13,172 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Json_parser {
|
||||
private byte[] src; private int src_len, pos; private final Gfo_number_parser num_parser = new Gfo_number_parser();
|
||||
public Json_factory Factory() {return factory;} private final Json_factory factory = new Json_factory();
|
||||
public Json_doc Parse_by_apos_ary(String... ary) {return Parse_by_apos(String_.Concat_lines_nl(ary));}
|
||||
public Json_doc Parse_by_apos(String s) {return Parse(Bry_.Replace(Bry_.new_u8(s), Byte_ascii.Apos, Byte_ascii.Quote));}
|
||||
public Json_doc Parse(String src) {return Parse(Bry_.new_u8(src));}
|
||||
public Json_doc Parse(byte[] src) {
|
||||
synchronized (factory) {
|
||||
this.src = src; if (src == null) return null;
|
||||
this.src_len = src.length; if (src_len == 0) return null;
|
||||
this.pos = 0;
|
||||
Skip_ws();
|
||||
boolean root_is_nde = true;
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Curly_bgn: root_is_nde = Bool_.Y; break;
|
||||
case Byte_ascii.Brack_bgn: root_is_nde = Bool_.N; break;
|
||||
default: return null;
|
||||
}
|
||||
Skip_ws();
|
||||
Json_doc doc = new Json_doc();
|
||||
Json_grp root = null;
|
||||
if (root_is_nde)
|
||||
root = Make_nde(doc);
|
||||
else
|
||||
root = Make_ary(doc);
|
||||
doc.Ctor(src, root);
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
private Json_nde Make_nde(Json_doc doc) {
|
||||
++pos; // brack_bgn
|
||||
Json_nde nde = new Json_nde(doc, pos);
|
||||
while (pos < src_len) {
|
||||
Skip_ws();
|
||||
if (src[pos] == Byte_ascii.Curly_end) {++pos; return nde;}
|
||||
else nde.Add(Make_kv(doc));
|
||||
Skip_ws();
|
||||
switch (src[pos++]) {
|
||||
case Byte_ascii.Comma: break;
|
||||
case Byte_ascii.Curly_end: return nde;
|
||||
default: throw Err_.new_unhandled(src[pos - 1]);
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos inside nde");
|
||||
}
|
||||
private Json_itm Make_kv(Json_doc doc) {
|
||||
Json_itm key = Make_string(doc);
|
||||
Skip_ws();
|
||||
Chk(Byte_ascii.Colon);
|
||||
Skip_ws();
|
||||
Json_itm val = Make_val(doc);
|
||||
return new Json_kv(key, val);
|
||||
}
|
||||
private Json_itm Make_val(Json_doc doc) {
|
||||
while (pos < src_len) {
|
||||
byte b = src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Ltr_n: return Make_literal(Bry_null_ull , 3, factory.Null());
|
||||
case Byte_ascii.Ltr_f: return Make_literal(Bry_bool_alse , 4, factory.Bool_n());
|
||||
case Byte_ascii.Ltr_t: return Make_literal(Bry_bool_rue , 3, factory.Bool_y());
|
||||
case Byte_ascii.Quote: return Make_string(doc);
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
case Byte_ascii.Dash: return Make_num(doc);
|
||||
case Byte_ascii.Brack_bgn: return Make_ary(doc);
|
||||
case Byte_ascii.Curly_bgn: return Make_nde(doc);
|
||||
}
|
||||
throw Err_.new_unhandled(Char_.To_str(b));
|
||||
}
|
||||
throw Err_.new_wo_type("eos reached in val");
|
||||
}
|
||||
private Json_itm Make_literal(byte[] remainder, int remainder_len, Json_itm singleton) {
|
||||
++pos; // 1st char
|
||||
int literal_end = pos + remainder_len;
|
||||
if (Bry_.Eq(src, pos, literal_end, remainder)) {
|
||||
pos = literal_end;
|
||||
return singleton;
|
||||
}
|
||||
throw Err_.new_("json.parser", "invalid literal", "excerpt", Bry_.Mid_by_len_safe(src, pos - 1, 16));
|
||||
}
|
||||
private Json_itm Make_string(Json_doc doc) {
|
||||
int bgn = pos++; // ++: quote_bgn
|
||||
boolean exact = true;
|
||||
while (pos < src_len) {
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Backslash:
|
||||
++pos; // backslash
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Ltr_u: pos += 4; break; // \uFFFF 4 hex-dec
|
||||
default: ++pos; break; // \? " \ / b f n r t
|
||||
}
|
||||
exact = false;
|
||||
break;
|
||||
case Byte_ascii.Quote:
|
||||
return factory.Str(doc, bgn, ++pos, exact); // ++: quote_end
|
||||
default:
|
||||
++pos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos reached inside quote");
|
||||
}
|
||||
private Json_itm Make_num(Json_doc doc) {
|
||||
int num_bgn = pos;
|
||||
boolean loop = true;
|
||||
while (loop) {
|
||||
if (pos == src_len) throw Err_.new_wo_type("eos reached inside num");
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
++pos;
|
||||
break;
|
||||
case Byte_ascii.Dot:
|
||||
case Byte_ascii.Dash: case Byte_ascii.Plus:
|
||||
case Byte_ascii.Ltr_E: case Byte_ascii.Ltr_e: // e e+ e- E E+ E-
|
||||
++pos;
|
||||
break;
|
||||
default:
|
||||
loop = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
num_parser.Parse(src, num_bgn, pos);
|
||||
if (num_parser.Has_frac())
|
||||
return factory.Decimal(doc, num_bgn, pos);
|
||||
else {
|
||||
if (num_parser.Is_int())
|
||||
return factory.Int(doc, num_bgn, pos);
|
||||
else
|
||||
return factory.Long(doc, num_bgn, pos);
|
||||
}
|
||||
}
|
||||
private 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;}
|
||||
else rv.Add(Make_val(doc));
|
||||
Skip_ws();
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Comma: ++pos; break;
|
||||
case Byte_ascii.Brack_end: ++pos; return rv;
|
||||
}
|
||||
}
|
||||
throw Err_.new_wo_type("eos inside ary");
|
||||
}
|
||||
private void Skip_ws() {
|
||||
while (pos < src_len) {
|
||||
switch (src[pos]) {
|
||||
case Byte_ascii.Space: case Byte_ascii.Nl: case Byte_ascii.Tab: case Byte_ascii.Cr: ++pos; break;
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Chk(byte expd) {
|
||||
if (src[pos] == expd)
|
||||
++pos;
|
||||
else
|
||||
throw err_(src, pos, "expected '{0}' but got '{1}'", Char_.To_str(expd), Char_.To_str(src[pos]));
|
||||
}
|
||||
private Err err_(byte[] src, int bgn, String fmt, Object... args) {return err_(src, bgn, src.length, fmt, args);}
|
||||
private Err err_(byte[] src, int bgn, int src_len, String fmt, Object... args) {
|
||||
String msg = String_.Format(fmt, args) + " " + Int_.To_str(bgn) + " " + String_.new_u8__by_len(src, bgn, 20);
|
||||
return Err_.new_wo_type(msg);
|
||||
}
|
||||
private static final byte[] Bry_bool_rue = Bry_.new_a7("rue"), Bry_bool_alse = Bry_.new_a7("alse"), Bry_null_ull = Bry_.new_a7("ull");
|
||||
}
|
||||
|
||||
@@ -13,60 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
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_w_size(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]), new Int_obj_val(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_.Instance.Warn_many("", "", msg + ": path=~{0}.~{1} excerpt=~{2}", context, kv.Key_as_bry(), Json_itm_.To_bry(tmp_bfr, cur_itm));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,56 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
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 json parser 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) {}
|
||||
}
|
||||
|
||||
@@ -13,86 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import org.junit.*;
|
||||
public class Json_parser_tst {
|
||||
private final Json_parser_fxt fxt = new Json_parser_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Null() {fxt.Test_parse_val0("{'k0':null}" , null);}
|
||||
@Test public void Bool_n() {fxt.Test_parse_val0("{'k0':false}" , false);}
|
||||
@Test public void Bool_y() {fxt.Test_parse_val0("{'k0':true}" , true);}
|
||||
@Test public void Num() {fxt.Test_parse_val0("{'k0':123}" , 123);}
|
||||
@Test public void Num_neg() {fxt.Test_parse_val0("{'k0':-123}" , -123);}
|
||||
@Test public void Str() {fxt.Test_parse_val0("{'k0':'v0'}" , "v0");}
|
||||
@Test public void Str_esc_quote() {fxt.Test_parse_val0("{'k0':'a\\\"b'}" , "a\"b");}
|
||||
@Test public void Str_esc_hex4() {fxt.Test_parse_val0("{'k0':'a\\u0021b'}" , "a!b");}
|
||||
@Test public void Num_dec() {fxt.Test_parse("{'k0':1.23}" , fxt.itm_nde_().Add_many(fxt.itm_kv_dec_("k0", "1.23")));}
|
||||
@Test public void Num_exp() {fxt.Test_parse("{'k0':1e+2}" , fxt.itm_nde_().Add_many(fxt.itm_kv_dec_("k0", "1e+2")));}
|
||||
@Test public void Num_mix() {fxt.Test_parse("{'k0':-1.23e-1}" , fxt.itm_nde_().Add_many(fxt.itm_kv_dec_("k0", "-1.23e-1")));}
|
||||
@Test public void Str_many() {fxt.Test_parse("{'k0':'v0','k1':'v1','k2':'v2'}", fxt.itm_nde_().Add_many(fxt.itm_kv_("k0", "v0"), fxt.itm_kv_("k1", "v1"), fxt.itm_kv_("k2", "v2")));}
|
||||
@Test public void Ary_empty() {fxt.Test_parse("{'k0':[]}", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_int_("k0")));}
|
||||
@Test public void Ary_int() {fxt.Test_parse("{'k0':[1,2,3]}", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_int_("k0", 1, 2, 3)));}
|
||||
@Test public void Ary_str() {fxt.Test_parse("{'k0':['a','b','c']}", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_str_("k0", "a", "b", "c")));}
|
||||
@Test public void Ary_ws() {fxt.Test_parse("{'k0': [ 1 , 2 , 3 ] }", fxt.itm_nde_().Add_many(fxt.itm_kv_ary_int_("k0", 1, 2, 3)));}
|
||||
@Test public void Subs_int() {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 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);}
|
||||
}
|
||||
class Json_parser_fxt {
|
||||
public void Clear() {
|
||||
if (parser == null) {
|
||||
parser = new Json_parser();
|
||||
factory = parser.Factory();
|
||||
}
|
||||
} Json_parser parser; Json_factory factory; Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
|
||||
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_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_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]));
|
||||
return factory.Kv(itm_str_(k), ary);
|
||||
}
|
||||
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_grp().Print_as_json(tmp_bfr, 0);
|
||||
String actl = tmp_bfr.To_str_and_clear();
|
||||
String expd = Xto_str(raw, doc, expd_ary, 0, expd_ary.length);
|
||||
Tfds.Eq_str_lines(expd, actl, actl);
|
||||
}
|
||||
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_kv kv = Json_kv.cast(doc.Root_nde().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);
|
||||
}
|
||||
String Xto_str(byte[] raw, Json_doc doc, Json_itm[] ary, int bgn, int end) {
|
||||
for (int i = bgn; i < end; i++) {
|
||||
Json_itm itm = ary[i];
|
||||
itm.Print_as_json(tmp_bfr, 0);
|
||||
}
|
||||
return tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,40 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
public class Json_printer {
|
||||
private final Json_parser parser = new Json_parser();
|
||||
private final Json_wtr wtr = new Json_wtr();
|
||||
public Json_printer Opt_quote_byte_(byte v) {wtr.Opt_quote_byte_(v); return this;}
|
||||
public Json_wtr Wtr() {return wtr;}
|
||||
public byte[] To_bry() {return wtr.To_bry_and_clear();}
|
||||
public String To_str() {return wtr.To_str_and_clear();}
|
||||
public Json_printer Print_by_bry(byte[] src) {
|
||||
Json_doc jdoc = parser.Parse(src);
|
||||
return (jdoc.Root_grp().Tid() == Json_itm_.Tid__ary)
|
||||
? Print_by_ary(jdoc.Root_ary())
|
||||
: Print_by_nde(jdoc.Root_nde())
|
||||
;
|
||||
}
|
||||
public Json_printer Print_by_ary(Json_ary ary) {
|
||||
wtr.Doc_ary_bgn();
|
||||
int len = ary.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_itm itm = ary.Get_at(i);
|
||||
wtr.Ary_itm_obj(wtr.Get_x(itm));
|
||||
}
|
||||
wtr.Doc_ary_end();
|
||||
return this;
|
||||
}
|
||||
public Json_printer Print_by_nde(Json_nde nde) {
|
||||
wtr.Doc_nde_bgn();
|
||||
int len = nde.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_kv kv = nde.Get_at_as_kv(i);
|
||||
Object kv_val = wtr.Get_x(kv.Val());
|
||||
wtr.Kv_obj(kv.Key_as_bry(), kv_val, Type_ids_.To_id_by_obj(kv_val));
|
||||
}
|
||||
wtr.Doc_nde_end();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,74 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import org.junit.*;
|
||||
public class Json_printer_tst {
|
||||
private final Json_printer_fxt fxt = new Json_printer_fxt();
|
||||
@Test public void Root_nde() {
|
||||
fxt.Test_print(Json_doc.Make_str_by_apos("{'k1':'v1','k2':'v2'}"), String_.Concat_lines_nl
|
||||
( "{ 'k1':'v1'"
|
||||
, ", 'k2':'v2'"
|
||||
, "}"
|
||||
));
|
||||
}
|
||||
@Test public void Root_ary() {
|
||||
fxt.Test_print(Json_doc.Make_str_by_apos("[1,2,3]"), String_.Concat_lines_nl
|
||||
( "[ 1"
|
||||
, ", 2"
|
||||
, ", 3"
|
||||
, "]"
|
||||
));
|
||||
}
|
||||
@Test public void Ary_w_ary() {
|
||||
fxt.Test_print(Json_doc.Make_str_by_apos("[[1,2],[3,4]]"), String_.Concat_lines_nl
|
||||
( "[ "
|
||||
, " [ 1"
|
||||
, " , 2"
|
||||
, " ]"
|
||||
, ", "
|
||||
, " [ 3"
|
||||
, " , 4"
|
||||
, " ]"
|
||||
, "]"
|
||||
));
|
||||
}
|
||||
@Test public void Ary_w_nde() {
|
||||
fxt.Test_print(Json_doc.Make_str_by_apos("[{'k1':'v1','k2':'v2'},{'k3':'v3','k4':'v4'}]"), String_.Concat_lines_nl
|
||||
( "[ "
|
||||
, " { 'k1':'v1'"
|
||||
, " , 'k2':'v2'"
|
||||
, " }"
|
||||
, ", "
|
||||
, " { 'k3':'v3'"
|
||||
, " , 'k4':'v4'"
|
||||
, " }"
|
||||
, "]"
|
||||
));
|
||||
}
|
||||
@Test public void Nde_w_ary() {
|
||||
fxt.Test_print(Json_doc.Make_str_by_apos("{'k1':[1,2],'k2':[3,4]}"), String_.Concat_lines_nl
|
||||
( "{ 'k1':"
|
||||
, " [ 1"
|
||||
, " , 2"
|
||||
, " ]"
|
||||
, ", 'k2':"
|
||||
, " [ 3"
|
||||
, " , 4"
|
||||
, " ]"
|
||||
, "}"
|
||||
));
|
||||
}
|
||||
// @Test public void Smoke() {
|
||||
// Json_printer printer = new Json_printer();
|
||||
// String url = "C:\\temp.json";
|
||||
// String s = printer.Pretty_print_as_str(Bry_.new_u8(Io_mgr.Instance.LoadFilStr(url)));
|
||||
// Io_mgr.Instance.SaveFilStr(url, s);
|
||||
// }
|
||||
}
|
||||
class Json_printer_fxt {
|
||||
private final Json_printer printer = new Json_printer().Opt_quote_byte_(Byte_ascii.Apos);
|
||||
public void Test_print(String raw, String expd) {
|
||||
Tfds.Eq_str_lines(expd, printer.Print_by_bry(Bry_.new_u8(raw)).To_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,300 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Json_wtr {
|
||||
private final Bry_bfr bfr = Bry_bfr_.New_w_size(255);
|
||||
private final Int_ary idx_stack = new Int_ary(4);
|
||||
private int idx = 0;
|
||||
public Bry_bfr Bfr() {return bfr;}
|
||||
public void Indent_(int v) {this.indent = v;} private int indent;
|
||||
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;
|
||||
public boolean Opt_ws() {return opt_ws;} public Json_wtr Opt_ws_(boolean v) {opt_ws = v; return this;} private boolean opt_ws = true;
|
||||
public boolean Opt_backslash_2x() {return opt_backslash_2x;} public Json_wtr Opt_backslash_2x_(boolean v) {opt_backslash_2x = v; return this;} private boolean opt_backslash_2x = false;
|
||||
public byte[] To_bry_and_clear() {return bfr.To_bry_and_clear();}
|
||||
public String To_str_and_clear() {return bfr.To_str_and_clear();}
|
||||
public Json_wtr () {this.Clear();}
|
||||
public Json_wtr Clear() {
|
||||
indent = -1;
|
||||
idx_stack.Clear();
|
||||
idx = 0;
|
||||
return this;
|
||||
}
|
||||
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_ary() {return Nde_bgn(Bry_.Empty);}
|
||||
public Json_wtr Nde_bgn(String key) {return Nde_bgn(Bry_.new_u8(key));}
|
||||
public Json_wtr Nde_bgn(byte[] key) {
|
||||
Write_indent_itm();
|
||||
if (key == Bry_.Empty) {
|
||||
if (opt_ws) bfr.Del_by_1(); // remove trailing space from Write_indent_itm
|
||||
++idx;
|
||||
}
|
||||
else
|
||||
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 Ary_bgn_ary() {return Ary_bgn(String_.Empty);}
|
||||
public Json_wtr Ary_bgn(String nde) {
|
||||
Write_indent_itm();
|
||||
if (nde == String_.Empty) {
|
||||
if (opt_ws) bfr.Del_by_1(); // remove trailing space from Write_indent_itm
|
||||
++idx;
|
||||
}
|
||||
else
|
||||
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_ids_.Id__str, itm);}
|
||||
public Json_wtr Ary_itm_bry(byte[] itm) {return Ary_itm_by_type_tid(Type_ids_.Id__bry, itm);}
|
||||
public Json_wtr Ary_itm_obj(Object itm) {return Ary_itm_by_type_tid(Type_ids_.To_id_by_obj(itm), itm);}
|
||||
public Json_wtr Ary_itm_by_type_tid(int itm_type_tid, Object itm) {
|
||||
Write_indent_itm();
|
||||
Write_val_obj(Bool_.Y, itm_type_tid, itm);
|
||||
Write_nl();
|
||||
++idx;
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Ary_end() {
|
||||
Write_grp_end(Bool_.N, Sym_ary_end);
|
||||
return Write_nl();
|
||||
}
|
||||
public Json_wtr Kv_bool_as_mw(String key, boolean val) {
|
||||
if (val) Kv_bry(key, Bry_.Empty); // if true, write 'key:""'; if false, write nothing
|
||||
return this;
|
||||
}
|
||||
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_.To_bry(val));}
|
||||
public Json_wtr Kv_long(String key, long val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Long_.To_str(val)));}
|
||||
public Json_wtr Kv_float(String key, float val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Float_.To_str(val)));}
|
||||
public Json_wtr Kv_double(String key, double val) {return Kv_raw(Bry_.new_u8(key), Bry_.new_a7(Double_.To_str(val)));}
|
||||
private Json_wtr Kv_raw(byte[] key, byte[] val) {
|
||||
Write_indent_itm();
|
||||
Write_key(key);
|
||||
bfr.Add(val);
|
||||
Write_nl();
|
||||
return this;
|
||||
}
|
||||
public Json_wtr Kv_str(String key, String val) {return Kv_bry(Bry_.new_u8(key), val == null ? null : 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) {
|
||||
Write_indent_itm();
|
||||
Write_key(key);
|
||||
Write_str(val);
|
||||
Write_nl();
|
||||
return this;
|
||||
}
|
||||
public Object Get_x(Json_itm itm) {
|
||||
switch (itm.Tid()) {
|
||||
case Json_itm_.Tid__ary:
|
||||
case Json_itm_.Tid__nde:
|
||||
return itm;
|
||||
default:
|
||||
case Json_itm_.Tid__kv: throw Err_.new_unsupported();
|
||||
case Json_itm_.Tid__bool:
|
||||
case Json_itm_.Tid__int:
|
||||
case Json_itm_.Tid__decimal:
|
||||
case Json_itm_.Tid__str:
|
||||
return itm.Data();
|
||||
}
|
||||
}
|
||||
public void Kv_itm_x(byte[] key, Json_itm itm) {
|
||||
Object val = Get_x(itm);
|
||||
int val_tid = Type_ids_.To_id_by_obj(val);
|
||||
Kv_obj(key, val, val_tid);
|
||||
}
|
||||
public Json_wtr Kv_obj(byte[] key, Object val, int val_tid) {
|
||||
Write_indent_itm();
|
||||
Write_key(key);
|
||||
Write_val_obj(Bool_.N, val_tid, val);
|
||||
Write_nl();
|
||||
return this;
|
||||
}
|
||||
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;
|
||||
if (write_indent) Write_indent();
|
||||
bfr.Add(grp_sym);
|
||||
return this;
|
||||
}
|
||||
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 Json_wtr Write_key(byte[] bry) {
|
||||
Write_str(bry); // "key"
|
||||
bfr.Add_byte_colon(); // ":"
|
||||
++idx;
|
||||
return this;
|
||||
}
|
||||
private void Write_val_obj(boolean called_by_ary, int type_tid, Object obj) {
|
||||
switch (type_tid) {
|
||||
case Type_ids_.Id__null: bfr.Add(Object_.Bry__null); break;
|
||||
case Type_ids_.Id__bool: bfr.Add_bool(Bool_.Cast(obj)); break;
|
||||
case Type_ids_.Id__byte: bfr.Add_byte(Byte_.Cast(obj)); break;
|
||||
case Type_ids_.Id__int: bfr.Add_int_variable(Int_.Cast(obj)); break;
|
||||
case Type_ids_.Id__long: bfr.Add_long_variable(Long_.cast(obj)); break;
|
||||
case Type_ids_.Id__float: bfr.Add_float(Float_.cast(obj)); break;
|
||||
case Type_ids_.Id__double: bfr.Add_double(Double_.cast(obj)); break;
|
||||
case Type_ids_.Id__str: Write_str(Bry_.new_u8((String)obj)); break;
|
||||
case Type_ids_.Id__bry: Write_str((byte[])obj); break;
|
||||
case Type_ids_.Id__char:
|
||||
case Type_ids_.Id__date:
|
||||
case Type_ids_.Id__decimal: Write_str(Bry_.new_u8(Object_.Xto_str_strict_or_empty(obj))); break;
|
||||
case Type_ids_.Id__obj:
|
||||
int grp_type = Grp_type__get(obj);
|
||||
if (grp_type < Grp_type__json_ary)
|
||||
Write_val_obj__nde(called_by_ary, grp_type, obj);
|
||||
else
|
||||
Write_val_itm__ary(called_by_ary, grp_type, obj);
|
||||
break;
|
||||
default: throw Err_.new_unhandled(type_tid);
|
||||
}
|
||||
}
|
||||
private void Handle_nde_as_ary_itm_0() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Write_val_obj__nde(boolean called_by_ary, int grp_type, Object obj) {
|
||||
if (grp_type == Grp_type__json_nde) {
|
||||
if (idx == 0) { // if nde, and first item, then put on new line
|
||||
if (!called_by_ary) {
|
||||
bfr.Del_by_1();
|
||||
if (opt_ws) {
|
||||
bfr.Add_byte_nl();
|
||||
++indent;
|
||||
Write_indent();
|
||||
--indent;
|
||||
}
|
||||
}
|
||||
}
|
||||
bfr.Add_byte_nl();
|
||||
Write_grp_bgn(Sym_nde_bgn, Bool_.Y);
|
||||
Json_nde sub_nde = (Json_nde)obj;
|
||||
int sub_nde_len = sub_nde.Len();
|
||||
for (int i = 0; i < sub_nde_len; ++i) {
|
||||
Json_kv sub_kv = sub_nde.Get_at_as_kv(i);
|
||||
Kv_itm_x(sub_kv.Key_as_bry(), sub_kv.Val());
|
||||
}
|
||||
}
|
||||
else {
|
||||
Handle_nde_as_ary_itm_0();
|
||||
Write_grp_bgn(Sym_nde_bgn, Bool_.N);
|
||||
Keyval[] kvy = (Keyval[])obj;
|
||||
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_ids_.To_id_by_obj(kv_val));
|
||||
}
|
||||
}
|
||||
Write_grp_end(Bool_.Y, Sym_nde_end);
|
||||
}
|
||||
private void Write_val_itm__ary(boolean called_by_ary, int grp_type, Object obj) {
|
||||
Ary_bgn_keyless();
|
||||
if (grp_type == Grp_type__json_ary) {
|
||||
Json_ary sub_ary = (Json_ary)(obj);
|
||||
int len = sub_ary.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Json_itm sub_itm = sub_ary.Get_at(i);
|
||||
Ary_itm_obj(Get_x(sub_itm));
|
||||
}
|
||||
}
|
||||
else {
|
||||
Object ary = Array_.cast(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) {
|
||||
if (bry == null) {bfr.Add(Object_.Bry__null); return;}
|
||||
int len = bry.length;
|
||||
int backslash_count = opt_backslash_2x ? 3 : 1; // NOTE: 3 handles backslashes usurped by javascript; EX: '{"val":"\\\\"}' --javascript--> '{"val":"\\"}' --json--> '{"val":"\"}'
|
||||
bfr.Add_byte(opt_quote_byte);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
byte b = bry[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash: bfr.Add_byte_repeat(Byte_ascii.Backslash, backslash_count).Add_byte(b); break; // "\" -> "\\"; needed else js will usurp \ as escape; EX: "\&" -> "&"; DATE:2014-06-24
|
||||
case Byte_ascii.Quote: bfr.Add_byte_repeat(Byte_ascii.Backslash, backslash_count).Add_byte(b); break;
|
||||
case Byte_ascii.Apos: // // "'" -> "'''"; needed else xocfg fails; DATE:2016-12-07
|
||||
if (opt_backslash_2x)
|
||||
bfr.Add_byte_repeat(Byte_ascii.Backslash, 1).Add_byte(b);
|
||||
else
|
||||
bfr.Add_byte(b);
|
||||
break;
|
||||
case Byte_ascii.Nl: bfr.Add_byte_repeat(Byte_ascii.Backslash, 2).Add_byte(Byte_ascii.Ltr_n); break; // "\n" -> "\\n"
|
||||
case Byte_ascii.Cr: bfr.Add_byte_repeat(Byte_ascii.Backslash, 2).Add_byte(Byte_ascii.Ltr_r); break; // "\r" -> "\\r"; DATE:2017-03-02
|
||||
case Byte_ascii.Tab: bfr.Add_byte_repeat(Byte_ascii.Backslash, 2).Add_byte(Byte_ascii.Ltr_t); break; // "\t" -> "\\t"; DATE:2017-03-02
|
||||
default: bfr.Add_byte(b); break;
|
||||
}
|
||||
}
|
||||
bfr.Add_byte(opt_quote_byte);
|
||||
}
|
||||
private void Write_indent_itm() {
|
||||
if (idx == 0) {
|
||||
if (opt_ws)
|
||||
bfr.Add_byte_space();
|
||||
}
|
||||
else {
|
||||
Write_indent();
|
||||
bfr.Add(Sym_itm_spr);
|
||||
if (opt_ws) bfr.Add_byte_space();
|
||||
}
|
||||
}
|
||||
private void Write_indent() {
|
||||
if (opt_ws && indent > 0)
|
||||
bfr.Add_byte_repeat(Byte_ascii.Space, indent * 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(",")
|
||||
;
|
||||
private static final int Grp_type__json_nde = 1, Grp_type__kv_ary = 2, Grp_type__json_ary = 3, Grp_type__obj_ary = 4;
|
||||
private static int Grp_type__get(Object obj) {
|
||||
Class<?> type = obj.getClass();
|
||||
if (Type_.Eq(type, Keyval[].class)) return Grp_type__kv_ary;
|
||||
else if (Type_.Is_array(type)) return Grp_type__obj_ary;
|
||||
else if (Type_.Eq(type, Json_nde.class)) return Grp_type__json_nde;
|
||||
else if (Type_.Eq(type, Json_ary.class)) return Grp_type__json_ary;
|
||||
else throw Err_.new_unhandled(type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,120 +13,3 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
|
||||
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_nde_bgn().Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Kv() {
|
||||
fxt.Wtr()
|
||||
.Doc_nde_bgn()
|
||||
.Kv_str("k0", "v0")
|
||||
.Kv_str("k1", "v1")
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 'k0':'v0'"
|
||||
, ", 'k1':'v1'"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Escaped() {
|
||||
fxt.Wtr()
|
||||
.Doc_nde_bgn()
|
||||
.Kv_str("backslash", "\\")
|
||||
.Kv_str("quote", "\"")
|
||||
.Kv_str("apos", "'")
|
||||
.Kv_str("nl", "\n")
|
||||
.Kv_str("cr", "\r")
|
||||
.Kv_str("tab", "\t")
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 'backslash':'\\\\'"
|
||||
, ", 'quote':'\\\"'"
|
||||
, ", 'apos':'\''"
|
||||
, ", 'nl':'\\\\n'"
|
||||
, ", 'cr':'\\\\r'"
|
||||
, ", 'tab':'\\\\t'"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Nde() {
|
||||
fxt.Wtr()
|
||||
.Doc_nde_bgn()
|
||||
.Nde_bgn("s0")
|
||||
.Nde_bgn("s00")
|
||||
.Nde_end()
|
||||
.Nde_end()
|
||||
.Nde_bgn("s1")
|
||||
.Nde_bgn("s10")
|
||||
.Nde_end()
|
||||
.Nde_end()
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 's0':"
|
||||
, " { 's00':"
|
||||
, " {"
|
||||
, " }"
|
||||
, " }"
|
||||
, ", 's1':"
|
||||
, " { 's10':"
|
||||
, " {"
|
||||
, " }"
|
||||
, " }"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@Test public void Ary() {
|
||||
fxt.Wtr()
|
||||
.Doc_nde_bgn()
|
||||
.Ary_bgn("a0")
|
||||
.Ary_itm_str("v0")
|
||||
.Ary_itm_str("v1")
|
||||
.Ary_end()
|
||||
.Doc_nde_end();
|
||||
fxt.Test
|
||||
( "{ 'a0':"
|
||||
, " [ 'v0'"
|
||||
, " , 'v1'"
|
||||
, " ]"
|
||||
, "}"
|
||||
);
|
||||
}
|
||||
@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);
|
||||
public void Clear() {wtr.Clear();}
|
||||
public Json_wtr Wtr() {return wtr;}
|
||||
public void Test(String... expd) {
|
||||
Tfds.Eq_ary_str
|
||||
( String_.Ary_add(expd, String_.Ary("")) // json_wtr always ends with "}\n"; rather than add "\n" to each test, just add it here
|
||||
, String_.SplitLines_nl(String_.new_u8(wtr.To_bry_and_clear()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user