Skip to content

Commit

Permalink
WIP QualifiedName and LocalizedText
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Jul 12, 2022
1 parent 1de7374 commit 3aeb5bb
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 8 deletions.
Expand Up @@ -801,12 +801,90 @@ public StatusCode readStatusCode(String field) throws UaSerializationException {

@Override
public QualifiedName readQualifiedName(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("readQualifiedName: %s != %s", field, nextName)
);
}
}

jsonReader.beginObject();

String name = null;
int namespaceIndex = 0;

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

switch (nextName) {
case "Name":
name = jsonReader.nextString();
break;
case "Uri":
namespaceIndex = jsonReader.nextInt();
break;
default:
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
String.format("readQualifiedName: unexpected field: " + nextName)
);
}
}

jsonReader.endObject();

return new QualifiedName(namespaceIndex, name);
} catch (IOException e) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, e);
}
}

@Override
public LocalizedText readLocalizedText(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("readLocalizedText: %s != %s", field, nextName)
);
}
}

jsonReader.beginObject();

String locale = null;
String text = null;

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

switch (nextName) {
case "Locale":
locale = jsonReader.nextString();
break;
case "Text":
text = jsonReader.nextString();
break;
default:
throw new UaSerializationException(
StatusCodes.Bad_DecodingError,
String.format("readLocalizedText: unexpected field: " + nextName)
);
}
}

jsonReader.endObject();

return new LocalizedText(locale, text);
} catch (IOException e) {
throw new UaSerializationException(StatusCodes.Bad_DecodingError, e);
}
}

@Override
Expand Down
Expand Up @@ -509,8 +509,12 @@ public void writeLocalizedText(String field, LocalizedText value) throws UaSeria

if (reversible) {
jsonWriter.beginObject();
jsonWriter.name("Locale").value(value.getLocale());
jsonWriter.name("Text").value(value.getText());
if (value.getLocale() != null) {
jsonWriter.name("Locale").value(value.getLocale());
}
if (value.getText() != null) {
jsonWriter.name("Text").value(value.getText());
}
jsonWriter.endObject();
} else {
jsonWriter.value(value.getText());
Expand Down
Expand Up @@ -23,7 +23,9 @@
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.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
import org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
Expand Down Expand Up @@ -538,10 +540,45 @@ void readStatusCode() throws IOException {
}

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

decoder.reset(new StringReader("{}"));
assertEquals(new QualifiedName(0, null), decoder.readQualifiedName(null));

decoder.reset(new StringReader("{\"Uri\":1}"));
assertEquals(new QualifiedName(1, null), decoder.readQualifiedName(null));

decoder.reset(new StringReader("{\"Name\":\"foo\"}"));
assertEquals(new QualifiedName(0, "foo"), decoder.readQualifiedName(null));

decoder.reset(new StringReader("{\"Name\":\"foo\",\"Uri\":1}"));
assertEquals(new QualifiedName(1, "foo"), decoder.readQualifiedName(null));

decoder.reset(new StringReader("{\"foo\":{\"Name\":\"foo\"}}"));
decoder.jsonReader.beginObject();
assertEquals(new QualifiedName(0, "foo"), decoder.readQualifiedName("foo"));
decoder.jsonReader.endObject();
}

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

decoder.reset(new StringReader("{\"Locale\":\"en\",\"Text\":\"foo\"}"));
assertEquals(LocalizedText.english("foo"), decoder.readLocalizedText(null));

decoder.reset(new StringReader("{\"Locale\":\"en\"}"));
assertEquals(new LocalizedText("en", null), decoder.readLocalizedText(null));

decoder.reset(new StringReader("{\"Text\":\"foo\"}"));
assertEquals(new LocalizedText(null, "foo"), decoder.readLocalizedText(null));

decoder.reset(new StringReader("{\"foo\":{\"Locale\":\"en\",\"Text\":\"foo\"}}"));
decoder.jsonReader.beginObject();
assertEquals(LocalizedText.english("foo"), decoder.readLocalizedText("foo"));
decoder.jsonReader.endObject();
}

@Test
void readExtensionObject() throws IOException {}
Expand Down
Expand Up @@ -713,11 +713,15 @@ public void writeLocalizedText() throws IOException {

encoder.reset(writer = new StringWriter());
encoder.writeLocalizedText(null, new LocalizedText("en", null));
assertEquals("{\"Locale\":\"en\",\"Text\":null}", writer.toString());
assertEquals("{\"Locale\":\"en\"}", writer.toString());

encoder.reset(writer = new StringWriter());
encoder.writeLocalizedText(null, new LocalizedText(null, "foo"));
assertEquals("{\"Locale\":null,\"Text\":\"foo\"}", writer.toString());
assertEquals("{\"Text\":\"foo\"}", writer.toString());

encoder.reset(writer = new StringWriter());
encoder.writeLocalizedText(null, new LocalizedText(null, null));
assertEquals("{}", writer.toString());

encoder.reversible = false;
encoder.reset(writer = new StringWriter());
Expand Down

0 comments on commit 3aeb5bb

Please sign in to comment.