Skip to content

JsonEncoder not correctly closing JSON for ExpandedNodeId - missing curly bracket #3209

@htbmw

Description

@htbmw

Type of issue

  • Bug
  • Enhancement
  • Compliance
  • Question
  • Help wanted

Current Behavior

While using the JsonEncoder to serialize event fields, I observed during testing that the final JSON was malformed.
On closer inspection it was found that the ExpandedNodeId is always missing a closing curly bracket when json encoded.

Here are examples of the incorrect string produced by JsonEndode for an ExpandedNodeId:

Single ExpandedNodeId as a JSON object: ending curly bracket is missing

{"field1":{"IdType":1,"Id":"StringIdentifier","Namespace":2}

Multiple ExpandedNodeIds as a JSON object: several curly brackets missing and leads to incorrect nesting

{"field4":{"IdType":1,"Id":"StringIdentifier","Namespace":2,"field5":{"IdType":1,"Id":"StringIdentifier","Namespace":2,"field6":{"IdType":1,"Id":"StringIdentifier","Namespace":2}

Expected Behavior

The correct JSON should look like

Single ExpandedNodeId:
{"field1":{"IdType":1,"Id":"StringIdentifier","Namespace":2}}

Multiple ExpandedNodeIds:
{"field4":{"IdType":1,"Id":"StringIdentifier","Namespace":2},"field5":{"IdType":1,"Id":"StringIdentifier","Namespace":2},"field6":{"IdType":1,"Id":"StringIdentifier","Namespace":2}}

Steps To Reproduce

Run the following code:
Different use cases have been included to see if something would work, eg wrapping the ExpandedNodeId in a variant, passing in the raw ExpandedNodeId without wrapping, calling the WriteExpandedNodeId instead of WriteVariant, but all yield the same incorrect result.

using Opc.Ua;

namespace OpcUaConsoleApp
{
    class Program
    {
        private static async Task Main(string[] args)
        {
            var expandedNodeId = new ExpandedNodeId(new NodeId("ns=2;s=StringIdentifier"),null,23);

            var encoder = new JsonEncoder(ServiceMessageContext.GlobalContext, false);
            encoder.WriteVariant("field1", expandedNodeId);            
            var s = encoder.CloseAndReturnText();
            Console.WriteLine(s);

            var encoder2 = new JsonEncoder(ServiceMessageContext.GlobalContext, false);
            encoder2.WriteVariant("field2", new Variant(expandedNodeId));            
            var s2 = encoder2.CloseAndReturnText();
            Console.WriteLine(s2);

            var encoder3 = new JsonEncoder(ServiceMessageContext.GlobalContext, false);
            encoder3.WriteExpandedNodeId("field3", expandedNodeId);
            var s3 = encoder3.CloseAndReturnText();
            Console.WriteLine(s3);

            var encoder4 = new JsonEncoder(ServiceMessageContext.GlobalContext, false);
            encoder4.WriteVariant("field4", expandedNodeId);
            encoder4.WriteVariant("field5", new Variant(expandedNodeId));
            encoder4.WriteExpandedNodeId("field6", expandedNodeId);
            var s4 = encoder4.CloseAndReturnText();
            Console.WriteLine(s4);
        }
    }
}

Environment

- OS: Any
- Environment: Any
- Runtime: dotnet9
- Nuget Version: Any, including latest
- Component: JsonEncoder
- Server:
- Client:

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions