UNPKG

typesxml

Version:

Open source XML library written in TypeScript

138 lines (137 loc) 7.2 kB
/******************************************************************************* * Copyright (c) 2023-2026 Maxprograms. * * This program and the accompanying materials * are made available under the terms of the Eclipse License 1.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/org/documents/epl-v10.html * * Contributors: * Maxprograms - initial API and implementation *******************************************************************************/ import { Readable } from "node:stream"; import { ParseSourceOptions, StreamParseOptions } from "../SAXParser.js"; import { XMLDocument } from "../XMLDocument.js"; export type JsonPrimitive = string | number | boolean | null; export type JsonValue = JsonPrimitive | JsonElementObject | Array<JsonValue>; export interface JsonElementObject { _attributes?: Record<string, string | number | boolean | null>; _text?: string; _cdata?: string | Array<string>; _comments?: string | Array<string>; _processingInstructions?: Array<JsonProcessingInstruction>; _content?: Array<JsonElementContentNode>; [childName: string]: JsonValue | Array<JsonValue> | string | Array<string> | Array<JsonProcessingInstruction> | Record<string, string | number | boolean | null> | Array<JsonElementContentNode> | undefined; } export interface JsonProcessingInstruction { target: string; data?: string; } export interface JsonElementContentTextNode { kind: "text"; value: string; } export interface JsonElementContentCDataNode { kind: "cdata"; value: string; } export interface JsonElementContentCommentNode { kind: "comment"; value: string; } export interface JsonElementContentProcessingInstructionNode { kind: "processingInstruction"; target: string; data?: string; } export interface JsonElementContentElementNode { kind: "element"; name: string; occurrence: number; } export type JsonElementContentNode = JsonElementContentTextNode | JsonElementContentCDataNode | JsonElementContentCommentNode | JsonElementContentProcessingInstructionNode | JsonElementContentElementNode; export interface XmlJsonDeclaration { version?: string; encoding?: string; standalone?: string; } export interface XmlJsonDoctype { name: string; publicId?: string; systemId?: string; internalSubset?: string; } export interface JsonCommentNode { type: "comment"; value: string; } export interface JsonProcessingInstructionNode { type: "processingInstruction"; target: string; data?: string; } export interface JsonTextNode { type: "text"; value: string; } export type JsonMiscNode = JsonCommentNode | JsonProcessingInstructionNode | JsonTextNode; export type JsonPrologNode = JsonMiscNode & { afterDoctype?: boolean; }; export interface XmlJsonDocument { rootName: string; root: JsonValue; declaration?: XmlJsonDeclaration; doctype?: XmlJsonDoctype; prolog?: Array<JsonPrologNode>; epilog?: Array<JsonMiscNode>; } export type JsonConversionMode = "simple" | "roundtrip"; export interface XmlDocumentToJsonSimpleOptions { mode?: "simple"; } export interface XmlDocumentToJsonRoundTripOptions { mode: "roundtrip"; } export type XmlDocumentToJsonOptions = XmlDocumentToJsonSimpleOptions | XmlDocumentToJsonRoundTripOptions; export interface XmlToJsonSimpleOptions extends ParseSourceOptions { mode?: "simple"; } export interface XmlToJsonRoundTripOptions extends ParseSourceOptions { mode: "roundtrip"; } export type XmlToJsonOptions = XmlToJsonSimpleOptions | XmlToJsonRoundTripOptions; export interface XmlFileToJsonSimpleOptions extends XmlDocumentToJsonSimpleOptions { encoding?: BufferEncoding; } export interface XmlFileToJsonRoundTripOptions extends XmlDocumentToJsonRoundTripOptions { encoding?: BufferEncoding; } export type XmlFileToJsonOptions = XmlFileToJsonSimpleOptions | XmlFileToJsonRoundTripOptions; export interface XmlStreamToJsonSimpleOptions extends StreamParseOptions { mode?: "simple"; } export interface XmlStreamToJsonRoundTripOptions extends StreamParseOptions { mode: "roundtrip"; } export type XmlStreamToJsonOptions = XmlStreamToJsonSimpleOptions | XmlStreamToJsonRoundTripOptions; export declare function xmlStringToJsonObject(xml: string, options?: XmlToJsonSimpleOptions): JsonValue; export declare function xmlStringToJsonObject(xml: string, options: XmlToJsonRoundTripOptions): XmlJsonDocument; export declare function xmlFileToJsonObject(path: string, options?: XmlFileToJsonSimpleOptions): Promise<JsonValue>; export declare function xmlFileToJsonObject(path: string, options: XmlFileToJsonRoundTripOptions): Promise<XmlJsonDocument>; export declare function xmlStreamToJsonObject(stream: Readable, options?: XmlStreamToJsonSimpleOptions): Promise<JsonValue>; export declare function xmlStreamToJsonObject(stream: Readable, options: XmlStreamToJsonRoundTripOptions): Promise<XmlJsonDocument>; export declare function xmlDocumentToJsonObject(document: XMLDocument, options?: XmlDocumentToJsonSimpleOptions): JsonValue; export declare function xmlDocumentToJsonObject(document: XMLDocument, options: XmlDocumentToJsonRoundTripOptions): XmlJsonDocument; export declare function xmlStringToJsonFile(xml: string, targetPath: string, options?: XmlToJsonOptions, indent?: number | string, encoding?: BufferEncoding): Promise<void>; export declare function xmlFileToJsonFile(path: string, targetPath: string, xmlEncoding?: BufferEncoding, indent?: number | string, jsonEncoding?: BufferEncoding, options?: XmlDocumentToJsonOptions): Promise<void>; export declare function xmlStreamToJsonFile(stream: Readable, targetPath: string, options?: XmlStreamToJsonOptions, indent?: number | string, encoding?: BufferEncoding): Promise<void>; export declare function xmlDocumentToJsonFile(document: XMLDocument, targetPath: string, indent?: number | string, encoding?: BufferEncoding, options?: XmlDocumentToJsonOptions): Promise<void>; export declare function jsonObjectToXmlDocument(json: JsonValue | XmlJsonDocument, rootElementName?: string): XMLDocument; export declare function jsonStringToXmlDocument(jsonText: string, rootElementName?: string): XMLDocument; export declare function jsonFileToXmlDocument(path: string, rootElementName?: string, encoding?: BufferEncoding): Promise<XMLDocument>; export declare function jsonStreamToXmlDocument(stream: Readable, rootElementName?: string, encoding?: BufferEncoding): Promise<XMLDocument>; export declare function jsonObjectToXmlFile(json: JsonValue | XmlJsonDocument, targetPath: string, rootElementName?: string, encoding?: BufferEncoding): Promise<void>; export declare function jsonStringToXmlFile(jsonText: string, targetPath: string, rootElementName?: string, encoding?: BufferEncoding): Promise<void>; export declare function jsonFileToXmlFile(path: string, targetPath: string, rootElementName?: string, jsonEncoding?: BufferEncoding, xmlEncoding?: BufferEncoding): Promise<void>; export declare function jsonStreamToXmlFile(stream: Readable, targetPath: string, rootElementName?: string, jsonEncoding?: BufferEncoding, xmlEncoding?: BufferEncoding): Promise<void>;