-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfromXML2JSON.ts
44 lines (41 loc) · 1.08 KB
/
fromXML2JSON.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import xml2js from 'xml2js'
import type { LwM2MType } from './resourceType.js'
/**
* JSON representation of an XML object
*/
type jsonObject = {
Name: string[]
Description1: string[]
ObjectID: string[]
ObjectURN: string[]
LWM2MVersion: string[]
ObjectVersion: string[]
MultipleInstances: ['Single'] | ['Multiple']
Mandatory: ['Optional'] | ['Mandatory']
Resources: [{ Item: Resource[] }]
Description2: string[]
}
type Resource = {
$: { ID: string }
Name: string[]
Operations: string[]
MultipleInstances: ['Single'] | ['Multiple']
Mandatory: ['Optional'] | ['Mandatory']
Type: LwM2MType[]
RangeEnumeration: string[]
Units: string[]
Description: string[]
}
/**
* From XML to JSON
*/
export const fromXML2JSON = async (id: number): Promise<jsonObject> => {
const baseDir = process.cwd()
const subDir = (...tree: string[]): string => path.join(baseDir, ...tree)
const jsonObject = await xml2js.parseStringPromise(
await readFile(subDir('lwm2m', `${id}.xml`), 'utf-8'),
)
return jsonObject.LWM2M.Object[0]
}