// To parse this data: // // import { Convert, Position } from "./file"; // // const position = Convert.toPosition(json); // // These functions will throw an error if the JSON doesn't // match the expected interface, even if the JSON is valid. export interface Position { accuracyMeters: number | null; accuracyPercentage: number | null; batteryLevel: number | null; bleType?: null | string; companyId?: null | string; correlationId: string; currentChampion?: null | CurrentChampion; dataSource: DataSource; date: Date; fillingStatus: number | null; gatewayIdentifier: null | string; inMotion?: boolean | null; isStrong?: boolean | null; latitude: number; longitude: number; magnetClosed: boolean | null; numberWakeUps: number | null; phoneId: null | string; reason?: Reason | null; tagId: null | string; tagIdentifier?: null | string; toolId: null | string; type: string; userId: null | string; uuid?: null | string; workingHours: number | null; workingMeasureInProgress: boolean | null; workingMinutes: number | null; } export interface CurrentChampion { accuracyMeters: number | null; accuracyPercentage: number | null; batteryLevel: number | null; bleType?: null | string; correlationId: string; dataSource: DataSource; date: Date; fillingStatus: number | null; gatewayIdentifier: null | string; inMotion?: boolean | null; isStrong?: boolean | null; latitude: number; longitude: number; magnetClosed: boolean | null; numberWakeUps: number | null; phoneId: null | string; reason?: Reason | null; tagId: null | string; tagIdentifier?: null | string; toolId: null | string; type: string; userId: null | string; uuid?: null | string; workingHours: number | null; workingMeasureInProgress: boolean | null; workingMinutes: number | null; [property: string]: any; } export enum DataSource { Btv = "BTV", Computed = "Computed", FullComputedLocation = "Full-ComputedLocation", FullGps = "Full-Gps", Gateway = "Gateway", IET10ComputedLocation = "IET10-ComputedLocation", IET10RC1Gps = "IET10RC1-Gps", IET10RC1HeartBeat33 = "IET10RC1-HeartBeat33", IET10RC1HeartBeat39 = "IET10RC1-HeartBeat39", IET10RC1Magnet = "IET10RC1-Magnet", IET10RC1NoWifiNoGps = "IET10RC1-NoWifi-NoGps", IET10RC1Wifi1Macs = "IET10RC1-Wifi1Macs", IET10RC1Wifi2Macs = "IET10RC1-Wifi2Macs", Manual = "Manual", MobileGateway = "MobileGateway", Phone = "Phone", PhoneSighting = "PhoneSighting", QRCode = "QRCode", } export enum Reason { Movement = "Movement", Periodic = "Periodic", } // Converts JSON strings to/from your types // and asserts the results of JSON.parse at runtime export class Convert { public static toPosition(json: string): Position { return cast(JSON.parse(json), r("Position")); } public static positionToJson(value: Position): string { return JSON.stringify(uncast(value, r("Position")), null, 2); } } function invalidValue(typ: any, val: any, key: any, parent: any = ''): never { const prettyTyp = prettyTypeName(typ); const parentText = parent ? ` on ${parent}` : ''; const keyText = key ? ` for key "${key}"` : ''; throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`); } function prettyTypeName(typ: any): string { if (Array.isArray(typ)) { if (typ.length === 2 && typ[0] === undefined) { return `an optional ${prettyTypeName(typ[1])}`; } else { return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`; } } else if (typeof typ === "object" && typ.literal !== undefined) { return typ.literal; } else { return typeof typ; } } function jsonToJSProps(typ: any): any { if (typ.jsonToJS === undefined) { const map: any = {}; typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ }); typ.jsonToJS = map; } return typ.jsonToJS; } function jsToJSONProps(typ: any): any { if (typ.jsToJSON === undefined) { const map: any = {}; typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ }); typ.jsToJSON = map; } return typ.jsToJSON; } function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any { function transformPrimitive(typ: string, val: any): any { if (typeof typ === typeof val) return val; return invalidValue(typ, val, key, parent); } function transformUnion(typs: any[], val: any): any { // val must validate against one typ in typs const l = typs.length; for (let i = 0; i < l; i++) { const typ = typs[i]; try { return transform(val, typ, getProps); } catch (_) {} } return invalidValue(typs, val, key, parent); } function transformEnum(cases: string[], val: any): any { if (cases.indexOf(val) !== -1) return val; return invalidValue(cases.map(a => { return l(a); }), val, key, parent); } function transformArray(typ: any, val: any): any { // val must be an array with no invalid elements if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent); return val.map(el => transform(el, typ, getProps)); } function transformDate(val: any): any { if (val === null) { return null; } const d = new Date(val); if (isNaN(d.valueOf())) { return invalidValue(l("Date"), val, key, parent); } return d; } function transformObject(props: { [k: string]: any }, additional: any, val: any): any { if (val === null || typeof val !== "object" || Array.isArray(val)) { return invalidValue(l(ref || "object"), val, key, parent); } const result: any = {}; Object.getOwnPropertyNames(props).forEach(key => { const prop = props[key]; const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined; result[prop.key] = transform(v, prop.typ, getProps, key, ref); }); Object.getOwnPropertyNames(val).forEach(key => { if (!Object.prototype.hasOwnProperty.call(props, key)) { result[key] = transform(val[key], additional, getProps, key, ref); } }); return result; } if (typ === "any") return val; if (typ === null) { if (val === null) return val; return invalidValue(typ, val, key, parent); } if (typ === false) return invalidValue(typ, val, key, parent); let ref: any = undefined; while (typeof typ === "object" && typ.ref !== undefined) { ref = typ.ref; typ = typeMap[typ.ref]; } if (Array.isArray(typ)) return transformEnum(typ, val); if (typeof typ === "object") { return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val) : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val) : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val) : invalidValue(typ, val, key, parent); } // Numbers can be parsed by Date but shouldn't be. if (typ === Date && typeof val !== "number") return transformDate(val); return transformPrimitive(typ, val); } function cast(val: any, typ: any): T { return transform(val, typ, jsonToJSProps); } function uncast(val: T, typ: any): any { return transform(val, typ, jsToJSONProps); } function l(typ: any) { return { literal: typ }; } function a(typ: any) { return { arrayItems: typ }; } function u(...typs: any[]) { return { unionMembers: typs }; } function o(props: any[], additional: any) { return { props, additional }; } function m(additional: any) { return { props: [], additional }; } function r(name: string) { return { ref: name }; } const typeMap: any = { "Position": o([ { json: "accuracyMeters", js: "accuracyMeters", typ: u(3.14, null) }, { json: "accuracyPercentage", js: "accuracyPercentage", typ: u(3.14, null) }, { json: "batteryLevel", js: "batteryLevel", typ: u(3.14, null) }, { json: "bleType", js: "bleType", typ: u(undefined, u(null, "")) }, { json: "companyId", js: "companyId", typ: u(undefined, u(null, "")) }, { json: "correlationId", js: "correlationId", typ: "" }, { json: "currentChampion", js: "currentChampion", typ: u(undefined, u(null, r("CurrentChampion"))) }, { json: "dataSource", js: "dataSource", typ: r("DataSource") }, { json: "date", js: "date", typ: Date }, { json: "fillingStatus", js: "fillingStatus", typ: u(3.14, null) }, { json: "gatewayIdentifier", js: "gatewayIdentifier", typ: u(null, "") }, { json: "inMotion", js: "inMotion", typ: u(undefined, u(true, null)) }, { json: "isStrong", js: "isStrong", typ: u(undefined, u(true, null)) }, { json: "latitude", js: "latitude", typ: 3.14 }, { json: "longitude", js: "longitude", typ: 3.14 }, { json: "magnetClosed", js: "magnetClosed", typ: u(true, null) }, { json: "numberWakeUps", js: "numberWakeUps", typ: u(3.14, null) }, { json: "phoneId", js: "phoneId", typ: u(null, "") }, { json: "reason", js: "reason", typ: u(undefined, u(r("Reason"), null)) }, { json: "tagId", js: "tagId", typ: u(null, "") }, { json: "tagIdentifier", js: "tagIdentifier", typ: u(undefined, u(null, "")) }, { json: "toolId", js: "toolId", typ: u(null, "") }, { json: "type", js: "type", typ: "" }, { json: "userId", js: "userId", typ: u(null, "") }, { json: "uuid", js: "uuid", typ: u(undefined, u(null, "")) }, { json: "workingHours", js: "workingHours", typ: u(3.14, null) }, { json: "workingMeasureInProgress", js: "workingMeasureInProgress", typ: u(true, null) }, { json: "workingMinutes", js: "workingMinutes", typ: u(3.14, null) }, ], false), "CurrentChampion": o([ { json: "accuracyMeters", js: "accuracyMeters", typ: u(3.14, null) }, { json: "accuracyPercentage", js: "accuracyPercentage", typ: u(3.14, null) }, { json: "batteryLevel", js: "batteryLevel", typ: u(3.14, null) }, { json: "bleType", js: "bleType", typ: u(undefined, u(null, "")) }, { json: "correlationId", js: "correlationId", typ: "" }, { json: "dataSource", js: "dataSource", typ: r("DataSource") }, { json: "date", js: "date", typ: Date }, { json: "fillingStatus", js: "fillingStatus", typ: u(3.14, null) }, { json: "gatewayIdentifier", js: "gatewayIdentifier", typ: u(null, "") }, { json: "inMotion", js: "inMotion", typ: u(undefined, u(true, null)) }, { json: "isStrong", js: "isStrong", typ: u(undefined, u(true, null)) }, { json: "latitude", js: "latitude", typ: 3.14 }, { json: "longitude", js: "longitude", typ: 3.14 }, { json: "magnetClosed", js: "magnetClosed", typ: u(true, null) }, { json: "numberWakeUps", js: "numberWakeUps", typ: u(3.14, null) }, { json: "phoneId", js: "phoneId", typ: u(null, "") }, { json: "reason", js: "reason", typ: u(undefined, u(r("Reason"), null)) }, { json: "tagId", js: "tagId", typ: u(null, "") }, { json: "tagIdentifier", js: "tagIdentifier", typ: u(undefined, u(null, "")) }, { json: "toolId", js: "toolId", typ: u(null, "") }, { json: "type", js: "type", typ: "" }, { json: "userId", js: "userId", typ: u(null, "") }, { json: "uuid", js: "uuid", typ: u(undefined, u(null, "")) }, { json: "workingHours", js: "workingHours", typ: u(3.14, null) }, { json: "workingMeasureInProgress", js: "workingMeasureInProgress", typ: u(true, null) }, { json: "workingMinutes", js: "workingMinutes", typ: u(3.14, null) }, ], "any"), "DataSource": [ "BTV", "Computed", "Full-ComputedLocation", "Full-Gps", "Gateway", "IET10-ComputedLocation", "IET10RC1-Gps", "IET10RC1-HeartBeat33", "IET10RC1-HeartBeat39", "IET10RC1-Magnet", "IET10RC1-NoWifi-NoGps", "IET10RC1-Wifi1Macs", "IET10RC1-Wifi2Macs", "Manual", "MobileGateway", "Phone", "PhoneSighting", "QRCode", ], "Reason": [ "Movement", "Periodic", ], };