001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.functions.conversion; 020 021import java.nio.charset.Charset; 022 023import lucee.commons.io.CharsetUtil; 024import lucee.commons.lang.StringUtil; 025import lucee.runtime.PageContext; 026import lucee.runtime.PageContextImpl; 027import lucee.runtime.converter.ConverterException; 028import lucee.runtime.converter.JSONConverter; 029import lucee.runtime.exp.FunctionException; 030import lucee.runtime.exp.PageException; 031import lucee.runtime.ext.function.Function; 032import lucee.runtime.functions.BIF; 033import lucee.runtime.op.Caster; 034 035/** 036 * Decodes Binary Data that are encoded as String 037 */ 038public final class SerializeJSON extends BIF implements Function { 039 040 private static final long serialVersionUID = -4632952919389635891L; 041 042 public static String call(PageContext pc, Object var) throws PageException { 043 return _call(pc, var, false, ((PageContextImpl)pc).getWebCharset()); 044 } 045 public static String call(PageContext pc, Object var,boolean serializeQueryByColumns) throws PageException { 046 return _call(pc, var, serializeQueryByColumns, ((PageContextImpl)pc).getWebCharset()); 047 } 048 public static String call(PageContext pc, Object var,boolean serializeQueryByColumns, String strCharset) throws PageException { 049 Charset cs=StringUtil.isEmpty(strCharset)?((PageContextImpl)pc).getWebCharset():CharsetUtil.toCharset(strCharset); 050 return _call(pc, var, serializeQueryByColumns, cs); 051 } 052 private static String _call(PageContext pc, Object var,boolean serializeQueryByColumns, Charset charset) throws PageException { 053 try { 054 return new JSONConverter(true,charset).serialize(pc,var,serializeQueryByColumns); 055 } catch (ConverterException e) { 056 throw Caster.toPageException(e); 057 } 058 } 059 060 @Override 061 public Object invoke(PageContext pc, Object[] args) throws PageException { 062 if (args.length == 1) { 063 return call(pc, args[0]); 064 } else if (args.length == 2) { 065 return call(pc, args[0], Caster.toBoolean(args[1])); 066 } else if (args.length == 3) { 067 return call(pc, args[0], Caster.toBoolean(args[1]), Caster.toString(args[2])); 068 } 069 throw new FunctionException(pc, "serializeJSON", 0, 2, args.length - 1); 070 } 071}