Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register custom TypeAdapters #57

Closed
jbaron opened this issue Jul 26, 2022 · 3 comments
Closed

register custom TypeAdapters #57

jbaron opened this issue Jul 26, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@jbaron
Copy link

jbaron commented Jul 26, 2022

Would love a way to register Gson custom TypeAdapters so I can stick closer to my domain model for the data in the charts and don't require all types of transformations. Perhaps this is already possible, but couldn't find it in the documentation.

For example (I use Kotlin), Gson by default doesn't handle Pair, Triple, Instant and my own custom types.

BTW, I use the engine.renderJsonOption(chart) before sending it to the browser.

@jbaron
Copy link
Author

jbaron commented Jul 26, 2022

Right now using my own Json rendering of the chart.option object, which also works fine. So not a high priority feature request.

@icepear-jzx
Copy link
Member

EChartsSerializer will support custom adapters in the next release.

Example:

    public void testCustomEChartsSerializer() {
        EChartsSerializer serializer = new EChartsSerializer(new CustomObjectAdapter());
        Object object = new CustomObject("name");
        JsonElement actual = serializer.toJsonTree(object);
        JsonElement expected = JsonParser.parseString("'name'");
        assertEquals(expected, actual);
    }

    class CustomObject {
        private String name;

        public CustomObject(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    class CustomObjectAdapter implements EChartsTypeAdapter<CustomObject> {
        public JsonElement serialize(CustomObject src, Type typeOfSrc, JsonSerializationContext context) {
            return context.serialize(src.getName());
        }

        public Type getType() {
            return CustomObject.class;
        }
    }

@jbaron
Copy link
Author

jbaron commented Aug 1, 2022

Great, then I can use the EChartsSerializer which is a bit cleaner then doing my own serialisation (since I noticed ECharts-Java also has some own Adapters for more advanced chart types)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants