struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-4034) Allow to use custom JSONwriter
Date Mon, 04 Dec 2017 12:39:00 GMT

    [ https://issues.apache.org/jira/browse/WW-4034?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16276720#comment-16276720
] 

ASF GitHub Bot commented on WW-4034:
------------------------------------

lukaszlenart closed pull request #47: WW-4034 Adds example for how customizing JSONWriter
URL: https://github.com/apache/struts-site/pull/47
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/source/plugins/json/index.md b/source/plugins/json/index.md
index 401dc60d..43e016de 100644
--- a/source/plugins/json/index.md
+++ b/source/plugins/json/index.md
@@ -380,6 +380,49 @@ The implementation should then be defined in `struts.xml` like:
 </struts>
 ```
 
+There is an example at [struts-examples/json-customize/FlexJSONWriter.java](https://gitbox.apache.org/repos/asf?p=struts-examples.git;a=blob_plain;f=json-customize/src/main/java/org/demo/FlexJSONWriter.java;hb=HEAD).
+It replaces Struts default json serializer with [Flexjson](http://flexjson.sourceforge.net/)
as below:
+
+```java
+import flexjson.JSONSerializer;
+import flexjson.transformer.DateTransformer;
+import org.apache.struts2.json.JSONException;
+import org.apache.struts2.json.JSONWriter;
+
+public class FlexJSONWriter implements JSONWriter {
+    private String dateFormatter;
+
+    public String write(Object object) throws JSONException {
+        return this.write(object, null, null, false);
+    }
+
+    public String write(Object object, Collection<Pattern> excludeProperties, Collection<Pattern>
includeProperties,
+                        boolean excludeNullProperties) throws JSONException {
+
+        JSONSerializer serializer = new JSONSerializer();
+        if (excludeProperties != null) {
+            for (Pattern p : excludeProperties) {
+                serializer = serializer.exclude(p.pattern());
+            }
+        }
+        if (includeProperties != null) {
+            for (Pattern p : includeProperties) {
+                serializer = serializer.include(p.pattern());
+            }
+        }
+        if (excludeNullProperties) {
+            serializer = serializer.transform(new ExcludeTransformer(), void.class);
+        }
+        if (dateFormatter != null) {
+            serializer = serializer.transform(new DateTransformer(dateFormatter), Date.class);
+        }
+        return serializer.serialize(object);
+    }
+    //...
+```
+
+> Flexjson is a lightweight library for serializing and deserializing Java objects into
and from JSON.
+
 ## Example
 
 ### Setup Action


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Allow to use custom JSONwriter
> ------------------------------
>
>                 Key: WW-4034
>                 URL: https://issues.apache.org/jira/browse/WW-4034
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Plugin - JSON
>            Reporter: Emir Buğra KÖKSALAN
>            Priority: Minor
>              Labels: JSON
>             Fix For: 2.5.14
>
>
> Throws when accessing to a private inner class in that method:
> private void map(Map map, Method method) throws JSONException
> May be pass when trying to access a private class. example source code should be:
> {code:java}
> private void map(Map map, Method method) throws JSONException {
>     this.add("{");
> ...
>     while (it.hasNext()) {
>         Map.Entry entry = (Map.Entry) it.next();
>         Object key = entry.getKey();
>         String expr = null;
>         if (this.buildExpr) {
>             try {
>                 if (key == null) {
>                     LOG.error("Cannot build expression for null key in " + this.exprStack);
>                     continue;
>                 } else {
>                     expr = this.expandExpr(key.toString());
>                     if (this.shouldExcludeProperty(expr)) {
>                         continue;
>                     }
>                     expr = this.setExprStack(expr);
>                 }
>             }
>             catch (Exception ex) {
>                 LOG.error("Error: " + ex.getLocalizedMessage());
>                 continue;
>             }
>         }
>         if (hasData) {
>             this.add(',');
>         }
> ...
>     this.add("}");
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message