MCPcopy Create free account
hub / github.com/zxlie/FeHelper / parseJSONLike

Function parseJSONLike

apps/json-format/json-auto-utils.js:421–478  ·  view source on GitHub ↗
(source, options)

Source from the content-addressed store, hash-verified

419 }
420
421 function parseJSONLike(source, options) {
422 options = options || {};
423 const meta = unwrapJSONLikeSource(source, options);
424 const candidates = buildJSONLikeCandidates(meta.source, options);
425
426 for (let i = 0; i < candidates.length; i++) {
427 const candidate = candidates[i];
428 let jsonObj = null;
429 const startsAsContainer = /^[\{\[]/.test(candidate);
430 const startsAsEscapedContainer = options.nestedEscapeParse && /^["']/.test(candidate);
431
432 if (!startsAsContainer && !startsAsEscapedContainer) {
433 continue;
434 }
435
436 try {
437 jsonObj = parseWithBigInt(candidate);
438 } catch (_) {
439 try {
440 jsonObj = new Function('return ' + candidate)();
441 } catch (exx) {
442 try {
443 jsonObj = new Function("return '" + candidate + "'")();
444 if (typeof jsonObj === 'string') {
445 try {
446 jsonObj = parseWithBigInt(jsonObj);
447 } catch (_) {
448 jsonObj = new Function('return ' + jsonObj)();
449 }
450 }
451 } catch (_) {
452 continue;
453 }
454 }
455 }
456
457 if (options.nestedEscapeParse && typeof jsonObj === 'string') {
458 jsonObj = unpackTopLevelEscapedJSON(jsonObj);
459 }
460 if (options.nestedEscapeParse && isJSONContainer(jsonObj)) {
461 jsonObj = deepParseJSONStrings(jsonObj);
462 }
463 if (!isJSONContainer(jsonObj)) {
464 continue;
465 }
466
467 try {
468 meta.source = candidate;
469 meta.normalizedSource = safeStringify(jsonObj);
470 meta.value = jsonObj;
471 return meta;
472 } catch (_) {
473 return null;
474 }
475 }
476
477 return null;
478 }

Callers 1

coerceDecodedJSONSourceFunction · 0.85

Calls 7

unwrapJSONLikeSourceFunction · 0.85
buildJSONLikeCandidatesFunction · 0.85
isJSONContainerFunction · 0.85
parseWithBigIntFunction · 0.70
deepParseJSONStringsFunction · 0.70
safeStringifyFunction · 0.70

Tested by

no test coverage detected