Skip to content

Commit

Permalink
#162, API changes/additions required to make JSON encoding a possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bjakke committed Nov 13, 2018
1 parent 5ffd216 commit 2ae4cd4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 34 deletions.
29 changes: 15 additions & 14 deletions src/main/java/org/opcfoundation/ua/builtintypes/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,37 @@

/**
* Super interface for all complex type serializable objects
*
* @author Toni Kalajainen ([email protected])
*/
public interface Structure extends IEncodeable, Cloneable {

/**
* <p>getTypeId.</p>
*
* @return a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* The NodeId for the actual DataType node defining this Structure type. Should never be null.
* Shall always contain the NamespaceUri within the {@link ExpandedNodeId}.
*/
ExpandedNodeId getTypeId();

/**
* <p>getXmlEncodeId.</p>
*
* @return a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* The NodeId for the "Default XML" encodings node of this Structure type. Can be null if not supported.
* Shall always contain the NamespaceUri within the {@link ExpandedNodeId}, if not null.
*/
ExpandedNodeId getXmlEncodeId();

/**
* <p>getBinaryEncodeId.</p>
*
* @return a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* The NodeId for the "Default Binary" encodings node of this Structure type. Should never be null as the encoding is mandatory.
* Shall always contain the NamespaceUri within the {@link ExpandedNodeId}.
*/
ExpandedNodeId getBinaryEncodeId();

/**
* The NodeId for the "Default JSON" encodings node of this Structure type. Can be null if not supported.
* Shall always contain the NamespaceUri within the {@link ExpandedNodeId}, if not null.
*/
ExpandedNodeId getJsonEncodeId();

/**
* As every Structure is Cloneable, this method provides convinience method for
* calling .clone for an unknown Structure. Classes implementing Structure should change the signature
* to return the type of the implementing class.
*
* @return a deep clone of this Structure
* to return the type of the implementing class. Returns a deep clone of this Structure object.
*/
Structure clone();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/opcfoundation/ua/encoding/EncodeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
*/
public enum EncodeType {
Binary,
Xml
Xml,
Json;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.opcfoundation.ua.encoding.binary.IEncodeableSerializer;

/**
* Base for a simple serializer that can serialize only one type of class.
* Base for a simple serializer that can serialize only one type of class. Extending this class via non-code-generating ways should be avoided.
*/
public abstract class AbstractSerializer implements IEncodeableSerializer {

Expand Down Expand Up @@ -63,28 +63,41 @@ private static ExpandedNodeId fixAndValidateId(ExpandedNodeId id) {
ExpandedNodeId nodeId;
ExpandedNodeId binaryId;
ExpandedNodeId xmlId;

ExpandedNodeId jsonId;

/**
* <p>Constructor for AbstractSerializer.</p>
*
* @param clazz a {@link java.lang.Class} object.
* @param binaryId a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* @param xmlId a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* Creates new {@link AbstractSerializer}. This will call {@link #AbstractSerializer(Class, ExpandedNodeId, ExpandedNodeId, ExpandedNodeId)} with type nodeId null.
*
* @param clazz Structure to serialize
* @param binaryId binary id for the serialization, shall not be null
* @param xmlId xml id for the serialization, can be null
*/
public AbstractSerializer(Class<? extends IEncodeable> clazz, ExpandedNodeId binaryId, ExpandedNodeId xmlId){
this(clazz, binaryId, xmlId, null);
}

/**
* <p>Constructor for AbstractSerializer.</p>
*
* @param clazz a {@link java.lang.Class} object.
* @param binaryId a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* @param xmlId a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* @param nodeId a {@link org.opcfoundation.ua.builtintypes.ExpandedNodeId} object.
* Creates new {@link AbstractSerializer}. This will call {@link #AbstractSerializer(Class, ExpandedNodeId, ExpandedNodeId, ExpandedNodeId, ExpandedNodeId)} with jsonId null.
*
* @param clazz Structure to serialize
* @param binaryId binary id for the serialization, shall not be null
* @param xmlId xml id for the serialization, can be null
* @param nodeId node id of the DataType node of the Structure, can be null
*/
public AbstractSerializer(Class<? extends IEncodeable> clazz, ExpandedNodeId binaryId, ExpandedNodeId xmlId, ExpandedNodeId nodeId){
this(clazz, binaryId, xmlId, nodeId, null);
}

/**
* Creates new {@link AbstractSerializer}.
*
* @param clazz Structure to serialize
* @param binaryId binary id for the serialization, shall not be null
* @param xmlId xml id for the serialization, can be null
* @param nodeId node id of the DataType node of the Structure, can be null
* @param jsonId json id for the serializationm can be null
*/
public AbstractSerializer(Class<? extends IEncodeable> clazz, ExpandedNodeId binaryId, ExpandedNodeId xmlId, ExpandedNodeId nodeId, ExpandedNodeId jsonId){
if (clazz==null) {
throw new IllegalArgumentException("Given parameters cannot be null");
}
Expand All @@ -94,6 +107,7 @@ public AbstractSerializer(Class<? extends IEncodeable> clazz, ExpandedNodeId bin
this.binaryId = fixAndValidateId(binaryId);
this.xmlId = fixAndValidateId(xmlId);
this.nodeId = fixAndValidateId(nodeId);
this.jsonId = fixAndValidateId(jsonId);
}

/**
Expand Down Expand Up @@ -153,15 +167,24 @@ public void putEncodeable(Class<? extends IEncodeable> clazz,
/** {@inheritDoc} */
@Override
public Class<? extends IEncodeable> getClass(ExpandedNodeId id) {
return (id.equals(binaryId) || id.equals(xmlId) || (nodeId!= null && id.equals(nodeId))) ? clazz : null;
return (id.equals(binaryId) || id.equals(xmlId) || id.equals(jsonId) || (nodeId!= null && id.equals(nodeId))) ? clazz : null;
}

/** {@inheritDoc} */
@Override
public ExpandedNodeId getNodeId(Class<? extends IEncodeable> clazz, EncodeType type) {
if (type==null) return nodeId; //XXX not pretty, but will do for the moment
if (type==EncodeType.Binary) return binaryId;
if (type==EncodeType.Xml) return xmlId;
if (type==null) {
return nodeId; //XXX not pretty, but will do for the moment
}
if (type==EncodeType.Binary) {
return binaryId;
}
if (type==EncodeType.Xml) {
return xmlId;
}
if(type==EncodeType.Json) {
return jsonId;
}
return null;
}

Expand All @@ -183,10 +206,15 @@ public void getSupportedClasses(Collection<Class<? extends IEncodeable>> result)
/** {@inheritDoc} */
@Override
public void getSupportedNodeIds(Collection<ExpandedNodeId> result) {
if (binaryId!=null)
if (binaryId!=null) {
result.add(binaryId);
if (xmlId!=null)
}
if (xmlId!=null) {
result.add(xmlId);
}
if(jsonId!=null) {
result.add(jsonId);
}
}


Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/opcfoundation/ua/utils/AbstractStructure.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opcfoundation.ua.utils;

import org.opcfoundation.ua.builtintypes.ExpandedNodeId;
import org.opcfoundation.ua.builtintypes.Structure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,5 +27,20 @@ public AbstractStructure clone() {
}
}

/**
* Returns null by default unless overridden.
*/
@Override
public ExpandedNodeId getXmlEncodeId() {
return null;
}

/**
* Returns null by default unless overridden.
*/
@Override
public ExpandedNodeId getJsonEncodeId() {
return null;
}

}

0 comments on commit 2ae4cd4

Please sign in to comment.