    001// Copyright 2011-2013 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// 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
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.json.modules;
    016
    017import org.apache.tapestry5.commons.Configuration;
    018import org.apache.tapestry5.commons.MappedConfiguration;
    019import org.apache.tapestry5.commons.services.CoercionTuple;
    020import org.apache.tapestry5.commons.services.TypeCoercer;
    021import org.apache.tapestry5.internal.json.StringToJSONArray;
    022import org.apache.tapestry5.internal.json.StringToJSONObject;
    023import org.apache.tapestry5.ioc.annotations.Contribute;
    024import org.apache.tapestry5.json.JSONArray;
    025import org.apache.tapestry5.json.JSONObject;
    026
    027/**
    028 * A module that integrates JSON into Tapestry in terms of type coercions.  tapestry-json can still
    029 * be used independently of the rest of Tapestry (since its a 'provided' dependency),
    030 * but when used with tapestry-ioc on the classpath, the coercions described by this module become available.
    031 *
    032 * @since 5.3
    033 */
    034public class JSONModule
    035{
    036    /**
    037     * <ul>
    038     * <li>{@link String} to {@link org.apache.tapestry5.json.JSONObject}</li>
    039     * <li>{@link String} to {@link org.apache.tapestry5.json.JSONArray}</li>
    040     * </ul>
    041     * @param configuration the configuration to provide the type coercer to
    042     */
    043    @Contribute(TypeCoercer.class)
    044    public static void provideCoercions(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration)
    045    {
    046        CoercionTuple<String, JSONObject> stringToJsonObject = CoercionTuple.create(String.class, JSONObject.class, new StringToJSONObject());
    047        configuration.add(stringToJsonObject.getKey(), stringToJsonObject);
    048
    049        CoercionTuple<String, JSONArray> stringToJsonArray = CoercionTuple.create(String.class, JSONArray.class, new StringToJSONArray());
    050        configuration.add(stringToJsonArray.getKey(), stringToJsonArray);
    051    }
    052}