001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.serializer;
018
019import org.apache.juneau.commons.reflect.*;
020import org.apache.juneau.commons.svl.*;
021import org.apache.juneau.marshall.*;
022
023/**
024 * Utility classes and methods for the {@link SerializerConfig @SerializerConfig} annotation.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a>
028 * </ul>
029 */
030@SuppressWarnings({
031   "rawtypes" // Raw types required for reflective annotation application.
032})
033public class SerializerConfigAnnotation {
034
035   /**
036    * Prevents instantiation.
037    */
038   private SerializerConfigAnnotation() {}
039
040   /**
041    * Applies {@link SerializerConfig} annotations to a {@link OutputStreamSerializer.Builder}.
042    */
043   public static class OutputStreamSerializerApply extends AnnotationApplier<SerializerConfig,OutputStreamSerializer.Builder> {
044
045      /**
046       * Constructor.
047       *
048       * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>.
049       */
050      public OutputStreamSerializerApply(VarResolverSession vr) {
051         super(SerializerConfig.class, OutputStreamSerializer.Builder.class, vr);
052      }
053
054      @Override
055      public void apply(AnnotationInfo<SerializerConfig> ai, OutputStreamSerializer.Builder b) {
056         SerializerConfig a = ai.inner();
057
058         string(a.binaryFormat()).map(BinaryFormat::valueOf).ifPresent(b::binaryFormat);
059      }
060   }
061
062   /**
063    * Applies {@link SerializerConfig} annotations to a {@link Serializer.Builder}.
064    */
065   @SuppressWarnings({
066      "unchecked" // Raw types required for reflective annotation application.
067   })
068   public static class SerializerApply extends AnnotationApplier<SerializerConfig,Serializer.Builder> {
069
070      /**
071       * Constructor.
072       *
073       * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>.
074       */
075      public SerializerApply(VarResolverSession vr) {
076         super(SerializerConfig.class, Serializer.Builder.class, vr);
077      }
078
079      @Override
080      public void apply(AnnotationInfo<SerializerConfig> ai, Serializer.Builder b) {
081         SerializerConfig a = ai.inner();
082
083         bool(a.addBeanTypes()).ifPresent(b::addBeanTypes);
084         bool(a.addRootType()).ifPresent(b::addRootType);
085         bool(a.keepNullProperties()).ifPresent(b::keepNullProperties);
086         bool(a.nonDefault()).ifPresent(b::nonDefault);
087         type(a.listener()).ifPresent(b::listener);
088         bool(a.sortCollections()).ifPresent(b::sortCollections);
089         bool(a.sortMaps()).ifPresent(b::sortMaps);
090         bool(a.trimEmptyCollections()).ifPresent(b::trimEmptyCollections);
091         bool(a.trimEmptyMaps()).ifPresent(b::trimEmptyMaps);
092         bool(a.trimStrings()).ifPresent(b::trimStrings);
093         string(a.uriContext()).map(UriContext::of).ifPresent(b::uriContext);
094         string(a.uriRelativity()).map(UriRelativity::valueOf).ifPresent(b::uriRelativity);
095         string(a.uriResolution()).map(UriResolution::valueOf).ifPresent(b::uriResolution);
096         bool(a.detectRecursions()).ifPresent(b::detectRecursions);
097         bool(a.ignoreRecursions()).ifPresent(b::ignoreRecursions);
098         integer(a.initialDepth(), "initialDepth").ifPresent(b::initialDepth);
099         integer(a.maxDepth(), "maxDepth").ifPresent(b::maxDepth);
100      }
101   }
102
103   /**
104    * Applies {@link SerializerConfig} annotations to a {@link WriterSerializer.Builder}.
105    */
106   public static class WriterSerializerApply extends AnnotationApplier<SerializerConfig,WriterSerializer.Builder> {
107
108      /**
109       * Constructor.
110       *
111       * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>.
112       */
113      public WriterSerializerApply(VarResolverSession vr) {
114         super(SerializerConfig.class, WriterSerializer.Builder.class, vr);
115      }
116
117      @Override
118      public void apply(AnnotationInfo<SerializerConfig> ai, WriterSerializer.Builder b) {
119         SerializerConfig a = ai.inner();
120
121         integer(a.maxIndent(), "maxIndent").ifPresent(b::maxIndent);
122         character(a.quoteChar(), "quoteChar").ifPresent(b::quoteChar);
123         charset(a.streamCharset()).ifPresent(b::streamCharset);
124         bool(a.useWhitespace()).ifPresent(b::useWhitespace);
125      }
126   }
127}