Skip to content

Cannot serialise enums with anonymous classes #256

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

Closed
willtemperley opened this issue Nov 28, 2019 · 1 comment
Closed

Cannot serialise enums with anonymous classes #256

willtemperley opened this issue Nov 28, 2019 · 1 comment

Comments

@willtemperley
Copy link

Why are enums with anonymous classes are not supported? I have an enum which is composed of constants, some of which are anonymous classes, which I would like to serialise. Even with a custom encoder this is not possible. It should be incredibly easy to serialise an enum, i.e. it just needs to be a string in the serialisation, surely?

Enums constants without an anonymous class work fine on the same class (x.y.z.Symbol)

The exception:

com.jsoniter.spi.JsonException: anonymous class not supported: class x.y.z.Symbol$2

	at com.jsoniter.spi.TypeLiteral.generateCacheKey(TypeLiteral.java:96)
	at com.jsoniter.spi.TypeLiteral.generateDecoderCacheKey(TypeLiteral.java:84)
	at com.jsoniter.spi.TypeLiteral.createNew(TypeLiteral.java:180)
	at com.jsoniter.spi.TypeLiteral.create(TypeLiteral.java:170)
	at com.jsoniter.spi.Config.getEncoderCacheKey(Config.java:65)
	at com.jsoniter.output.JsonStream.writeVal(JsonStream.java:361)
	at com.jsoniter.output.ReflectionObjectEncoder.writeEncodeTo(ReflectionObjectEncoder.java:121)
	at com.jsoniter.output.ReflectionObjectEncoder.enocde_(ReflectionObjectEncoder.java:78)
	at com.jsoniter.output.ReflectionObjectEncoder.encode(ReflectionObjectEncoder.java:37)
	at com.jsoniter.output.JsonStream.writeVal(JsonStream.java:381)
	at com.jsoniter.output.JsonStream.serialize(JsonStream.java:490)
	at com.jsoniter.output.JsonStream.serialize(JsonStream.java:470)
	at io.temperley.backtest.ParamsSerDe.serialize(ParamsSerDe.java:45)
	at io.temperley.backtest.ParamsSerDeTest.serialize(ParamsSerDeTest.java:19)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

@willtemperley
Copy link
Author

OK I figured out how to do this, I simply needed a class-specific property decoder which simply encodes/decodes the enum as a string. As it's a very simple operation I feel like there should be an easier way to do this, but this works.

JsoniterSpi.registerPropertyEncoder(Params.class, "symbol", (obj, stream) -> stream.writeVal(obj.toString()));

JsoniterSpi.registerPropertyDecoder(Params.class, "symbol", iter -> Symbol.valueOf(iter.readString()));

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

No branches or pull requests

1 participant