Prepare json literal inpsection
This commit is contained in:
77
backend/src/JsonAstParser.ts
Normal file
77
backend/src/JsonAstParser.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
const parse = require('json-to-ast')
|
||||
|
||||
interface JsonPropertyLocation {
|
||||
path: string
|
||||
line: number
|
||||
column: number
|
||||
}
|
||||
|
||||
type JsonAst = JsonAstLiteral | JsonAstObject | JsonAstArray
|
||||
|
||||
interface JsonAstLocation {
|
||||
start: {
|
||||
line: number
|
||||
column: number
|
||||
offset: number
|
||||
}
|
||||
end: {
|
||||
line: number
|
||||
column: number
|
||||
offset: number
|
||||
}
|
||||
source: null | string
|
||||
}
|
||||
|
||||
interface JsonAstIdentifier {
|
||||
type: 'Identifier'
|
||||
value: string
|
||||
raw: string
|
||||
loc: JsonAstLocation
|
||||
}
|
||||
|
||||
interface JsonAstObjectProperty {
|
||||
type: 'Property'
|
||||
key: JsonAstIdentifier
|
||||
value: JsonAst
|
||||
loc: JsonAstLocation
|
||||
}
|
||||
|
||||
interface JsonAstLiteral {
|
||||
type: 'Literal'
|
||||
value: string | number | boolean | any
|
||||
raw: string
|
||||
loc: JsonAstLocation
|
||||
}
|
||||
|
||||
interface JsonAstObject {
|
||||
type: 'Object'
|
||||
children: Array<JsonAstObjectProperty>
|
||||
loc: JsonAstLocation
|
||||
}
|
||||
|
||||
interface JsonAstArray {
|
||||
type: 'Array'
|
||||
children: Array<JsonAst>
|
||||
loc: JsonAstLocation
|
||||
}
|
||||
|
||||
function jsonToPropertyPaths(ast: JsonAst, previousPath: Array<string> = []): Array<JsonPropertyLocation> {
|
||||
let children: Array<Array<JsonPropertyLocation>> = []
|
||||
if (ast.type === 'Literal') {
|
||||
return [{
|
||||
path: previousPath.join('.'),
|
||||
line: ast.loc.start.line,
|
||||
column: ast.loc.start.column,
|
||||
}]
|
||||
} else if (ast.type === 'Array') {
|
||||
children = ast.children.map((value, idx) => jsonToPropertyPaths(value, previousPath.slice().concat([String(idx)])))
|
||||
} else if (ast.type === 'Object') {
|
||||
children = ast.children.map(property => jsonToPropertyPaths(property.value, previousPath.slice().concat([property.key.value])))
|
||||
}
|
||||
|
||||
return children.reduce((a, b) => a.concat(b), [])
|
||||
}
|
||||
|
||||
export function parseJson(formattedJson: string): Array<JsonPropertyLocation> {
|
||||
return jsonToPropertyPaths((parse(formattedJson) as JsonAst), [])
|
||||
}
|
||||
48
backend/src/spec/JsonAstParser.spec.ts
Normal file
48
backend/src/spec/JsonAstParser.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'mocha'
|
||||
|
||||
import { expect } from 'chai'
|
||||
import { parseJson } from '../JsonAstParser'
|
||||
const dotProp = require('dot-prop')
|
||||
|
||||
describe('access JSON values via dot property paths', () => {
|
||||
it('object with literal', () => {
|
||||
let data = {
|
||||
foo: 4,
|
||||
}
|
||||
|
||||
let result = parseJson(JSON.stringify(data, undefined, 2))
|
||||
expect(result[0].path).to.eq('foo')
|
||||
expect(result[0].line).to.eq(2)
|
||||
})
|
||||
|
||||
it('nested object', () => {
|
||||
let data = {
|
||||
foo: {
|
||||
bar: 4
|
||||
},
|
||||
}
|
||||
|
||||
let result = parseJson(JSON.stringify(data, undefined, 2))
|
||||
expect(result[0].path).to.eq('foo.bar')
|
||||
expect(result[0].line).to.eq(3)
|
||||
expect(dotProp.get(data, result[0].path)).to.eq(4)
|
||||
|
||||
})
|
||||
|
||||
it('array path', () => {
|
||||
let data = {
|
||||
foo: [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
],
|
||||
}
|
||||
|
||||
let result = parseJson(JSON.stringify(data, undefined, 2))
|
||||
expect(result.length).to.eq(3)
|
||||
|
||||
expect(result[2].path).to.eq('foo.2')
|
||||
expect(result[2].line).to.eq(5)
|
||||
expect(dotProp.get(data, result[2].path)).to.eq(3)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user