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.utils; 014 015import java.io.*; 016 017import org.apache.juneau.*; 018import org.apache.juneau.http.*; 019import org.apache.juneau.json.*; 020import org.apache.juneau.serializer.*; 021 022/** 023 * A serializer/object pair used for delayed object serialization. 024 * 025 * <p> 026 * Useful in certain conditions such as logging when you don't want to needlessly serialize objects. 027 * 028 * <p> 029 * Instances of this method are created by the {@link WriterSerializer#toStringObject(Object)} method. 030 * 031 * <h5 class='section'>Example:</h5> 032 * <p class='bcode'> 033 * <jc>// The POJO will not be serialized unless DEBUG is enabled.</jc> 034 * logger.log(<jsf>DEBUG</jsf>, <js>"Object contents are: {0}"</js>, JsonSerializer.<jsf>DEFAULT</jsf>.toObjectString(myPojo)); 035 * </p> 036 */ 037public class StringObject implements CharSequence, Writable { 038 039 private final WriterSerializer s; 040 private final Object o; 041 private String results; 042 043 /** 044 * Constructor. 045 * 046 * @param s The serializer to use to serialize the object. 047 * @param o The object to be serialized. 048 */ 049 public StringObject(WriterSerializer s, Object o) { 050 this.s = s; 051 this.o = o; 052 } 053 054 /** 055 * Constructor with default serializer {@link JsonSerializer#DEFAULT_LAX} 056 * 057 * @param o The object to be serialized. 058 */ 059 public StringObject(Object o) { 060 this(JsonSerializer.DEFAULT_LAX, o); 061 } 062 063 @Override /* Object */ 064 public String toString() { 065 if (results == null) 066 results = s.toString(o); 067 return results; 068 } 069 070 @Override /* CharSequence */ 071 public int length() { 072 return toString().length(); 073 } 074 075 @Override /* CharSequence */ 076 public char charAt(int index) { 077 return toString().charAt(index); 078 } 079 080 @Override /* CharSequence */ 081 public CharSequence subSequence(int start, int end) { 082 return toString().subSequence(start, end); 083 } 084 085 @Override /* Writable */ 086 public Writer writeTo(Writer w) throws IOException { 087 try { 088 s.serialize(o, w); 089 return w; 090 } catch (SerializeException e) { 091 throw new IOException(e); 092 } 093 } 094 095 @Override /* Writable */ 096 public MediaType getMediaType() { 097 return s.getMediaTypes()[0]; 098 } 099}