public static String createSuccessResponse(Integer httpCode, Object result, SerializerFeature serializerFeature, SerializeFilter filter, HttpServletResponse response){ PrintWriter printWriter = null; String jsonString = ""; try { response.setCharacterEncoding(RESPONSE_CHARACTERENCODING); response.setContentType(RESPONSE_CONTENTTYPE); response.setStatus(httpCode); printWriter = response.getWriter(); if(null != result){ if(null!=filter){ jsonString = JSONObject.toJSONString(result,filter,serializerFeature); }else{ // jsonString = JSONObject.toJSONString(result, serializerFeature); jsonString = JSONObject.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:ss:mm",serializerFeature); } printWriter.write(jsonString); } printWriter.flush(); } catch (Exception e) { log.error("createResponse failed", e); } finally { if(null!=printWriter)printWriter.close(); } return jsonString; }
public void test_enum() throws Exception { Model model = new Model(); model.t1 = Type.A; model.t2 = null; ValueFilter valueFilter = new ValueFilter() { public Object process(Object object, String name, Object value) { return value; } }; SerializeFilter[] filters = {valueFilter}; String text = JSON.toJSONString(model, SerializeConfig.getGlobalInstance(), // filters, null, 0, // SerializerFeature.QuoteFieldNames, // SerializerFeature.BrowserCompatible, // SerializerFeature.WriteEnumUsingName); Assert.assertEquals("{\"t1\":\"A\"}", text); }
public static final String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (SerializerFeature feature : features) { serializer.config(feature, true); } serializer.config(SerializerFeature.WriteDateUseDateFormat, true); setFilter(serializer, filter); serializer.write(object); String serializeWriter = out.toString(); return serializeWriter; } finally { out.close(); } }
public static final String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (SerializerFeature feature : features) { serializer.config(feature, true); } serializer.config(SerializerFeature.WriteDateUseDateFormat, true); setFilter(serializer, filters); serializer.write(object); String serializeWriter = out.toString(); return serializeWriter; } finally { out.close(); } }
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) { if (filter != null) { if (filter instanceof PropertyPreFilter) { serializer.getPropertyPreFilters().add((PropertyPreFilter) filter); } if (filter instanceof NameFilter) { serializer.getNameFilters().add((NameFilter) filter); } if (filter instanceof ValueFilter) { serializer.getValueFilters().add((ValueFilter) filter); } if (filter instanceof PropertyFilter) { serializer.getPropertyFilters().add((PropertyFilter) filter); } if (filter instanceof BeforeFilter) { serializer.getBeforeFilters().add((BeforeFilter) filter); } if (filter instanceof AfterFilter) { serializer.getAfterFilters().add((AfterFilter) filter); } } }
public static final String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.config(SerializerFeature.WriteDateUseDateFormat, true); setFilter(serializer, filter); serializer.write(object); return out.toString(); } finally { out.close(); } }
public static final String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.config(SerializerFeature.WriteDateUseDateFormat, true); setFilter(serializer, filters); serializer.write(object); return out.toString(); } finally { out.close(); } }
public static final String toJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out, config); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } setFilter(serializer, filter); serializer.write(object); return out.toString(); } finally { out.close(); } }
public static final String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out, config); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } setFilter(serializer, filters); serializer.write(object); return out.toString(); } finally { out.close(); } }
@Deprecated public void addSerializeFilter(SerializeFilter filter) { if (filter == null) { return; } int length = this.fastJsonConfig.getSerializeFilters().length; SerializeFilter[] filters = new SerializeFilter[length + 1]; System.arraycopy(this.fastJsonConfig.getSerializeFilters(), 0, filters, 0, length); filters[filters.length - 1] = filter; this.fastJsonConfig.setSerializeFilters(filters); }
/** * init param. */ public FastJsonConfig() { this.charset = Charset.forName("UTF-8"); this.serializeConfig = SerializeConfig.getGlobalInstance(); this.parserConfig = new ParserConfig(); this.serializerFeatures = new SerializerFeature[] { SerializerFeature.BrowserSecure }; this.serializeFilters = new SerializeFilter[0]; this.features = new Feature[0]; }
@Test public void test_context_value_filter_not_effected () { List<Object> params = new ArrayList<Object>(); Map data = new HashMap(); data.put("name", "ace"); params.add(data); //fail Actual :[{"name":"ace"}] Assert.assertEquals("[{\"name\":\"mark-ace\"}]", JSON.toJSONString(params, new SerializeFilter[]{ contextValueFilter }) ); }
@Test public void test_context_value_filter_effected() { List<Object> params = new ArrayList<Object>(); Map data = new HashMap(); data.put("name", "ace"); params.add(data); //success Assert.assertEquals("[{\"name\":\"mark-ace\"}]", JSON.toJSONString(params, new SerializeFilter[]{ contextValueFilter, valueFilter }) ); }
public void test_for_issue() throws Exception { Map map = Collections.singletonMap("value", 123); String json = JSON.toJSONString(map , SerializeConfig.globalInstance , new SerializeFilter[0] , null , JSON.DEFAULT_GENERATE_FEATURE & ~SerializerFeature.QuoteFieldNames.mask ); assertEquals("{value:123}", json); }
public void test_for_bug() throws Exception { ContactTemplateParam param = new ContactTemplateParam(); param.setAuditStatus(AuditStatusType.AUDIT_FAILURE); String json = JSON.toJSONString(param, new SerializeFilter[] { new IntEnumFilter("auditStatus") }); assertEquals("{\"auditStatus\":0}", json); }
public void test_for_issue() throws Exception { VO vo = new VO(); SerializeFilter filter = null; JSON.toJSONString(vo, SerializeConfig.getGlobalInstance(), filter); JSON.toJSONString(vo, SerializeConfig.getGlobalInstance(), new SerializeFilter[0]); }
public static final String toJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out, config); for (SerializerFeature feature : features) { serializer.config(feature, true); } setFilter(serializer, filter); serializer.write(object); String serializeWriter = out.toString(); return serializeWriter; } finally { out.close(); } }
public static final String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out, config); for (SerializerFeature feature : features) { serializer.config(feature, true); } setFilter(serializer, filters); serializer.write(object); String serializeWriter = out.toString(); return serializeWriter; } finally { out.close(); } }
public String thin(Object bean) { toJSONString(bean, new SerializeFilter[]{firstValueFilter}); val ret = toJSONString(bean, new SerializeFilter[]{nameFilter, secondValueFilter}, WriteMapNullValue); this.keyMapping = WestJsonUtils.invert(innerKeyMapping); this.valueMapping = WestJsonUtils.invert(innerValueMapping); return ret; }
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) { if (filter == null) { return; } if (filter instanceof PropertyPreFilter) { serializer.getPropertyPreFilters().add((PropertyPreFilter) filter); } if (filter instanceof NameFilter) { serializer.getNameFilters().add((NameFilter) filter); } if (filter instanceof ValueFilter) { serializer.getValueFilters().add((ValueFilter) filter); } if (filter instanceof PropertyFilter) { serializer.getPropertyFilters().add((PropertyFilter) filter); } if (filter instanceof BeforeFilter) { serializer.getBeforeFilters().add((BeforeFilter) filter); } if (filter instanceof AfterFilter) { serializer.getAfterFilters().add((AfterFilter) filter); } }
public static String createSuccessResponse(Integer httpCode, String message, Object result, SerializerFeature serializerFeature, SerializeFilter filter, HttpServletResponse response){ PrintWriter printWriter = null; String jsonString = ""; try { response.setCharacterEncoding(RESPONSE_CHARACTERENCODING); response.setContentType(RESPONSE_CONTENTTYPE); response.setStatus(httpCode); printWriter = response.getWriter(); SerializeConfig config = new SerializeConfig(); config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); Map<String, Object> map = new HashMap<String, Object>(); if(null != result){ map.put("res_code", httpCode); map.put("message", message); map.put("data",result); if(null!=filter){ jsonString = JSONObject.toJSONString(map,filter,serializerFeature); }else{ // jsonString = JSONObject.toJSONString(map,config,serializerFeature); jsonString = JSONObject.toJSONStringWithDateFormat(map,"yyyy-MM-dd"); } printWriter.write(jsonString); } printWriter.flush(); } catch (Exception e) { log.error("createResponse failed", e); } finally { if(null!=printWriter)printWriter.close(); } return jsonString; }
public SerializeFilter[] fastjsonFilters() { if (null == fastjsonFilters) { return null; } int len = fastjsonFilters.length; SerializeFilter[] filters = new SerializeFilter[len]; for (int i = 0; i < len; ++i) { filters[i] = app().getInstance(fastjsonFilters[i]); } return filters; }
public FastJsonConfig(SerializeConfig serializeConfig, SerializerFeature[] serializerFeatures, Map<Class<?>, SerializeFilter> serializeFilters, ParserConfig parserConfig, Feature[] features) { this.serializeConfig = serializeConfig; this.parserConfig = parserConfig; this.serializerFeatures = serializerFeatures; this.features = features; this.serializeFilters = serializeFilters; }
protected FullHttpResponse createHttpResponse(Map<String, Object> rsp) { FullHttpResponse response = null; if (rsp == null || rsp.isEmpty()) { response = new DefaultFullHttpResponse(HTTP_1_1, OK); } else { if (rsp.containsKey(Fields.F_rspOut)) { response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(rsp.get(Fields.F_rspOut).toString(), CharsetUtil.UTF_8)); } else { Object filter = rsp.remove(Fields.F_filter); if (filter != null && filter instanceof SerializeFilter) { response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(JsonUtil.encode(rsp, (SerializeFilter) filter), CharsetUtil.UTF_8)); } else { response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(JsonUtil.encode(rsp), CharsetUtil.UTF_8)); } } } if (gzip) { // response.headers().set(CONTENT_ENCODING, "gzip"); } response.headers().set(CONTENT_TYPE, "application/json; charset=UTF-8"); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); if (keepAlive) { response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } return response; }
@Deprecated public SerializeFilter[] getFilters() { return this.fastJsonConfig.getSerializeFilters(); }
@Deprecated public void setFilters(SerializeFilter... filters) { this.fastJsonConfig.setSerializeFilters(filters); }
@Override protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { ByteArrayOutputStream outnew = new ByteArrayOutputStream(); try { HttpHeaders headers = outputMessage.getHeaders(); //获取全局配置的filter SerializeFilter[] globalFilters = fastJsonConfig.getSerializeFilters(); List<SerializeFilter> allFilters = new ArrayList<SerializeFilter>(Arrays.asList(globalFilters)); boolean isJsonp = false; //不知道为什么会有这行代码, 但是为了保持和原来的行为一致,还是保留下来 Object value = strangeCodeForJackson(object); if (value instanceof FastJsonContainer) { FastJsonContainer fastJsonContainer = (FastJsonContainer) value; PropertyPreFilters filters = fastJsonContainer.getFilters(); allFilters.addAll(filters.getFilters()); value = fastJsonContainer.getValue(); } //revise 2017-10-23 , // 保持原有的MappingFastJsonValue对象的contentType不做修改 保持旧版兼容。 // 但是新的JSONPObject将返回标准的contentType:application/javascript ,不对是否有function进行判断 if (value instanceof MappingFastJsonValue) { if(!StringUtils.isEmpty(((MappingFastJsonValue) value).getJsonpFunction())){ isJsonp = true; } } else if (value instanceof JSONPObject) { isJsonp = true; } int len = JSON.writeJSONString(outnew, // fastJsonConfig.getCharset(), // value, // fastJsonConfig.getSerializeConfig(), // //fastJsonConfig.getSerializeFilters(), // allFilters.toArray(new SerializeFilter[allFilters.size()]), fastJsonConfig.getDateFormat(), // JSON.DEFAULT_GENERATE_FEATURE, // fastJsonConfig.getSerializerFeatures()); if (isJsonp) { headers.setContentType(APPLICATION_JAVASCRIPT); } if (fastJsonConfig.isWriteContentLength()) { headers.setContentLength(len); } outnew.writeTo(outputMessage.getBody()); } catch (JSONException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); } finally { outnew.close(); } }
/** * @return the serializeFilters */ public SerializeFilter[] getSerializeFilters() { return serializeFilters; }
/** * @param serializeFilters the serializeFilters to set */ public void setSerializeFilters(SerializeFilter... serializeFilters) { this.serializeFilters = serializeFilters; }
/** * @return the classSerializeFilters */ public Map<Class<?>, SerializeFilter> getClassSerializeFilters() { return classSerializeFilters; }
public void test_for_issue() throws Exception { VO vo = new VO(); vo.setValue(new Date(1460434818838L)); String text = JSON.toJSONString(vo, new SerializeFilter[0]); Assert.assertEquals("{\"value\":1460434818838}", text); }
public static final String toJSONString(Object object, SerializeConfig config, SerializerFeature... features) { return toJSONString(object, config, (SerializeFilter) null, features); }
private static void setFilter(JSONSerializer serializer, SerializeFilter... filters) { for (SerializeFilter filter : filters) { setFilter(serializer, filter); } }
public static String convertNumberToString(String raw) { Object parse = JSON.parse(raw); return JSON.toJSONString(parse, new SerializeFilter[]{filter}, WriteMapNullValue); }