// Copyright 2011-2013 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.json.modules;

import org.apache.tapestry5.commons.Configuration;
import org.apache.tapestry5.commons.services.CoercionTuple;
import org.apache.tapestry5.commons.services.TypeCoercer;
import org.apache.tapestry5.internal.json.StringToJSONArray;
import org.apache.tapestry5.internal.json.StringToJSONObject;
import org.apache.tapestry5.ioc.annotations.Contribute;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;

/**
 * A module that integrates JSON into Tapestry in terms of type coercions.  tapestry-json can still
 * be used independently of the rest of Tapestry (since its a 'provided' dependency),
 * but when used with tapestry-ioc on the classpath, the coercions described by this module become available.
 *
 * @since 5.3
 */
public class JSONModule
{
    /**
     * <ul>
     * <li>{@link String} to {@link org.apache.tapestry5.json.JSONObject}</li>
     * <li>{@link String} to {@link org.apache.tapestry5.json.JSONArray}</li>
     * </ul>
     * @param configuration the configuration to provide the type coercer to
     */
    @Contribute(TypeCoercer.class)
    public static void provideCoercions(Configuration<CoercionTuple> configuration)
    {
        configuration.add(CoercionTuple.create(String.class, JSONObject.class, new StringToJSONObject()));

        configuration.add(CoercionTuple.create(String.class, JSONArray.class, new StringToJSONArray()));
    }
}

@@ -697,16 +697,73 @@
 ration;%0A
+import org.apache.tapestry5.commons.MappedConfiguration;%0A
 import o
@@ -1838,16 +1838,22 @@
 ercions(
+Mapped
 Configur
@@ -1858,16 +1858,35 @@
 uration%3C
+CoercionTuple.Key, 
 Coercion
@@ -1921,34 +1921,71 @@
         
-configuration.add(
+CoercionTuple%3CString, JSONObject%3E stringToJsonObject = 
 Coercion
@@ -2054,19 +2054,17 @@
 bject())
-);%0A
+;
 %0A       
@@ -2082,16 +2082,128 @@
 ion.add(
+stringToJsonObject.getKey(), stringToJsonObject);%0A%0A        CoercionTuple%3CString, JSONArray%3E stringToJsonArray = 
 Coercion
@@ -2270,16 +2270,89 @@
 Array())
+;%0A        configuration.add(stringToJsonArray.getKey(), stringToJsonArray
 );%0A    %7D

Change TypeCoercer configuration to be a mapped