• 2 Posts
  • 3 Comments
Joined 3 months ago
cake
Cake day: December 29th, 2025

help-circle


  • i noticed that this morning, it’s add that glyph or a zero length character. breaks a lot of things. i had to update the JSON parser i use to try and accommodate those additions. works, some of the time.

    parseJSON: (input) => { if (!input) return null;

            // 1. OBJECT HANDLING (Already parsed)
            if (typeof input === 'object') {
                if (typeof input.text === 'string') return Utils.parseJSON(input.text);
                if (typeof input.generatedText === 'string') return Utils.parseJSON(input.generatedText);
                return input;
            }
    
            // 2. STRING HANDLING (The Toughened Shield)
            if (typeof input === 'string') {
                let clean = input.trim();
                
                // A. Strip Markdown Code Blocks
                clean = clean.replace(/```json/gi, "").replace(/```/g, "");
                
                // B. REINFORCED EXTRACTION
                // We find the FIRST occurrence of an opening bracket/brace
                // and the LAST occurrence of the matching closing bracket/brace.
                const firstObj = clean.indexOf('{');
                const firstArr = clean.indexOf('[');
                
                let firstOpen = -1;
                let lastClose = -1;
    
                if (firstObj !== -1 && (firstArr === -1 || firstObj < firstArr)) {
                    firstOpen = firstObj;
                    lastClose = clean.lastIndexOf('}');
                } else if (firstArr !== -1) {
                    firstOpen = firstArr;
                    lastClose = clean.lastIndexOf(']');
                }
                
                if (firstOpen !== -1 && lastClose !== -1 && lastClose > firstOpen) {
                    // C. HARD TRUNCATION
                    // We extract ONLY what is between the brackets.
                    // This kills the trailing "〖" and any other AI rambling.
                    clean = clean.substring(firstOpen, lastClose + 1);
    
                    // D. Post-Extraction Cleanup
                    clean = clean.replace(/,(\s*[}\]])/g, '$1'); // Remove trailing commas
                    
                    try {
                        return JSON.parse(clean);
                    } catch (e) {
                        // LAST DITCH: If it still fails, it's usually unescaped newlines in a string
                        try {
                            // Replace literal newlines inside JSON strings with escaped \n
                            const escaped = clean.replace(/\n/g, "\\n").replace(/\r/g, "\\r");
                            return JSON.parse(escaped);
                        } catch (e2) {
                            console.warn("[Utils] JSON Parse Failed despite extraction:", e.message);
                        }
                    }
                }
            }
            
            console.warn("[Utils] JSON Parse Total Failure. Input sample:", input.substring(0, 100));
            return null;
        }