Skip to content

TokenBufferdoes not implement writeString(Reader reader, int len) #3816

@kilink

Description

@kilink

Describe the bug

Calling the writeString method on JsonGenerator that accepts a Reader throws when the underlying implementation is TokenBuffer. This happens for instance one when calls convertValue, but makes it awkward for the Serializer to be agnostic to whether it's being called for conversion or serialization.

Version information
Which Jackson version(s) was this for?

2.14.x

To Reproduce

Here's a contrived example:

@JsonSerialize(using = Foo.Serializer.class)
class Foo {
    static class Serializer extends StdSerializer<Foo> {
        Serializer() {
            super(Foo.class);
        }

        @Override
        public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) throws IOException {
            gen.writeStartObject();
            gen.writeFieldName("field");
            gen.writeString(new StringReader("foobar"), 6);
            gen.writeEndObject();
        }
    }
}

ObjectMapper mapper = new ObjectMapper();
Foo obj = new Foo();
// works
String serialized = mapper.writeValueAsString(obj);
// fails
Map<String, String> map = mapper.convertValue(obj, new TypeReference<Map<String, String>>() {});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions