Skip to content

Commit

Permalink
correctly parse json with comments (#74)
Browse files Browse the repository at this point in the history
* correctly parse json with comments

* comments in header + strict parser config
  • Loading branch information
generall authored Jul 7, 2023
1 parent 61f5aa1 commit cb951c9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@vitejs/plugin-react": "^4.0.0",
"autocomplete-openapi": "^0.1.0",
"axios": "^1.3.4",
"jsonc-parser": "^3.2.0",
"mui-chips-input": "^2.0.1",
"openapi-client-axios": "^7.1.1",
"prop-types": "^15.8.1",
Expand Down
13 changes: 11 additions & 2 deletions src/components/CodeEditorWindow/config/RequesFromCode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import { parse as JsoncParse } from "jsonc-parser";

export function RequestFromCode(text) {
const data = codeParse(text);
Expand Down Expand Up @@ -31,16 +32,24 @@ export function RequestFromCode(text) {

export function codeParse(codeText) {
const codeArray = codeText.split(/\r?\n/);
const headerLine = codeArray.shift();
let headerLine = codeArray.shift();
// Remove possible comments
headerLine = headerLine.replace(/\/\/.*$/gm, "");
const body = codeArray.join("\n");
//Extract the header
const method = headerLine.split(" ")[0];
const endpoint = headerLine.split(" ")[1];

const parserConfig = {
allowTrailingComma: false,
disallowComments: false,
allowEmptyContent: false
};

var reqBody = {};
if (body) {
try {
reqBody = body === "\n" ? {} : JSON.parse(body);
reqBody = body === "\n" ? {} : JsoncParse(body, null, parserConfig);
} catch (e) {
return {
method: null,
Expand Down
2 changes: 1 addition & 1 deletion src/components/EditorCommon/config/Rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function selectBlock(blocks, location) {
}

export function GetCodeBlocks(codeText) {
const codeArray = codeText.replace(/\/\/.*$/gm, "").split(/\r?\n/);
const codeArray = codeText.split(/\r?\n/);
var blocksArray = [];
var block = { blockText: "", blockStartLine: null, blockEndLine: null };
var backetcount = 0;
Expand Down

0 comments on commit cb951c9

Please sign in to comment.