how to decode list of enum as root element excluding unknown enum - kotlinx.serialization

this is my json to parse:
["AA", "BB", "unknown"]
I need to decode with List<enum> that doesn't contain unknown element. I can get list of 3 element with 1 null or list of 2 element.
i tryed KindListSerializer but how to apply to root element?

solution:
Json.decodeFromString(KindListSerializer(EnumClass.serializer()), """["AA", "BB", "unknown"]""")

Related

Find documents where all array field values matches predicate

Given the following document schema
{
"type": ["A", "B"]
}
where field type is an indexed field of keyword type.
I want to find documents for which all values of type field should match some predicate p.
Basically I need to check if all values from type field are present in another array. E.g. for ["A", "B", "C] doc above matches, for ["A", "D"] not.
You can use scripts to workaround this. The idea here would be to check if all the elements of doc are present in input_array. Refer: subsets-discussion
In version 6.3, there's native support for this via terms_set query. Refer:
terms-set-query-dsl

Why doesn't this XPath query work as I assumed? (searching for elements with some # while excluding those with other #)

I have the following query (XPath 2.0):
//xref[contains(#href,'#') and #class='- topic/xref ' and #type!='step' and #type!='fig' and #type!='substep']
As you can see, I want to find topic/xref elements with a hash in their href attribute. I want to exclude ceratin types of elements.
Problem is, the above query does not display elements with #outputclass='expandable'
I had to run a seperate one to identify them:
//xref[contains(#href,'#') and #outputclass='expandable']
Why does the first, longer query, do not display those elements? I also tried contains(#class='- topic/xref ) instead of #class=' - topic/xref ' and it didn't help.
Try below XPath:
//xref[#class="- topic/xref " and contains(#href, "#") and not(#type=("fig", "substep", "table") or #outputclass="expandable")]
that will return xref elements with class="- topic/xref " and # in href attribute but doesn't have type attribute with values "fig", "substep", "table" or outputclass attribute with value "expandable"
You haven't shown your XML source, but I suspect you were expecting to retrieve elements having no #type attribute. However, the condition #type!='XYZ' is true only for an element that has an #type attribute whose value is something other than 'XYZ'. If my guess is correct, you intended not(#type='XYZ').

Cannot list a hash on key-value pairs in Freemarker

What's wrong with the following template?
package ${packageName}
public interface ${entityName} {
<#list methods as methodName, map >
public void ${methodName}(${map}) ;
</#list>
}
which gives on version 2.3.23:
freemarker.core.ParseException: Syntax error in template "javaclass.ftl" in line 5, column 29:
Encountered ",", but was expecting:
">"
at freemarker.core.FMParser.generateParseException(FMParser.java:5251)
at freemarker.core.FMParser.jj_consume_token(FMParser.java:5122)
at freemarker.core.FMParser.List(FMParser.java:1431)
at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:2827)
at freemarker.core.FMParser.MixedContent(FMParser.java:3081)
at freemarker.core.FMParser.OptionalBlock(FMParser.java:3253)
at freemarker.core.FMParser.Root(FMParser.java:3432)
at freemarker.template.Template.<init>(Template.java:208)
at freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:495)
The documentation gives the following example for a hash structure
Listing hashes is very similar, but you need to provide two variable
names after the as; one for the hash key, and another for the
associated value. Assuming products is { "apple": 5, "banana": 10,
"kiwi": 15 }:
<#list products as name, price>
<p>${name}: ${price}
</#list>
<p>apple: 5
<p>banan: 10
<p>kiwi: 15
Note that my example is before submitting content.
That is expected since listing key-value was added in 2.3.25.
http://freemarker.org/docs/ref_directive_list.html#ref.directive.list
... and to list the key-value pairs of a hash (since 2.3.25):
<#list hash as key, value>
Part repeated for each key-value pair
</#list>
So, upgrade if you can or rewrite your list.
See also:
Freemarker iterating over hashmap keys

Iterating over a map of Object: List in Freemarker

I'm trying to iterate in a freemarker template over a HashMap<SeapSubscription, List<PiNotice>>.
The map doesn't contain any nulls (in keys or values).
The code in Freemarker is:
<#list subscriptionsWithPiNotices?keys as s>
${s.title}
<#list subscriptionsWithPiNotices[s] as piNotice>
Autoritate contractanta: ${piNotice.contractingAuthorityName}
.
.
</#list>
</#list>
If I remove the iteration from the second list (<#list subscriptionsWithPiNotices[s] as piNotice>) it all works (that is iterating over the map keys, but when I add the second part, trying to iterate over the map-s value, i get a Null / missing exception
FreeMarker template error: The following has evaluated to null or missing:
==> subscriptionsWithPiNotices[s] [in template "seap-subscription-newsletter.ftl" at line 21, column 16]
Tip: If the failing expression is known to be legally null/missing,
either specify a default value with myOptionalVar!myDefault, or use
<#if myOptionalVar??>when-present<#else>when-missing. (These
only cover the last step of the expression; to cover the whole
expression, use parenthessis: (myOptionVar.foo)!myDefault,
(myOptionVar.foo)??
The failing instruction (FTL stack trace):
==> #list subscriptionsWithPiNotices[s] a... [in template "seap-subscription-newsletter.ftl" at line 21, column 9]
#list subscriptionsWithPiNotices?keys... [in template "seap-subscription-newsletter.ftl" at line 18, column 5]
I repeat, I dumped that HashMap, and it only has one key with one ArrayList having one item inside. So there's no reason to report a null, is it ?
Finally i managed to solve the issue, by configuring a
BeansWrapper bw = BeansWrapper.getDefaultInstance();
bw.setSimpleMapWrapper(true);
cfg.setObjectWrapper(bw);
and then using the
map(key) syntax instead of map[key] or map.key. It seems to work with any type of key class.
So this is the old issue that the [] operator only supports string keys. Since 2.3.22 you can use someMap?api.get(someNonStringKey) to work that around. It needs some configuring to enable, but nothing that breaks an existing application. See this answer or this FAQ entry.

XPath Expression to Return Nodes with Attributes of a Certain Length

I need to grab all nodes of particular type within an XML document but only if their value attribute is not empty. I've tried a few permutations on the below, but with no luck:
".//Foo[not([#Value =''])]"
This should work for you
//Foo[#Value!='']
Or if you really want to only return nodes with a value attribute of a certain length use something like:
//Foo[string-length(#Value) > 3]
or
//Foo[string-length(#Value) = 0]

Resources