-
Notifications
You must be signed in to change notification settings - Fork 420
/
OpcUaToJson.java
102 lines (80 loc) · 2.9 KB
/
OpcUaToJson.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2024 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.milo.sdk.core.types.json;
import java.util.UUID;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonPrimitive;
import org.eclipse.milo.opcua.stack.core.types.builtin.ByteString;
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime;
import org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
public class OpcUaToJson {
public static JsonElement toBoolean(boolean value) {
return new JsonPrimitive(value);
}
public static JsonElement toSByte(byte value) {
return new JsonPrimitive(value);
}
public static JsonElement toByte(UByte value) {
return new JsonPrimitive(value.shortValue());
}
public static JsonElement toInt16(short value) {
return new JsonPrimitive(value);
}
public static JsonElement toUInt16(UShort value) {
return new JsonPrimitive(value.intValue());
}
public static JsonElement toInt32(int value) {
return new JsonPrimitive(value);
}
public static JsonElement toUInt32(UInteger value) {
return new JsonPrimitive(value.longValue());
}
public static JsonElement toInt64(long value) {
return new JsonPrimitive(value);
}
public static JsonElement toUInt64(ULong value) {
return new JsonPrimitive(value.toBigInteger());
}
public static JsonElement toFloat(float value) {
return new JsonPrimitive(value);
}
public static JsonElement toDouble(double value) {
return new JsonPrimitive(value);
}
public static JsonElement toString(String value) {
return new JsonPrimitive(value);
}
public static JsonElement toDateTime(DateTime value) {
return new JsonPrimitive(value.toIso8601String());
}
public static JsonElement toGuid(UUID value) {
return new JsonPrimitive(value.toString());
}
public static JsonElement toByteString(ByteString value) {
var jsonArray = new JsonArray();
for (byte b : value.bytesOrEmpty()) {
jsonArray.add(b);
}
return jsonArray;
}
public static JsonElement toXmlElement(XmlElement value) {
if (value.isNull()) {
return JsonNull.INSTANCE;
} else {
return new JsonPrimitive(value.getFragment());
}
}
}