mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Notebooks: Use much faster json parser when loading notebook initially (#6552)
* Use much faster json parser * fallback to old json parser
This commit is contained in:
@@ -25,8 +25,12 @@ export class LocalContentManager implements nb.ContentManager {
|
|||||||
constructor(@IFileService private readonly fileService: IFileService) { }
|
constructor(@IFileService private readonly fileService: IFileService) { }
|
||||||
|
|
||||||
public async loadFromContentString(contentString: string): Promise<nb.INotebookContents> {
|
public async loadFromContentString(contentString: string): Promise<nb.INotebookContents> {
|
||||||
let contents: JSONObject = json.parse(contentString);
|
let contents: JSONObject;
|
||||||
|
if (contentString === '' || contentString === undefined) {
|
||||||
|
return v4.createEmptyNotebook();
|
||||||
|
} else {
|
||||||
|
contents = this.parseFromJson(contentString);
|
||||||
|
}
|
||||||
if (contents) {
|
if (contents) {
|
||||||
if (contents.nbformat === 4) {
|
if (contents.nbformat === 4) {
|
||||||
return v4.readNotebook(<any>contents);
|
return v4.readNotebook(<any>contents);
|
||||||
@@ -36,9 +40,6 @@ export class LocalContentManager implements nb.ContentManager {
|
|||||||
if (contents.nbformat) {
|
if (contents.nbformat) {
|
||||||
throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any));
|
throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any));
|
||||||
}
|
}
|
||||||
} else if (contentString === '' || contentString === undefined) {
|
|
||||||
// Empty?
|
|
||||||
return v4.createEmptyNotebook();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// else, fallthrough condition
|
// else, fallthrough condition
|
||||||
@@ -53,7 +54,13 @@ export class LocalContentManager implements nb.ContentManager {
|
|||||||
// Note: intentionally letting caller handle exceptions
|
// Note: intentionally letting caller handle exceptions
|
||||||
let notebookFileBuffer = await this.fileService.readFile(notebookUri);
|
let notebookFileBuffer = await this.fileService.readFile(notebookUri);
|
||||||
let stringContents = notebookFileBuffer.value.toString();
|
let stringContents = notebookFileBuffer.value.toString();
|
||||||
let contents: JSONObject = json.parse(stringContents);
|
let contents: JSONObject;
|
||||||
|
if (stringContents === '' || stringContents === undefined) {
|
||||||
|
// Empty?
|
||||||
|
return v4.createEmptyNotebook();
|
||||||
|
} else {
|
||||||
|
contents = this.parseFromJson(stringContents);
|
||||||
|
}
|
||||||
|
|
||||||
if (contents) {
|
if (contents) {
|
||||||
if (contents.nbformat === 4) {
|
if (contents.nbformat === 4) {
|
||||||
@@ -64,9 +71,6 @@ export class LocalContentManager implements nb.ContentManager {
|
|||||||
if (contents.nbformat) {
|
if (contents.nbformat) {
|
||||||
throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any));
|
throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any));
|
||||||
}
|
}
|
||||||
} else if (stringContents === '' || stringContents === undefined) {
|
|
||||||
// Empty?
|
|
||||||
return v4.createEmptyNotebook();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// else, fallthrough condition
|
// else, fallthrough condition
|
||||||
@@ -81,6 +85,15 @@ export class LocalContentManager implements nb.ContentManager {
|
|||||||
return notebook;
|
return notebook;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private parseFromJson(contentString: string): JSONObject {
|
||||||
|
let contents: JSONObject;
|
||||||
|
try {
|
||||||
|
contents = JSON.parse(contentString);
|
||||||
|
} catch {
|
||||||
|
contents = json.parse(contentString);
|
||||||
|
}
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace v4 {
|
namespace v4 {
|
||||||
|
|||||||
Reference in New Issue
Block a user