Skip to content

Commit

Permalink
Use JsonArray for OptionSet fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Jan 9, 2025
1 parent 82fc1ba commit 279c3fb
Showing 1 changed file with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 the Eclipse Milo Authors
* Copyright (c) 2025 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -10,7 +10,7 @@

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

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

var valueArray = new JsonArray();
for (byte b : value.bytesOrEmpty()) {
valueArray.add(JsonConversions.fromSByte(b));
}
var validBitsArray = new JsonArray();
for (byte b : validBits.bytesOrEmpty()) {
validBitsArray.add(JsonConversions.fromSByte(b));
}

var jsonObject = new JsonObject();
jsonObject.add("Value", JsonConversions.fromByteString(value));
jsonObject.add("ValidBits", JsonConversions.fromByteString(validBits));
jsonObject.add("Value", valueArray);
jsonObject.add("ValidBits", validBitsArray);

return new JsonStruct(dataType, jsonObject);
}

@Override
public void encodeType(EncodingContext context, UaEncoder encoder, JsonStruct value)
throws UaSerializationException {
JsonElement valueElement = value.getJsonObject().get("Value");
JsonElement validBitsElement = value.getJsonObject().get("ValidBits");
JsonArray valueArray = value.getJsonObject().getAsJsonArray("Value");
JsonArray validBitsArray = value.getJsonObject().getAsJsonArray("ValidBits");

byte[] valueBs = new byte[valueArray.size()];
for (int i = 0; i < valueArray.size(); i++) {
valueBs[i] = JsonConversions.toSByte(valueArray.get(i));
}

byte[] validBitsBs = new byte[validBitsArray.size()];
for (int i = 0; i < validBitsArray.size(); i++) {
validBitsBs[i] = JsonConversions.toSByte(validBitsArray.get(i));
}

encoder.encodeByteString("Value", JsonConversions.toByteString(valueElement));
encoder.encodeByteString("ValidBits", JsonConversions.toByteString(validBitsElement));
encoder.encodeByteString("Value", new ByteString(valueBs));
encoder.encodeByteString("ValidBits", new ByteString(validBitsBs));
}
}

0 comments on commit 279c3fb

Please sign in to comment.