node-opcua-json 0.50.0 → 2.182.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +168 -4
- package/dist/104/index.d.ts +56 -0
- package/dist/104/index.js +93 -0
- package/dist/104/index.js.map +1 -0
- package/dist/105/index.d.ts +55 -0
- package/dist/105/index.js +95 -0
- package/dist/105/index.js.map +1 -0
- package/dist/decode_opcua_json.d.ts +31 -0
- package/dist/decode_opcua_json.js +61 -0
- package/dist/decode_opcua_json.js.map +1 -0
- package/dist/extension_object_constructor.d.ts +46 -0
- package/dist/extension_object_constructor.js +26 -0
- package/dist/extension_object_constructor.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +100 -0
- package/dist/index.js.map +1 -0
- package/dist/json_basic_encoding_body_functor.d.ts +34 -0
- package/dist/json_basic_encoding_body_functor.js +170 -0
- package/dist/json_basic_encoding_body_functor.js.map +1 -0
- package/dist/json_basic_encoding_decoding_byte_string.d.ts +27 -0
- package/dist/json_basic_encoding_decoding_byte_string.js +27 -0
- package/dist/json_basic_encoding_decoding_byte_string.js.map +1 -0
- package/dist/json_basic_encoding_decoding_data_value.d.ts +81 -0
- package/dist/json_basic_encoding_decoding_data_value.js +290 -0
- package/dist/json_basic_encoding_decoding_data_value.js.map +1 -0
- package/dist/json_basic_encoding_decoding_date_time.d.ts +26 -0
- package/dist/json_basic_encoding_decoding_date_time.js +43 -0
- package/dist/json_basic_encoding_decoding_date_time.js.map +1 -0
- package/dist/json_basic_encoding_decoding_extension_object.d.ts +65 -0
- package/dist/json_basic_encoding_decoding_extension_object.js +461 -0
- package/dist/json_basic_encoding_decoding_extension_object.js.map +1 -0
- package/dist/json_basic_encoding_decoding_int64.d.ts +30 -0
- package/dist/json_basic_encoding_decoding_int64.js +56 -0
- package/dist/json_basic_encoding_decoding_int64.js.map +1 -0
- package/dist/json_basic_encoding_decoding_localizedtext.d.ts +33 -0
- package/dist/json_basic_encoding_decoding_localizedtext.js +62 -0
- package/dist/json_basic_encoding_decoding_localizedtext.js.map +1 -0
- package/dist/json_basic_encoding_decoding_nodeid.d.ts +73 -0
- package/dist/json_basic_encoding_decoding_nodeid.js +314 -0
- package/dist/json_basic_encoding_decoding_nodeid.js.map +1 -0
- package/dist/json_basic_encoding_decoding_qualifiedname.d.ts +35 -0
- package/dist/json_basic_encoding_decoding_qualifiedname.js +126 -0
- package/dist/json_basic_encoding_decoding_qualifiedname.js.map +1 -0
- package/dist/json_basic_encoding_decoding_statuscode.d.ts +39 -0
- package/dist/json_basic_encoding_decoding_statuscode.js +139 -0
- package/dist/json_basic_encoding_decoding_statuscode.js.map +1 -0
- package/dist/json_basic_encoding_decoding_variant.d.ts +101 -0
- package/dist/json_basic_encoding_decoding_variant.js +234 -0
- package/dist/json_basic_encoding_decoding_variant.js.map +1 -0
- package/dist/json_encoding_scheme.d.ts +50 -0
- package/dist/json_encoding_scheme.js +117 -0
- package/dist/json_encoding_scheme.js.map +1 -0
- package/package.json +90 -35
- package/source/104/index.ts +117 -0
- package/source/105/index.ts +118 -0
- package/source/decode_opcua_json.ts +67 -0
- package/source/extension_object_constructor.ts +48 -0
- package/source/index.ts +71 -0
- package/source/json_basic_encoding_body_functor.ts +190 -0
- package/source/json_basic_encoding_decoding_byte_string.ts +52 -0
- package/source/json_basic_encoding_decoding_data_value.ts +392 -0
- package/source/json_basic_encoding_decoding_date_time.ts +42 -0
- package/source/json_basic_encoding_decoding_extension_object.ts +531 -0
- package/source/json_basic_encoding_decoding_int64.ts +74 -0
- package/source/json_basic_encoding_decoding_localizedtext.ts +69 -0
- package/source/json_basic_encoding_decoding_nodeid.ts +375 -0
- package/source/json_basic_encoding_decoding_qualifiedname.ts +144 -0
- package/source/json_basic_encoding_decoding_statuscode.ts +151 -0
- package/source/json_basic_encoding_decoding_variant.ts +339 -0
- package/source/json_encoding_scheme.ts +114 -0
- package/LICENSE.md +0 -40
- package/dist2/decode_opcua_json.d.ts +0 -12
- package/dist2/index.d.ts +0 -17
- package/dist2/index.js +0 -1
- package/dist2/json_basic_encoding_body_functor.d.ts +0 -14
- package/dist2/json_basic_encoding_decoding_byte_string.d.ts +0 -8
- package/dist2/json_basic_encoding_decoding_data_value.d.ts +0 -26
- package/dist2/json_basic_encoding_decoding_date_time.d.ts +0 -6
- package/dist2/json_basic_encoding_decoding_extension_object.d.ts +0 -32
- package/dist2/json_basic_encoding_decoding_int64.d.ts +0 -9
- package/dist2/json_basic_encoding_decoding_localizedtext.d.ts +0 -14
- package/dist2/json_basic_encoding_decoding_nodeid.d.ts +0 -33
- package/dist2/json_basic_encoding_decoding_qualifiedname.d.ts +0 -14
- package/dist2/json_basic_encoding_decoding_statuscode.d.ts +0 -16
- package/dist2/json_basic_encoding_decoding_variant.d.ts +0 -0
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ==========================================================================
|
|
3
|
+
* Copyright (c) 2021-2026 Sterfive - etienne.rossignon@sterfive.com
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
|
13
|
+
* all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
18
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
* IN THE SOFTWARE.
|
|
22
|
+
* ==========================================================================
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { UAString } from "node-opcua-basic-types";
|
|
26
|
+
import type { DiagnosticInfoOptions, LocalizedTextOptions, QualifiedNameOptions } from "node-opcua-data-model";
|
|
27
|
+
import { DataValue, type DataValueOptions } from "node-opcua-data-value";
|
|
28
|
+
import { make_errorLog } from "node-opcua-debug";
|
|
29
|
+
import { ExtensionObject } from "node-opcua-extension-object";
|
|
30
|
+
import {
|
|
31
|
+
BasicTypeSchema,
|
|
32
|
+
type FieldType,
|
|
33
|
+
getStandardDataTypeFactory,
|
|
34
|
+
type IStructuredTypeSchema,
|
|
35
|
+
type StructuredTypeSchema
|
|
36
|
+
} from "node-opcua-factory";
|
|
37
|
+
import type { NodeId } from "node-opcua-nodeid";
|
|
38
|
+
import { NumericRange } from "node-opcua-numeric-range";
|
|
39
|
+
import { DataType, type VariantOptions } from "node-opcua-variant";
|
|
40
|
+
import type { Pojo } from "./decode_opcua_json.js";
|
|
41
|
+
import {
|
|
42
|
+
bodyDecodeFunctor,
|
|
43
|
+
bodyEncodeFunctor,
|
|
44
|
+
type DecoderFunc,
|
|
45
|
+
type EncoderFunc,
|
|
46
|
+
type ExtensionObjectBuilder
|
|
47
|
+
} from "./json_basic_encoding_body_functor.js";
|
|
48
|
+
import { type NodeIdJSON, opcuaJsonDecodeNodeId, opcuaJsonEncodeNodeId } from "./json_basic_encoding_decoding_nodeid.js";
|
|
49
|
+
import { JsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
50
|
+
|
|
51
|
+
const errorLog = make_errorLog("JSON");
|
|
52
|
+
|
|
53
|
+
export interface ExtensionObjectJSON104 {
|
|
54
|
+
TypeId: NodeIdJSON;
|
|
55
|
+
Body: Pojo;
|
|
56
|
+
UaTypeId?: never;
|
|
57
|
+
UaBody?: never;
|
|
58
|
+
}
|
|
59
|
+
export interface ExtensionObjectJSON105 {
|
|
60
|
+
UaTypeId: NodeIdJSON;
|
|
61
|
+
UaBody: Pojo;
|
|
62
|
+
TypeId?: never;
|
|
63
|
+
Body?: never;
|
|
64
|
+
}
|
|
65
|
+
export type ExtensionObjectJSON = ExtensionObjectJSON104 | ExtensionObjectJSON105;
|
|
66
|
+
|
|
67
|
+
const g_DataType = DataType as unknown as Record<string | number, DataType>;
|
|
68
|
+
function toDataType(field: FieldType): DataType {
|
|
69
|
+
if (field.category === "enumeration") {
|
|
70
|
+
return DataType.Int32; // enumeration is always Int32
|
|
71
|
+
}
|
|
72
|
+
let dataType: DataType | undefined = g_DataType[field.fieldType];
|
|
73
|
+
if (dataType === undefined) {
|
|
74
|
+
dataType = field.basicDataType as DataType | undefined;
|
|
75
|
+
if (dataType === undefined) {
|
|
76
|
+
if (field.schema instanceof BasicTypeSchema) {
|
|
77
|
+
dataType = g_DataType[field.schema.subType] || g_DataType[field.schema.name];
|
|
78
|
+
} else {
|
|
79
|
+
dataType = g_DataType[field.schema.name];
|
|
80
|
+
if (!dataType) {
|
|
81
|
+
g_DataType[(field.schema as unknown as { subType: string }).subType];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return dataType;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function encodeNumericRange(numericRange: NumericRange, _scheme: JsonEncodingScheme): string | null | undefined {
|
|
90
|
+
return numericRange.toEncodeableString();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function findEncoder(field: FieldType): EncoderFunc<unknown> | null {
|
|
94
|
+
const dataType: DataType = toDataType(field);
|
|
95
|
+
|
|
96
|
+
if (dataType === DataType.String) {
|
|
97
|
+
// like basic : NumericRange
|
|
98
|
+
if (field.fieldType === "NumericRange") {
|
|
99
|
+
return encodeNumericRange as EncoderFunc<unknown>;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!dataType) {
|
|
103
|
+
if (field.fieldType === "UAString") {
|
|
104
|
+
return ((str: UAString, _scheme: JsonEncodingScheme, _namespaceArray?: string[]) => {
|
|
105
|
+
return str;
|
|
106
|
+
}) as EncoderFunc<unknown>;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const encoderFunc = (() => {
|
|
110
|
+
switch (field.category) {
|
|
111
|
+
case "complex":
|
|
112
|
+
if (field.fieldType === "ExtensionObject") {
|
|
113
|
+
return opcuaJsonEncodeExtensionObject;
|
|
114
|
+
} else {
|
|
115
|
+
return opcuaJsonEncodeExtensionObjectBody;
|
|
116
|
+
}
|
|
117
|
+
case "basic":
|
|
118
|
+
return bodyEncodeFunctor(dataType);
|
|
119
|
+
default:
|
|
120
|
+
return (a: number) => a;
|
|
121
|
+
}
|
|
122
|
+
})();
|
|
123
|
+
|
|
124
|
+
// istanbul ignore next
|
|
125
|
+
if (!encoderFunc) {
|
|
126
|
+
errorLog("findEncoder: Cannot find encoderFunc for ", DataType[dataType], field);
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
return encoderFunc as EncoderFunc<unknown>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function findDecoder(field: FieldType): DecoderFunc<unknown> | null {
|
|
133
|
+
const dataType = toDataType(field);
|
|
134
|
+
if (dataType === DataType.String) {
|
|
135
|
+
// like basic : NumericRange
|
|
136
|
+
if (field.fieldType === "NumericRange") {
|
|
137
|
+
return ((numericRange: string) => {
|
|
138
|
+
return new NumericRange(numericRange);
|
|
139
|
+
}) as DecoderFunc<unknown>;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (!dataType) {
|
|
143
|
+
if (field.fieldType === "UAString") {
|
|
144
|
+
return ((str: UAString) => {
|
|
145
|
+
return str;
|
|
146
|
+
}) as DecoderFunc<unknown>;
|
|
147
|
+
}
|
|
148
|
+
// dataType = toDataType((field.schema as unknown as { subType: string }).subType);
|
|
149
|
+
}
|
|
150
|
+
const decoderFunc = (() => {
|
|
151
|
+
switch (field.category) {
|
|
152
|
+
case "complex":
|
|
153
|
+
if (field.fieldType === "ExtensionObject") {
|
|
154
|
+
return opcuaJsonDecodeExtensionObject;
|
|
155
|
+
} else {
|
|
156
|
+
if (field.schema) {
|
|
157
|
+
return opcuaJsonDecodeExtensionObjectBody.bind(null, field.schema as StructuredTypeSchema);
|
|
158
|
+
} else {
|
|
159
|
+
const factory = getStandardDataTypeFactory();
|
|
160
|
+
const schema = factory.getStructuredTypeSchema(field.fieldType);
|
|
161
|
+
return opcuaJsonDecodeExtensionObjectBody.bind(null, schema);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
case "basic":
|
|
165
|
+
return bodyDecodeFunctor(dataType);
|
|
166
|
+
default:
|
|
167
|
+
return (a: number) => a;
|
|
168
|
+
}
|
|
169
|
+
})();
|
|
170
|
+
// istanbul ignore next
|
|
171
|
+
if (!decoderFunc) {
|
|
172
|
+
errorLog("findEncoder: Cannot find decoderFunc for ", DataType[dataType], field);
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
return decoderFunc as DecoderFunc<unknown>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function getAllExtensionObjectFields1(schema: IStructuredTypeSchema): FieldType[] {
|
|
179
|
+
const baseSchema = schema.getBaseSchema();
|
|
180
|
+
const baseFields: FieldType[] = baseSchema ? getAllExtensionObjectFields1(baseSchema) : ([] as FieldType[]);
|
|
181
|
+
return [...baseFields, ...schema.fields];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function getAllExtensionObjectFields(extensionObject: ExtensionObject) {
|
|
185
|
+
return getAllExtensionObjectFields1(extensionObject.schema);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface ExtensionObjectWithOptionalSwitchField extends ExtensionObject {
|
|
189
|
+
switchField?: number;
|
|
190
|
+
}
|
|
191
|
+
export function makeBody(extensionObject: ExtensionObject, scheme: JsonEncodingScheme): Pojo | undefined {
|
|
192
|
+
const switchField = (extensionObject as ExtensionObjectWithOptionalSwitchField).switchField;
|
|
193
|
+
const isUnion = switchField !== undefined;
|
|
194
|
+
|
|
195
|
+
const pojo: Pojo = {};
|
|
196
|
+
if (isUnion) {
|
|
197
|
+
pojo.SwitchField = switchField;
|
|
198
|
+
}
|
|
199
|
+
const fields = getAllExtensionObjectFields(extensionObject);
|
|
200
|
+
|
|
201
|
+
let allAreDefault = true;
|
|
202
|
+
|
|
203
|
+
for (const field of fields) {
|
|
204
|
+
const value = (extensionObject as unknown as Record<string, unknown>)[field.name];
|
|
205
|
+
const encoder = findEncoder(field);
|
|
206
|
+
|
|
207
|
+
const fieldName = isUnion ? "Value" : field.originalName;
|
|
208
|
+
|
|
209
|
+
// istanbul ignore next
|
|
210
|
+
if (!encoder) {
|
|
211
|
+
errorLog("cannot find encoder for field", field);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (field.isArray) {
|
|
215
|
+
if (!value) {
|
|
216
|
+
// xx pojo[name] = null;
|
|
217
|
+
} else if (typeof value === "object" && value && (value as { length?: number }).length === 0) {
|
|
218
|
+
if (scheme === JsonEncodingScheme.Compact) {
|
|
219
|
+
// encoding empty array as undefined in compact mode
|
|
220
|
+
} else {
|
|
221
|
+
pojo[fieldName] = [];
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
const valueAsArray = value as unknown[];
|
|
225
|
+
// in array default value must be explicitly set to default
|
|
226
|
+
pojo[fieldName] = valueAsArray.map((a: unknown) => (a !== undefined && a !== null ? encoder(a, scheme) : null));
|
|
227
|
+
allAreDefault = false;
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
if (value !== undefined) {
|
|
231
|
+
const fieldValue = encoder(value, scheme);
|
|
232
|
+
if (fieldValue !== undefined) {
|
|
233
|
+
pojo[fieldName] = fieldValue;
|
|
234
|
+
if (allAreDefault && !isDefault(field, fieldValue)) {
|
|
235
|
+
allAreDefault = false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (allAreDefault && scheme === JsonEncodingScheme.Compact) {
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
return pojo;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function isDefault(field: FieldType, value: unknown): boolean {
|
|
248
|
+
if (value === undefined || value === null) {
|
|
249
|
+
return true; // undefined or null is always default
|
|
250
|
+
}
|
|
251
|
+
const dataType = toDataType(field);
|
|
252
|
+
// detect if the filedl
|
|
253
|
+
switch (dataType) {
|
|
254
|
+
case DataType.Null:
|
|
255
|
+
// a null value was already answered above; a non-null one of the Null type is not
|
|
256
|
+
// a default, and there is nothing else it could be
|
|
257
|
+
return false;
|
|
258
|
+
case DataType.Boolean:
|
|
259
|
+
return value === false;
|
|
260
|
+
case DataType.Byte:
|
|
261
|
+
case DataType.Int16:
|
|
262
|
+
case DataType.Int32:
|
|
263
|
+
case DataType.UInt16:
|
|
264
|
+
case DataType.UInt32:
|
|
265
|
+
case DataType.Float:
|
|
266
|
+
case DataType.Double:
|
|
267
|
+
case DataType.SByte:
|
|
268
|
+
return value === 0;
|
|
269
|
+
case DataType.Int64:
|
|
270
|
+
case DataType.UInt64:
|
|
271
|
+
if (typeof value === "number") {
|
|
272
|
+
return value === 0;
|
|
273
|
+
} else if (typeof value === "bigint") {
|
|
274
|
+
return value === BigInt(0);
|
|
275
|
+
} else if (Array.isArray(value) && value.length === 2) {
|
|
276
|
+
return value[0] === 0 && value[1] === 0;
|
|
277
|
+
}
|
|
278
|
+
return false;
|
|
279
|
+
case DataType.DateTime:
|
|
280
|
+
return !value || (value instanceof Date && value.getTime() === 0);
|
|
281
|
+
case DataType.String:
|
|
282
|
+
return !value || value === "";
|
|
283
|
+
case DataType.LocalizedText:
|
|
284
|
+
return !value || (!(value as LocalizedTextOptions).text && !(value as LocalizedTextOptions).locale);
|
|
285
|
+
case DataType.NodeId:
|
|
286
|
+
return (
|
|
287
|
+
!value || (!(value as NodeId).identifierType && (value as NodeId).namespace === 0 && (value as NodeId).value === 0)
|
|
288
|
+
);
|
|
289
|
+
case DataType.Guid:
|
|
290
|
+
return !value || value === "00000000-0000-0000-0000-000000000000";
|
|
291
|
+
case DataType.QualifiedName:
|
|
292
|
+
return !value || (!(value as QualifiedNameOptions).name && (value as QualifiedNameOptions).namespaceIndex === 0);
|
|
293
|
+
case DataType.ExtensionObject:
|
|
294
|
+
// ExtensionObject is default if it has no body
|
|
295
|
+
return !value || (Object.keys(value).length === 0 && value.constructor === ExtensionObject);
|
|
296
|
+
case DataType.Variant:
|
|
297
|
+
// Variant is default if it has no body
|
|
298
|
+
return !value || (value as VariantOptions).dataType === DataType.Null;
|
|
299
|
+
case DataType.DataValue:
|
|
300
|
+
// DataValue is default if it has no body
|
|
301
|
+
return (
|
|
302
|
+
!value ||
|
|
303
|
+
((value as DataValueOptions).value === null &&
|
|
304
|
+
value instanceof DataValue &&
|
|
305
|
+
(value as DataValue).statusCode.value === 0 &&
|
|
306
|
+
(value as DataValueOptions).serverPicoseconds === 0 &&
|
|
307
|
+
(value as DataValueOptions).sourcePicoseconds === 0)
|
|
308
|
+
);
|
|
309
|
+
case DataType.DiagnosticInfo: {
|
|
310
|
+
// DiagnosticInfo is default if it has no body
|
|
311
|
+
const valueAsDiag = value as DiagnosticInfoOptions;
|
|
312
|
+
return (
|
|
313
|
+
valueAsDiag.symbolicId === 0 &&
|
|
314
|
+
valueAsDiag.namespaceURI === 0 &&
|
|
315
|
+
valueAsDiag.locale === 0 &&
|
|
316
|
+
valueAsDiag.localizedText === 0 &&
|
|
317
|
+
!valueAsDiag.additionalInfo &&
|
|
318
|
+
!valueAsDiag.innerStatusCode &&
|
|
319
|
+
!valueAsDiag.innerDiagnosticInfo
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
case DataType.StatusCode: {
|
|
323
|
+
// StatusCode is default if it has no body
|
|
324
|
+
const valueAsStatusCode = value as {
|
|
325
|
+
code: number;
|
|
326
|
+
description?: string;
|
|
327
|
+
symbol?: string;
|
|
328
|
+
};
|
|
329
|
+
return valueAsStatusCode.code === 0 && valueAsStatusCode.symbol === "Good" && valueAsStatusCode.description === "";
|
|
330
|
+
}
|
|
331
|
+
case DataType.ByteString:
|
|
332
|
+
// ByteString is default if it has no body
|
|
333
|
+
return value instanceof Buffer && value.length === 0;
|
|
334
|
+
case DataType.XmlElement:
|
|
335
|
+
// XmlElement is default if it has no body
|
|
336
|
+
return typeof value === "string" && value.length === 0;
|
|
337
|
+
}
|
|
338
|
+
return false; // default value is not defined for this type
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function adaptBody(
|
|
342
|
+
body: Pojo,
|
|
343
|
+
schema: IStructuredTypeSchema,
|
|
344
|
+
builder: ExtensionObjectBuilder,
|
|
345
|
+
namespaceArray: string[]
|
|
346
|
+
): Pojo | Pojo[] {
|
|
347
|
+
if (Array.isArray(body)) {
|
|
348
|
+
const result: Pojo[] = body.map((b) => adaptBody(b, schema, builder, namespaceArray) as Pojo);
|
|
349
|
+
return result;
|
|
350
|
+
}
|
|
351
|
+
if (body === null || body === undefined) {
|
|
352
|
+
return body;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const pojo: Pojo = {};
|
|
356
|
+
const allFields = getAllExtensionObjectFields1(schema);
|
|
357
|
+
const isUnion = body.SwitchField !== undefined;
|
|
358
|
+
|
|
359
|
+
if (isUnion) {
|
|
360
|
+
const field = allFields[body.SwitchField as number];
|
|
361
|
+
const value = body.Value || body[field.originalName];
|
|
362
|
+
const decoder = findDecoder(field);
|
|
363
|
+
build(field, value, decoder);
|
|
364
|
+
return pojo;
|
|
365
|
+
}
|
|
366
|
+
for (const field of allFields) {
|
|
367
|
+
const value = body[field.originalName];
|
|
368
|
+
// istanbul ignore next
|
|
369
|
+
if (value === undefined) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const decoder = findDecoder(field);
|
|
373
|
+
build(field, value, decoder);
|
|
374
|
+
}
|
|
375
|
+
return pojo;
|
|
376
|
+
|
|
377
|
+
function build(field: FieldType, value: unknown, decoder?: DecoderFunc<unknown> | null) {
|
|
378
|
+
if (!decoder) {
|
|
379
|
+
errorLog("Cannot find decoder for", field);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (field.isArray) {
|
|
383
|
+
if (!value) {
|
|
384
|
+
pojo[field.name] = [];
|
|
385
|
+
} else if (typeof value === "object" && value && (value as { length?: number }).length === 0) {
|
|
386
|
+
pojo[field.name] = [];
|
|
387
|
+
} else {
|
|
388
|
+
const valueAsArray = value as unknown[];
|
|
389
|
+
pojo[field.name] = valueAsArray.map((a: unknown) => (a ? decoder(a as never, builder, namespaceArray) : undefined));
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
if (value !== undefined && value !== null) {
|
|
393
|
+
pojo[field.name] = decoder(value as never, builder, namespaceArray);
|
|
394
|
+
} else if (value === null) {
|
|
395
|
+
pojo[field.name] = null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function opcuaJsonDecodeExtensionObjectBody(
|
|
402
|
+
schema: IStructuredTypeSchema,
|
|
403
|
+
body: Pojo,
|
|
404
|
+
builder: ExtensionObjectBuilder,
|
|
405
|
+
namespaceArray: string[] = []
|
|
406
|
+
): Pojo | Pojo[] {
|
|
407
|
+
const result = adaptBody(body, schema, builder, namespaceArray);
|
|
408
|
+
return result;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function opcuaJsonEncodeExtensionObjectBody(
|
|
412
|
+
extensionObject: ExtensionObject | null,
|
|
413
|
+
scheme: JsonEncodingScheme,
|
|
414
|
+
_namespaceArray?: string[]
|
|
415
|
+
): ExtensionObjectJSON | Pojo | null | undefined {
|
|
416
|
+
if (!extensionObject) {
|
|
417
|
+
if (scheme === JsonEncodingScheme.Compact) {
|
|
418
|
+
// Compact encoding does not encode empty ExtensionObject
|
|
419
|
+
return undefined;
|
|
420
|
+
}
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
return makeBody(extensionObject, scheme);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
*
|
|
428
|
+
* ExtensionObject values shall be encoded as a JSON object with the fields shown:
|
|
429
|
+
*
|
|
430
|
+
* JSON Object - Definition for a ExtensionObject
|
|
431
|
+
* Name Description
|
|
432
|
+
* TypeId The NodeId of a DataTypeEncoding Node formatted using the rules in 5.4.2.10.
|
|
433
|
+
* Encoding The format of the Body field encoded as a JSON number.
|
|
434
|
+
* This value is 0 if the body is Structure encoded as a JSON object (see 5.4.6).
|
|
435
|
+
* This value is 1 if the body is a ByteString value encoded as a JSON string (see 5.4.2.8).
|
|
436
|
+
* This value is 2 if the body is a XmlElement value encoded as a JSON string (see 5.4.2.9).
|
|
437
|
+
* This field is omitted if the value is 0.
|
|
438
|
+
* Body Body of the ExtensionObject. The type of this field is specified by the Encoding field.
|
|
439
|
+
* If the Body is empty, the ExtensionObject is NULL and is omitted or encoded as a JSON null.
|
|
440
|
+
*
|
|
441
|
+
* For the non-reversible form, ExtensionObject values shall be encoded as a JSON object containing only
|
|
442
|
+
* the value of the Body field. The TypeId and Encoding fields are dropped.
|
|
443
|
+
*/
|
|
444
|
+
export function opcuaJsonEncodeExtensionObject(
|
|
445
|
+
extensionObject: ExtensionObject | ExtensionObject[] | null,
|
|
446
|
+
scheme: JsonEncodingScheme,
|
|
447
|
+
namespaceArray?: string[]
|
|
448
|
+
): ExtensionObjectJSON | Pojo | (ExtensionObjectJSON | Pojo)[] | null | undefined {
|
|
449
|
+
if (!extensionObject) {
|
|
450
|
+
if (scheme === JsonEncodingScheme.Compact) {
|
|
451
|
+
// Compact encoding does not encode empty ExtensionObject
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
454
|
+
return null;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (Array.isArray(extensionObject)) {
|
|
458
|
+
return extensionObject.map(singleItem);
|
|
459
|
+
}
|
|
460
|
+
return singleItem(extensionObject);
|
|
461
|
+
|
|
462
|
+
function singleItem(extensionObject: ExtensionObject): ExtensionObjectJSON | Pojo {
|
|
463
|
+
const body = opcuaJsonEncodeExtensionObjectBody(extensionObject, scheme, namespaceArray);
|
|
464
|
+
const typeId = opcuaJsonEncodeNodeId(extensionObject.schema.dataTypeNodeId, scheme, namespaceArray);
|
|
465
|
+
switch (scheme) {
|
|
466
|
+
case JsonEncodingScheme.DeprecatedNonReversible:
|
|
467
|
+
return body || {};
|
|
468
|
+
case JsonEncodingScheme.DeprecatedReversible:
|
|
469
|
+
return {
|
|
470
|
+
TypeId: typeId,
|
|
471
|
+
Body: body
|
|
472
|
+
};
|
|
473
|
+
case JsonEncodingScheme.Verbose:
|
|
474
|
+
case JsonEncodingScheme.Compact:
|
|
475
|
+
if ((body === undefined || body === null) && scheme === JsonEncodingScheme.Compact) {
|
|
476
|
+
return {
|
|
477
|
+
UaTypeId: typeId
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
UaTypeId: typeId,
|
|
482
|
+
UaBody: body
|
|
483
|
+
// UaEncoding: 0, // always 0 for ExtensionObject in JSON (omitted if Json)
|
|
484
|
+
// use UaEncoding: 1 for binary encoding => Base64 encoded ByteString
|
|
485
|
+
// use UaEncoding: 2 for binary encoding => string as XmlElement
|
|
486
|
+
};
|
|
487
|
+
default:
|
|
488
|
+
throw new Error(`Unknown JsonEncodingScheme ${scheme}`);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export function opcuaJsonDecodeExtensionObject(
|
|
494
|
+
_pojo: ExtensionObjectJSON104 | ExtensionObjectJSON105,
|
|
495
|
+
builder: ExtensionObjectBuilder,
|
|
496
|
+
namespaceArray: string[]
|
|
497
|
+
): ExtensionObject | ExtensionObject[] | null {
|
|
498
|
+
if (!_pojo) {
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
const dataTypeNodeId = opcuaJsonDecodeNodeId(_pojo.TypeId || _pojo.UaTypeId, builder, namespaceArray);
|
|
502
|
+
const body = _pojo.Body || _pojo.UaBody;
|
|
503
|
+
|
|
504
|
+
if (dataTypeNodeId.namespace === 0) {
|
|
505
|
+
const factory = getStandardDataTypeFactory();
|
|
506
|
+
const schema = factory.findStructureInfoForDataType(dataTypeNodeId);
|
|
507
|
+
if (!schema) return null;
|
|
508
|
+
const Constructor = schema.constructor;
|
|
509
|
+
if (!Constructor) {
|
|
510
|
+
return null;
|
|
511
|
+
}
|
|
512
|
+
const body2 = adaptBody(body, Constructor.schema, builder, namespaceArray);
|
|
513
|
+
if (Array.isArray(body2)) {
|
|
514
|
+
const result: ExtensionObject[] = body2.map((b) => new Constructor(b));
|
|
515
|
+
return result;
|
|
516
|
+
}
|
|
517
|
+
return new Constructor(body2);
|
|
518
|
+
} else {
|
|
519
|
+
if (!builder) {
|
|
520
|
+
throw new Error("Expecting a extension object builder for non-0 namespace extension objects");
|
|
521
|
+
}
|
|
522
|
+
const Constructor = builder.getExtensionObjectConstructor(dataTypeNodeId);
|
|
523
|
+
const body2 = adaptBody(body, Constructor.schema, builder, namespaceArray);
|
|
524
|
+
|
|
525
|
+
if (Array.isArray(body2)) {
|
|
526
|
+
const result: ExtensionObject[] = body2.map((b) => new Constructor(b));
|
|
527
|
+
return result;
|
|
528
|
+
}
|
|
529
|
+
return new Constructor(body2);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ==========================================================================
|
|
3
|
+
* Copyright (c) 2021-2026 Sterfive - etienne.rossignon@sterfive.com
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
|
13
|
+
* all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
18
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
* IN THE SOFTWARE.
|
|
22
|
+
* ==========================================================================
|
|
23
|
+
*/
|
|
24
|
+
import type { Int64, UInt64 } from "node-opcua-basic-types";
|
|
25
|
+
import type { ExtensionObjectBuilder } from "./json_basic_encoding_body_functor.js";
|
|
26
|
+
import { JsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
27
|
+
|
|
28
|
+
const signMask = 1n << 31n;
|
|
29
|
+
const shiftHigh = 1n << 32n;
|
|
30
|
+
export function opcuaJsonEncodeInt64(value: Int64, _scheme: JsonEncodingScheme = JsonEncodingScheme.Compact): string {
|
|
31
|
+
if (value instanceof BigInt) {
|
|
32
|
+
return value.toString();
|
|
33
|
+
}
|
|
34
|
+
if (Array.isArray(value) && value.length === 2) {
|
|
35
|
+
const h = BigInt(value[0]);
|
|
36
|
+
const l = BigInt(value[1]);
|
|
37
|
+
if ((h & signMask) === signMask) {
|
|
38
|
+
const v = (h & ~signMask) * shiftHigh + l - 0x8000000000000000n;
|
|
39
|
+
return v.toString();
|
|
40
|
+
} else {
|
|
41
|
+
const v = h * shiftHigh + l;
|
|
42
|
+
return v.toString();
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
return BigInt(value as unknown as number).toString();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function opcuaJsonEncodeUInt64(value: UInt64, _scheme: JsonEncodingScheme = JsonEncodingScheme.Compact): string {
|
|
49
|
+
if (value instanceof BigInt) {
|
|
50
|
+
return value.toString();
|
|
51
|
+
}
|
|
52
|
+
if (Array.isArray(value) && value.length === 2) {
|
|
53
|
+
const h = BigInt(value[0]);
|
|
54
|
+
const l = BigInt(value[1]);
|
|
55
|
+
const v = h * shiftHigh + l;
|
|
56
|
+
return v.toString();
|
|
57
|
+
} else {
|
|
58
|
+
return BigInt(value as unknown as number).toString();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function opcuaJsonDecodeInt64(value: string): Int64 {
|
|
63
|
+
const r = BigInt(value);
|
|
64
|
+
if (r >= 0) {
|
|
65
|
+
return [Number(r / shiftHigh), Number(r % shiftHigh)];
|
|
66
|
+
}
|
|
67
|
+
const v = 0x8000000000000000n + r;
|
|
68
|
+
return [Number((v / shiftHigh) | signMask), Number(v % shiftHigh)];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function opcuaJsonDecodeUInt64(value: string, _builder?: ExtensionObjectBuilder, _namespaceArray?: string[]): Int64 {
|
|
72
|
+
const r = BigInt(value);
|
|
73
|
+
return [Number(r / shiftHigh), Number(r % shiftHigh)];
|
|
74
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ==========================================================================
|
|
3
|
+
* Copyright (c) 2021-2026 Sterfive - etienne.rossignon@sterfive.com
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in
|
|
13
|
+
* all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
18
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
* IN THE SOFTWARE.
|
|
22
|
+
* ==========================================================================
|
|
23
|
+
*/
|
|
24
|
+
import { LocalizedText } from "node-opcua-data-model";
|
|
25
|
+
import { JsonEncodingScheme } from "./json_encoding_scheme.js";
|
|
26
|
+
|
|
27
|
+
export interface LocalizedTextJSON {
|
|
28
|
+
/** The Text portion of LocalizedText values shall be encoded as a JSON string. */
|
|
29
|
+
Text: string | null;
|
|
30
|
+
/** The Locale portion of LocalizedText values shall be encoded as a JSON string */
|
|
31
|
+
Locale?: string;
|
|
32
|
+
}
|
|
33
|
+
export function opcuaJsonEncodeLocalizedText(
|
|
34
|
+
localizedText: LocalizedText | null,
|
|
35
|
+
scheme = JsonEncodingScheme.Compact,
|
|
36
|
+
_namespaceArray?: string[]
|
|
37
|
+
): LocalizedTextJSON | string | null | undefined {
|
|
38
|
+
if (!localizedText || (!localizedText.text && !localizedText.locale)) {
|
|
39
|
+
if (scheme === JsonEncodingScheme.Compact) {
|
|
40
|
+
// Compact encoding does not encode empty LocalizedText
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
if (scheme === JsonEncodingScheme.DeprecatedNonReversible) {
|
|
46
|
+
// For the non-reversible form, LocalizedText value shall be encoded as a JSON string containing the Text component.
|
|
47
|
+
return localizedText.text || null;
|
|
48
|
+
}
|
|
49
|
+
const pojo: LocalizedTextJSON = {
|
|
50
|
+
Text: localizedText.text || null
|
|
51
|
+
};
|
|
52
|
+
if (localizedText.locale) {
|
|
53
|
+
pojo.Locale = localizedText.locale;
|
|
54
|
+
}
|
|
55
|
+
return pojo;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function opcuaJsonDecodeLocalizedText(pojo: null | undefined | string | LocalizedTextJSON): LocalizedText {
|
|
59
|
+
if (!pojo) {
|
|
60
|
+
return new LocalizedText();
|
|
61
|
+
}
|
|
62
|
+
if (typeof pojo === "string") {
|
|
63
|
+
return new LocalizedText({ text: pojo });
|
|
64
|
+
}
|
|
65
|
+
return new LocalizedText({
|
|
66
|
+
text: pojo.Text,
|
|
67
|
+
locale: pojo.Locale
|
|
68
|
+
});
|
|
69
|
+
}
|