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.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.Serializer.Builder}. 035 */ 036 public static class SerializerApply extends AnnotationApplier<SerializerConfig,Serializer.Builder> { 037 038 /** 039 * Constructor. 040 * 041 * @param vr The resolver for resolving values in annotations. 042 */ 043 public SerializerApply(VarResolverSession vr) { 044 super(SerializerConfig.class, Serializer.Builder.class, vr); 045 } 046 047 @Override 048 public void apply(AnnotationInfo<SerializerConfig> ai, Serializer.Builder b) { 049 SerializerConfig a = ai.inner(); 050 051 bool(a.addBeanTypes()).ifPresent(x -> b.addBeanTypes(x)); 052 bool(a.addRootType()).ifPresent(x -> b.addRootType(x)); 053 bool(a.keepNullProperties()).ifPresent(x -> b.keepNullProperties(x)); 054 type(a.listener()).ifPresent(x -> b.listener(x)); 055 bool(a.sortCollections()).ifPresent(x -> b.sortCollections(x)); 056 bool(a.sortMaps()).ifPresent(x -> b.sortMaps(x)); 057 bool(a.trimEmptyCollections()).ifPresent(x -> b.trimEmptyCollections(x)); 058 bool(a.trimEmptyMaps()).ifPresent(x -> b.trimEmptyMaps(x)); 059 bool(a.trimStrings()).ifPresent(x -> b.trimStrings(x)); 060 string(a.uriContext()).map(UriContext::of).ifPresent(x -> b.uriContext(x)); 061 string(a.uriRelativity()).map(UriRelativity::valueOf).ifPresent(x -> b.uriRelativity(x)); 062 string(a.uriResolution()).map(UriResolution::valueOf).ifPresent(x -> b.uriResolution(x)); 063 bool(a.detectRecursions()).ifPresent(x -> b.detectRecursions(x)); 064 bool(a.ignoreRecursions()).ifPresent(x -> b.ignoreRecursions(x)); 065 integer(a.initialDepth(), "initialDepth").ifPresent(x -> b.initialDepth(x)); 066 integer(a.maxDepth(), "maxDepth").ifPresent(x -> b.maxDepth(x)); 067 } 068 } 069 070 /** 071 * Applies {@link SerializerConfig} annotations to a {@link org.apache.juneau.serializer.OutputStreamSerializer.Builder}. 072 */ 073 public static class OutputStreamSerializerApply extends AnnotationApplier<SerializerConfig,OutputStreamSerializer.Builder> { 074 075 /** 076 * Constructor. 077 * 078 * @param vr The resolver for resolving values in annotations. 079 */ 080 public OutputStreamSerializerApply(VarResolverSession vr) { 081 super(SerializerConfig.class, OutputStreamSerializer.Builder.class, vr); 082 } 083 084 @Override 085 public void apply(AnnotationInfo<SerializerConfig> ai, OutputStreamSerializer.Builder b) { 086 SerializerConfig a = ai.inner(); 087 088 string(a.binaryFormat()).map(BinaryFormat::valueOf).ifPresent(x -> b.binaryFormat(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}