Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions eng/versioning/version_client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ com.azure:azure-messaging-eventhubs;5.1.2;5.2.0-beta.1
com.azure:azure-messaging-eventhubs-checkpointstore-blob;1.1.2;1.2.0-beta.1
com.azure:azure-messaging-servicebus;7.0.0-beta.3;7.0.0-beta.4
com.azure:azure-search-documents;11.0.0;11.1.0-beta.1
com.azure:azure-search-documents-serializer-json-jackson;1.0.0;1.0.0-beta.1
com.azure:azure-security-keyvault-certificates;4.0.4;4.1.0-beta.5
com.azure:azure-security-keyvault-keys;4.1.4;4.2.0-beta.6
com.azure:azure-security-keyvault-secrets;4.1.4;4.2.0-beta.5
Expand Down Expand Up @@ -72,6 +73,11 @@ com.microsoft.azure:spring-data-cosmosdb;3.0.0-beta.1;3.0.0-beta.1

unreleased_com.azure:azure-messaging-servicebus;7.0.0-beta.4
unreleased_com.azure:azure-security-keyvault-keys;4.2.0-beta.6
unreleased_com.azure:azure-core-experimental;1.0.0-beta.2
unreleased_com.azure:azure-core-serializer-json-jackson;1.0.0-beta.3
unreleased_com.azure:azure-core-serializer-json-gson;1.0.0-beta.3
unreleased_com.azure-search-documents-serializer-json-jackson;1.0.0-beta.1
unreleased_com.azure-search-documents-serializer-json-gson;1.0.0-beta.1

# Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current
# version and set the version to the released beta. Released beta dependencies are only valid
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.experimental.serializer;

/**
* The enum to indicate how to include
*/
public enum JsonInclusion {
ALWAYS,
DEFAULT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.experimental.serializer;

/**
* The json serializer options.
*/
public class JsonOptions {
private JsonInclusion jsonInclusion;

/**
* Gets the json inclusion value.
* @return The enum field json inclusion.
*/
public JsonInclusion getJsonInclusion() {
return jsonInclusion;
}

/**
* Sets the json inclusion value.
* @param jsonInclusion The enum field json inclusion.
* @return The {@link JsonOptions} object itself.
*/
public JsonOptions setJsonInclusion(JsonInclusion jsonInclusion) {
this.jsonInclusion = jsonInclusion;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public interface JsonSerializer extends ObjectSerializer {
@Override
<T> Mono<T> deserialize(InputStream stream, Class<T> clazz);

/**
* Reads a JSON stream into its object representation.
*
* @param stream JSON stream.
* @param toType {@link Type} The type Json stream converts to.
* @param <T> Type of the object.
* @return The object represented by the deserialized JSON stream.
*/
@Override
<T> Mono<T> deserialize(InputStream stream, Type<T> toType);

/**
* Reads a JSON tree into its object representation.
*
Expand Down Expand Up @@ -68,4 +79,25 @@ public interface JsonSerializer extends ObjectSerializer {
* @return The JSON tree representing the object.
*/
Mono<JsonNode> toTree(Object value);


/**
* Converts a Java object into a target {@link Type}.
*
* @param fromValue The value which converts from
* @param toType The type which converts to
* @param <T> The generic type to covert to.
* @return The JSON tree representing the deserialized JSON byte array.
*/
<T> Mono<T> convertValue(Object fromValue, Type<T> toType);

/**
* Converts a Java object into a target model class.
*
* @param fromValue The value which converts from
* @param clazz Representing the class.
* @param <T> The generic type to covert to.
* @return The JSON tree representing the deserialized JSON byte array.
*/
<T> Mono<T> convertValue(Object fromValue, Class<T> clazz);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ public interface JsonSerializerProvider {
* @return A new {@link JsonSerializer} instance.
*/
JsonSerializer createInstance();


/**
* Creates a new instance of the {@link JsonSerializer} that this JsonSerializerProvider is configured to create.
*
* @param jsonOptions The json options for the serializer.
* @return A new {@link JsonSerializer} instance.
*/
JsonSerializer createInstance(JsonOptions jsonOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public static JsonSerializer createInstance() {
return defaultProvider.createInstance();
}


/**
* Creates an instance of {@link JsonSerializer} using the first {@link JsonSerializerProvider} found in the
* classpath.
*
* @param jsonOptions The json options for the serializer.
* @return A new instance of {@link JsonSerializer}.
*/
public static JsonSerializer createInstance(JsonOptions jsonOptions) {
if (defaultProvider == null) {
loadFromClasspath();
}

return defaultProvider.createInstance(jsonOptions);
}

private static synchronized void loadFromClasspath() {
if (attemptedLoad && defaultProvider != null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public interface ObjectSerializer {
*/
<T> Mono<T> deserialize(InputStream stream, Class<T> clazz);

/**
* Reads a stream into its object representation.
*
* @param stream {@link InputStream} of data.
* @param type {@link Type} The type Json stream converts to.
* @param <T> Type of the object.
* @return The object represented by the deserialized stream.
*/
<T> Mono<T> deserialize(InputStream stream, Type<T> type);

/**
* Writes the object into a stream.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.core.experimental.serializer;

import java.lang.reflect.ParameterizedType;

public abstract class Type<T> {

private final java.lang.reflect.Type javaType;

/**
* Constructor
* @throws IllegalArgumentException if the reference is constructed without actual type information
*/
public Type() {
java.lang.reflect.Type superClass = this.getClass().getGenericSuperclass();
if (superClass instanceof Class) {
throw new IllegalArgumentException(
"Internal error: TypeReference constructed without actual type information");
} else {
this.javaType = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}
}

/**
* Return class T type
* @return type
*/
public java.lang.reflect.Type getJavaType() {
return javaType;
}

/**
* Returns is the type a ParameterizedType (collection, string etc.)
* @return boolean value
*/
public boolean isParameterizedType() {
if (!(javaType instanceof ParameterizedType)) {
return false;
}

return true;
}

/**
* Get the type of the actual type of the Type object
* @return ava.lang.reflect.Type
*/
public java.lang.reflect.Type getListType() {
assert isParameterizedType();
ParameterizedType parameterizedType = (ParameterizedType) javaType;
return parameterizedType.getActualTypeArguments()[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Deserializes a JSON object into a {@link Geometry}.
*/
final class GeometryDeserializer extends JsonDeserializer<Geometry> {
public final class GeometryDeserializer extends JsonDeserializer<Geometry> {
Copy link
Member

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.

Copy link
Contributor Author

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.

private static final ClientLogger LOGGER = new ClientLogger(GeometryDeserializer.class);

/*
Expand Down Expand Up @@ -60,6 +60,10 @@ final class GeometryDeserializer extends JsonDeserializer<Geometry> {
.addDeserializer(CollectionGeometry.class, geometrySubclassDeserializer(CollectionGeometry.class));
}

public static SimpleModule getModule() {
return MODULE;
}

@Override
public Geometry deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return read(ctxt.readTree(p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
/**
* Serializes a {@link Geometry} into JSON.
*/
final class GeometrySerializer extends JsonSerializer<Geometry> {
public final class GeometrySerializer extends JsonSerializer<Geometry> {
private static final ClientLogger LOGGER = new ClientLogger(GeometrySerializer.class);

static final SimpleModule MODULE;

static {
MODULE = new SimpleModule();
MODULE.addSerializer(Geometry.class, new GeometrySerializer());
/**
* Gets a module wrapping this serializer as an adapter for the Jackson
* ObjectMapper.
*
* @return a simple module to be plugged onto Jackson ObjectMapper.
*/
public static SimpleModule getModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(Geometry.class, new GeometrySerializer());
return module;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -28,7 +28,7 @@ public class GeometrySerializerTests {
private static final ObjectMapper MAPPER;

static {
MAPPER = new ObjectMapper().registerModule(GeometrySerializer.MODULE);
MAPPER = new ObjectMapper().registerModule(GeometrySerializer.getModule());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.core.experimental.serializer.JsonNode;
import com.azure.core.experimental.serializer.JsonSerializer;
import com.azure.core.experimental.serializer.Type;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -36,6 +37,12 @@ public <T> Mono<T> deserialize(InputStream stream, Class<T> clazz) {
return Mono.fromCallable(() -> gson.fromJson(new InputStreamReader(stream, StandardCharsets.UTF_8), clazz));
}

@Override
public <T> Mono<T> deserialize(InputStream stream, Type<T> toType) {
return Mono.fromCallable(() -> gson.fromJson(new InputStreamReader(stream, StandardCharsets.UTF_8),
toType.getJavaType()));
}

@Override
public <T> Mono<T> deserializeTree(JsonNode jsonNode, Class<T> clazz) {
return Mono.fromCallable(() -> gson.fromJson(JsonNodeUtils.toGsonElement(jsonNode), clazz));
Expand Down Expand Up @@ -68,5 +75,14 @@ public Mono<JsonNode> toTree(Object value) {
return Mono.fromCallable(() -> JsonNodeUtils.fromGsonElement(gson.toJsonTree(value)));
}

@Override
public <T> Mono<T> convertValue(Object fromValue, Type<T> toType) {
return Mono.fromCallable(() -> gson.fromJson(gson.toJson(fromValue), toType.getJavaType()));
}

@Override
public <T> Mono<T> convertValue(Object fromValue, Class<T> clazz) {
return Mono.fromCallable(() -> gson.fromJson(gson.toJson(fromValue), clazz));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@

package com.azure.core.serializer.json.gson;

import com.azure.core.experimental.serializer.JsonInclusion;
import com.azure.core.experimental.serializer.JsonOptions;
import com.google.gson.Gson;

/**
* Fluent builder class that configures and instantiates instances of {@link GsonJsonSerializer}.
*/
public final class GsonJsonSerializerBuilder {
private Gson gson;
private JsonOptions options;

/**
* Constructs a new instance of {@link GsonJsonSerializer} with the configurations set in this builder.
*
* @return A new instance of {@link GsonJsonSerializer}.
*/
public GsonJsonSerializer build() {
return (gson == null)
? new GsonJsonSerializer(new Gson())
: new GsonJsonSerializer(gson);
Gson localGson = (gson == null) ? new Gson() : gson;

if (options != null && options.getJsonInclusion() == JsonInclusion.ALWAYS) {
localGson.serializeNulls();
}
return new GsonJsonSerializer(localGson);
}

/**
Expand All @@ -34,4 +40,18 @@ public GsonJsonSerializerBuilder serializer(Gson gson) {
this.gson = gson;
return this;
}


/**
* Sets the {@link Gson} that will be used during serialization.
* <p>
* If this is set to {@code null} the default {@link Gson} will be used.
*
* @param options {@link Gson} that will be used during serialization.
* @return The updated GsonJsonSerializerBuilder class.
*/
public GsonJsonSerializerBuilder options(JsonOptions options) {
this.options = options;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.core.serializer.json.gson;

import com.azure.core.experimental.serializer.JsonOptions;
import com.azure.core.experimental.serializer.JsonSerializer;
import com.azure.core.experimental.serializer.JsonSerializerProvider;

Expand All @@ -14,4 +15,9 @@ public class GsonJsonSerializerProvider implements JsonSerializerProvider {
public JsonSerializer createInstance() {
return new GsonJsonSerializerBuilder().build();
}

@Override
public JsonSerializer createInstance(JsonOptions jsonOptions) {
return new GsonJsonSerializerBuilder().options(jsonOptions).build();
}
}
Loading