Comments (3)
would it be possible to create one serializer and make the different action handlers (like MessageAction) override this method to aovid having a single class for each action handler (MessageActionSerializer)
from okaeri-configs.
Your example is missing the config POJO itself, which probably reveals some unsuitable field type, but my best guess is that you are attempting to create a collection where the contents are all concrete implementations of a single interface.
After the object is transformed into the YAML data using okaeri-configs, the information about the type is lost. The only source of the type truth is the config definition class itself. This means that while it may be possible to serialize such collection, it will not be possible to deserialize it without including additional data, such as field 'type' with an enum or enum-like type.
For one of the solutions, you would need to create a field List<MyInterfaceType>
, register a serializer for only the MyInterfaceType
, and then implement MyInterfaceType
serializer to pass the data to specific serializers or deserialize it in place, all while routing based on the previously mentioned type field.
Example of such a solution, where the type field is represented as action
:
public class MyActionSerializer implements ObjectSerializer<MyAction> {
@Override
public boolean supports(@NonNull Class<? super MyAction> type) {
return MyAction.class.isAssignableFrom(type);
}
@Override
public void serialize(@NonNull MyAction action, @NonNull SerializationData data, @NonNull GenericsDeclaration generics) {
data.add("action", action.getAction());
switch (action.getAction()) {
case ACTION_ONE -> {
data.add("field1", ((MyActionOne) action).getFieldA1(), SomeType1.class);
data.addCollection("field2", ((MyActionOne) action).getFieldA2(), SomeType2.class);
}
case ACTION_TWO -> {
data.add("field1", ((MyActionTwo) action).getFieldB1(), SomeType3.class);
}
}
}
@Override
public MyAction deserialize(@NonNull DeserializationData data, @NonNull GenericsDeclaration generics) {
MyActionType action = data.get("action", MyActionType.class);
return (switch (action) {
case ACTION_ONE -> new MyActionOne(
data.get("field", SomeType1.class),
data.getAsList("field2", SomeType2.class)
);
case ACTION_TWO -> new MyActionTwo(
data.get("field1", SomeType3.class)
);
case NOOP -> new NoopAction();
});
}
/** FIXME: remove if using auto-registration with ConfigSerializable, the better way to serialize first-party types
public static class Serdes implements OkaeriSerdesPack {
@Override
public void register(@NonNull SerdesRegistry registry) {
registry.register(new MyActionSerializer());
}
}
**/
}
@Data
public abstract class MyAction implements ConfigSerializable {
private static final MyActionSerializer MY_ACTION_SERIALIZER= new MyActionSerializer();
private final MyActionType action;
public abstract void execute(@NonNull MyContext context);
@Override
public void serialize(@NonNull SerializationData data, @NonNull GenericsDeclaration generics) {
MY_ACTION_SERIALIZER.serialize(this, data, generics);
}
public static MyAction deserialize(@NonNull DeserializationData data, @NonNull GenericsDeclaration generics) {
return MY_ACTION_SERIALIZER.deserialize(data, generics);
}
}
@Data
public class MyObject extends OkaeriConfig {
// ...
private List<MyAction> actions = new ArrayList<>();
// ...
}
from okaeri-configs.
what a beautiful person you are!
worked like a charm thank you
for those in the future this is the updated code:
https://paste.md-5.net/safusuhebi.java
from okaeri-configs.
Related Issues (20)
- No comments for all options HOT 3
- Not compatible with Java 17 HOT 5
- JSON Gson configs do not encode special characters HOT 3
- Null value after config update HOT 7
- Newly added variables are not in the proper order HOT 4
- Deserialization of Map<MyEnum, List<MyOkaeriConfig>> results in Map<MyEnum, List<LinkedHashMap>> HOT 2
- Orphaned keys not removed after save HOT 1
- Empty string is added each time the configuration is loaded. HOT 4
- Subconfigurations validation seem to be not implemented HOT 1
- Comments inside subconfigs from collections and maps don't work HOT 1
- Itemstack serializer do not serialize a color HOT 1
- Extend serdes-bukkit's ItemStackSerializer with support for complex items
- Give access to root object in serializer HOT 5
- Add TOML support HOT 4
- YamlSnakeYamlConfigurer isn't compatible with latest versions of snakeyaml
- Minestom (Community Edition) platform HOT 2
- Add friendly exception messages for errors
- Support for `.properties` file
- OkaeriConfig.save method will clear file of content if errors to simplify any object HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from okaeri-configs.