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

Investigate Optimizing JsonFlatten Serialization #21505

Closed
alzimmermsft opened this issue May 14, 2021 · 1 comment
Closed

Investigate Optimizing JsonFlatten Serialization #21505

alzimmermsft opened this issue May 14, 2021 · 1 comment
Assignees
Labels
Azure.Core azure-core Client This issue points to a problem in the data-plane of the library.
Milestone

Comments

@alzimmermsft
Copy link
Member

Investigate manually handling JsonFlatten serialization instead of writing the object to a tree and mutating nodes. The following is code which was an initial investigation along with comments of its short-comings:

// Investigation into serializing the class ourselves to prevent string manipulation.
// Code below doesn't handle the following:
// - Null values are serialized without respecting ObjectMapper inclusion.
// - JsonType information isn't included.
if (defaultSerializer instanceof BeanSerializer) {
    BeanSerializer beanSerializer = (BeanSerializer) defaultSerializer;

    jgen.writeStartObject();

    Iterator<PropertyWriter> writerIterator = beanSerializer.properties();
    while (writerIterator.hasNext()) {
        BeanPropertyWriter writer = (BeanPropertyWriter) writerIterator.next();
        String jsonPropertyName = writer.getName();
        if ((writer.getMember().hasAnnotation(JsonFlatten.class) || classHasJsonFlatten)
            && CHECK_IF_FLATTEN_PROPERTY_PATTERN.matcher(jsonPropertyName).matches()) {
            String[] flatteningNames = SPLIT_FLATTEN_PROPERTY_PATTERN.split(jsonPropertyName);

            jgen.writeFieldName(flatteningNames[0]);
            jgen.writeStartObject();

            for (int i = 1; i < flatteningNames.length - 1; i++) {
                jgen.writeFieldName(flatteningNames[i]);
                jgen.writeStartObject();
            }

            jgen.writeFieldName(flatteningNames[flatteningNames.length - 1]);
            writeValue(jgen, writer, value);

            for (int i = 1; i < flatteningNames.length - 1; i++) {
                jgen.writeEndObject();
            }

            jgen.writeEndObject();
        } else {
            jgen.writeFieldName(jsonPropertyName);
            writeValue(jgen, writer, value);
        }
    }

    jgen.writeEndObject();

    return;
}

private void writeValue(JsonGenerator jgen, BeanPropertyWriter writer, Object value) {
    try {
        mapper.writeValue(jgen, writer.get(value));
    } catch (Exception e) {
        throw logger.logExceptionAsError(new UncheckedIOException(new JsonGenerationException(e, jgen)));
    }
}
@alzimmermsft alzimmermsft added Client This issue points to a problem in the data-plane of the library. Azure.Core azure-core labels May 14, 2021
@alzimmermsft alzimmermsft added this to the [2021] September milestone May 27, 2021
@lmolkova lmolkova self-assigned this Sep 30, 2021
@alzimmermsft alzimmermsft self-assigned this May 9, 2022
@alzimmermsft
Copy link
Member Author

Closing as this won't be done as the current direction is moving away from usage of this code path

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Azure.Core azure-core Client This issue points to a problem in the data-plane of the library.
Projects
Status: Done
Development

No branches or pull requests

2 participants