(file: any, options?: JsonInputOptionsBrowser)
| 48 | * ``` |
| 49 | */ |
| 50 | const $readJSON = async (file: any, options?: JsonInputOptionsBrowser) => { |
| 51 | const { method, headers, frameConfig } = { method: "GET", headers: {}, frameConfig: {}, ...options } |
| 52 | |
| 53 | if (typeof file === "string" && file.startsWith("http")) { |
| 54 | |
| 55 | return new Promise(resolve => { |
| 56 | fetch(file, { method, headers }).then(response => { |
| 57 | if (response.status !== 200) { |
| 58 | throw new Error(`Failed to load ${file}`) |
| 59 | } |
| 60 | response.json().then(json => { |
| 61 | resolve(new DataFrame(json, frameConfig)); |
| 62 | }); |
| 63 | }).catch((err) => { |
| 64 | throw new Error(err) |
| 65 | }) |
| 66 | }) |
| 67 | |
| 68 | } else if (file instanceof File) { |
| 69 | return new Promise(resolve => { |
| 70 | const reader = new FileReader(); |
| 71 | reader.readAsText(file); |
| 72 | reader.onload = (event) => { |
| 73 | const jsonObj = JSON.parse(event?.target?.result as string); |
| 74 | resolve(new DataFrame(jsonObj, frameConfig)); |
| 75 | } |
| 76 | }) |
| 77 | } else { |
| 78 | throw new Error("ParamError: File not supported. file must be a url or an input File object") |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | /** |
nothing calls this directly
no test coverage detected