Jack9
Jack9
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
ObjectMapper mapper = new ObjectMapper(); MapType collectionType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, String[].class); HashMap<String,Object> result = mapper.convertValue(exampleDto, collectionType); This is an initialization that is needed to pass the test. It does not survive additional field types, but it does avoid the conversion to ArrayList succinctly.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
There's an approach of mapper.read out some json, then deserialize that with https://github.com/Pretius/pretius-jddl/
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
There are a lot of discussions around a polymorphic deserializer here: https://github.com/FasterXML/jackson-databind/issues/1627 There was never a formal method. Lots of little pieces for subtypes and such.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
If you can make due with the bytes as an Object[], rather than a String[] , mapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
The following seems overkill, but it works (more or less, no float etc) added as: ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(HashMap.class, new CustomDeSerializer()); mapper.registerModule(module); HashMap<String,Object> result = mapper.convertValue(exampleDto, new TypeReference<>(){});
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
This was a long-winded way of saying "I don't know and don't care". GL with whatever.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
They implement a defaultSerializer which does not involve json
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
This is incorrect. Jackson provides a conversion API via convertValue.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
@JsonSerialize addresses this same X/Y problem, from a different direction. I don't understand the avoidance.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
I have another API that needs a HashMap and it can only handle primitive members and primitive arrays.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
This isn't relevant. The question is about methodology, not circumstance. Why I care is that I am trying to figure out a way to do it, beyond my current solution. Let's just assume I can't control the source datastructure (it's part of another API in jar), does that matter? No.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
I don't understand.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
[1,2,3] is simpler datatype that exists in JSON than ArrayList(){{ 1,2,3 }}. The question remains, other than a fairly robust deserializer to a mapper as a module, is there a simpler way?
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
I believe what is happening is that the serializer is called to make json, which is then tokenized with a json parser. Then the jackson json parser is used to make the HashMap.class, which has a default jackson serializer. In order to ensure type safety of each element of an array, it uses an ArrayList. So the String[] type disappears. Making a ~50ln deserializer works to coerce the type back to String[] - but that seems overkill. Is there any other way? I would imagine this has come up before for Jackson.
29 replies
JCHJava Community | Help. Code. Learn.
Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
The test is to demonstrate the conversion to ArrayList. The question is how to convert a String[] property into a Hashmap<String, Object> with a <String, String[]> entry instead of the coerced <String, ArrayList<String>> entry.
29 replies