Ignition 8.0.13
When I try to write a gateway Document Tag with a dictionnary, I have the following error:
dico = {"test" : "10"}
system.tag.writeBlocking(["[default]path/to/tag"],[dico])
[Error_TypeConversion("Cannot coerce to document from class type 'class org.python.core.PyDictionary'")]
What’s wrong ?
2 Likes
I don’t know why but there’s no auto-conversion from a Python Dictionary to JSON when writing to Document tags.
Encode your dictionary with system.util.jsonEncode
before writing it.
5 Likes
To clarify, you also need to convert it into a dictioanary when reading it as well if you want to manipulate it.
i.e.
documentTag = system.tag.readBlocking([tagPath])[0].value
document = system.util.jsonDecode(str(documentTag))
If you dont do this then the DocumentObject wrapping the top level keeps you from being able to write to anything inside the document
So to edit the document you have to go through the following steps:
- Read the tag
- Cast it as a string
- jsonDecode it into a dictionary
- manipulate the dictionary
- jsonEncode it back into a string
- Write your changes back to the tag
Feels a bit overcomplicated, but I would just assume that the auto-conversion would probably be a good feature request, as with the rise of perspective, document tags are going to start becoming a lot more popular.
2 Likes
You can skip steps 2 and 3 in the response from kgamble above. The read document value has a .toDict() function to jump to a dictionary, then modify, encode and write back.
6 Likes
I know this is pretty old, but is there any improvement (or plan to) over document tags?I feel like they could be a great tool to store proper serialisable types, but fighting with encoding/decoding all the time makes almost plain strings more appealing. Thanks!
I just started playing with Document tags last year, and haven't needed any JSON encoding/decoding. Just wrapping in dict()
on read, and writing a dict()
. What have you tried lately?
Hey @pturmel , thank you so much as usual for the super quick feedback. Indeed, after some trying, I finally managed to write objects/lists and even perspective properties properly in a document tag.
Here is a fun behaviour I found tho. I had inadvertently set up a document field to Object (e.g. [Tag Provider]DocumentTag['default'] was set to object).
When testing to write a perspective property (of type ArrayWrapper) to it, instead of returning a write failed, the object "default" got converted to type value, and the arraywrapper was converted to string and assigned to it.
This threw me off a bit, and therefore led me to believe some proper conversion was not yet in place for the document tags. I do believe the correct behaviour for this would be to either refuse the write, or convert to the proper Array type.
I will probably open a separate topic for this tho.
I would only expect complete writes of the entire tag to work, and only using actual jython dictionaries and lists. Perspective properties are not native jython data types, so I would expect them to always fail in this way.
There are a number of topics here showing how to deep copy Perspective objects into programmer-friendly jython objects. You can also use helpers from my Integration Toolkit to do this.
I agree, that's why a "write but with inconsistent type" threw me completely off.
I will follow through on a different thread then, and I will check your helpers. Thanks for the support.