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; 014 015import org.apache.juneau.*; 016import org.apache.juneau.utils.*; 017 018/** 019 * Subclass of {@link Serializer} for character-based serializers. 020 */ 021public abstract class WriterSerializer extends Serializer { 022 023 /** 024 * Constructor. 025 * 026 * @param ps 027 * The property store containing all the settings for this object. 028 * @param produces 029 * The media type that this serializer produces. 030 * @param accept 031 * The accept media types that the serializer can handle. 032 * <p> 033 * Can contain meta-characters per the <code>media-type</code> specification of 034 * <a class="doclink" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">RFC2616/14.1</a> 035 * <p> 036 * If empty, then assumes the only media type supported is <code>produces</code>. 037 * <p> 038 * For example, if this serializer produces <js>"application/json"</js> but should handle media types of 039 * <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be: 040 * <p class='bcode'> 041 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json"</js>, <js>"text/json"</js>); 042 * </p> 043 * <br>...or... 044 * <p class='bcode'> 045 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*​/json"</js>); 046 * </p> 047 */ 048 protected WriterSerializer(PropertyStore ps, String produces, String...accept) { 049 super(ps, produces, accept); 050 } 051 052 053 //-------------------------------------------------------------------------------- 054 // Abstract methods 055 //-------------------------------------------------------------------------------- 056 057 @Override /* SerializerSession */ 058 public abstract WriterSerializerSession createSession(SerializerSessionArgs args); 059 060 061 //-------------------------------------------------------------------------------- 062 // Other methods 063 //-------------------------------------------------------------------------------- 064 065 @Override /* Serializer */ 066 public final boolean isWriterSerializer() { 067 return true; 068 } 069 070 /** 071 * Convenience method for serializing an object to a <code>String</code>. 072 * 073 * @param o The object to serialize. 074 * @return The output serialized to a string. 075 * @throws SerializeException If a problem occurred trying to convert the output. 076 */ 077 @Override /* Serializer */ 078 public final String serialize(Object o) throws SerializeException { 079 return createSession(createDefaultSessionArgs()).serialize(o); 080 } 081 082 /** 083 * Identical to {@link #serialize(Object)} except throws a {@link RuntimeException} instead of a {@link SerializeException}. 084 * 085 * <p> 086 * This is typically good enough for debugging purposes. 087 * 088 * @param o The object to serialize. 089 * @return The serialized object. 090 */ 091 public final String toString(Object o) { 092 try { 093 return serialize(o); 094 } catch (Exception e) { 095 throw new RuntimeException(e); 096 } 097 } 098 099 /** 100 * Wraps the specified object inside a {@link StringObject}. 101 * 102 * @param o The object to wrap. 103 * @return The wrapped object. 104 */ 105 public final StringObject toStringObject(Object o) { 106 return new StringObject(this, o); 107 } 108 109 /** 110 * Convenience method for serializing an object and sending it to STDOUT. 111 * 112 * @param o The object to serialize. 113 * @return This object (for method chaining). 114 */ 115 public final WriterSerializer println(Object o) { 116 System.out.println(toString(o)); // NOT DEBUG 117 return this; 118 } 119}