Syntax for array to component V14

Hi,
I’m looking for syntax advice to send an array to a javascript component.
I’m a Vaadin & Java guy, and know little about Javascript :frowning_with_open_mouth:
Using Vaadin 14.10.9.

I believe the syntax in javascript world is like

menu: [‘shape’, ‘draw’],

However, I can’t successfully set it from Vaadin/Java.
I have been trying many options including …

myElement.getElement().setProperty(“menu”, “[‘shape’, ‘draw’]”);

What is the correct syntax for setting this data?

Any info appreciated. Thx.

I have this code snippet tucked away, it might help (I haven’t tested it in a long while):

   private static ObjectMapper objectMapper = new ObjectMapper();
   // Helper method to convert a List to JsonArray with ObjectMapper
   public static JsonArray listToJson(List<?> list) throws JsonProcessingException {
      return Json.instance().parse(objectMapper.writeValueAsString(list));
   }
   
   public MyComponent() {
       List<String> namesList = getNames();
       try {
           getElement().setPropertyJson("names", listToJson(namesList));
       } catch (JsonProcessingException e) {
           // ...
       }

The same functionality should be available directly in the Element API in later versions

Brilliant. Thx Olli.

For simple cases built in JSON conversion works, example here: ColorPicker/src/main/java/org/vaadin/addons/tatu/ColorPicker.java at master · TatuLund/ColorPicker · GitHub

setPropertyList isn’t available in V14 yet: https://vaadin.com/api/platform/14.10.8/com/vaadin/flow/dom/Element.html