Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
yawkat committed Oct 8, 2024
1 parent 5d4b468 commit db23ccd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ final class DefaultSerdeConfiguration implements SerdeConfiguration {
@Bindable(defaultValue = LimitingStream.DEFAULT_MAXIMUM_DEPTH + "") int maximumNestingDepth,
@Bindable(defaultValue = DEFAULT_INET_ADDRESS_AS_NUMERIC + "") boolean inetAddressAsNumeric,
@Nullable String propertyNamingStrategy,
@Property(name = "jackson.json-view.enabled", defaultValue = "false") boolean jsonViewEnabled) {
@Bindable(defaultValue = "false") boolean jsonViewEnabled,
@Property(name = "jackson.json-view.enabled", defaultValue = "false") boolean jacksonJsonViewEnabled) {
this.dateFormat = dateFormat;
this.timeWriteShape = timeWriteShape;
this.numericTimeUnit = numericTimeUnit;
Expand All @@ -74,7 +75,7 @@ final class DefaultSerdeConfiguration implements SerdeConfiguration {
this.inetAddressAsNumeric = inetAddressAsNumeric;
this.propertyNamingStrategyName = propertyNamingStrategy;
this.propertyNamingStrategy = PropertyNamingStrategy.forName(propertyNamingStrategy).orElse(null);
this.jsonViewEnabled = jsonViewEnabled;
this.jsonViewEnabled = jsonViewEnabled || jacksonJsonViewEnabled;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import io.micronaut.serde.config.DeserializationConfiguration;
import io.micronaut.serde.config.SerdeConfiguration;
import io.micronaut.serde.config.SerializationConfiguration;
import io.micronaut.serde.support.util.ViewUtil;
import io.micronaut.serde.support.util.JsonViewUtil;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.reactivestreams.Processor;
Expand Down Expand Up @@ -144,7 +144,7 @@ public JsonMapper createSpecific(@NonNull Argument<?> type) {
// In a case of unknown type return this non-specific mapper
mapper = this;
}
Class<?> viewClass = ViewUtil.extractView(serdeConfiguration, type, view);
Class<?> viewClass = JsonViewUtil.extractView(serdeConfiguration, type, view);
if (viewClass != view) {
return mapper.cloneWithViewClass(viewClass);
}
Expand Down Expand Up @@ -231,7 +231,7 @@ private <T> void writeValue(JsonGenerator gen, T value, Argument<T> argument) th
if (argument.equalsType(specificType)) {
serializer = (Serializer<? super T>) specificSerializer;
} else {
Class<?> viewClass = ViewUtil.extractView(serdeConfiguration, argument, view);
Class<?> viewClass = JsonViewUtil.extractView(serdeConfiguration, argument, view);
if (viewClass != view) {
encoderContext = registry.newEncoderContext(viewClass);
}
Expand All @@ -257,7 +257,7 @@ private <T> T readValue0(JsonParser parser, Argument<?> type) throws IOException
if (type.equalsType(specificType)) {
deserializer = specificDeserializer;
} else {
Class<?> viewClass = ViewUtil.extractView(serdeConfiguration, type, view);
Class<?> viewClass = JsonViewUtil.extractView(serdeConfiguration, type, view);
if (viewClass != view) {
decoderContext = registry.newDecoderContext(viewClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import io.micronaut.serde.support.util.BufferingJsonNodeProcessor;
import io.micronaut.serde.support.util.JsonNodeDecoder;
import io.micronaut.serde.support.util.JsonNodeEncoder;
import io.micronaut.serde.support.util.ViewUtil;
import io.micronaut.serde.support.util.JsonViewUtil;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.json.Json;
Expand Down Expand Up @@ -96,7 +96,7 @@ public JsonMapper cloneWithViewClass(Class<?> viewClass) {

@Override
public <T> T readValueFromTree(JsonNode tree, Argument<T> type) throws IOException {
Deserializer.DecoderContext context = registry.newDecoderContext(ViewUtil.extractView(serdeConfiguration, type, view));
Deserializer.DecoderContext context = registry.newDecoderContext(JsonViewUtil.extractView(serdeConfiguration, type, view));
final Deserializer<? extends T> deserializer = context.findDeserializer(type).createSpecific(context, type);
return deserializer.deserialize(
JsonNodeDecoder.create(tree, limits()),
Expand All @@ -121,7 +121,7 @@ public <T> T readValue(byte[] byteArray, Argument<T> type) throws IOException {

private <T> T readValue(JsonParser parser, Argument<T> type) throws IOException {
Decoder decoder = new JsonParserDecoder(parser, limits());
Deserializer.DecoderContext context = registry.newDecoderContext(ViewUtil.extractView(serdeConfiguration, type, view));
Deserializer.DecoderContext context = registry.newDecoderContext(JsonViewUtil.extractView(serdeConfiguration, type, view));
final Deserializer<? extends T> deserializer = context.findDeserializer(type).createSpecific(context, type);
return deserializer.deserialize(
decoder,
Expand Down Expand Up @@ -196,7 +196,7 @@ private void serialize(Encoder encoder, Object object) throws IOException {
}

private void serialize(Encoder encoder, Object object, Argument type) throws IOException {
Serializer.EncoderContext context = registry.newEncoderContext(ViewUtil.extractView(serdeConfiguration, type, view));
Serializer.EncoderContext context = registry.newEncoderContext(JsonViewUtil.extractView(serdeConfiguration, type, view));
final Serializer<Object> serializer = context.findSerializer(type).createSpecific(context, type);
serializer.serialize(
encoder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
* @since 2.12
*/
@Internal
public final class ViewUtil {
private ViewUtil() {
public final class JsonViewUtil {
private JsonViewUtil() {
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/main/docs/guide/jacksonAnnotations.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,9 @@ or Maven:
<scope>provided</scope>
</dependency>
----

=== `@JsonView` on controllers

The Micronaut HTTP server supports declaring the Jackson `@JsonView` annotation on controllers to configure a subset of
fields to be serialized. `micronaut-serialization` supports this feature when enabled through the
`jackson.json-view.enabled` or `micronaut.serde.json-view-enabled` configuration property.

0 comments on commit db23ccd

Please sign in to comment.