Browse files

Provide basic JSON encodings

  • Loading branch information...
1 parent b5616cd commit 1a9eba5e4abe1c1957830f0f26cc550f4a1987af @hlship committed Jul 15, 2011
View
27 tapx-json/src/main/java/com/howardlewisship/tapx/internal/json/IdentityJSONEncoder.java
@@ -0,0 +1,27 @@
+// Copyright 2011 Howard M. Lewis Ship
+//
+// 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 com.howardlewisship.tapx.internal.json;
+
+import com.howardlewisship.tapx.json.JSONEncoder;
+
+/**
+ * Identity encoder for objects that are already valid JSON values.
+ */
+public class IdentityJSONEncoder implements JSONEncoder<Object> {
+ @Override
+ public Object encodeAsJSON(Object input) {
+ return input;
+ }
+}
View
45 tapx-json/src/main/java/com/howardlewisship/tapx/internal/json/IterableJSONEncoder.java
@@ -0,0 +1,45 @@
+// Copyright 2011 Howard M. Lewis Ship
+//
+// 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 com.howardlewisship.tapx.internal.json;
+
+import com.howardlewisship.tapx.json.JSONEncoder;
+import org.apache.tapestry5.ioc.annotations.Primary;
+import org.apache.tapestry5.json.JSONArray;
+
+/**
+ * Encodes an iterable object (such as a java.util.List) to a JSONArray.
+ */
+public class IterableJSONEncoder implements JSONEncoder<Iterable> {
+
+ private final JSONEncoder primaryEncoder;
+
+ public IterableJSONEncoder(@Primary JSONEncoder primaryEncoder) {
+ this.primaryEncoder = primaryEncoder;
+ }
+
+ @Override
+ public Object encodeAsJSON(Iterable input) {
+
+ JSONArray result = new JSONArray();
+
+ for (Object value : input) {
+ Object encoded = primaryEncoder.encodeAsJSON(value);
+
+ result.put(encoded);
+ }
+
+ return result;
+ }
+}
View
49 tapx-json/src/main/java/com/howardlewisship/tapx/internal/json/MapJSONEncoder.java
@@ -0,0 +1,49 @@
+// Copyright 2011 Howard M. Lewis Ship
+//
+// 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 com.howardlewisship.tapx.internal.json;
+
+import com.howardlewisship.tapx.json.JSONEncoder;
+import org.apache.tapestry5.ioc.annotations.Primary;
+import org.apache.tapestry5.json.JSONObject;
+
+import java.util.Map;
+
+/**
+ * Encodes a java.util.Map (with String keys) as an {@link org.apache.tapestry5.json.JSONObject}.
+ */
+public class MapJSONEncoder implements JSONEncoder<Map<String, Object>> {
+
+ private final JSONEncoder primaryEncoder;
+
+ public MapJSONEncoder(@Primary JSONEncoder primaryEncoder) {
+ this.primaryEncoder = primaryEncoder;
+ }
+
+ @Override
+ public Object encodeAsJSON(Map<String, Object> input) {
+
+ JSONObject result = new JSONObject(input);
+
+ for (Map.Entry<String, Object> entry :
+ input.entrySet()) {
+
+ Object encoded = primaryEncoder.encodeAsJSON(entry.getValue());
+
+ result.put(entry.getKey(), encoded);
+ }
+
+ return result;
+ }
+}
View
33 tapx-json/src/main/java/com/howardlewisship/tapx/json/JSONEncoder.java
@@ -0,0 +1,33 @@
+// 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 com.howardlewisship.tapx.json;
+
+import org.apache.tapestry5.ioc.annotations.UsesMappedConfiguration;
+
+/**
+ * Used to coerce objects into JSON format.
+ */
+@UsesMappedConfiguration(key = Class.class, value = JSONEncoder.class)
+public interface JSONEncoder<T> {
+ /**
+ * Encodes an arbitrary input object into a JSON object
+ * (an object that may be used as a value in a {@link org.apache.tapestry5.json.JSONArray}
+ * or {@link org.apache.tapestry5.json.JSONObject}.
+ *
+ * @param input
+ * @return a valid JSON value
+ */
+ Object encodeAsJSON(T input);
+}
View
32 tapx-json/src/main/java/com/howardlewisship/tapx/json/JSONModule.java
@@ -14,5 +14,37 @@
package com.howardlewisship.tapx.json;
+import com.howardlewisship.tapx.internal.json.IdentityJSONEncoder;
+import com.howardlewisship.tapx.internal.json.IterableJSONEncoder;
+import com.howardlewisship.tapx.internal.json.MapJSONEncoder;
+import org.apache.tapestry5.ioc.MappedConfiguration;
+import org.apache.tapestry5.ioc.annotations.Primary;
+import org.apache.tapestry5.ioc.services.StrategyBuilder;
+import org.apache.tapestry5.json.JSONArray;
+import org.apache.tapestry5.json.JSONObject;
+import org.apache.tapestry5.json.JSONString;
+
+import java.util.Map;
+
public class JSONModule {
+
+ @Primary
+ public static JSONEncoder buildJSONEncoder(StrategyBuilder builder, Map<Class, JSONEncoder> configuration) {
+ return builder.build(JSONEncoder.class, configuration);
+ }
+
+ public static void standardJSONEncoders(MappedConfiguration<Class, JSONEncoder> configuration) {
+ JSONEncoder identity = new IdentityJSONEncoder();
+
+ configuration.add(String.class, identity);
+ configuration.add(Number.class, identity);
+ configuration.add(Boolean.class, identity);
+ configuration.add(JSONObject.class, identity);
+ configuration.add(JSONArray.class, identity);
+ configuration.add(JSONString.class, identity);
+
+ configuration.addInstance(Iterable.class, IterableJSONEncoder.class);
+ configuration.addInstance(Map.class, MapJSONEncoder.class);
+ }
+
}

0 comments on commit 1a9eba5

Please sign in to comment.