Skip to content

Gson.toJson: CharSequence passed to Appendable does not implement toString() #1702

Closed

Description

@Marcono1234

When calling Gson.toJson(..., Appendable) and Appendable is not an instance of Writer, then Gson creates a CharSequence which does not fulfill the toString() requirements:

Returns a string containing the characters in this sequence in the same order as this sequence. The length of the string will be the length of this sequence.

Contrived example:

static class MyAppendable implements Appendable {
    private final StringBuilder stringBuilder = new StringBuilder();
    
    @Override
    public Appendable append(char c) throws IOException {
        stringBuilder.append(c);
        return this;
    }
    
    @Override
    public Appendable append(CharSequence csq) throws IOException {
        if (csq == null) {
            append("null");
        } else {
            append(csq, 0, csq.length());
        }
        return this;
    }
    
    public Appendable append(CharSequence csq, int start, int end) throws IOException {
        if (csq == null) {
            csq == "null";
        }
        
        // According to doc, toString() must return string representation
        String s = csq.toString();
        stringBuilder.append(s, start, end);
        return this;
    }
}

public static void main(String[] args) {
    MyAppendable myAppendable = new MyAppendable();
    new Gson().toJson("test", myAppendable);
    // Prints `com.` (first 4 chars of `com.google.gson.internal.Streams.AppendableWriter.CurrentWrite`)
    System.out.println(myAppendable.stringBuilder);
}

Activity

  1. added a commit that references this issue on May 21, 2020
    ebe8e5a
  2. Marcono1234 commented on Jul 26, 2022

    @Marcono1234
    ContributorAuthor

    This is actually a duplicate of #527 (though maybe this issue here can be kept open because it includes a self-contained reproduction example)?

  3. added a commit that references this issue on Aug 3, 2022
    a1d2ebc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions