Skip to content

Instantly share code, notes, and snippets.

@Leopotam
Created September 14, 2017 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leopotam/6216f793982f39571546a606a3a68ea5 to your computer and use it in GitHub Desktop.
Save Leopotam/6216f793982f39571546a606a3a68ea5 to your computer and use it in GitHub Desktop.
Json parsing with single- / multiline comments support.
const commentsRegexp = /\/(\/[^\n]*|\*(?:.|\n)*?\*\/)/g
/**
* Converts a JavaScript Object Notation (JSON) string into an object.
* @param text A valid JSON string.
* @param reviver A function that transforms the results. This function is called for each member of the object.
* If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
export const parse = (text: string, reviver?: (key: any, value: any) => any): any => {
return JSON.parse(text.replace(commentsRegexp, ''), reviver)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment