| 1 |
|
| /* |
| 2 |
|
| * JSON.xs - File::Raw::JSON XS surface |
| 3 |
|
| * |
| 4 |
|
| * Two FilePlugins (json, jsonl) registered at BOOT against File::Raw's |
| 5 |
|
| * plugin API (see <file_plugin.h>). All four phases wired: |
| 6 |
|
| * READ - bytes -> Perl structure |
| 7 |
|
| * WRITE - Perl structure -> bytes |
| 8 |
|
| * STREAM - chunked feed for jsonl (json plugin rejects with helpful msg) |
| 9 |
|
| * RECORD - not implemented (record-derived ops route through READ) |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #define PERL_NO_GET_CONTEXT |
| 13 |
|
| #include "EXTERN.h" |
| 14 |
|
| #include "perl.h" |
| 15 |
|
| #include "XSUB.h" |
| 16 |
|
| |
| 17 |
|
| #include "file_plugin.h" |
| 18 |
|
| #include "file_raw_json.h" |
| 19 |
|
| #include "frj_abi.h" /* public C ABI table reached via _abi_ptr */ |
| 20 |
|
| |
| 21 |
|
| #include <string.h> |
| 22 |
|
| #include <stdlib.h> |
| 23 |
|
| |
| 24 |
|
| /* ============================================================ |
| 25 |
|
| * Boolean singletons + default stash (declared extern in frj.h so the |
| 26 |
|
| * codec can hand them out from the parse hot path). Allocated in |
| 27 |
|
| * BOOT and marked read-only - users mutating the inner SV would |
| 28 |
|
| * corrupt every other reference, so we make the OS catch the mistake |
| 29 |
|
| * at write time. */ |
| 30 |
|
| |
| 31 |
|
| SV *g_frj_true_sv = NULL; |
| 32 |
|
| SV *g_frj_false_sv = NULL; |
| 33 |
|
| HV *g_frj_default_stash = NULL; |
| 34 |
|
| |
| 35 |
|
| static const char *g_boolean_class_name = "File::Raw::JSON::Boolean"; |
| 36 |
|
| |
| 37 |
|
| static void |
| 38 |
30 |
| init_boolean_singletons(pTHX) |
| 39 |
|
| { |
| 40 |
|
| SV *t_inner, *f_inner; |
| 41 |
30 |
▶ | if (g_frj_true_sv) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g_frj_true_sv) return; |
0 |
30 |
30 |
|
| 42 |
|
| |
| 43 |
30 |
| g_frj_default_stash = gv_stashpv(g_boolean_class_name, GV_ADD); |
| 44 |
|
| |
| 45 |
|
| /* Singletons are NOT marked SvREADONLY: Perl's overload machinery |
| 46 |
|
| * sets magic on the blessed RV the first time `use overload` is |
| 47 |
|
| * processed against the class, and that magic write fails on a |
| 48 |
|
| * read-only RV with "Modification of a read-only value". The |
| 49 |
|
| * inner scalar could be read-only without breaking overload setup |
| 50 |
|
| * but in practice overload-blessing also touches the inner state, |
| 51 |
|
| * so we just leave both writable. The convention "don't mutate |
| 52 |
|
| * the singleton" is documented; users who do are on their own. */ |
| 53 |
30 |
| t_inner = newSViv(1); |
| 54 |
30 |
| f_inner = newSViv(0); |
| 55 |
30 |
| g_frj_true_sv = sv_bless(newRV_noinc(t_inner), g_frj_default_stash); |
| 56 |
30 |
| g_frj_false_sv = sv_bless(newRV_noinc(f_inner), g_frj_default_stash); |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| static HV * |
| 60 |
200 |
| get_boolean_stash(pTHX) |
| 61 |
|
| { |
| 62 |
200 |
▶ | if (!g_frj_default_stash) init_boolean_singletons(aTHX); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g_frj_default_stash) init_boolean_singletons(aTHX); |
0 |
200 |
200 |
|
| 63 |
200 |
| return g_frj_default_stash; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| static HV * |
| 67 |
200 |
| resolve_boolean_stash(pTHX_ const char *class_name) |
| 68 |
|
| { |
| 69 |
200 |
▶ | if (!class_name) return get_boolean_stash(aTHX); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!class_name) return get_boolean_stash(aTHX); |
199 |
1 |
200 |
|
| 70 |
1 |
| return gv_stashpv(class_name, GV_ADD); |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| /* Alias an existing File::Raw::JSON CV into the caller's package |
| 74 |
|
| * under the same short name. Mirrors File::Raw's selective-import |
| 75 |
|
| * recipe (file.c install_import_entry): create a fresh CV in the |
| 76 |
|
| * destination glob whose XSUB pointer matches the source. */ |
| 77 |
|
| static void |
| 78 |
23 |
| fjson_install_alias(pTHX_ const char *pkg, const char *name) |
| 79 |
|
| { |
| 80 |
|
| char src_full[256]; |
| 81 |
|
| char dst_full[256]; |
| 82 |
|
| CV *src; |
| 83 |
23 |
| snprintf(src_full, sizeof(src_full), "File::Raw::JSON::%s", name); |
| 84 |
23 |
| snprintf(dst_full, sizeof(dst_full), "%s::%s", pkg, name); |
| 85 |
23 |
| src = get_cv(src_full, 0); |
| 86 |
23 |
▶ | if (!src) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) { |
0 |
23 |
23 |
|
| 87 |
0 |
| warn("File::Raw::JSON: source CV '%s' not found", src_full); |
| 88 |
0 |
| return; |
| 89 |
|
| } |
| 90 |
|
| /* newXS overwrites the destination CV if one already exists. */ |
| 91 |
23 |
| newXS(dst_full, CvXSUB(src), __FILE__); |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| /* Build a transient options HV from a trailing key/value pair list. |
| 95 |
|
| * Returns a mortal HV* or NULL if there are no options. Croaks on |
| 96 |
|
| * odd-count input. Used by file_json_decode / file_json_encode - |
| 97 |
|
| * lets us reuse decode_opts() unchanged. ax/items mirror dXSARGS; |
| 98 |
|
| * first_idx is the position of the first key on the stack. */ |
| 99 |
|
| static HV * |
| 100 |
208 |
| build_opts_hv(pTHX_ I32 ax, I32 items, I32 first_idx, const char *fn) |
| 101 |
|
| { |
| 102 |
|
| HV *opts; |
| 103 |
|
| I32 i; |
| 104 |
208 |
▶ | if (first_idx >= items) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (first_idx >= items) return NULL; |
136 |
72 |
208 |
|
| 105 |
72 |
▶ | if ((items - first_idx) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - first_idx) % 2 != 0) |
2 |
70 |
72 |
|
| 106 |
2 |
| croak("%s: odd number of options", fn); |
| 107 |
70 |
| opts = newHV(); |
| 108 |
70 |
| sv_2mortal((SV *)opts); |
| 109 |
149 |
▶ | for (i = first_idx; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = first_idx; i + 1 < items; i += 2) { |
79 |
70 |
149 |
|
| 110 |
|
| STRLEN klen; |
| 111 |
79 |
| const char *key = SvPV(ST(i), klen); |
| 112 |
79 |
| SV *vcopy = newSVsv(ST(i + 1)); |
| 113 |
79 |
▶ | if (!hv_store(opts, key, klen, vcopy, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_store(opts, key, klen, vcopy, 0)) |
0 |
79 |
79 |
|
| 114 |
0 |
| SvREFCNT_dec(vcopy); |
| 115 |
|
| } |
| 116 |
70 |
| return opts; |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| /* ============================================================ |
| 120 |
|
| * Option decoding |
| 121 |
|
| * |
| 122 |
|
| * Maps a Perl HV onto json_options_t. Unknown keys croak (catches |
| 123 |
|
| * typos like 'pretty_print'). The boolean_class option is returned |
| 124 |
|
| * separately because it lives outside the struct. */ |
| 125 |
|
| |
| 126 |
|
| static const char *VALID_OPT_KEYS[] = { |
| 127 |
|
| "mode", "pretty", "indent", "sort_keys", "canonical", |
| 128 |
|
| "utf8", "relaxed", "allow_nonref", "allow_nan_inf", |
| 129 |
|
| "ordered", |
| 130 |
|
| "max_depth", "eol", "boolean_class", |
| 131 |
|
| "plugin", /* present in HV file_plugin_dispatch_* builds */ |
| 132 |
|
| NULL |
| 133 |
|
| }; |
| 134 |
|
| |
| 135 |
|
| static int |
| 136 |
278 |
| known_opt(const char *key, STRLEN klen) |
| 137 |
|
| { |
| 138 |
|
| const char *const *p; |
| 139 |
2755 |
▶ | for (p = VALID_OPT_KEYS; *p; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = VALID_OPT_KEYS; *p; p++) { |
2753 |
2 |
2755 |
|
| 140 |
2753 |
▶ | if (strlen(*p) == klen && memcmp(*p, key, klen) == 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (strlen(*p) == klen && memcmp(*p, key, klen) == 0) return 1; |
610 |
2143 |
2753 |
| if (strlen(*p) == klen && memcmp(*p, key, klen) == 0) return 1; |
276 |
334 |
610 |
|
| 141 |
|
| } |
| 142 |
2 |
| return 0; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| static const char * |
| 146 |
352 |
| decode_opts(pTHX_ HV *opts_hv, json_options_t *o) |
| 147 |
|
| { |
| 148 |
352 |
| const char *boolean_class = NULL; |
| 149 |
|
| HE *he; |
| 150 |
|
| |
| 151 |
352 |
▶ | if (!opts_hv) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opts_hv) return NULL; |
136 |
216 |
352 |
|
| 152 |
|
| |
| 153 |
216 |
| hv_iterinit(opts_hv); |
| 154 |
489 |
▶ | while ((he = hv_iternext(opts_hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(opts_hv))) { |
278 |
211 |
489 |
|
| 155 |
|
| I32 klen_i; |
| 156 |
278 |
| const char *key = hv_iterkey(he, &klen_i); |
| 157 |
278 |
| STRLEN klen = (STRLEN)klen_i; |
| 158 |
278 |
| SV *val = hv_iterval(opts_hv, he); |
| 159 |
|
| |
| 160 |
278 |
▶ | if (!known_opt(key, klen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!known_opt(key, klen)) { |
2 |
276 |
278 |
|
| 161 |
2 |
| croak("File::Raw::JSON: unknown option '%.*s'", |
| 162 |
|
| (int)klen, key); |
| 163 |
|
| } |
| 164 |
276 |
▶ | if (!SvOK(val)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(val)) continue; |
0 |
276 |
276 |
|
| 165 |
|
| |
| 166 |
288 |
▶ | if (klen == 4 && memcmp(key, "mode", 4) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == 4 && memcmp(key, "mode", 4) == 0) { |
13 |
263 |
276 |
| if (klen == 4 && memcmp(key, "mode", 4) == 0) { |
13 |
0 |
13 |
|
| 167 |
|
| STRLEN mlen; |
| 168 |
13 |
| const char *mpv = SvPV(val, mlen); |
| 169 |
13 |
▶ | if (mlen == 8 && memcmp(mpv, "document", 8) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mlen == 8 && memcmp(mpv, "document", 8) == 0) |
1 |
12 |
13 |
| if (mlen == 8 && memcmp(mpv, "document", 8) == 0) |
0 |
1 |
1 |
|
| 170 |
0 |
| o->mode = JSON_MODE_DOCUMENT; |
| 171 |
13 |
▶ | else if (mlen == 5 && memcmp(mpv, "lines", 5) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (mlen == 5 && memcmp(mpv, "lines", 5) == 0) |
12 |
1 |
13 |
| else if (mlen == 5 && memcmp(mpv, "lines", 5) == 0) |
12 |
0 |
12 |
|
| 172 |
12 |
| o->mode = JSON_MODE_LINES; |
| 173 |
|
| else |
| 174 |
1 |
| croak("File::Raw::JSON: mode must be 'document' or 'lines' " |
| 175 |
|
| "(got '%.*s')", (int)mlen, mpv); |
| 176 |
|
| } |
| 177 |
263 |
▶ | else if (klen == 6 && memcmp(key, "pretty", 6) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 6 && memcmp(key, "pretty", 6) == 0) |
163 |
100 |
263 |
| else if (klen == 6 && memcmp(key, "pretty", 6) == 0) |
14 |
149 |
163 |
|
| 178 |
14 |
| o->pretty = SvTRUE(val) ? 1 : 0; |
| 179 |
249 |
▶ | else if (klen == 6 && memcmp(key, "indent", 6) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 6 && memcmp(key, "indent", 6) == 0) { |
149 |
100 |
249 |
| else if (klen == 6 && memcmp(key, "indent", 6) == 0) { |
4 |
145 |
149 |
|
| 180 |
4 |
| IV n = SvIV(val); |
| 181 |
4 |
▶ | if (n != 2 && n != 4) |
Branch: 2 branches
| Branch | True | False | Total |
| if (n != 2 && n != 4) |
3 |
1 |
4 |
| if (n != 2 && n != 4) |
2 |
1 |
3 |
|
| 182 |
2 |
| croak("File::Raw::JSON: indent must be 2 or 4 (got %ld); " |
| 183 |
|
| "arbitrary indent strings planned for v0.02", |
| 184 |
|
| (long)n); |
| 185 |
2 |
| o->indent = (int)n; |
| 186 |
|
| } |
| 187 |
245 |
▶ | else if (klen == 9 && memcmp(key, "sort_keys", 9) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 9 && memcmp(key, "sort_keys", 9) == 0) |
59 |
186 |
245 |
| else if (klen == 9 && memcmp(key, "sort_keys", 9) == 0) |
55 |
4 |
59 |
|
| 188 |
55 |
| o->sort_keys = SvTRUE(val) ? 1 : 0; |
| 189 |
190 |
▶ | else if (klen == 9 && memcmp(key, "canonical", 9) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 9 && memcmp(key, "canonical", 9) == 0) |
4 |
186 |
190 |
| else if (klen == 9 && memcmp(key, "canonical", 9) == 0) |
3 |
1 |
4 |
|
| 190 |
3 |
| o->canonical = SvTRUE(val) ? 1 : 0; |
| 191 |
187 |
▶ | else if (klen == 4 && memcmp(key, "utf8", 4) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 4 && memcmp(key, "utf8", 4) == 0) |
0 |
187 |
187 |
| else if (klen == 4 && memcmp(key, "utf8", 4) == 0) |
0 |
0 |
0 |
|
| 192 |
0 |
| o->utf8 = SvTRUE(val) ? 1 : 0; |
| 193 |
187 |
▶ | else if (klen == 7 && memcmp(key, "relaxed", 7) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 7 && memcmp(key, "relaxed", 7) == 0) |
36 |
151 |
187 |
| else if (klen == 7 && memcmp(key, "relaxed", 7) == 0) |
5 |
31 |
36 |
|
| 194 |
5 |
| o->relaxed = SvTRUE(val) ? 1 : 0; |
| 195 |
182 |
▶ | else if (klen == 12 && memcmp(key, "allow_nonref", 12) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 12 && memcmp(key, "allow_nonref", 12) == 0) |
1 |
181 |
182 |
| else if (klen == 12 && memcmp(key, "allow_nonref", 12) == 0) |
1 |
0 |
1 |
|
| 196 |
1 |
| o->allow_nonref = SvTRUE(val) ? 1 : 0; |
| 197 |
181 |
▶ | else if (klen == 13 && memcmp(key, "allow_nan_inf", 13) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 13 && memcmp(key, "allow_nan_inf", 13) == 0) |
3 |
178 |
181 |
| else if (klen == 13 && memcmp(key, "allow_nan_inf", 13) == 0) |
2 |
1 |
3 |
|
| 198 |
2 |
| o->allow_nan_inf = SvTRUE(val) ? 1 : 0; |
| 199 |
179 |
▶ | else if (klen == 7 && memcmp(key, "ordered", 7) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 7 && memcmp(key, "ordered", 7) == 0) |
31 |
148 |
179 |
| else if (klen == 7 && memcmp(key, "ordered", 7) == 0) |
31 |
0 |
31 |
|
| 200 |
31 |
| o->ordered = SvTRUE(val) ? 1 : 0; |
| 201 |
148 |
▶ | else if (klen == 9 && memcmp(key, "max_depth", 9) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 9 && memcmp(key, "max_depth", 9) == 0) { |
1 |
147 |
148 |
| else if (klen == 9 && memcmp(key, "max_depth", 9) == 0) { |
1 |
0 |
1 |
|
| 202 |
1 |
| IV n = SvIV(val); |
| 203 |
1 |
▶ | if (n < 1) croak("File::Raw::JSON: max_depth must be >= 1"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 1) croak("File::Raw::JSON: max_depth must be >= 1"); |
0 |
1 |
1 |
|
| 204 |
1 |
| o->max_depth = (int)n; |
| 205 |
|
| } |
| 206 |
147 |
▶ | else if (klen == 3 && memcmp(key, "eol", 3) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 3 && memcmp(key, "eol", 3) == 0) { |
1 |
146 |
147 |
| else if (klen == 3 && memcmp(key, "eol", 3) == 0) { |
1 |
0 |
1 |
|
| 207 |
|
| STRLEN elen; |
| 208 |
1 |
| const char *epv = SvPV(val, elen); |
| 209 |
1 |
▶ | if (elen == 0 || elen > 3) |
Branch: 2 branches
| Branch | True | False | Total |
| if (elen == 0 || elen > 3) |
1 |
0 |
1 |
| if (elen == 0 || elen > 3) |
0 |
1 |
1 |
|
| 210 |
0 |
| croak("File::Raw::JSON: eol must be 1-3 bytes " |
| 211 |
|
| "(got %lu)", (unsigned long)elen); |
| 212 |
1 |
| memcpy(o->eol, epv, elen); |
| 213 |
1 |
| o->eol[elen] = '\0'; |
| 214 |
1 |
| o->eol_len = (int)elen; |
| 215 |
|
| } |
| 216 |
146 |
▶ | else if (klen == 13 && memcmp(key, "boolean_class", 13) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (klen == 13 && memcmp(key, "boolean_class", 13) == 0) { |
1 |
145 |
146 |
| else if (klen == 13 && memcmp(key, "boolean_class", 13) == 0) { |
1 |
0 |
1 |
|
| 217 |
|
| STRLEN clen; |
| 218 |
1 |
| boolean_class = SvPV(val, clen); |
| 219 |
|
| (void)clen; |
| 220 |
|
| } |
| 221 |
|
| /* "plugin" is the dispatch key; ignore here. */ |
| 222 |
|
| } |
| 223 |
211 |
| return boolean_class; |
| 224 |
|
| } |
| 225 |
|
| |
| 226 |
|
| /* ============================================================ |
| 227 |
|
| * Plugin phase functions |
| 228 |
|
| * ============================================================ */ |
| 229 |
|
| |
| 230 |
|
| /* Per-plugin state pointers (so the plugin descriptor can carry mode). */ |
| 231 |
|
| static json_mode_t MODE_DOCUMENT_TAG = JSON_MODE_DOCUMENT; |
| 232 |
|
| static json_mode_t MODE_LINES_TAG = JSON_MODE_LINES; |
| 233 |
|
| |
| 234 |
|
| static SV * |
| 235 |
79 |
| json_read(pTHX_ FilePluginContext *ctx) |
| 236 |
|
| { |
| 237 |
|
| json_options_t o; |
| 238 |
|
| const char *boolean_class; |
| 239 |
|
| HV *bool_stash; |
| 240 |
|
| STRLEN len; |
| 241 |
|
| const char *pv; |
| 242 |
|
| |
| 243 |
79 |
| json_options_defaults(&o); |
| 244 |
79 |
▶ | if (ctx->plugin_state) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->plugin_state) |
79 |
0 |
79 |
|
| 245 |
79 |
| o.mode = *(const json_mode_t *)ctx->plugin_state; |
| 246 |
|
| |
| 247 |
79 |
| boolean_class = decode_opts(aTHX_ ctx->options, &o); |
| 248 |
77 |
| bool_stash = resolve_boolean_stash(aTHX_ boolean_class); |
| 249 |
|
| |
| 250 |
77 |
▶ | if (!ctx->data) return &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx->data) return &PL_sv_undef; |
0 |
77 |
77 |
|
| 251 |
77 |
| pv = SvPV(ctx->data, len); |
| 252 |
|
| |
| 253 |
77 |
▶ | if (o.mode == JSON_MODE_LINES) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o.mode == JSON_MODE_LINES) { |
28 |
49 |
77 |
|
| 254 |
28 |
| AV *av = json_decode_lines(aTHX_ pv, len, &o, bool_stash); |
| 255 |
27 |
| return newRV_noinc((SV *)av); |
| 256 |
|
| } |
| 257 |
49 |
| return json_decode_document(aTHX_ pv, len, &o, bool_stash); |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
|
| static SV * |
| 261 |
51 |
| json_write(pTHX_ FilePluginContext *ctx) |
| 262 |
|
| { |
| 263 |
|
| json_options_t o; |
| 264 |
51 |
| json_options_defaults(&o); |
| 265 |
51 |
▶ | if (ctx->plugin_state) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->plugin_state) |
51 |
0 |
51 |
|
| 266 |
51 |
| o.mode = *(const json_mode_t *)ctx->plugin_state; |
| 267 |
51 |
| (void)decode_opts(aTHX_ ctx->options, &o); |
| 268 |
|
| |
| 269 |
49 |
▶ | if (o.mode == JSON_MODE_LINES) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o.mode == JSON_MODE_LINES) { |
11 |
38 |
49 |
|
| 270 |
11 |
| return json_encode_lines(aTHX_ ctx->data, &o); |
| 271 |
|
| } |
| 272 |
38 |
| return json_encode_document(aTHX_ ctx->data, &o); |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
|
| /* The 'json' plugin rejects STREAM with a helpful redirect. */ |
| 276 |
|
| static int |
| 277 |
1 |
| json_stream_reject(pTHX_ FilePluginContext *ctx, |
| 278 |
|
| const char *chunk, size_t len, int eof) |
| 279 |
|
| { |
| 280 |
|
| PERL_UNUSED_ARG(chunk); |
| 281 |
|
| PERL_UNUSED_ARG(len); |
| 282 |
|
| PERL_UNUSED_ARG(eof); |
| 283 |
1 |
| ctx->cancel = 1; |
| 284 |
1 |
| croak("File::Raw::JSON: the 'json' plugin does not support streaming; " |
| 285 |
|
| "use 'jsonl' for concatenated JSON values, or slurp the whole " |
| 286 |
|
| "document via File::Raw::slurp(...)"); |
| 287 |
|
| return 1; |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| /* ============================================================ |
| 291 |
|
| * jsonl streaming |
| 292 |
|
| * |
| 293 |
|
| * Buffers bytes across chunks; brace-balancer slices off complete |
| 294 |
|
| * top-level values; each is parsed by yyjson and emitted via |
| 295 |
|
| * call_sv(ctx->callback, ...). Mirrors File::Raw::Separated's |
| 296 |
|
| * sep_stream pattern. State lives in ctx->call_state. */ |
| 297 |
|
| |
| 298 |
|
| typedef struct { |
| 299 |
|
| char *acc_buf; |
| 300 |
|
| STRLEN acc_len; |
| 301 |
|
| STRLEN acc_cap; |
| 302 |
|
| json_options_t opts; |
| 303 |
|
| HV *bool_stash; |
| 304 |
|
| SV *die_msg; /* propagation slot */ |
| 305 |
|
| } jsonl_stream_state_t; |
| 306 |
|
| |
| 307 |
|
| static void |
| 308 |
16 |
| jsonl_stream_state_free(pTHX_ jsonl_stream_state_t *s) |
| 309 |
|
| { |
| 310 |
16 |
▶ | if (!s) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return; |
0 |
16 |
16 |
|
| 311 |
16 |
▶ | if (s->acc_buf) Safefree(s->acc_buf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->acc_buf) Safefree(s->acc_buf); |
16 |
0 |
16 |
|
| 312 |
16 |
▶ | if (s->die_msg) SvREFCNT_dec(s->die_msg); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->die_msg) SvREFCNT_dec(s->die_msg); |
1 |
15 |
16 |
|
| 313 |
16 |
| Safefree(s); |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
|
| static void |
| 317 |
20 |
| jsonl_acc_append(pTHX_ jsonl_stream_state_t *s, const char *p, STRLEN n) |
| 318 |
|
| { |
| 319 |
20 |
▶ | if (s->acc_len + n > s->acc_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->acc_len + n > s->acc_cap) { |
4 |
16 |
20 |
|
| 320 |
4 |
▶ | STRLEN newcap = s->acc_cap ? s->acc_cap : 8192; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN newcap = s->acc_cap ? s->acc_cap : 8192; |
4 |
0 |
4 |
|
| 321 |
12 |
▶ | while (newcap < s->acc_len + n) newcap *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (newcap < s->acc_len + n) newcap *= 2; |
8 |
4 |
12 |
|
| 322 |
4 |
| Renew(s->acc_buf, newcap, char); |
| 323 |
4 |
| s->acc_cap = newcap; |
| 324 |
|
| } |
| 325 |
20 |
▶ | if (n) memcpy(s->acc_buf + s->acc_len, p, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) memcpy(s->acc_buf + s->acc_len, p, n); |
20 |
0 |
20 |
|
| 326 |
20 |
| s->acc_len += n; |
| 327 |
20 |
| } |
| 328 |
|
| |
| 329 |
|
| static int |
| 330 |
6041 |
| jsonl_emit_one(pTHX_ FilePluginContext *ctx, jsonl_stream_state_t *s, |
| 331 |
|
| const char *vp, STRLEN vlen) |
| 332 |
|
| { |
| 333 |
|
| yyjson_read_err err; |
| 334 |
|
| yyjson_doc *doc; |
| 335 |
|
| SV *value; |
| 336 |
6041 |
| int count, rc = 0; |
| 337 |
|
| SV *errsv; |
| 338 |
|
| |
| 339 |
6041 |
| doc = yyjson_read_opts((char *)vp, (size_t)vlen, |
| 340 |
|
| 0, NULL, &err); |
| 341 |
6041 |
▶ | if (!doc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!doc) { |
0 |
6041 |
6041 |
|
| 342 |
|
| char ctx_buf[64]; |
| 343 |
0 |
| STRLEN copy_len = vlen < sizeof(ctx_buf) - 1 |
| 344 |
|
| ? vlen : sizeof(ctx_buf) - 1; |
| 345 |
0 |
| memcpy(ctx_buf, vp, copy_len); |
| 346 |
0 |
| ctx_buf[copy_len] = '\0'; |
| 347 |
0 |
| ctx->cancel = 1; |
| 348 |
0 |
▶ | croak("File::Raw::JSON: stream parse error: %s near \"%s\"", |
Branch: 1 branch
| Branch | True | False | Total |
| croak("File::Raw::JSON: stream parse error: %s near \"%s\"", |
0 |
0 |
0 |
|
| 349 |
|
| err.msg ? err.msg : "unknown", ctx_buf); |
| 350 |
|
| } |
| 351 |
12082 |
▶ | value = json_sv_from_yyjson(aTHX_ yyjson_doc_get_root(doc), |
Branch: 1 branch
| Branch | True | False | Total |
| value = json_sv_from_yyjson(aTHX_ yyjson_doc_get_root(doc), |
6041 |
0 |
6041 |
|
| 352 |
|
| s->bool_stash, s->opts.ordered, |
| 353 |
|
| s->opts.max_depth); |
| 354 |
|
| yyjson_doc_free(doc); |
| 355 |
|
| |
| 356 |
|
| { |
| 357 |
6041 |
| dSP; |
| 358 |
6041 |
| ENTER; SAVETMPS; |
| 359 |
6041 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6041 |
6041 |
|
| 360 |
6041 |
▶ | XPUSHs(sv_2mortal(value)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(value)); |
0 |
6041 |
6041 |
|
| 361 |
6041 |
| PUTBACK; |
| 362 |
6041 |
| count = call_sv(ctx->callback, G_DISCARD | G_EVAL); |
| 363 |
6041 |
| SPAGAIN; |
| 364 |
|
| PERL_UNUSED_VAR(count); |
| 365 |
6041 |
▶ | errsv = ERRSV; |
Branch: 1 branch
| Branch | True | False | Total |
| errsv = ERRSV; |
6041 |
0 |
6041 |
|
| 366 |
6041 |
▶ | if (SvTRUE(errsv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTRUE(errsv)) { |
1 |
6040 |
6041 |
|
| 367 |
1 |
| s->die_msg = newSVsv(errsv); |
| 368 |
1 |
| rc = 1; |
| 369 |
|
| } |
| 370 |
6041 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
6041 |
0 |
6041 |
|
| 371 |
|
| } |
| 372 |
6041 |
| return rc; |
| 373 |
|
| } |
| 374 |
|
| |
| 375 |
|
| static int |
| 376 |
35 |
| jsonl_drain_buffer(pTHX_ FilePluginContext *ctx, jsonl_stream_state_t *s, |
| 377 |
|
| int allow_truncate) |
| 378 |
|
| { |
| 379 |
35 |
| STRLEN cursor = 0; |
| 380 |
6075 |
▶ | while (cursor < s->acc_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (cursor < s->acc_len) { |
6045 |
30 |
6075 |
|
| 381 |
|
| STRLEN start, end, np; |
| 382 |
6045 |
| jsonl_scan_t rc = json_jsonl_next(s->acc_buf + cursor, |
| 383 |
6045 |
| s->acc_len - cursor, |
| 384 |
|
| &start, &end, &np); |
| 385 |
6045 |
▶ | if (rc == JSONL_FOUND) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == JSONL_FOUND) { |
6041 |
4 |
6045 |
|
| 386 |
6041 |
▶ | if (jsonl_emit_one(aTHX_ ctx, s, |
Branch: 1 branch
| Branch | True | False | Total |
| if (jsonl_emit_one(aTHX_ ctx, s, |
1 |
6040 |
6041 |
|
| 387 |
6041 |
| s->acc_buf + cursor + start, end - start)) { |
| 388 |
5 |
| return 1; /* user die */ |
| 389 |
|
| } |
| 390 |
6040 |
| cursor += np; |
| 391 |
6040 |
| continue; |
| 392 |
|
| } |
| 393 |
4 |
▶ | if (rc == JSONL_NEED_MORE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == JSONL_NEED_MORE) { |
4 |
0 |
4 |
|
| 394 |
|
| /* Shift the unconsumed prefix to the front and bail until |
| 395 |
|
| * the next chunk arrives. The "junk before the value" case |
| 396 |
|
| * (i.e. start > 0 with NEED_MORE) means leading whitespace |
| 397 |
|
| * + truncated value; just shift everything from cursor. */ |
| 398 |
4 |
▶ | if (cursor > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cursor > 0) { |
4 |
0 |
4 |
|
| 399 |
4 |
| memmove(s->acc_buf, s->acc_buf + cursor, |
| 400 |
4 |
| s->acc_len - cursor); |
| 401 |
4 |
| s->acc_len -= cursor; |
| 402 |
|
| } |
| 403 |
4 |
▶ | if (!allow_truncate) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!allow_truncate) return 0; |
4 |
0 |
4 |
|
| 404 |
|
| /* eof + truncated value: croak. */ |
| 405 |
0 |
| ctx->cancel = 1; |
| 406 |
0 |
| croak("File::Raw::JSON: truncated trailing JSON value at end " |
| 407 |
|
| "of stream"); |
| 408 |
|
| } |
| 409 |
|
| /* JSONL_NO_OPENER: only whitespace remains, or junk byte. */ |
| 410 |
0 |
| cursor += start; |
| 411 |
0 |
▶ | if (cursor >= s->acc_len) break; /* all whitespace */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (cursor >= s->acc_len) break; /* all whitespace */ |
0 |
0 |
0 |
|
| 412 |
0 |
▶ | if (s->opts.relaxed) { cursor++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->opts.relaxed) { cursor++; continue; } |
0 |
0 |
0 |
|
| 413 |
0 |
| ctx->cancel = 1; |
| 414 |
0 |
| croak("File::Raw::JSON: unexpected byte at offset %lu in stream " |
| 415 |
|
| "(expected '{' or '[' to start a JSONL value)", |
| 416 |
|
| (unsigned long)cursor); |
| 417 |
|
| } |
| 418 |
|
| /* Drop the consumed prefix so the buffer doesn't grow without |
| 419 |
|
| * bound across chunks. */ |
| 420 |
30 |
▶ | if (cursor > 0 && cursor < s->acc_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cursor > 0 && cursor < s->acc_len) { |
15 |
15 |
30 |
| if (cursor > 0 && cursor < s->acc_len) { |
0 |
15 |
15 |
|
| 421 |
0 |
| memmove(s->acc_buf, s->acc_buf + cursor, s->acc_len - cursor); |
| 422 |
0 |
| s->acc_len -= cursor; |
| 423 |
30 |
▶ | } else if (cursor >= s->acc_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cursor >= s->acc_len) { |
30 |
0 |
30 |
|
| 424 |
30 |
| s->acc_len = 0; |
| 425 |
|
| } |
| 426 |
30 |
| return 0; |
| 427 |
|
| } |
| 428 |
|
| |
| 429 |
|
| static int |
| 430 |
35 |
| jsonl_stream(pTHX_ FilePluginContext *ctx, |
| 431 |
|
| const char *chunk, size_t len, int eof) |
| 432 |
|
| { |
| 433 |
35 |
| jsonl_stream_state_t *s = (jsonl_stream_state_t *)ctx->call_state; |
| 434 |
|
| |
| 435 |
35 |
▶ | if (!s) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) { |
16 |
19 |
35 |
|
| 436 |
|
| const char *boolean_class; |
| 437 |
16 |
| Newxz(s, 1, jsonl_stream_state_t); |
| 438 |
16 |
| json_options_defaults(&s->opts); |
| 439 |
16 |
▶ | if (ctx->plugin_state) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->plugin_state) |
16 |
0 |
16 |
|
| 440 |
16 |
| s->opts.mode = *(const json_mode_t *)ctx->plugin_state; |
| 441 |
16 |
| boolean_class = decode_opts(aTHX_ ctx->options, &s->opts); |
| 442 |
16 |
| s->bool_stash = resolve_boolean_stash(aTHX_ boolean_class); |
| 443 |
16 |
| s->acc_cap = 8192; |
| 444 |
16 |
| Newx(s->acc_buf, s->acc_cap, char); |
| 445 |
16 |
| s->acc_len = 0; |
| 446 |
16 |
| ctx->call_state = s; |
| 447 |
|
| } |
| 448 |
|
| |
| 449 |
35 |
▶ | if (chunk && len > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (chunk && len > 0) { |
20 |
15 |
35 |
| if (chunk && len > 0) { |
20 |
0 |
20 |
|
| 450 |
20 |
| jsonl_acc_append(aTHX_ s, chunk, len); |
| 451 |
|
| } |
| 452 |
|
| |
| 453 |
35 |
▶ | if (jsonl_drain_buffer(aTHX_ ctx, s, eof ? 1 : 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (jsonl_drain_buffer(aTHX_ ctx, s, eof ? 1 : 0)) { |
1 |
34 |
35 |
|
| 454 |
1 |
| SV *die_msg = s->die_msg; |
| 455 |
|
| STRLEN dlen; |
| 456 |
1 |
▶ | SV *m = die_msg ? newSVsv(die_msg) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *m = die_msg ? newSVsv(die_msg) : NULL; |
1 |
0 |
1 |
|
| 457 |
1 |
| jsonl_stream_state_free(aTHX_ s); |
| 458 |
1 |
| ctx->call_state = NULL; |
| 459 |
1 |
| ctx->cancel = 1; |
| 460 |
1 |
▶ | if (m) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (m) { |
1 |
0 |
1 |
|
| 461 |
1 |
| const char *dpv = SvPV(m, dlen); |
| 462 |
1 |
| sv_2mortal(m); |
| 463 |
1 |
| croak("%.*s", (int)dlen, dpv); |
| 464 |
|
| } |
| 465 |
0 |
| croak("File::Raw::JSON: stream cancelled"); |
| 466 |
|
| } |
| 467 |
|
| |
| 468 |
34 |
▶ | if (eof) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eof) { |
15 |
19 |
34 |
|
| 469 |
15 |
| jsonl_stream_state_free(aTHX_ s); |
| 470 |
15 |
| ctx->call_state = NULL; |
| 471 |
|
| } |
| 472 |
34 |
| return 0; |
| 473 |
|
| } |
| 474 |
|
| |
| 475 |
|
| /* ============================================================ |
| 476 |
|
| * Plugin descriptors. Statics so the registry's non-owning pointer |
| 477 |
|
| * stays valid for the life of the process. |
| 478 |
|
| * ============================================================ */ |
| 479 |
|
| |
| 480 |
|
| static FilePlugin json_plugin; |
| 481 |
|
| static FilePlugin jsonl_plugin; |
| 482 |
|
| |
| 483 |
|
| /* ============================================================ |
| 484 |
|
| * Public C ABI (frj_abi.h) |
| 485 |
|
| * |
| 486 |
|
| * A versioned function-pointer table consumers reach through |
| 487 |
|
| * File::Raw::JSON::_abi_ptr: parse/serialise JSON entirely in C with |
| 488 |
|
| * no per-call Perl codec dispatch. The wrappers translate the stable |
| 489 |
|
| * frj_opts into json_options_t (seeded from json_options_defaults so |
| 490 |
|
| * new internal fields keep sane values) and reuse the same internal |
| 491 |
|
| * entry points as file_json_decode / file_json_encode. |
| 492 |
|
| * ============================================================ */ |
| 493 |
|
| |
| 494 |
2 |
| static void frj_opts_to_internal(const frj_opts *in, json_options_t *o) |
| 495 |
|
| { |
| 496 |
2 |
| json_options_defaults(o); |
| 497 |
2 |
▶ | if (!in) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) return; |
0 |
2 |
2 |
|
| 498 |
2 |
| o->utf8 = in->utf8; |
| 499 |
2 |
| o->ordered = in->ordered; |
| 500 |
2 |
| o->relaxed = in->relaxed; |
| 501 |
2 |
| o->allow_nonref = in->allow_nonref; |
| 502 |
2 |
| o->allow_nan_inf = in->allow_nan_inf; |
| 503 |
2 |
| o->pretty = in->pretty; |
| 504 |
2 |
| o->sort_keys = in->sort_keys; |
| 505 |
2 |
| o->canonical = in->canonical; |
| 506 |
2 |
▶ | if (in->max_depth > 0) o->max_depth = in->max_depth; /* 0 => keep default */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (in->max_depth > 0) o->max_depth = in->max_depth; /* 0 => keep default */ |
2 |
0 |
2 |
|
| 507 |
2 |
▶ | if (in->indent > 0) o->indent = in->indent; /* 0 => keep default */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (in->indent > 0) o->indent = in->indent; /* 0 => keep default */ |
2 |
0 |
2 |
|
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
1 |
| static void frj_abi_opts_init(frj_opts *o) |
| 511 |
|
| { |
| 512 |
|
| json_options_t d; |
| 513 |
1 |
▶ | if (!o) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o) return; |
0 |
1 |
1 |
|
| 514 |
1 |
| json_options_defaults(&d); |
| 515 |
1 |
| o->utf8 = d.utf8; |
| 516 |
1 |
| o->ordered = d.ordered; |
| 517 |
1 |
| o->relaxed = d.relaxed; |
| 518 |
1 |
| o->allow_nonref = d.allow_nonref; |
| 519 |
1 |
| o->allow_nan_inf = d.allow_nan_inf; |
| 520 |
1 |
| o->max_depth = d.max_depth; |
| 521 |
1 |
| o->pretty = d.pretty; |
| 522 |
1 |
| o->indent = d.indent; |
| 523 |
1 |
| o->sort_keys = d.sort_keys; |
| 524 |
1 |
| o->canonical = d.canonical; |
| 525 |
|
| } |
| 526 |
|
| |
| 527 |
1 |
| static SV *frj_abi_decode(pTHX_ const char *bytes, STRLEN len, const frj_opts *in) |
| 528 |
|
| { |
| 529 |
|
| json_options_t o; |
| 530 |
1 |
| frj_opts_to_internal(in, &o); |
| 531 |
1 |
| o.mode = JSON_MODE_DOCUMENT; |
| 532 |
1 |
| return json_decode_document(aTHX_ bytes, len, &o, get_boolean_stash(aTHX)); |
| 533 |
|
| } |
| 534 |
|
| |
| 535 |
0 |
| static AV *frj_abi_decode_lines(pTHX_ const char *bytes, STRLEN len, const frj_opts *in) |
| 536 |
|
| { |
| 537 |
|
| json_options_t o; |
| 538 |
0 |
| frj_opts_to_internal(in, &o); |
| 539 |
0 |
| o.mode = JSON_MODE_LINES; |
| 540 |
0 |
| return json_decode_lines(aTHX_ bytes, len, &o, get_boolean_stash(aTHX)); |
| 541 |
|
| } |
| 542 |
|
| |
| 543 |
1 |
| static SV *frj_abi_encode(pTHX_ SV *value, const frj_opts *in) |
| 544 |
|
| { |
| 545 |
|
| json_options_t o; |
| 546 |
1 |
| frj_opts_to_internal(in, &o); |
| 547 |
1 |
| o.mode = JSON_MODE_DOCUMENT; |
| 548 |
1 |
| return json_encode_document(aTHX_ value, &o); |
| 549 |
|
| } |
| 550 |
|
| |
| 551 |
0 |
| static SV *frj_abi_encode_lines(pTHX_ SV *payload, const frj_opts *in) |
| 552 |
|
| { |
| 553 |
|
| json_options_t o; |
| 554 |
0 |
| frj_opts_to_internal(in, &o); |
| 555 |
0 |
| o.mode = JSON_MODE_LINES; |
| 556 |
0 |
| return json_encode_lines(aTHX_ payload, &o); |
| 557 |
|
| } |
| 558 |
|
| |
| 559 |
|
| static const frj_abi FRJ_ABI = { |
| 560 |
|
| FRJ_ABI_VERSION, |
| 561 |
|
| frj_abi_opts_init, |
| 562 |
|
| frj_abi_decode, |
| 563 |
|
| frj_abi_decode_lines, |
| 564 |
|
| frj_abi_encode, |
| 565 |
|
| frj_abi_encode_lines, |
| 566 |
|
| }; |
| 567 |
|
| |
| 568 |
|
| /* ============================================================ */ |
| 569 |
|
| |
| 570 |
|
| MODULE = File::Raw::JSON PACKAGE = File::Raw::JSON |
| 571 |
|
| |
| 572 |
|
| PROTOTYPES: DISABLE |
| 573 |
|
| |
| 574 |
|
| # ---- direct in-memory codec entry points ------------------------ |
| 575 |
|
| # |
| 576 |
|
| # file_json_decode($bytes, ?key => value, ...) -> parsed value |
| 577 |
|
| # file_json_encode($value, ?key => value, ...) -> JSON bytes |
| 578 |
|
| # |
| 579 |
|
| # These bypass File::Raw's plugin pipeline entirely - no path, no |
| 580 |
|
| # syscalls, just bytes <-> Perl structure. Same options grammar as |
| 581 |
|
| # the plugin tail (mode, pretty, indent, sort_keys, canonical, |
| 582 |
|
| # ordered, relaxed, allow_nonref, allow_nan_inf, max_depth, eol, |
| 583 |
|
| # boolean_class, utf8); odd-count tails croak. See build_opts_hv |
| 584 |
|
| # above the first MODULE block for the option-collection helper. |
| 585 |
|
| |
| 586 |
|
| # File::Raw::JSON->import(...) - selective installer. Mirrors |
| 587 |
|
| # File::Raw's pattern (file.c XS_file_import). `use File::Raw::JSON` |
| 588 |
|
| # with no arg list = no-op; with a list of names, each requested |
| 589 |
|
| # function CV is aliased into the caller's package via newXS, sharing |
| 590 |
|
| # the underlying XSUB pointer with the source CV. |
| 591 |
|
| # |
| 592 |
|
| # Recognised: file_json_decode, file_json_encode, :codec (= both), |
| 593 |
|
| # :all (= same). Unknown names warn but don't die, matching File::Raw. |
| 594 |
|
| void |
| 595 |
|
| import(...) |
| 596 |
|
| PREINIT: |
| 597 |
|
| const char *pkg; |
| 598 |
|
| I32 i; |
| 599 |
|
| PPCODE: |
| 600 |
38 |
▶ | pkg = CopSTASHPV(PL_curcop); |
Branch: 7 branches
| Branch | True | False | Total |
| pkg = CopSTASHPV(PL_curcop); |
38 |
0 |
38 |
| pkg = CopSTASHPV(PL_curcop); |
38 |
0 |
38 |
| pkg = CopSTASHPV(PL_curcop); |
38 |
0 |
38 |
| pkg = CopSTASHPV(PL_curcop); |
0 |
38 |
38 |
| pkg = CopSTASHPV(PL_curcop); |
0 |
0 |
0 |
| pkg = CopSTASHPV(PL_curcop); |
38 |
0 |
38 |
| pkg = CopSTASHPV(PL_curcop); |
0 |
38 |
38 |
|
| 601 |
38 |
▶ | if (items <= 1) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (items <= 1) XSRETURN_EMPTY; |
24 |
14 |
38 |
|
| 602 |
|
| |
| 603 |
36 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
22 |
14 |
36 |
|
| 604 |
|
| STRLEN len; |
| 605 |
22 |
| const char *arg = SvPV(ST(i), len); |
| 606 |
|
| |
| 607 |
22 |
▶ | if ((len == 6 && strEQ(arg, ":codec")) || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((len == 6 && strEQ(arg, ":codec")) || |
2 |
20 |
22 |
| if ((len == 6 && strEQ(arg, ":codec")) || |
0 |
2 |
2 |
|
| 608 |
20 |
▶ | (len == 4 && strEQ(arg, ":all"))) |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 4 && strEQ(arg, ":all"))) |
3 |
17 |
20 |
| (len == 4 && strEQ(arg, ":all"))) |
1 |
2 |
3 |
|
| 609 |
|
| { |
| 610 |
3 |
| fjson_install_alias(aTHX_ pkg, "file_json_decode"); |
| 611 |
3 |
| fjson_install_alias(aTHX_ pkg, "file_json_encode"); |
| 612 |
20 |
| continue; |
| 613 |
|
| } |
| 614 |
19 |
▶ | if ((len == 16 && strEQ(arg, "file_json_decode")) || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((len == 16 && strEQ(arg, "file_json_decode")) || |
17 |
2 |
19 |
| if ((len == 16 && strEQ(arg, "file_json_decode")) || |
9 |
8 |
17 |
|
| 615 |
11 |
▶ | (len == 16 && strEQ(arg, "file_json_encode"))) |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 16 && strEQ(arg, "file_json_encode"))) |
9 |
2 |
11 |
| (len == 16 && strEQ(arg, "file_json_encode"))) |
9 |
0 |
9 |
|
| 616 |
|
| { |
| 617 |
17 |
| fjson_install_alias(aTHX_ pkg, arg); |
| 618 |
17 |
| continue; |
| 619 |
|
| } |
| 620 |
2 |
| warn("File::Raw::JSON: '%.*s' is not exported", (int)len, arg); |
| 621 |
|
| } |
| 622 |
14 |
| XSRETURN_EMPTY; |
| 623 |
|
| |
| 624 |
|
| SV * |
| 625 |
|
| file_json_decode(bytes, ...) |
| 626 |
|
| SV *bytes |
| 627 |
|
| PREINIT: |
| 628 |
|
| json_options_t o; |
| 629 |
|
| HV *opts_hv; |
| 630 |
|
| const char *boolean_class; |
| 631 |
|
| HV *bool_stash; |
| 632 |
|
| STRLEN len; |
| 633 |
|
| const char *pv; |
| 634 |
|
| CODE: |
| 635 |
109 |
| json_options_defaults(&o); |
| 636 |
109 |
| o.mode = JSON_MODE_DOCUMENT; |
| 637 |
109 |
| opts_hv = build_opts_hv(aTHX_ ax, items, 1, "file_json_decode"); |
| 638 |
108 |
| boolean_class = decode_opts(aTHX_ opts_hv, &o); |
| 639 |
107 |
| bool_stash = resolve_boolean_stash(aTHX_ boolean_class); |
| 640 |
|
| |
| 641 |
107 |
▶ | if (!bytes || !SvOK(bytes)) XSRETURN_UNDEF; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!bytes || !SvOK(bytes)) XSRETURN_UNDEF; |
107 |
0 |
107 |
| if (!bytes || !SvOK(bytes)) XSRETURN_UNDEF; |
1 |
106 |
107 |
|
| 642 |
106 |
| pv = SvPV(bytes, len); |
| 643 |
|
| |
| 644 |
106 |
▶ | if (o.mode == JSON_MODE_LINES) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o.mode == JSON_MODE_LINES) { |
10 |
96 |
106 |
|
| 645 |
10 |
| AV *av = json_decode_lines(aTHX_ pv, len, &o, bool_stash); |
| 646 |
10 |
| RETVAL = newRV_noinc((SV *)av); |
| 647 |
|
| } else { |
| 648 |
96 |
| SV *out = json_decode_document(aTHX_ pv, len, &o, bool_stash); |
| 649 |
94 |
▶ | RETVAL = out ? out : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = out ? out : &PL_sv_undef; |
94 |
0 |
94 |
|
| 650 |
94 |
▶ | if (RETVAL == &PL_sv_undef) SvREFCNT_inc(RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (RETVAL == &PL_sv_undef) SvREFCNT_inc(RETVAL); |
0 |
94 |
94 |
|
| 651 |
|
| } |
| 652 |
|
| OUTPUT: |
| 653 |
|
| RETVAL |
| 654 |
|
| |
| 655 |
|
| SV * |
| 656 |
|
| file_json_encode(value, ...) |
| 657 |
|
| SV *value |
| 658 |
|
| PREINIT: |
| 659 |
|
| json_options_t o; |
| 660 |
|
| HV *opts_hv; |
| 661 |
|
| CODE: |
| 662 |
99 |
| json_options_defaults(&o); |
| 663 |
99 |
| o.mode = JSON_MODE_DOCUMENT; |
| 664 |
99 |
| opts_hv = build_opts_hv(aTHX_ ax, items, 1, "file_json_encode"); |
| 665 |
98 |
| (void)decode_opts(aTHX_ opts_hv, &o); |
| 666 |
|
| |
| 667 |
98 |
▶ | if (o.mode == JSON_MODE_LINES) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o.mode == JSON_MODE_LINES) { |
2 |
96 |
98 |
|
| 668 |
2 |
| RETVAL = json_encode_lines(aTHX_ value, &o); |
| 669 |
|
| } else { |
| 670 |
96 |
| RETVAL = json_encode_document(aTHX_ value, &o); |
| 671 |
|
| } |
| 672 |
93 |
▶ | if (!RETVAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) { |
0 |
93 |
93 |
|
| 673 |
0 |
| RETVAL = &PL_sv_undef; |
| 674 |
0 |
| SvREFCNT_inc(RETVAL); |
| 675 |
|
| } |
| 676 |
|
| OUTPUT: |
| 677 |
|
| RETVAL |
| 678 |
|
| |
| 679 |
|
| # Address of the C ABI table (frj_abi.h). A consumer XS module (e.g. Hyperman) |
| 680 |
|
| # fetches this once at boot, INT2PTRs it to a `const frj_abi *`, and checks |
| 681 |
|
| # ->abi_version == FRJ_ABI_VERSION before use. Not part of the public Perl API. |
| 682 |
|
| IV |
| 683 |
|
| _abi_ptr() |
| 684 |
|
| CODE: |
| 685 |
2 |
▶ | RETVAL = PTR2IV(&FRJ_ABI); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2IV(&FRJ_ABI); |
0 |
2 |
2 |
|
| 686 |
|
| OUTPUT: |
| 687 |
|
| RETVAL |
| 688 |
|
| |
| 689 |
|
| # Self-test: resolve the ABI table and round-trip a small document through the |
| 690 |
|
| # function pointers entirely in C. Returns 1 on success, 0 on any mismatch. |
| 691 |
|
| # Exercises the same path a real consumer takes, without a second dist. |
| 692 |
|
| IV |
| 693 |
|
| _abi_selftest() |
| 694 |
|
| CODE: |
| 695 |
|
| { |
| 696 |
1 |
| const frj_abi *A = &FRJ_ABI; |
| 697 |
1 |
| const char *src = "{\"a\":[1,true,null]}"; |
| 698 |
1 |
| STRLEN slen = strlen(src); |
| 699 |
|
| frj_opts o; |
| 700 |
|
| SV *decoded, *encoded; |
| 701 |
|
| STRLEN elen; |
| 702 |
|
| const char *ep; |
| 703 |
1 |
| RETVAL = 0; |
| 704 |
1 |
▶ | if (A->abi_version == FRJ_ABI_VERSION) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (A->abi_version == FRJ_ABI_VERSION) { |
1 |
0 |
1 |
|
| 705 |
1 |
| A->opts_init(&o); |
| 706 |
1 |
| o.canonical = 1; |
| 707 |
1 |
| decoded = A->decode(aTHX_ src, slen, &o); |
| 708 |
1 |
| encoded = A->encode(aTHX_ decoded, &o); |
| 709 |
1 |
| ep = SvPV(encoded, elen); |
| 710 |
1 |
▶ | if (elen == slen && memcmp(ep, src, slen) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (elen == slen && memcmp(ep, src, slen) == 0) |
1 |
0 |
1 |
| if (elen == slen && memcmp(ep, src, slen) == 0) |
1 |
0 |
1 |
|
| 711 |
1 |
| RETVAL = 1; |
| 712 |
1 |
| SvREFCNT_dec(decoded); |
| 713 |
1 |
| SvREFCNT_dec(encoded); |
| 714 |
|
| } |
| 715 |
|
| } |
| 716 |
|
| OUTPUT: |
| 717 |
|
| RETVAL |
| 718 |
|
| |
| 719 |
|
| BOOT: |
| 720 |
|
| { |
| 721 |
30 |
| init_boolean_singletons(aTHX); |
| 722 |
|
| |
| 723 |
30 |
| json_plugin.name = "json"; |
| 724 |
30 |
| json_plugin.read_fn = json_read; |
| 725 |
30 |
| json_plugin.write_fn = json_write; |
| 726 |
30 |
| json_plugin.record_fn = NULL; |
| 727 |
30 |
| json_plugin.stream_fn = json_stream_reject; |
| 728 |
30 |
| json_plugin.state = &MODE_DOCUMENT_TAG; |
| 729 |
30 |
▶ | if (file_register_plugin(aTHX_ &json_plugin) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file_register_plugin(aTHX_ &json_plugin) <= 0) |
0 |
30 |
30 |
|
| 730 |
0 |
| warn("File::Raw::JSON: failed to register 'json' plugin"); |
| 731 |
|
| |
| 732 |
30 |
| jsonl_plugin.name = "jsonl"; |
| 733 |
30 |
| jsonl_plugin.read_fn = json_read; /* mode tag selects MODE_LINES */ |
| 734 |
30 |
| jsonl_plugin.write_fn = json_write; |
| 735 |
30 |
| jsonl_plugin.record_fn = NULL; |
| 736 |
30 |
| jsonl_plugin.stream_fn = jsonl_stream; |
| 737 |
30 |
| jsonl_plugin.state = &MODE_LINES_TAG; |
| 738 |
30 |
▶ | if (file_register_plugin(aTHX_ &jsonl_plugin) <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file_register_plugin(aTHX_ &jsonl_plugin) <= 0) |
0 |
30 |
30 |
|
| 739 |
0 |
| warn("File::Raw::JSON: failed to register 'jsonl' plugin"); |
| 740 |
|
| } |
| 741 |
|
| |
| 742 |
|
| |
| 743 |
|
| # ============================================================ |
| 744 |
|
| # File::Raw::JSON::Boolean - XSUB constructors + overload bodies |
| 745 |
|
| # ============================================================ |
| 746 |
|
| # |
| 747 |
|
| # All four overload entry points (bool / numify / stringify / not) |
| 748 |
|
| # run as XSUBs rather than Perl subs, ~3x faster than the pure-Perl |
| 749 |
|
| # overload that was here before. The overload table itself is still |
| 750 |
|
| # wired up by `use overload ...` in Boolean.pm - that's the cheapest |
| 751 |
|
| # way to register, and the dispatch cost is identical regardless of |
| 752 |
|
| # whether the body is a Perl sub or an XSUB. |
| 753 |
|
| # |
| 754 |
|
| # Calling convention: overload invokes our handlers with three args |
| 755 |
|
| # (self, other, swap). We only need self; ignore the rest. Returning |
| 756 |
|
| # the static PL_sv_yes / PL_sv_no avoids per-call SV allocation. |
| 757 |
|
| |
| 758 |
|
| MODULE = File::Raw::JSON PACKAGE = File::Raw::JSON::Boolean |
| 759 |
|
| |
| 760 |
|
| PROTOTYPES: DISABLE |
| 761 |
|
| |
| 762 |
|
| void |
| 763 |
|
| TRUE(...) |
| 764 |
|
| PPCODE: |
| 765 |
|
| PERL_UNUSED_VAR(items); |
| 766 |
6 |
▶ | if (!g_frj_true_sv) init_boolean_singletons(aTHX); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g_frj_true_sv) init_boolean_singletons(aTHX); |
0 |
6 |
6 |
|
| 767 |
6 |
▶ | SvREFCNT_inc_simple_void(g_frj_true_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| SvREFCNT_inc_simple_void(g_frj_true_sv); |
6 |
0 |
6 |
|
| 768 |
6 |
▶ | XPUSHs(sv_2mortal(g_frj_true_sv)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(g_frj_true_sv)); |
0 |
6 |
6 |
|
| 769 |
6 |
| XSRETURN(1); |
| 770 |
|
| |
| 771 |
|
| void |
| 772 |
|
| FALSE(...) |
| 773 |
|
| PPCODE: |
| 774 |
|
| PERL_UNUSED_VAR(items); |
| 775 |
4 |
▶ | if (!g_frj_false_sv) init_boolean_singletons(aTHX); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g_frj_false_sv) init_boolean_singletons(aTHX); |
0 |
4 |
4 |
|
| 776 |
4 |
▶ | SvREFCNT_inc_simple_void(g_frj_false_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| SvREFCNT_inc_simple_void(g_frj_false_sv); |
4 |
0 |
4 |
|
| 777 |
4 |
▶ | XPUSHs(sv_2mortal(g_frj_false_sv)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(g_frj_false_sv)); |
0 |
4 |
4 |
|
| 778 |
4 |
| XSRETURN(1); |
| 779 |
|
| |
| 780 |
|
| SV * |
| 781 |
|
| overload_bool(self, other, swap) |
| 782 |
|
| SV *self |
| 783 |
|
| SV *other |
| 784 |
|
| SV *swap |
| 785 |
|
| OVERLOAD: bool |
| 786 |
|
| CODE: |
| 787 |
|
| PERL_UNUSED_VAR(other); |
| 788 |
|
| PERL_UNUSED_VAR(swap); |
| 789 |
7 |
▶ | RETVAL = (SvROK(self) && SvTRUE(SvRV(self))) |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (SvROK(self) && SvTRUE(SvRV(self))) |
7 |
0 |
7 |
|
| 790 |
14 |
▶ | ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| ? &PL_sv_yes : &PL_sv_no; |
7 |
0 |
7 |
|
| 791 |
7 |
▶ | SvREFCNT_inc_simple_void(RETVAL); /* OUTPUT typemap will mortalise */ |
Branch: 1 branch
| Branch | True | False | Total |
| SvREFCNT_inc_simple_void(RETVAL); /* OUTPUT typemap will mortalise */ |
7 |
0 |
7 |
|
| 792 |
|
| OUTPUT: |
| 793 |
|
| RETVAL |
| 794 |
|
| |
| 795 |
|
| SV * |
| 796 |
|
| overload_numify(self, other, swap) |
| 797 |
|
| SV *self |
| 798 |
|
| SV *other |
| 799 |
|
| SV *swap |
| 800 |
|
| OVERLOAD: 0+ |
| 801 |
|
| CODE: |
| 802 |
|
| PERL_UNUSED_VAR(other); |
| 803 |
|
| PERL_UNUSED_VAR(swap); |
| 804 |
0 |
▶ | RETVAL = newSViv( |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = newSViv( |
0 |
0 |
0 |
| RETVAL = newSViv( |
0 |
0 |
0 |
|
| 805 |
|
| (SvROK(self) && SvTRUE(SvRV(self))) ? 1 : 0 |
| 806 |
|
| ); |
| 807 |
|
| OUTPUT: |
| 808 |
|
| RETVAL |
| 809 |
|
| |
| 810 |
|
| SV * |
| 811 |
|
| overload_stringify(self, other, swap) |
| 812 |
|
| SV *self |
| 813 |
|
| SV *other |
| 814 |
|
| SV *swap |
| 815 |
|
| OVERLOAD: \"\" |
| 816 |
|
| CODE: |
| 817 |
|
| PERL_UNUSED_VAR(other); |
| 818 |
|
| PERL_UNUSED_VAR(swap); |
| 819 |
0 |
▶ | RETVAL = newSVpvn( |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = newSVpvn( |
0 |
0 |
0 |
| RETVAL = newSVpvn( |
0 |
0 |
0 |
|
| 820 |
|
| (SvROK(self) && SvTRUE(SvRV(self))) ? "1" : "0", 1 |
| 821 |
|
| ); |
| 822 |
|
| OUTPUT: |
| 823 |
|
| RETVAL |
| 824 |
|
| |
| 825 |
|
| SV * |
| 826 |
|
| overload_not(self, other, swap) |
| 827 |
|
| SV *self |
| 828 |
|
| SV *other |
| 829 |
|
| SV *swap |
| 830 |
|
| OVERLOAD: ! |
| 831 |
|
| CODE: |
| 832 |
|
| PERL_UNUSED_VAR(other); |
| 833 |
|
| PERL_UNUSED_VAR(swap); |
| 834 |
10 |
▶ | if (SvROK(self) && SvTRUE(SvRV(self))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(self) && SvTRUE(SvRV(self))) |
10 |
0 |
10 |
| if (SvROK(self) && SvTRUE(SvRV(self))) |
2 |
8 |
10 |
|
| 835 |
2 |
| RETVAL = newSVpvn("", 0); |
| 836 |
|
| else |
| 837 |
8 |
| RETVAL = newSViv(1); |
| 838 |
|
| OUTPUT: |
| 839 |
|
| RETVAL |
| 840 |
|
| |
| 841 |
|
| int |
| 842 |
|
| is_true(self) |
| 843 |
|
| SV *self |
| 844 |
|
| CODE: |
| 845 |
0 |
▶ | RETVAL = (SvROK(self) && sv_isa(self, "File::Raw::JSON::Boolean") |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (SvROK(self) && sv_isa(self, "File::Raw::JSON::Boolean") |
0 |
0 |
0 |
| RETVAL = (SvROK(self) && sv_isa(self, "File::Raw::JSON::Boolean") |
0 |
0 |
0 |
|
| 846 |
0 |
▶ | && SvTRUE(SvRV(self))) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| && SvTRUE(SvRV(self))) ? 1 : 0; |
0 |
0 |
0 |
| && SvTRUE(SvRV(self))) ? 1 : 0; |
0 |
0 |
0 |
|
| 847 |
|
| OUTPUT: |
| 848 |
|
| RETVAL |
| 849 |
|
| |
| 850 |
|
| int |
| 851 |
|
| is_false(self) |
| 852 |
|
| SV *self |
| 853 |
|
| CODE: |
| 854 |
0 |
▶ | RETVAL = (SvROK(self) && sv_isa(self, "File::Raw::JSON::Boolean") |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (SvROK(self) && sv_isa(self, "File::Raw::JSON::Boolean") |
0 |
0 |
0 |
| RETVAL = (SvROK(self) && sv_isa(self, "File::Raw::JSON::Boolean") |
0 |
0 |
0 |
|
| 855 |
0 |
▶ | && !SvTRUE(SvRV(self))) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| && !SvTRUE(SvRV(self))) ? 1 : 0; |
0 |
0 |
0 |
| && !SvTRUE(SvRV(self))) ? 1 : 0; |
0 |
0 |
0 |
|
| 856 |
|
| OUTPUT: |
| 857 |
|
| RETVAL |