Strange .Add behaviour in new JSON implementation

I’ve recently shifted from 2020r2.1 to 2021r1.1. Quite a lot of fallout in code, mostly with JSONitems (I’m still in classic framework).

Firstly changing all True/False to true/false as a check on document load has resolved a lot!

Current weirdness I’m left with is:

This doesn’t work (lights is empty):

Dim requestChild as new JSONitem
requestChild.value("on") = 1
requestChild.value("brightness") = params.lookup("brightness",0)
request.child("lights") = new JSONItem
request.child("lights").Add(requestChild)

This works (lights has one child with the correct content):

Dim requestChild as new JSONitem
requestChild.value("on") = 1
requestChild.value("brightness") = params.lookup("brightness",0)
Dim requestLights as new JSONItem
requestLights.Add(requestChild)
request.child("lights") = requestLights

Am I missing something or does every child need declaring as a property first before adding? I never used to and just defined the child(x) as a new JSONitem.

Many thanks,

James

Seems to be that is should work since it accepts (assigns) a jsonItem.
Maybe try to cast it?

Dim requestChild as new JSONitem
requestChild.value("on") = 1
requestChild.value("brightness") = params.lookup("brightness",0)
request.child("lights") = new JSONItem
JSONItem(request.child("lights")).Add(requestChild)

Doesn’t add anything either. Strange isn’t it?

Seems like a bug…

The old JSONItem had an internal status, so an empty one would turn into array or object depending on what you call next.

With the new JSONItem, the empty on is transitioned to text in-between on the assignment if I remember correctly. You may have run into an unspecified situation.

Please report via Feedback.

1 Like
Dim request As New JSONItem

Dim requestChild As New JSONitem
requestChild.value("on") = 1
requestChild.value("brightness") = 123
request.child("lights") = New JSONItem
request.child("lights").Add(requestChild)

Dim r As String = request.ToString
Dim s As String = request.child("lights").ToString

Break

If you run that, you see request.child(“lights”) is an empty JSON object. It didn’t become an array on Add call and the data was ignored. Sounds really like a bug.
e.g. Add should change empty object to array or raise exception.

Feedback case 65235: jsonitem add should change empty object to array or raise exeception

2 Likes

Make sure you declare lights as an array:

New JSONItem("[]")

Thanks for submitting that Christian.

Greg, it seems odd that it works when set as a property first it works but when set directly it doesn’t. Surely it’s the same object type and should work the same irrespective of if assigned to a property or a child.