Modify JSON variables in preprocessor

+2 votes
asked Feb 23, 2023 in Wanted features by Marries (140 points)

I would really like to modify JSON variables in the preprocessing, especially for the arrays. For example:

!$data = {"a": ["b", "c"]}
!$data.a[] = "d"
!$data.a[] = {"e": "f"}
!$data.x = "y"
%unset($data.a[0])

Which would result in:

{"a": ["c", "d", {"e": "f"}], "x": "y"}

Is this feasible?

Some context:
I want to create a plantUML library for event modeling, written in the preprocessor language. It would (ab)use the deployment diagram for this purpose.

The challenge is that the natural ordering for event modeling is different from the ordering required to create the correct plantUML diagram (I've got the correct layout working, so I'm very pleased with that). My planned approach was to use functions to construct a large JSON variable (with arrays), and then write a render function to convert the JSON variable to a diagram. But then I found out that I can't modify JSON variables...

As an alternative, I figured out that I can fake arrays in the preprocessor with such an approach:

!$i = 0
%set_variable_value("array" + $i, "a")
!$i = $i + 1
%set_variable_value("array" + $i, "b")
!$r = %get_variable_value("array1")

I could probably get it working with this approach, but it makes my eyes bleed so I would prefer to avoid that.

commented Feb 23, 2023 by The-Lu (63,920 points)

Hello M., and all,

Just FYI, here are some pseudo-similar requests here:

Regards.

commented Feb 27, 2023 by Marries (140 points)
Thanks, I didn't find those topics. It seems this feature won't be added soon so I'll continue with my proposed alternative and keep tissues at hand. Cheers!
commented Feb 1 by philCryo (140 points)

Yes please!  My use case:

I'm tracking in a sequence diagrams multiple queues which would greatly benefit from the ability to (a) start with an empty JSON array, and then (b) have the ability to add and remove entries.

My queues are:

  • MQTT topic subscribers
  • Messages in a MQTT topic

I've also commented in the above-linked discussions.

commented Feb 2 by chris (2,540 points)
... why not just process the JSON in another language and import it. I have done something similar before, you can:

1) reference a JSON file directly, so just write a file

2) use string templating in the language of your choice, e.g. JS/python and drop the JSON into a puml script within your code. This is a bit awkward as you have to escape backslashes and such for Java. e.g. a python script would take the JSON, process it, pass it to a template string representing a puml file, and then pass that to plantuml.jar using a file, or pass it directly by invoking process or whatever if you want it immediately.

The very idea of the array hack you've suggested is making my eyes bleed too!
commented Feb 4 by Marries (140 points)

Thanks for your suggestions chris. However I wanted to make a convenient library for quick iteration and using steps outside of PlantUML makes for a more complicated workflow. This is especially true when PlantUML is embedded in another application.

I've actually implemented the library with the array hack, which was doable with some wrapping functions: https://github.com/chilit-nl/plantuml-event-modeling

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

[Antispam2 Feature: please please wait 1 or 2 minutes (this message will disappear) before pressing the button otherwise it will fail](--------)
To avoid this verification in future, please log in or register.
...