Skip to content

TypeSignature.EXCLUDE It doesn't work. #280

@R2DA22

Description

@R2DA22

Hi, I'm facing a problem while serializing a JSON object using the library. The object has an interface as one of its attributes, and I've tried to suppress the $type in the serialized output with the @CompiledJson(typeSignature = CompiledJson.TypeSignature.EXCLUDE) annotation, but it's not having the desired effect.
Here's a code sample to illustrate:

    package org.example;

    import com.dslplatform.json.*;
    import com.dslplatform.json.runtime.Settings;

    import java.io.IOException;

    public class Main {

    public static void main(String[] args) throws IOException {
        DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime().includeServiceLoader());
        JsonWriter writer = dslJson.newWriter();

        Model instance = new Model();
        instance.iface = new WithCustomCtor(5);

        dslJson.serialize(writer, instance);
        System.out.println(writer);
    }

    @CompiledJson(typeSignature = CompiledJson.TypeSignature.EXCLUDE)
    static class Model {
        @JsonAttribute(typeSignature = CompiledJson.TypeSignature.EXCLUDE)
        public Interface iface;
    }

    @CompiledJson(typeSignature = CompiledJson.TypeSignature.EXCLUDE)
    public interface Interface {
        void x(int v);
        int x();
    }

    public static class WithCustomCtor implements Interface {
        private int x;

        @CompiledJson(typeSignature = CompiledJson.TypeSignature.EXCLUDE)
        public WithCustomCtor(int x) {
                this.x = x;
        }

        @Override
        public void x(int v) {
            x = v;
        }

        @Override
        public int x() {
            return x;
        }

    }
}

Result:

{"iface":{"$type":"org.example.Main.WithCustomCtor","x":5}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions