You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/07/02 02:06:12 UTC
svn commit: r1142126 - in /tapestry/tapestry5/trunk/tapestry-core/src:
main/java/org/apache/tapestry5/internal/services/json/
main/java/org/apache/tapestry5/services/
test/groovy/org/apache/tapestry5/root/
Author: hlship
Date: Sat Jul 2 00:06:12 2011
New Revision: 1142126
URL: http://svn.apache.org/viewvc?rev=1142126&view=rev
Log:
TAP5-1457: Support coercions from String to JSONObject and JSONArray
Added:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONArray.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONObject.java
tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/root/
tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/root/StringToJSONTests.groovy
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONArray.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONArray.java?rev=1142126&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONArray.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONArray.java Sat Jul 2 00:06:12 2011
@@ -0,0 +1,27 @@
+// Copyright 2011 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.internal.services.json;
+
+import org.apache.tapestry5.ioc.services.Coercion;
+import org.apache.tapestry5.json.JSONArray;
+
+/**
+ * @since 5.3
+ */
+public class StringToJSONArray implements Coercion<String,JSONArray> {
+ public JSONArray coerce(String input) {
+ return new JSONArray(input);
+ }
+}
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONObject.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONObject.java?rev=1142126&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONObject.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/json/StringToJSONObject.java Sat Jul 2 00:06:12 2011
@@ -0,0 +1,27 @@
+// Copyright 2011 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.internal.services.json;
+
+import org.apache.tapestry5.ioc.services.Coercion;
+import org.apache.tapestry5.json.JSONObject;
+
+/**
+ * @since 5.3
+ */
+public class StringToJSONObject implements Coercion<String,JSONObject> {
+ public JSONObject coerce(String input) {
+ return new JSONObject(input);
+ }
+}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=1142126&r1=1142125&r2=1142126&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Sat Jul 2 00:06:12 2011
@@ -43,6 +43,8 @@ import org.apache.tapestry5.internal.ser
import org.apache.tapestry5.internal.services.javascript.DateFieldStack;
import org.apache.tapestry5.internal.services.javascript.JavaScriptStackPathConstructor;
import org.apache.tapestry5.internal.services.javascript.JavaScriptStackSourceImpl;
+import org.apache.tapestry5.internal.services.json.StringToJSONArray;
+import org.apache.tapestry5.internal.services.json.StringToJSONObject;
import org.apache.tapestry5.internal.services.linktransform.LinkTransformerImpl;
import org.apache.tapestry5.internal.services.linktransform.LinkTransformerInterceptor;
import org.apache.tapestry5.internal.services.messages.PropertiesFileParserImpl;
@@ -993,6 +995,8 @@ public final class TapestryModule
* <li>{@link ComponentClassTransformWorker} to {@link ComponentClassTransformWorker2}</li>
* <li>{@link Resource} to {@link DynamicTemplate}</li>
* <li>{@link Asset} to {@link Resource}</li>
+ * <li>String to {@link JSONObject}</li>
+ * <li>String to {@link JSONArray}</li>
* </ul>
*/
public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration,
@@ -1165,6 +1169,10 @@ public final class TapestryModule
configuration.add(CoercionTuple.create(String.class, ClientValidation.class, stringToClientValidationCoercion));
configuration.add(CCTWToCCTW2Coercion.TUPLE);
+
+ configuration.add(CoercionTuple.create(String.class, JSONObject.class, new StringToJSONObject()));
+
+ configuration.add(CoercionTuple.create(String.class, JSONArray.class, new StringToJSONArray()));
}
/**
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/root/StringToJSONTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/root/StringToJSONTests.groovy?rev=1142126&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/root/StringToJSONTests.groovy (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/root/StringToJSONTests.groovy Sat Jul 2 00:06:12 2011
@@ -0,0 +1,47 @@
+// Copyright 2011 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.root
+
+import org.apache.tapestry5.internal.test.InternalBaseTestCase
+import org.apache.tapestry5.ioc.services.TypeCoercer
+import org.apache.tapestry5.json.JSONObject
+import org.testng.annotations.BeforeClass
+import org.testng.annotations.Test
+import org.apache.tapestry5.json.JSONArray
+
+/**
+ * @since 5.3
+ */
+class StringToJSONTests extends InternalBaseTestCase {
+
+ TypeCoercer typeCoercer
+
+ @BeforeClass
+ void setup() {
+ typeCoercer = getService(TypeCoercer.class)
+ }
+
+
+ @Test
+ void string_to_JSONObject() {
+ assert typeCoercer.coerce(/{ "fred" : "flinstone" }/, JSONObject.class) == new JSONObject("fred", "flinstone")
+ }
+
+ @Test
+ void string_to_JSONArray() {
+ assert typeCoercer.coerce(/[1, 2, 3]/, JSONArray.class) == new JSONArray(1, 2, 3)
+ }
+
+}