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