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.httppart;
014
015import org.apache.juneau.*;
016import org.apache.juneau.uon.*;
017
018/**
019 * An extension of {@link UonPartSerializer} with plain-text string handling.
020 * 
021 * <p>
022 * Uses UON notation for beans and maps (serialized as UON objects), and plain text for everything else.
023 * <br>Collections/arrays are also serialized as comma-delimited lists.
024 * 
025 * <p>
026 * The downside to this class vs. {@link UonPartSerializer} is that you may lose type information on the parse side.
027 * For example, it's not possible to distinguish between the boolean <jk>false</jk> and the string <js>"false"</js>.
028 * The same is true of numbers.  Also, whitespace in strings or strings containing single quotes may get lost if using
029 * the {@link UonPartParser} to process them.
030 */
031public class SimpleUonPartSerializer extends UonPartSerializer {
032
033   //-------------------------------------------------------------------------------------------------------------------
034   // Predefined instances
035   //-------------------------------------------------------------------------------------------------------------------
036
037   /** Reusable instance of {@link SimpleUonPartSerializer}, all default settings. */
038   public static final SimpleUonPartSerializer DEFAULT = new SimpleUonPartSerializer(PropertyStore.DEFAULT);
039
040
041   //-------------------------------------------------------------------------------------------------------------------
042   // Instance
043   //-------------------------------------------------------------------------------------------------------------------
044
045   /**
046    * Constructor.
047    * 
048    * @param ps
049    *    The property store containing all the settings for this object.
050    */
051   public SimpleUonPartSerializer(PropertyStore ps) {
052      super(
053         ps.builder()
054            .set(UON_paramFormat, ParamFormat.PLAINTEXT)
055            .build() 
056      );
057   }
058   
059   @Override /* Context */
060   public SimpleUonPartSerializerBuilder builder() {
061      return new SimpleUonPartSerializerBuilder(getPropertyStore());
062   }
063
064   /**
065    * Instantiates a new clean-slate {@link SimpleUonPartSerializerBuilder} object.
066    * 
067    * <p>
068    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 
069    * the settings of the object called on.
070    * 
071    * @return A new {@link UonPartSerializerBuilder} object.
072    */
073   public static SimpleUonPartSerializerBuilder create() {
074      return new SimpleUonPartSerializerBuilder();
075   }
076}