Skip to content

Commit 279c3fb

Browse files
committed
Use JsonArray for OptionSet fields
1 parent 82fc1ba commit 279c3fb

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

opc-ua-sdk/codec-json/src/main/java/org/eclipse/milo/sdk/core/types/json/JsonOptionSetCodec.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 the Eclipse Milo Authors
2+
* Copyright (c) 2025 the Eclipse Milo Authors
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -10,7 +10,7 @@
1010

1111
package org.eclipse.milo.sdk.core.types.json;
1212

13-
import com.google.gson.JsonElement;
13+
import com.google.gson.JsonArray;
1414
import com.google.gson.JsonObject;
1515
import org.eclipse.milo.opcua.sdk.core.typetree.DataType;
1616
import org.eclipse.milo.opcua.stack.core.UaSerializationException;
@@ -39,20 +39,39 @@ public JsonStruct decodeType(EncodingContext context, UaDecoder decoder)
3939
ByteString value = decoder.decodeByteString("Value");
4040
ByteString validBits = decoder.decodeByteString("ValidBits");
4141

42+
var valueArray = new JsonArray();
43+
for (byte b : value.bytesOrEmpty()) {
44+
valueArray.add(JsonConversions.fromSByte(b));
45+
}
46+
var validBitsArray = new JsonArray();
47+
for (byte b : validBits.bytesOrEmpty()) {
48+
validBitsArray.add(JsonConversions.fromSByte(b));
49+
}
50+
4251
var jsonObject = new JsonObject();
43-
jsonObject.add("Value", JsonConversions.fromByteString(value));
44-
jsonObject.add("ValidBits", JsonConversions.fromByteString(validBits));
52+
jsonObject.add("Value", valueArray);
53+
jsonObject.add("ValidBits", validBitsArray);
4554

4655
return new JsonStruct(dataType, jsonObject);
4756
}
4857

4958
@Override
5059
public void encodeType(EncodingContext context, UaEncoder encoder, JsonStruct value)
5160
throws UaSerializationException {
52-
JsonElement valueElement = value.getJsonObject().get("Value");
53-
JsonElement validBitsElement = value.getJsonObject().get("ValidBits");
61+
JsonArray valueArray = value.getJsonObject().getAsJsonArray("Value");
62+
JsonArray validBitsArray = value.getJsonObject().getAsJsonArray("ValidBits");
63+
64+
byte[] valueBs = new byte[valueArray.size()];
65+
for (int i = 0; i < valueArray.size(); i++) {
66+
valueBs[i] = JsonConversions.toSByte(valueArray.get(i));
67+
}
68+
69+
byte[] validBitsBs = new byte[validBitsArray.size()];
70+
for (int i = 0; i < validBitsArray.size(); i++) {
71+
validBitsBs[i] = JsonConversions.toSByte(validBitsArray.get(i));
72+
}
5473

55-
encoder.encodeByteString("Value", JsonConversions.toByteString(valueElement));
56-
encoder.encodeByteString("ValidBits", JsonConversions.toByteString(validBitsElement));
74+
encoder.encodeByteString("Value", new ByteString(valueBs));
75+
encoder.encodeByteString("ValidBits", new ByteString(validBitsBs));
5776
}
5877
}

0 commit comments

Comments
 (0)