-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[POC] A draft version for the search serializer to collect feedback #13218
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
Changes from 3 commits
37eacbc
ee0335a
c83105e
026b17e
8e4e728
7cf5f12
0eec309
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| exports com.azure.core.experimental.serializer; | ||
| exports com.azure.core.experimental.spatial; | ||
|
|
||
| opens com.azure.core.experimental.spatial to com.fasterxml.jackson.databind; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't believe this needs to open to databind as no databinding will happen with the types in spatial. They all should be using streaming deserialization.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It indeed complaint about the PointGeometry did not open module to databind. Why spatial doesn't need to do reflection when serialize? |
||
| uses com.azure.core.experimental.serializer.AvroSerializerProvider; | ||
| uses com.azure.core.experimental.serializer.JsonSerializerProvider; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,20 +6,20 @@ | |
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| /** | ||
| * Fluent builder class that configures and instantiates instances of {@link JacksonJsonSerializer}. | ||
| * Fluent builder class that configures and instantiates instances of {@link JacksonSearchSerializer}. | ||
| */ | ||
| public final class JacksonJsonSerializerBuilder { | ||
| private ObjectMapper objectMapper; | ||
|
|
||
| /** | ||
| * Constructs a new instance of {@link JacksonJsonSerializer} with the configurations set in this builder. | ||
| * Constructs a new instance of {@link JacksonSearchSerializer} with the configurations set in this builder. | ||
| * | ||
| * @return A new instance of {@link JacksonJsonSerializer}. | ||
| * @return A new instance of {@link JacksonSearchSerializer}. | ||
| */ | ||
| public JacksonJsonSerializer build() { | ||
| public JacksonSearchSerializer build() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was the Azure Core implementation renamed? Just prototyping?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Caused by IDE refactoring |
||
| return (objectMapper == null) | ||
| ? new JacksonJsonSerializer(new ObjectMapper()) | ||
| : new JacksonJsonSerializer(objectMapper); | ||
| ? new JacksonSearchSerializer(new ObjectMapper()) | ||
| : new JacksonSearchSerializer(objectMapper); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import com.azure.core.serializer.json.jackson.JacksonSearchJsonSerializerProvider; | ||
|
|
||
| module com.azure.core.serializer.json.jackson { | ||
| requires transitive com.azure.core; | ||
| requires transitive com.azure.core.experimental; | ||
|
|
||
| exports com.azure.core.serializer.json.jackson; | ||
|
|
||
| provides com.azure.core.experimental.serializer.JsonSerializerProvider | ||
| with com.azure.core.serializer.json.jackson.JacksonJsonSerializerProvider; | ||
| with JacksonSearchJsonSerializerProvider; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| com.azure.core.serializer.json.jackson.JacksonJsonSerializerProvider | ||
| com.azure.core.serializer.json.jackson.JacksonSearchJsonSerializerProvider |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| <!-- | ||
| ~ Copyright (c) Microsoft Corporation. All rights reserved. | ||
| ~ Licensed under the MIT License. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>com.azure</groupId> | ||
| <artifactId>azure-client-sdk-parent</artifactId> | ||
| <version>1.7.0</version> <!-- {x-version-update;com.azure:azure-client-sdk-parent;current} --> | ||
| <relativePath>../../parents/azure-client-sdk-parent</relativePath> | ||
| </parent> | ||
|
|
||
| <groupId>com.azure</groupId> | ||
| <artifactId>azure-search-documents-serializer-jackson</artifactId> | ||
| <packaging>jar</packaging> | ||
| <version>1.0.0-beta.1</version> <!-- {x-version-update;com.azure:azure-search-documents-serializer-json-jackson;current} --> | ||
|
|
||
| <name>Microsoft Azure Search Jackson JSON Serializer Library</name> | ||
| <description>This package contains the Jackson JSON serializer client plugin for azure-search-documents.</description> | ||
| <url>https://github.com/Azure/azure-sdk-for-java</url> | ||
|
|
||
| <licenses> | ||
| <license> | ||
| <name>The MIT License (MIT)</name> | ||
| <url>http://opensource.org/licenses/MIT</url> | ||
| <distribution>repo</distribution> | ||
| </license> | ||
| </licenses> | ||
|
|
||
| <distributionManagement> | ||
| <site> | ||
| <id>azure-java-build-docs</id> | ||
| <url>${site.url}/site/${project.artifactId}</url> | ||
| </site> | ||
| </distributionManagement> | ||
|
|
||
| <scm> | ||
| <url>https://github.com/Azure/azure-sdk-for-java</url> | ||
| <connection>scm:git:https://github.com/Azure/azure-sdk-for-java.git</connection> | ||
| <developerConnection>scm:git:https://github.com/Azure/azure-sdk-for-java.git</developerConnection> | ||
| </scm> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <legal><![CDATA[[INFO] Any downloads listed may be third party software. Microsoft grants you no rights for third party software.]]></legal> | ||
| </properties> | ||
|
|
||
| <developers> | ||
| <developer> | ||
| <id>microsoft</id> | ||
| <name>Microsoft</name> | ||
| </developer> | ||
| </developers> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.azure</groupId> | ||
| <artifactId>azure-core-experimental</artifactId> | ||
| <version>1.0.0-beta.2</version> <!-- {x-version-update;com.azure:azure-core;dependency} --> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.azure</groupId> | ||
| <artifactId>azure-search-documents-serializer</artifactId> | ||
| <version>1.0.0-beta.1</version> | ||
| </dependency> | ||
|
|
||
| <!-- Test dependencies --> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <version>5.6.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} --> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <version>5.6.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} --> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-params</artifactId> | ||
| <version>5.6.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} --> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <version>3.0.0</version><!-- {x-version-update;org.mockito:mockito-core;external_dependency} --> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.projectreactor</groupId> | ||
| <artifactId>reactor-test</artifactId> | ||
| <version>3.3.5.RELEASE</version> <!-- {x-version-update;io.projectreactor:reactor-test;external_dependency} --> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <profiles> | ||
| <profile> | ||
| <id>java-lts</id> | ||
| <activation> | ||
| <jdk>[11,)</jdk> | ||
| </activation> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} --> | ||
| <configuration> | ||
| <argLine> | ||
| --add-exports com.azure.search.documents.serializer.json.jackson/com.azure.search.documents.serializer.json.jackson=com.fasterxml.jackson.databind | ||
| --add-opens com.azure.search.documents.serializer.json.jackson/com.azure.search.documents.serializer.json.jackson=ALL-UNNAMED | ||
| </argLine> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </profile> | ||
| </profiles> | ||
|
|
||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.search.documents.serializer.jackson; | ||
|
|
||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.azure.search.documents.serializer.SearchSerializer; | ||
| import com.azure.search.documents.serializer.SearchType; | ||
| import com.azure.search.documents.serializer.SerializationInclusion; | ||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.type.TypeFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Reader; | ||
|
|
||
| public class SearchJacksonSerializer implements SearchSerializer { | ||
| private final ClientLogger logger = new ClientLogger(SearchJacksonSerializer.class); | ||
| private final ObjectMapper mapper; | ||
| private final TypeFactory typeFactory; | ||
|
|
||
| SearchJacksonSerializer(ObjectMapper mapper) { | ||
| this.mapper = mapper; | ||
| this.typeFactory = mapper.getTypeFactory(); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T convertValue(Object o, SearchType<T> type) { | ||
| return mapper.convertValue(o, typeFactory.constructType(type.getType())); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T convertValue(Object o, SearchType<T> type, SerializationInclusion inclusion) { | ||
| if (inclusion == SerializationInclusion.ALWAYS) { | ||
| mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); | ||
| } | ||
| return mapper.convertValue(o, typeFactory.constructType(type.getType())); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T convertValue(Object o, Class<T> clazz) { | ||
| return mapper.convertValue(o, clazz); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T convertValue(Object o, Class<T> clazz, SerializationInclusion inclusion) { | ||
| if (inclusion == SerializationInclusion.ALWAYS) { | ||
| mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); | ||
| } | ||
| return mapper.convertValue(o, clazz); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T readValue(Reader reader, SearchType<T> type) { | ||
| try { | ||
| return mapper.readValue(reader, typeFactory.constructType(type.getType())); | ||
| } catch (IOException ex) { | ||
| throw logger.logExceptionAsError(new RuntimeException(ex)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T readValue(String jsonString, SearchType<T> type) { | ||
| try { | ||
| return mapper.readValue(jsonString, typeFactory.constructType(type.getType())); | ||
| } catch (IOException ex) { | ||
| throw logger.logExceptionAsError(new RuntimeException(ex)); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.search.documents.serializer.jackson; | ||
|
|
||
| import com.azure.core.util.serializer.JacksonAdapter; | ||
| import com.azure.search.documents.serializer.SearchSerializer; | ||
| import com.azure.search.documents.serializer.jackson.implementation.SerializationUtil; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| public class SearchJacksonSerializerBuilder { | ||
| private ObjectMapper objectMapper; | ||
| private static final ObjectMapper DEFAULT_MAPPER; | ||
| static { | ||
| DEFAULT_MAPPER = new JacksonAdapter().serializer(); | ||
| SerializationUtil.configureMapper(DEFAULT_MAPPER); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new instance of {@link SearchSerializer} with the configurations set in this builder. | ||
| * | ||
| * @return A new instance of {@link SearchSerializer}. | ||
| */ | ||
| public SearchSerializer build() { | ||
| return (objectMapper == null) | ||
| ? new SearchJacksonSerializer(DEFAULT_MAPPER) | ||
| : new SearchJacksonSerializer(objectMapper); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the {@link ObjectMapper} that will be used during serialization. | ||
| * <p> | ||
| * If this is set to {@code null} the default {@link ObjectMapper} will be used. | ||
| * | ||
| * @param objectMapper {@link ObjectMapper} that will be used during serialization. | ||
| * @return The updated JacksonJsonSerializerBuilder class. | ||
| */ | ||
| public SearchJacksonSerializerBuilder serializer(ObjectMapper objectMapper) { | ||
| this.objectMapper = objectMapper; | ||
| return this; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine for testing this, in the future when this work is merged into Azure Core it'll become package private again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I think it doesn't hurt to public the serializer before we move back Azure Core.