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 java.io.*; 016 017import org.apache.juneau.*; 018import org.apache.juneau.internal.*; 019import org.apache.juneau.uon.*; 020 021/** 022 * @deprecated Use {@link UonSerializer} 023 */ 024@Deprecated 025public class UonPartSerializer extends UonSerializer { 026 027 //------------------------------------------------------------------------------------------------------------------- 028 // Predefined instances 029 //------------------------------------------------------------------------------------------------------------------- 030 031 /** Reusable instance of {@link UonPartSerializer}, all default settings. */ 032 public static final UonPartSerializer DEFAULT = new UonPartSerializer(PropertyStore.DEFAULT); 033 034 035 //------------------------------------------------------------------------------------------------------------------- 036 // Instance 037 //------------------------------------------------------------------------------------------------------------------- 038 039 /** 040 * Constructor. 041 * 042 * @param ps 043 * The property store containing all the settings for this object. 044 */ 045 public UonPartSerializer(PropertyStore ps) { 046 super( 047 ps.builder() 048 .set(UON_encoding, false) 049 .build() 050 ); 051 } 052 053 @Override /* Context */ 054 public UonPartSerializerBuilder builder() { 055 return new UonPartSerializerBuilder(getPropertyStore()); 056 } 057 058 /** 059 * Instantiates a new clean-slate {@link UonPartSerializerBuilder} object. 060 * 061 * <p> 062 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 063 * the settings of the object called on. 064 * 065 * @return A new {@link UonPartSerializerBuilder} object. 066 */ 067 public static UonPartSerializerBuilder create() { 068 return new UonPartSerializerBuilder(); 069 } 070 071 //-------------------------------------------------------------------------------- 072 // Entry point methods 073 //-------------------------------------------------------------------------------- 074 075 @Override 076 public String serialize(HttpPartType type, Object value) { 077 try { 078 // Shortcut for simple types. 079 ClassMeta<?> cm = getClassMetaForObject(value); 080 if (cm != null) { 081 if (cm.isNumber() || cm.isBoolean()) 082 return ClassUtils.toString(value); 083 if (cm.isString()) { 084 String s = ClassUtils.toString(value); 085 if (s.isEmpty() || ! UonUtils.needsQuotes(s)) 086 return s; 087 } 088 } 089 StringWriter w = new StringWriter(); 090 UonSerializerSession s = new UonSerializerSession(this, false, createDefaultSessionArgs()); 091 s.serialize(value, w); 092 return w.toString(); 093 } catch (Exception e) { 094 throw new RuntimeException(e); 095 } 096 } 097}