★ wanayoo — archive 1999 https://github.com/json-iterator/java/issues/145Nouvelle recherche | Portail wanayoo
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

Static encoder codegen with Any #145

Closed
nedtwigg opened this issue Jan 12, 2018 · 2 comments
Closed

Static encoder codegen with Any #145

nedtwigg opened this issue Jan 12, 2018 · 2 comments

Comments

@nedtwigg
Copy link

@nedtwigg nedtwigg commented Jan 12, 2018

For jsoniter 0.9.19, with this class:

public class EmailSelf {
	public String subject;
	public String body;
	public Any cidMap;
}

jsoniter codegen generates this:

public class EmailSelf implements com.jsoniter.spi.Encoder {
public void encode(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {
if (obj == null) { stream.writeNull(); return; }
stream.writeRaw("{\"subject\":", 11);
encode_((java2ts.EmailSelf)obj, stream);
stream.write((byte)'}');
}
public static void encode_(java2ts.EmailSelf obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {
if (obj.subject == null) { stream.writeNull(); } else {
stream.writeVal((java.lang.String)obj.subject);
}
stream.writeRaw(",\"body\":", 8);
if (obj.body == null) { stream.writeNull(); } else {
stream.writeVal((java.lang.String)obj.body);
}
stream.writeRaw(",\"cidMap\":", 10);
if (obj.cidMap == null) { stream.writeNull(); } else {
stream.write((byte)'{', (byte)'}');
jsoniter_codegen.cfg9223372036100299902.encoder.com.jsoniter.any.Any.encode_((com.jsoniter.any.Any)obj.cidMap, stream);

}
}

The problem is if (obj.cidMap == null) { stream.writeNull(); } else { stream.write((byte)'{', (byte)'}');. No matter what value is assigned to cid, it shows up as cid: {} in the generated json. This isn't a problem when the same code is used in dynamic mode.

@taowen taowen closed this in 68990b2 Jan 13, 2018
@taowen
Copy link
Contributor

@taowen taowen commented Jan 13, 2018

public class DemoCodegenConfig implements StaticCodegenConfig {

    @Override
    public void setup() {
        // register custom decoder or extensions before codegen
        // so that we doing codegen, we know in which case, we need to callback
        Any.registerEncoders();
        JsonIterator.setMode(DecodingMode.STATIC_MODE);
        JsonStream.setMode(EncodingMode.STATIC_MODE);
        JsonStream.setIndentionStep(2);
        JsoniterSpi.registerPropertyDecoder(User.class, "score", new Decoder.IntDecoder() {
            @Override
            public int decodeInt(JsonIterator iter) throws IOException {
                return Integer.valueOf(iter.readString());
            }
        });
    }

added Any.registerEncoders(); to fix this issue

@nedtwigg
Copy link
Author

@nedtwigg nedtwigg commented Jan 13, 2018

Wow, thanks! Speedy!

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.