How to read in StringArray tag into Java List

Using the Tagprovider’s readAsync method, how would I read in a tag that is of the StringArray type into a Java List

readAsync will return a list of QualifiedValues; extract the value with getValue, and then the inner Object should be an Array<String>; you could then use Arrays.asList() to wrap the array in a list.

I tried this and it didn’t seem to work. I will post my code below so you can confirm if I am doing what you suggested properly.

I set the tag’s value to be [“a”, “b”, “c”, “d”, “e”] for this test.

List<QualifiedValue> values = provider.readAsync(paths, SecurityContext.emptyContext()).get(timeout, TimeUnit.MILLISECONDS);
List<Object> readList = Arrays.asList(values.get(0).getValue());

if (readList.get(0).equals("a")) {
	log.info("WORKED?");
} else {
	log.info("NO WORKY");
}

Note that I read in readList as List<Object> because it complains if I try to do it directly to List<String>

You can log the actual value of values.get(0).getValue() and it’s type to see what you’re actually getting?

I’m not especially familiar with the tag system, so my assumption was that it returned a vanilla string array, but it may be some more specific UA datatype still.

This is the code:

List<QualifiedValue> values = provider.readAsync(paths, SecurityContext.emptyContext()).get(timeout, TimeUnit.MILLISECONDS);
log.info("LOOK HERE 1: " + values.get(0));
log.info("LOOK HERE 2: " + values.get(0).getValue());
log.info("LOOK HERE 3: " + values.get(0).getClass());

And the logs are below:
image

I also just logged the values.get(0).getValue().getClass() and it logged as “[Ljava.lang.String;”

That’s the toString() for a String[], alright. Which should be just fine to pass to Arrays.asList.

Try logging the individual elements of the array? Make sure they’re what you expect?

I thought so too but I can’t seem to get it to log the value. Below is the code I tried:

List<QualifiedValue> values = provider.readAsync(paths, SecurityContext.emptyContext()).get(timeout, TimeUnit.MILLISECONDS);
List<Object> values2 = Arrays.asList(values.get(0).getValue());
log.info("LOOK HERE 4: " + values2.get(0));
log.info("LOOK HERE 5: " + values2.get(0).toString());
log.info("LOOK HERE 6: " + (String) values2.get(0));

Below is the log:

LOOK HERE 5: [Ljava.lang.String;@49bfa951
LOOK HERE 4: [Ljava.lang.String;@49bfa951

and LOOK HERE 6 ((String) values2.get(0)) produces a cast exception [Ljava.lang.String; cannot be cast to class java.lang.String

Part of the issue seems is that the result of values2.get(0) returns a String[] but java thinks its a String or Object not an array so it wont compile if I tried to access it as I would an array. But if I cannot access it as I would an array then how am I supposed to get the value?

List<QualifiedValue> values = 
    provider.readAsync(paths, SecurityContext.emptyContext())
        .get(timeout, TimeUnit.MILLISECONDS);

String[] ss = (String[]) values.get(0).getValue();

// option 1
List<String> list1 = List.of(ss);

// option 2
List<String> list2 = Arrays.stream(ss).collect(Collectors.toList());