Skip to content

Add support for JSON data types #310

@TimPommerDybdahl

Description

@TimPommerDybdahl

Feature Request

Is your feature request related to a problem? Please describe

MSSQL has 5 more data types than the ones listed in the Data Type Mapping.
These are

This feature request is specifically to support the JSON data type

Describe the solution you'd like

Ability to support JSON columns and read/write these into data classes similar to the Json class of r2dbc-postgresql. The implementation of the class should enable developers to map efficiently to other POJOs, for example using Jackson, via custom converters. These converters are NOT part of this feature request, only efficient support for them.

Describe alternatives you've considered

The current String Codec almost supports the Json type. It can write json-strings to columns of the JSON type and these will be mapped correctly by the database engine. Reading, however, is currently not supported.

The issue related to reading comes from the plpLength being -2 which causes a validation error once transformed into a Length object (from what I can see, the LengthStrategy is PARTLENTYPE in these cases). If the validation is somehow bypassed (I have done this by running the code in debug mode and reassigning the value before and after validation), then the StringCodec does work and Json columns can successfully be read and subsequently be processed by a custom converter.

Teachability, Documentation, Adoption, Migration Strategy

Example of a custom reading converter:

@Slf4j
@ReadingConverter
public class JsonMapReadingConverter implements Converter<Json, Map<?, ?>> {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Map<?, ?> convert(Json source) {
        return source.mapInputStream(inputStream -> {
            try {
                return objectMapper.readValue(inputStream, Map.class);
            } catch (IOException e) {
                log.debug("Failed to deserialize  JSON: {}", source, e);
                throw new SerializationFailedException("Failed to deserialize  JSON: " + source, e);
            }
        });
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions