Skip to content

Commit

Permalink
WIP OpcUaJsonDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Jul 8, 2022
1 parent 5a7941a commit 266ea68
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
Expand Up @@ -47,6 +47,8 @@

public class OpcUaJsonDecoder implements UaDecoder {

SerializationContext serializationContext;

JsonReader jsonReader;

public OpcUaJsonDecoder(Reader reader) {
Expand Down Expand Up @@ -634,7 +636,10 @@ public NodeId readNodeId(String field) throws UaSerializationException {
jsonReader.endObject();

if (id == null) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, "id == null");
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
"readNodeId: id == null"
);
} else {
switch (idType) {
case Numeric:
Expand All @@ -650,7 +655,10 @@ public NodeId readNodeId(String field) throws UaSerializationException {
assert id instanceof ByteString;
return new NodeId(namespaceIndex, (ByteString) id);
default:
throw new UaSerializationException(StatusCodes.Bad_DecodingError, "idType: " + idType);
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
"readNodeId: idType: " + idType
);
}
}
} catch (IOException e) {
Expand All @@ -663,7 +671,101 @@ public NodeId readNodeId(String field) throws UaSerializationException {

@Override
public ExpandedNodeId readExpandedNodeId(String field) throws UaSerializationException {
return null;
try {
if (field != null) {
String nextName = jsonReader.nextName();
if (!field.equals(nextName)) {
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
String.format("readExpandedNodeId: %s != %s", field, nextName)
);
}
}

IdType idType = IdType.Numeric;
Object id = null;
int namespaceIndex = 0;
String namespaceUri = null;
int serverIndex = 0;

try {
jsonReader.beginObject();

while (jsonReader.peek() == JsonToken.NAME) {
String propertyName = jsonReader.nextName();

switch (propertyName) {
case "IdType": {
int value = jsonReader.nextInt();
idType = IdType.from(value);
if (idType == null) {
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
"readExpandedNodeId: invalid IdType: " + value
);
}
break;
}
case "Id": {
switch (idType) {
case Numeric:
id = uint(jsonReader.nextInt());
break;
case String:
id = jsonReader.nextString();
break;
case Guid:
id = UUID.fromString(jsonReader.nextString());
break;
case Opaque:
id = ByteString.of(Base64.getDecoder().decode(jsonReader.nextString()));
break;
}
break;
}
case "Namespace": {
switch (jsonReader.peek()) {
case NUMBER:
namespaceIndex = jsonReader.nextInt();
break;
case STRING:
namespaceUri = jsonReader.nextString();
break;
default:
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
"readExpandedNodeId: unexpected Namespace token: " + jsonReader.peek()
);
}
break;
}
case "ServerUri": {
serverIndex = jsonReader.nextInt();
break;
}
}
}

jsonReader.endObject();

if (id == null) {
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
"readExpandedNodeId: id == null"
);
} else {
if (namespaceUri == null) {
return new ExpandedNodeId(ushort(namespaceIndex), null, id, uint(serverIndex));
} else {
return new ExpandedNodeId(ushort(namespaceIndex), namespaceUri, id, uint(serverIndex));
}
}
} catch (IOException e) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, e);
}
} catch (IOException e) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, e);
}
}

@Override
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.milo.opcua.stack.core.UaSerializationException;
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.ExpandedNodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
Expand All @@ -29,6 +30,8 @@
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
import org.junit.jupiter.api.Test;

import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ushort;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -483,7 +486,24 @@ void readNodeId() throws IOException {
}

@Test
void readExpandedNodeId() throws IOException {}
void readExpandedNodeId() throws IOException {
var decoder = new OpcUaJsonDecoder(new StringReader(""));

// namespace URI specified
var xni = new ExpandedNodeId(ushort(0), "http://opcfoundation.org/UA/", "foo");
decoder.reset(new StringReader("{\"IdType\":1,\"Id\":\"foo\",\"Namespace\":\"http://opcfoundation.org/UA/\"}"));
assertEquals(xni, decoder.readExpandedNodeId(null));

// remote server index
xni = new ExpandedNodeId(ushort(0), null, "foo", uint(1));
decoder.reset(new StringReader("{\"IdType\":1,\"Id\":\"foo\",\"ServerUri\":1}"));
assertEquals(xni, decoder.readExpandedNodeId(null));

decoder.reset(new StringReader("{\"foo\":{\"IdType\":1,\"Id\":\"foo\",\"ServerUri\":1}}"));
decoder.jsonReader.beginObject();
assertEquals(xni, decoder.readExpandedNodeId("foo"));
decoder.jsonReader.endObject();
}

@Test
void readStatusCode() throws IOException {}
Expand Down

0 comments on commit 266ea68

Please sign in to comment.