code wiki / (root) / nx_lib_parse.nx

nx_lib_parse.nx source

↩ module page · 84 lines · 3650 B

1// nx_lib_parse.nx -- sovereign JSON -> work-record parser (retires the Python 2// json + pydantic parse of API responses = the "validation" census axis). 3// WIRES the ecosystem's RFC-8259 tokenizer nx_json: extracts top-level string 4// fields (title/doi/published/license/abstract/authors) from a work JSON object 5// into a TAB-delimited nx_lib_store record. Pure, local, no network. The live 6// fetch (nx_https_get) + this parser complete the http-client axis. 7// license_tier: ORIGINAL | genealogy_id: nishi_library_sovereign_parse_2026_07_01 8import "nx_json.nx" 9 10// does the STRING token at [off,off+len) (quotes included) equal key[0..keylen)? 11func lpr_key_eq(json: *u8, off: i64, len: i64, key: *u8, keylen: i64) -> i64 { 12 let clen: i64 = len - 2 // strip the two quotes 13 if clen != keylen { return 0 } 14 var i: i64 = 0 15 while i < clen { if json[off + 1 + i] != key[i] { return 0 } i = i + 1 } 16 return 1 17} 18 19// find the top-level (depth-1) "key":"value"; copy value content into out. 20// returns value length, or -1 if absent / not a string. 21func nx_json_get_str(json: *u8, jlen: i64, key: *u8, keylen: i64, out: *u8) -> i64 { 22 let t: *NxJsonTok = nx_json_new(json, jlen) 23 var depth: i64 = 0 24 var result: i64 = 0 - 1 25 var go: i64 = 1 26 while go == 1 { 27 let k: i64 = nx_json_next(t) 28 if k == NX_JSON_EOF { go = 0 } 29 else { if k == NX_JSON_ERROR { go = 0 } 30 else { if k == NX_JSON_LBRACE { depth = depth + 1 } 31 else { if k == NX_JSON_LBRACKET { depth = depth + 1 } 32 else { if k == NX_JSON_RBRACE { depth = depth - 1 } 33 else { if k == NX_JSON_RBRACKET { depth = depth - 1 } 34 else { if k == NX_JSON_STRING { 35 if depth == 1 { 36 if lpr_key_eq(json, t.tok_off, t.tok_len, key, keylen) == 1 { 37 let k2: i64 = nx_json_next(t) 38 if k2 == NX_JSON_COLON { 39 let k3: i64 = nx_json_next(t) 40 if k3 == NX_JSON_STRING { 41 let voff: i64 = t.tok_off + 1 42 let vlen: i64 = t.tok_len - 2 43 var i: i64 = 0 44 while i < vlen { out[i] = json[voff + i]; i = i + 1 } 45 result = vlen 46 go = 0 47 } 48 } 49 } 50 } 51 } } } } } } } 52 } 53 return result 54} 55 56// append the JSON string field `key` (if present) to out at off; return new off. 57func lpr_field(json: *u8, jlen: i64, key: *u8, keylen: i64, out: *u8, off: i64) -> i64 { 58 let tmp: *u8 = sys_mmap(131072) 59 let n: i64 = nx_json_get_str(json, jlen, key, keylen, tmp) 60 if n < 0 { return off } 61 var i: i64 = 0 62 var o: i64 = off 63 while i < n { out[o] = tmp[i]; o = o + 1; i = i + 1 } 64 return o 65} 66 67// parse a work JSON object into a TAB-delimited nx_lib_store record: 68// title<TAB>doi<TAB>published<TAB>license<TAB>abstract<TAB>authors 69// returns record length. 70func nx_lib_parse_work(json: *u8, jlen: i64, out: *u8) -> i64 { 71 var o: i64 = 0 72 o = lpr_field(json, jlen, "title" as *u8, 5, out, o) 73 out[o] = 9 as u8; o = o + 1 74 o = lpr_field(json, jlen, "doi" as *u8, 3, out, o) 75 out[o] = 9 as u8; o = o + 1 76 o = lpr_field(json, jlen, "published" as *u8, 9, out, o) 77 out[o] = 9 as u8; o = o + 1 78 o = lpr_field(json, jlen, "license" as *u8, 7, out, o) 79 out[o] = 9 as u8; o = o + 1 80 o = lpr_field(json, jlen, "abstract" as *u8, 8, out, o) 81 out[o] = 9 as u8; o = o + 1 82 o = lpr_field(json, jlen, "authors" as *u8, 7, out, o) 83 return o 84}