parse()

in packages/azure-openapi-validator/core/src/jsonParser.ts [14:40]


  parse(text: string) {
    const errors :any[] = []
    const rootNode = parseTree(text, errors, { disallowComments: true })
    if (errors.length || rootNode == undefined) {
      throw new Error("Parser failed with errors:" + JSON.stringify(errors))
    }
    return {
      getLocation: (path: JsonPath) => {
        // JSONPath does not include the root '$', replace number string to number
        const targetPath = [...path]
        if (targetPath.length === 0) {
          return getRange(text, rootNode)
        }
        while (targetPath.length > 0) {
          const root = findNodeAtLocation(rootNode, targetPath)
          if (root) {
            return getRange(text, root)
          }
          targetPath.pop()
        }
        throw new Error("Invalid JSONPath:" + path.join("."))
      },
      getValue: () => {
        return Object.assign({}, getNodeValue(rootNode))
      }
    }
  }