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.serializer.annotation;
018
019import org.apache.juneau.*;
020import org.apache.juneau.commons.reflect.*;
021import org.apache.juneau.serializer.*;
022import org.apache.juneau.svl.*;
023
024/**
025 * Utility classes and methods for the {@link SerializerConfig @SerializerConfig} annotation.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a>
029 * </ul>
030 */
031public class SerializerConfigAnnotation {
032
033   /**
034    * Applies {@link SerializerConfig} annotations to a {@link org.apache.juneau.serializer.OutputStreamSerializer.Builder}.
035    */
036   public static class OutputStreamSerializerApply extends AnnotationApplier<SerializerConfig,OutputStreamSerializer.Builder> {
037
038      /**
039       * Constructor.
040       *
041       * @param vr The resolver for resolving values in annotations.
042       */
043      public OutputStreamSerializerApply(VarResolverSession vr) {
044         super(SerializerConfig.class, OutputStreamSerializer.Builder.class, vr);
045      }
046
047      @Override
048      public void apply(AnnotationInfo<SerializerConfig> ai, OutputStreamSerializer.Builder b) {
049         SerializerConfig a = ai.inner();
050
051         string(a.binaryFormat()).map(BinaryFormat::valueOf).ifPresent(x -> b.binaryFormat(x));
052      }
053   }
054
055   /**
056    * Applies {@link SerializerConfig} annotations to a {@link org.apache.juneau.serializer.Serializer.Builder}.
057    */
058   public static class SerializerApply extends AnnotationApplier<SerializerConfig,Serializer.Builder> {
059
060      /**
061       * Constructor.
062       *
063       * @param vr The resolver for resolving values in annotations.
064       */
065      public SerializerApply(VarResolverSession vr) {
066         super(SerializerConfig.class, Serializer.Builder.class, vr);
067      }
068
069      @Override
070      public void apply(AnnotationInfo<SerializerConfig> ai, Serializer.Builder b) {
071         SerializerConfig a = ai.inner();
072
073         bool(a.addBeanTypes()).ifPresent(x -> b.addBeanTypes(x));
074         bool(a.addRootType()).ifPresent(x -> b.addRootType(x));
075         bool(a.keepNullProperties()).ifPresent(x -> b.keepNullProperties(x));
076         type(a.listener()).ifPresent(x -> b.listener(x));
077         bool(a.sortCollections()).ifPresent(x -> b.sortCollections(x));
078         bool(a.sortMaps()).ifPresent(x -> b.sortMaps(x));
079         bool(a.trimEmptyCollections()).ifPresent(x -> b.trimEmptyCollections(x));
080         bool(a.trimEmptyMaps()).ifPresent(x -> b.trimEmptyMaps(x));
081         bool(a.trimStrings()).ifPresent(x -> b.trimStrings(x));
082         string(a.uriContext()).map(UriContext::of).ifPresent(x -> b.uriContext(x));
083         string(a.uriRelativity()).map(UriRelativity::valueOf).ifPresent(x -> b.uriRelativity(x));
084         string(a.uriResolution()).map(UriResolution::valueOf).ifPresent(x -> b.uriResolution(x));
085         bool(a.detectRecursions()).ifPresent(x -> b.detectRecursions(x));
086         bool(a.ignoreRecursions()).ifPresent(x -> b.ignoreRecursions(x));
087         integer(a.initialDepth(), "initialDepth").ifPresent(x -> b.initialDepth(x));
088         integer(a.maxDepth(), "maxDepth").ifPresent(x -> b.maxDepth(x));
089      }
090   }
091
092   /**
093    * Applies {@link SerializerConfig} annotations to a {@link org.apache.juneau.serializer.WriterSerializer.Builder}.
094    */
095   public static class WriterSerializerApply extends AnnotationApplier<SerializerConfig,WriterSerializer.Builder> {
096
097      /**
098       * Constructor.
099       *
100       * @param vr The resolver for resolving values in annotations.
101       */
102      public WriterSerializerApply(VarResolverSession vr) {
103         super(SerializerConfig.class, WriterSerializer.Builder.class, vr);
104      }
105
106      @Override
107      public void apply(AnnotationInfo<SerializerConfig> ai, WriterSerializer.Builder b) {
108         SerializerConfig a = ai.inner();
109
110         charset(a.fileCharset()).ifPresent(x -> b.fileCharset(x));
111         integer(a.maxIndent(), "maxIndent").ifPresent(x -> b.maxIndent(x));
112         character(a.quoteChar(), "quoteChar").ifPresent(x -> b.quoteChar(x));
113         charset(a.streamCharset()).ifPresent(x -> b.streamCharset(x));
114         bool(a.useWhitespace()).ifPresent(x -> b.useWhitespace(x));
115      }
116   }
117}