Does declaring the variable as transient not work in this case? lol
|
I'm currently using [Only registered and activated users can see links. ]to serialize my player saves, but I can't seem to find how to ignore/skip specific classes from being serialized. I've tried using the annotation @[Only registered and activated users can see links. ]IgnoreType like it was recommended [Only registered and activated users can see links. ], but that only seems to work for actual fields (not entries in a list). I've also tried tagging the classes I want to ignore with @[Only registered and activated users can see links. ]IgnoreType and using a custom annotation introspector, but it just saves the empty classes without the fields (except for className):
Result:Code:saveMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() { @Override public boolean hasIgnoreMarker(AnnotatedMember m) { return super.hasIgnoreMarker(m) || m.getDeclaringClass().getAnnotation(JsonIgnoreType.class) != null; } }); ... @[Only registered and activated users can see links. ]IgnoreType private static class ScriptA extends Script { private int dontKeep; public ScriptA() { dontKeep = 25; } }
Here's a simplified snippet of what I'm trying to achieve:Code:{ "scripts" : [ { "className" : "com.JsonExemple$ScriptA" }, { "className" : "com.JsonExemple$ScriptB", "keep" : 50 } ] }
Edit: Ended up getting some help and the easy answer seems to be to use a custom getters for the scripts:Code:package com; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @author clem585 * @[Only registered and activated users can see links. ]d 28/02/2021 - 1:43 AM */ public class JsonExemple { public static void main(String[] args) { try { // create Jackson JSON writer ObjectMapper saveMapper = new ObjectMapper() .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); ObjectWriter saveWriter = saveMapper.writer().withDefaultPrettyPrinter(); // instantiate class I want to save ExempleClass exempleClass = new ExempleClass(); // create empty file File file = new File("D:/Desktop/exemple.json"); new File("D:/Desktop/").mkdirs(); file.createNewFile(); saveWriter.writeValue(file, exempleClass); System.out.println("Saved exemple.json on the desktop of the D drive"); } catch (Exception e) { System.out.println("Exception attempting to save exemple"); e.printStackTrace(); } } private static class ScriptB extends Script { @[Only registered and activated users can see links. ]Property private int keep; public ScriptB() { keep = 50; } } // ignore this class private static class ScriptA extends Script { private int dontKeep; public ScriptA() { dontKeep = 25; } } @[Only registered and activated users can see links. ]TypeInfo(use = JsonTypeInfo.Id.CLASS, property = "className") private static abstract class Script { } private static class ExempleClass { @[Only registered and activated users can see links. ]Property private List<Script> scripts; public ExempleClass() { scripts = new ArrayList<>(); scripts.add(new ScriptA()); scripts.add(new ScriptB()); } } }
I still want to save some scripts, but not all of them.
Skip all:
Partial skip:Code:private transient List<PlayerScript> playerScripts;
Skip none:Code:@[Only registered and activated users can see links. ]Property("playerScripts") private List<PlayerScript> filterScriptsForJson() { return playerScripts.stream() .filter(s -> s.getClass().getAnnotation(JsonIgnoreType.class) == null) .collect(Collectors.toList()); }
Code:private List<PlayerScript> playerScripts;
if i have understood your issue correctly you want a way to tell jackson to only serialise the types in a list belonging to a particular subclass (that has a polymorphic relationship) so your issue is jackson needs to be able to distinguish between instances of A and B (without treating them as the same type)
if my assumption is correct take a look at the above article, you can use DEDUCTION to exclude instances of B from serialisation based on the presence of a particular property, this should mean you can skip them when you deal with a list like the above
This is just a way to add custom type identifiers during serialization (VS just using class name). It's a bit more work since you have to specify a type for each script with @[Only registered and activated users can see links. ]TypeName("name") instead of the player script doing it automatically with the annotation code I posted. At the end of the day you would still need the @[Only registered and activated users can see links. ]Property getter to exclude the scripts as this would just provide a different mean of identifying the original class during deserialization, nothing to do with filtering. Thanks for the suggestion though!
« Model help | Need client help for building a new packet end point » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |