Handle invalid json
- fix style
This commit is contained in:
@@ -74,16 +74,24 @@ function jsonToPropertyPaths(ast: JsonAst, previousPath: Array<string> = []): Ar
|
||||
return children.reduce((a, b) => a.concat(b), [])
|
||||
}
|
||||
|
||||
// Used for testing only
|
||||
export function parseJson(formattedJson: string): Array<JsonPropertyLocation> {
|
||||
return jsonToPropertyPaths((parse(formattedJson) as JsonAst), [])
|
||||
const parsedJson = parse(formattedJson) as JsonAst
|
||||
|
||||
return jsonToPropertyPaths(parsedJson, [])
|
||||
}
|
||||
|
||||
export function literalsMappedByLines(formattedJson: string): Array<JsonPropertyLocation> {
|
||||
const literals = jsonToPropertyPaths((parse(formattedJson) as JsonAst), [])
|
||||
const lines = []
|
||||
for (const literal of literals) {
|
||||
lines[literal.line - 1] = literal
|
||||
export function literalsMappedByLines(formattedJson: string): Array<JsonPropertyLocation> | undefined {
|
||||
try {
|
||||
const parsedJson = parse(formattedJson) as JsonAst
|
||||
const literals = jsonToPropertyPaths(parsedJson, [])
|
||||
const lines = []
|
||||
for (const literal of literals) {
|
||||
lines[literal.line - 1] = literal
|
||||
}
|
||||
|
||||
return lines
|
||||
} catch (error) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import 'mocha'
|
||||
|
||||
import { expect } from 'chai'
|
||||
import { parseJson } from '../JsonAstParser'
|
||||
import 'mocha'
|
||||
|
||||
const dotProp = require('dot-prop')
|
||||
|
||||
describe('access JSON values via dot property paths', () => {
|
||||
it('object with literal', () => {
|
||||
let data = {
|
||||
const data = {
|
||||
foo: 4,
|
||||
}
|
||||
|
||||
let result = parseJson(JSON.stringify(data, undefined, 2))
|
||||
const 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 = {
|
||||
const data = {
|
||||
foo: {
|
||||
bar: 4
|
||||
bar: 4,
|
||||
},
|
||||
}
|
||||
|
||||
let result = parseJson(JSON.stringify(data, undefined, 2))
|
||||
const 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)
|
||||
@@ -30,7 +30,7 @@ describe('access JSON values via dot property paths', () => {
|
||||
})
|
||||
|
||||
it('array path', () => {
|
||||
let data = {
|
||||
const data = {
|
||||
foo: [
|
||||
1,
|
||||
2,
|
||||
@@ -38,11 +38,17 @@ describe('access JSON values via dot property paths', () => {
|
||||
],
|
||||
}
|
||||
|
||||
let result = parseJson(JSON.stringify(data, undefined, 2))
|
||||
const 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)
|
||||
})
|
||||
|
||||
it('should fail parsing invalid json', () => {
|
||||
expect(() => {
|
||||
const result = parseJson("BLE2MQTT-8C48")
|
||||
}).to.throw()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user