konsoletyper / teavm-flavour

Framework for writing client-side applications using TeaVM
Apache License 2.0
110 stars 19 forks source link

Serialization fails when serializing same object #50

Closed Ihromant closed 4 years ago

Ihromant commented 4 years ago

Simple code snippet fails:

public class Client {
    public static void main(String[] args) {
        System.out.println(JSON.serialize(Arrays.asList(Field.of(0, 0), Field.of(0, 0))).stringify());
    }

    @JsonPersistable
    public static class Field {
        public static final int WIDTH = 10;
        public static final int HEIGHT = 10;
        public static final Field[] CACHE = IntStream.range(0, WIDTH * HEIGHT)
                .mapToObj(i -> new Field(i / HEIGHT, i % HEIGHT)).toArray(Field[]::new);
        private int x;
        private int y;

        public Field() {}

        public Field(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() { return x; }

        public void setX(int x) { this.x = x; }

        public int getY() { return y; }

        public void setY(int y) { this.y = y; }

        public static Field of(int x, int y) {
            return CACHE[x * HEIGHT + y];
            // this works return new Field(x, y);
        }
    }
}

with

Uncaught Error: Java exception thrown at $rt_exception (classes.js:228) at $rt_throw (classes.js:220) at otfjs_JsonSerializerContext_touch (JsonSerializerContext.java:43) at otfjs_JsonSerializerContext.$touch (classes.js:535) at otfjs_JsonSerializer$proxy$4_106_0_serialize (JsonSerializerEmitter.java:220) at Object.$serialize (classes.js:540) at otfj_JSON_serialize (JSON.java:53) at otfjs_JsonSerializer$proxy$4_7_0_serialize (JsonSerializerEmitter.java:425) at Object.$serialize (classes.js:540) at otfjs_ListSerializer_serializeNonNull (ListSerializer.java:34)

I tried to reproduce it in test, but from IDEA it seems that it runs serialization with Jackson (which obviously works), but from console with "mvn clean install" SerializerTest just hangs in console and doesn't finish.

ScraM-Team commented 4 years ago

Have you tried wrapping the List in a new class that implements JsonPersistable before serializing? List itself doesn't implement JsonPersistable and may not be supported.