001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.ajax.json;
018
019import com.github.openjson.JSONString;
020import org.apache.wicket.util.io.IClusterable;
021import org.apache.wicket.util.lang.Args;
022
023/**
024 * Represents a Json function. When written out these values are not escaped, so it's possible to write out raw
025 * JavaScript.
026 */
027public class JSONFunction implements JSONString, CharSequence, IClusterable
028{
029        private static final long serialVersionUID = 1L;
030        private final CharSequence value;
031
032        /**
033         * Function to be used to output the json value without quotes
034         * @param value the value
035         */
036        public JSONFunction(CharSequence value)
037        {
038                this.value = Args.notNull(value, "value");
039        }
040
041        @Override
042        public String toString()
043        {
044                return toJSONString();
045        }
046
047        @Override
048        public String toJSONString()
049        {
050                return value.toString();
051        }
052
053        @Override
054        public int length()
055        {
056                return value.length();
057        }
058
059        @Override
060        public char charAt(int index)
061        {
062                return value.charAt(index);
063        }
064
065        @Override
066        public CharSequence subSequence(int start, int end)
067        {
068                return value.subSequence(start, end);
069        }
070}