Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 00350200D50 for ; Mon, 4 Dec 2017 13:39:06 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id F2BF8160C05; Mon, 4 Dec 2017 12:39:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 1E666160BF9 for ; Mon, 4 Dec 2017 13:39:04 +0100 (CET) Received: (qmail 14358 invoked by uid 500); 4 Dec 2017 12:39:04 -0000 Mailing-List: contact issues-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list issues@struts.apache.org Received: (qmail 14348 invoked by uid 99); 4 Dec 2017 12:39:04 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Dec 2017 12:39:04 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 7201DC48CF for ; Mon, 4 Dec 2017 12:39:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.211 X-Spam-Level: X-Spam-Status: No, score=-99.211 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id yHwASyS6s4wV for ; Mon, 4 Dec 2017 12:39:02 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id 0F6365F3CF for ; Mon, 4 Dec 2017 12:39:02 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 3AE23E0295 for ; Mon, 4 Dec 2017 12:39:01 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id EF6ED255C3 for ; Mon, 4 Dec 2017 12:39:00 +0000 (UTC) Date: Mon, 4 Dec 2017 12:39:00 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: issues@struts.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (WW-4034) Allow to use custom JSONwriter MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Mon, 04 Dec 2017 12:39:06 -0000 [ https://issues.apache.org/jira/browse/WW-4034?page=3Dcom.atlassian.ji= ra.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=3D1627672= 0#comment-16276720 ]=20 ASF GitHub Bot commented on WW-4034: ------------------------------------ lukaszlenart closed pull request #47: WW-4034 Adds example for how customiz= ing JSONWriter URL: https://github.com/apache/struts-site/pull/47 =20 =20 =20 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.x= ml` like: ``` =20 +There is an example at [struts-examples/json-customize/FlexJSONWriter.java= ](https://gitbox.apache.org/repos/asf?p=3Dstruts-examples.git;a=3Dblob_plai= n;f=3Djson-customize/src/main/java/org/demo/FlexJSONWriter.java;hb=3DHEAD). +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 excludeProperti= es, Collection includeProperties, + boolean excludeNullProperties) throws JSONExceptio= n { + + JSONSerializer serializer =3D new JSONSerializer(); + if (excludeProperties !=3D null) { + for (Pattern p : excludeProperties) { + serializer =3D serializer.exclude(p.pattern()); + } + } + if (includeProperties !=3D null) { + for (Pattern p : includeProperties) { + serializer =3D serializer.include(p.pattern()); + } + } + if (excludeNullProperties) { + serializer =3D serializer.transform(new ExcludeTransformer(), = void.class); + } + if (dateFormatter !=3D null) { + serializer =3D serializer.transform(new DateTransformer(dateFo= rmatter), Date.class); + } + return serializer.serialize(object); + } + //... +``` + +> Flexjson is a lightweight library for serializing and deserializing Java= objects into and from JSON. + ## Example =20 ### Setup Action =20 ---------------------------------------------------------------- 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. =20 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=C4=9Fra K=C3=96KSALAN > 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 sh= ould be: > {code:java} > private void map(Map map, Method method) throws JSONException { > this.add("{"); > ... > while (it.hasNext()) { > Map.Entry entry =3D (Map.Entry) it.next(); > Object key =3D entry.getKey(); > String expr =3D null; > if (this.buildExpr) { > try { > if (key =3D=3D null) { > LOG.error("Cannot build expression for null key in " = + this.exprStack); > continue; > } else { > expr =3D this.expandExpr(key.toString()); > if (this.shouldExcludeProperty(expr)) { > continue; > } > expr =3D 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)