|
68 | 68 | - Configuration Files: JSONC is useful for configuration files where comments can provide explanations or instructions. |
69 | 69 | - Data Annotation: JSONC allows developers to annotate JSON data with comments for better understanding and maintenance. |
70 | 70 |
|
| 71 | +## JSONC Validator |
| 72 | + |
| 73 | +Try out JSONC validation directly in your browser: |
| 74 | + |
| 75 | +<div class="jsonc-validator"> |
| 76 | + <textarea id="jsonc-input" placeholder="Enter your JSONC data here... |
| 77 | +{ |
| 78 | + // This is a comment |
| 79 | + "name": "Example", |
| 80 | + "version": "1.0.0" |
| 81 | +}" rows="10"></textarea> |
| 82 | + |
| 83 | + <div class="validator-controls"> |
| 84 | + <button id="validate-btn" onclick="validateJSONC()">Validate JSONC</button> |
| 85 | + <button id="clear-btn" onclick="clearValidator()">Clear</button> |
| 86 | + </div> |
| 87 | + |
| 88 | + <div id="validation-result" class="validation-result"></div> |
| 89 | +</div> |
| 90 | + |
| 91 | +< script src= "https://unpkg.com/[email protected]/lib/umd/main.js"></ script> |
| 92 | +<script> |
| 93 | +function validateJSONC() { |
| 94 | + const input = document.getElementById('jsonc-input'); |
| 95 | + const result = document.getElementById('validation-result'); |
| 96 | + const validateBtn = document.getElementById('validate-btn'); |
| 97 | + |
| 98 | + const jsoncText = input.value.trim(); |
| 99 | + |
| 100 | + if (!jsoncText) { |
| 101 | + result.innerHTML = '<div class="error">Please enter some JSONC data to validate.</div>'; |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + validateBtn.textContent = 'Validating...'; |
| 106 | + validateBtn.disabled = true; |
| 107 | + |
| 108 | + try { |
| 109 | + // Parse the JSONC using microsoft/node-jsonc-parser |
| 110 | + const parseErrors = []; |
| 111 | + const parsed = jsoncParser.parse(jsoncText, parseErrors); |
| 112 | + |
| 113 | + if (parseErrors.length > 0) { |
| 114 | + let errorMessages = parseErrors.map(error => { |
| 115 | + const line = jsoncText.substring(0, error.offset).split('\n').length; |
| 116 | + const column = error.offset - jsoncText.lastIndexOf('\n', error.offset - 1); |
| 117 | + return `Line ${line}, Column ${column}: ${getErrorMessage(error.error)}`; |
| 118 | + }).join('<br>'); |
| 119 | + |
| 120 | + result.innerHTML = `<div class="error"> |
| 121 | + <strong>❌ Invalid JSONC</strong><br> |
| 122 | + ${errorMessages} |
| 123 | + </div>`; |
| 124 | + } else { |
| 125 | + const jsonString = JSON.stringify(parsed, null, 2); |
| 126 | + result.innerHTML = `<div class="success"> |
| 127 | + <strong>✅ Valid JSONC!</strong><br> |
| 128 | + Successfully parsed ${Object.keys(parsed || {}).length} top-level properties. |
| 129 | + <details> |
| 130 | + <summary>Parsed JSON (click to expand)</summary> |
| 131 | + <pre><code>${escapeHtml(jsonString)}</code></pre> |
| 132 | + </details> |
| 133 | + </div>`; |
| 134 | + } |
| 135 | + } catch (error) { |
| 136 | + result.innerHTML = `<div class="error"> |
| 137 | + <strong>❌ Parsing Error</strong><br> |
| 138 | + ${escapeHtml(error.message)} |
| 139 | + </div>`; |
| 140 | + } |
| 141 | + |
| 142 | + validateBtn.textContent = 'Validate JSONC'; |
| 143 | + validateBtn.disabled = false; |
| 144 | +} |
| 145 | + |
| 146 | +function clearValidator() { |
| 147 | + document.getElementById('jsonc-input').value = ''; |
| 148 | + document.getElementById('validation-result').innerHTML = ''; |
| 149 | +} |
| 150 | + |
| 151 | +function getErrorMessage(errorCode) { |
| 152 | + const errorMessages = { |
| 153 | + 1: 'Invalid symbol', |
| 154 | + 2: 'Invalid number', |
| 155 | + 3: 'Invalid string', |
| 156 | + 4: 'Invalid character', |
| 157 | + 5: 'Unexpected end of comment', |
| 158 | + 6: 'Unexpected end of string', |
| 159 | + 7: 'Unexpected end of number', |
| 160 | + 8: 'Invalid character in string escape sequence', |
| 161 | + 9: 'Invalid Unicode escape sequence', |
| 162 | + 10: 'Invalid escape character', |
| 163 | + 11: 'Unexpected end of file', |
| 164 | + 12: 'Property name expected', |
| 165 | + 13: 'Value expected', |
| 166 | + 14: 'Colon expected', |
| 167 | + 15: 'Comma expected', |
| 168 | + 16: 'Closing bracket expected', |
| 169 | + 17: 'Closing brace expected' |
| 170 | + }; |
| 171 | + return errorMessages[errorCode] || `Error code ${errorCode}`; |
| 172 | +} |
| 173 | + |
| 174 | +function escapeHtml(text) { |
| 175 | + const div = document.createElement('div'); |
| 176 | + div.textContent = text; |
| 177 | + return div.innerHTML; |
| 178 | +} |
| 179 | +</script> |
| 180 | + |
71 | 181 | ## Tools and Libraries |
72 | 182 | Several tools and libraries support JSONC, enabling developers to parse and generate JSONC data easily. |
73 | 183 |
|
|
0 commit comments