use of org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamEncoder in project milo by eclipse.
the class OpcUaDefaultXmlEncoding method encode.
@Override
public Object encode(SerializationContext context, Object struct, NodeId encodingId) {
try {
@SuppressWarnings("unchecked") OpcUaXmlDataTypeCodec<Object> codec = (OpcUaXmlDataTypeCodec<Object>) context.getDataTypeManager().getCodec(encodingId);
if (codec == null) {
throw new UaSerializationException(StatusCodes.Bad_EncodingError, "no codec registered for encodingId=" + encodingId);
}
// We have to use writer.writeStruct() instead of codec.decode() because
// XML-encoded structs are wrapped in a container element with the struct name.
OpcUaXmlStreamEncoder writer = new OpcUaXmlStreamEncoder(context);
writer.writeStruct(null, struct, codec);
return new XmlElement(writer.getDocumentXml());
} catch (ClassCastException e) {
throw new UaSerializationException(StatusCodes.Bad_EncodingError, e);
}
}
Aggregations