import * as jsonc from 'jsonc-parser' ; import { localize } from '../localize' ; /**
 * Has extra logic to remove a BOM character if it exists and handle comments
 */ export function parseJson < T extends object > ( data ) { if ( data . charCodeAt ( 0 ) === 0xFEFF ) { data = data . slice ( 1 ) ; } const errors = [ ] ; const result = < T > jsonc . parse ( data , errors , { allowTrailingComma : true } ) ; if ( errors . length > 0 ) { const [ line , column ] = getLineAndColumnFromOffset ( data , errors [ 0 ] . offset ) ; throw new Error ( localize ( 'jsonParseError' , '{0} near line "{1}", column "{2}"' , jsonc . printParseErrorCode ( errors [ 0 ] . error ) , line , column ) ) ; } else { return result ; } } export function getLineAndColumnFromOffset ( data , offset ) { const lines = data . split ( '\n' ) ; let charCount = 0 ; let lineCount = 0 ; let column = 0 ; for ( const line of lines ) { lineCount += 1 ; const lineLength = line . length + 1 ; charCount += lineLength ; if ( charCount >= offset ) { column = offset - ( charCount - lineLength ) ; break ; } } return [ lineCount , column ] ; }