As indicated before, I’m using RO SDK to “open” parts of our applications to the world.
This requires generating JSON for objects that have Variant
properties. While doing it in Delphi gives a coherent JSON that can get read quite well, it is now time to “port” the code to the C#/.Net part and I’m thus using the following code to generate the JSON:
using JsonMessage JSONMessage = new();
JSONMessage.InitializeMessage(null, "dummy", "dummy", MessageType.Response);
JSONMessage.Write("variables", ROVariables, typeof(TVariablesArray[]), StreamingFormat.Default;
JSONMessage.WriteToStream(AStream);
This does not crash but gives strange results when placing a double
or a bool
inside the Variant
property:
{
"Description":null,
"Filter":null,
"Id":0,
"Kind":"evkDouble",
"Locked":false,
"Max":null,
"Min":null,
"Name":"DoubleVar",
"Value":"40,5"
},
{
"Description":null,
"Filter":null,
"Id":0,
"Kind":"evkBoolean",
"Locked":false,
"Max":null,
"Min":null,
"Name":"BoolVar",
"Value":"True"
}
As you can see, the two values are output as string when I would have expected a direct JSON value, just like what happens with Delphi: "Value":40.5
and "Value":true
.
What’s even worse is that it is using localized values which means trouble later on.
Note: I have not used a int
value in this example, but it behaves just like double
, giving a quoted string instead of a direct value.
There is also a problem when doing the reverse, that is read a JSON content to instantiate an array of objects like this:
using JsonMessage JSONMessage = new();
JSONMessage.ReadFromStream(AStream);
return (TVariablesArray[])JSONMessage.Read("variables", typeof(TVariablesArray[]), StreamingFormat.Default);
There, if I give a valid JSON as created from Delphi, I do get the appropriate number of items in the array but the Variant
property is not always set to a valid type:
- For
DateTime
, the value in JSON is"2022-10-05T17:55:21.735"
but I get astring
instead of the expectedDateTime
instance and thatstring
is equal to the source string - For
Date
, the value in JSON is"2022-10-05T00:00:00"
and I get a validDateTime
instance just fine. - For
Int
, the value in JSON is72
and I get adouble
instance. Odd as Delphi gives a valid Integer, but not unrealistic. - For
Double
, the value in JSON is40.5
and I get a validdouble
instance - For
Boolean
, the value in JSON istrue
but I get anull
value which is completely strange
So double
and string
values are fine, int
is acceptable, DateTime
seems not to like having a time portion but boolean
is completely off the mark. All this despite the C# code being completely similar to the Delphi code that works fine.
We are using version 10.0.0.1489 which I agree is a bit old, but there are no mentions of JSON related changes in the what’s new documentation.
Is there anything that I missed here?
What can I try to overcome those obstacles?