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.internal.json.StringToJSONArray;
018import org.apache.tapestry5.internal.json.StringToJSONObject;
019import org.apache.tapestry5.ioc.Configuration;
020import org.apache.tapestry5.ioc.annotations.Contribute;
021import org.apache.tapestry5.ioc.services.CoercionTuple;
022import org.apache.tapestry5.ioc.services.TypeCoercer;
023import org.apache.tapestry5.json.JSONArray;
024import org.apache.tapestry5.json.JSONObject;
025
026/**
027 * A module that integrates JSON into Tapestry in terms of type coercions.  tapestry-json can still
028 * be used independently of the rest of Tapestry (since its a 'provided' dependency),
029 * but when used with tapestry-ioc on the classpath, the coercions described by this module become available.
030 *
031 * @since 5.3
032 */
033public class JSONModule
034{
035    /**
036     * <ul>
037     * <li>{@link String} to {@link org.apache.tapestry5.json.JSONObject}</li>
038     * <li>{@link String} to {@link org.apache.tapestry5.json.JSONArray}</li>
039     * </ul>
040     */
041    @Contribute(TypeCoercer.class)
042    public static void provideCoercions(Configuration<CoercionTuple> configuration)
043    {
044        configuration.add(CoercionTuple.create(String.class, JSONObject.class, new StringToJSONObject()));
045
046        configuration.add(CoercionTuple.create(String.class, JSONArray.class, new StringToJSONArray()));
047    }
048}