Java Code Examples for com.alibaba.fastjson.serializer.JSONSerializer#write()
The following examples show how to use
com.alibaba.fastjson.serializer.JSONSerializer#write() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonUtils.java From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * 可以允许指定一些过滤字段进行生成json对象 */ public static String marshalToString(Object obj, String... fliterFields) { final List<String> propertyFliters = Arrays.asList(fliterFields); SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(new PropertyFilter() { public boolean apply(Object source, String name, Object value) { return !propertyFliters.contains(name); } }); serializer.write(obj); return out.toString(); } finally { out.close(); } }
Example 2
Source File: MappingFastJsonValue.java From uavstack with Apache License 2.0 | 6 votes |
public void write(JSONSerializer serializer, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter writer = serializer.out; if(jsonpFunction == null){ serializer.write(value); return; } if ((features & BrowserSecureMask) != 0 || (writer.isEnabled(BrowserSecureMask))) { writer.write(SECURITY_PREFIX); } writer.write(jsonpFunction); writer.write('('); serializer.write(value); writer.write(')'); }
Example 3
Source File: JsonKit.java From zbus-server with MIT License | 6 votes |
private static final byte[] toJSONBytes(Object object, String charsetName, SerializerFeature... features) { if(charsetName == null){ charsetName = DEFAULT_ENCODING; } SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (SerializerFeature feature : features) { serializer.config(feature, true); } serializer.write(object); return out.toBytes(charsetName); } finally { out.close(); } }
Example 4
Source File: JSONPObject.java From uavstack with Apache License 2.0 | 6 votes |
public void write(JSONSerializer serializer, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter writer = serializer.out; if ((features & SerializerFeature.BrowserSecure.mask) != 0 || (writer.isEnabled(SerializerFeature.BrowserSecure.mask))) { writer.write(SECURITY_PREFIX); } writer.write(function); writer.write('('); for (int i = 0; i < parameters.size(); ++i) { if (i != 0) { writer.write(','); } serializer.write(parameters.get(i)); } writer.write(')'); }
Example 5
Source File: JsonUtil.java From game-server with MIT License | 6 votes |
/** * 对象转json * <p> * 需要jsonFiled注解 * </p> * * @param object a {@link java.lang.Object} object. * @return a {@link java.lang.String} object. */ public static String toJSONString(Object object) { SerializeWriter out = new SerializeWriter(); try { Map<String, Field> fieldCacheMap = new HashMap<>(); ParserConfig.parserAllFieldToCache(object.getClass(), fieldCacheMap); JSONSerializer serializer = new JSONSerializer(out); PropertyFilter filter = (Object source, String name, Object value) -> { Field field = ParserConfig.getFieldFromCache(name, fieldCacheMap); if (field != null) { JSONField fieldAnnotation = field.getAnnotation(JSONField.class); if (fieldAnnotation != null) { return true; } return false; } return true; }; serializer.getPropertyFilters().add(filter); serializer.write(object); return out.toString(); } finally { out.close(); } }
Example 6
Source File: JsonUtils.java From feeyo-hlsserver with Apache License 2.0 | 6 votes |
/** * 可以允许指定一些过滤字段进行生成json对象 */ public static String marshalToString(Object obj, String... fliterFields) { final List<String> propertyFliters = Arrays.asList(fliterFields); SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(new PropertyFilter() { public boolean apply(Object source, String name, Object value) { return !propertyFliters.contains(name); } }); serializer.write(obj); return out.toString(); } finally { out.close(); } }
Example 7
Source File: AbstractSerializer.java From joyrpc with Apache License 2.0 | 5 votes |
/** * 写值 * * @param serializer 输出 * @param field 字段 * @param value 值 * @param ignoreNull 是否忽略null值 * @param separator 分隔符位置 */ protected void write(final JSONSerializer serializer, final String field, final Object value, final boolean ignoreNull, final int separator) { if (value != null || !ignoreNull) { SerializeWriter out = serializer.getWriter(); if (separator == BEFORE) { out.write(','); } out.writeFieldName(field); serializer.write(value); if (separator == AFTER) { out.write(','); } } }
Example 8
Source File: AjaxAuthenticationFilter.java From java-platform with Apache License 2.0 | 5 votes |
protected void writeObject(ServletRequest request, ServletResponse response, Object result) throws IOException { String characterEncoding = ((HttpServletRequest) request).getCharacterEncoding(); Charset charset = Strings.isNullOrEmpty(characterEncoding) ? DEFAULT_CHARSET : Charset.forName(characterEncoding); OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream(), charset); SerializeWriter writer = new SerializeWriter(out); JSONSerializer serializer = new JSONSerializer(writer, SpringContext.getBean(SerializeConfig.class)); serializer.config(SerializerFeature.DisableCircularReferenceDetect, true); serializer.write(result); writer.flush(); out.close(); }
Example 9
Source File: FastJsonObjectOutput.java From JobX with Apache License 2.0 | 5 votes |
public void writeObject(Object obj) throws IOException { SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.config(SerializerFeature.WriteEnumUsingToString, true); serializer.write(obj); out.writeTo(writer); out.close(); // for reuse SerializeWriter buf writer.println(); writer.flush(); }
Example 10
Source File: EnumDict.java From hsweb-framework with Apache License 2.0 | 5 votes |
@Override default void write(JSONSerializer jsonSerializer, Object o, Type type, int i) throws IOException { if (isWriteJSONObjectEnabled()) { jsonSerializer.write(getWriteJSONObject()); } else { jsonSerializer.write(getValue()); } }
Example 11
Source File: JfinalRecordSerializer.java From t-io with Apache License 2.0 | 5 votes |
/** * @param serializer * @param object * @param fieldName * @param fieldType * @param features * @throws IOException * @author tanyaowu */ @Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { if (object == null) { serializer.out.writeNull(); return; } Record record = (Record) object; Map<String, Object> map = record.getColumns(); serializer.write(map); }
Example 12
Source File: PageSerializer.java From java-platform with Apache License 2.0 | 5 votes |
@Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.getWriter(); if (object == null) { out.writeNull(); return; } Page<?> page = (Page<?>) object; out.write('{'); out.writeFieldName(DEFAULT_CURRENT_FIELD); out.writeInt(page.getNumber()); out.write(','); out.writeFieldName(DEFAULT_ROWCOUNT_FIELD); out.writeInt(page.getSize()); out.write(','); out.writeFieldName(DEFAULT_TOTAL_FIELD); out.writeLong(page.getTotalElements()); out.write(','); out.writeFieldName(DEFAULT_ROWS_FIELD); serializer.write(page.getContent()); out.write('}'); }
Example 13
Source File: FastJsonObjectOutput.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
@Override public void writeObject(Object obj) throws IOException { SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.config(SerializerFeature.WriteEnumUsingToString, true); serializer.write(obj); out.writeTo(writer); out.close(); // for reuse SerializeWriter buf writer.println(); writer.flush(); }
Example 14
Source File: FastjsonHttpMessageConverter.java From java-platform with Apache License 2.0 | 5 votes |
@Override protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { Charset charset = getCharset(outputMessage.getHeaders()); OutputStreamWriter out = new OutputStreamWriter(outputMessage.getBody(), charset); SerializeWriter writer = new SerializeWriter(out); JSONSerializer serializer = new JSONSerializer(writer, this.serializeConfig); serializer.config(SerializerFeature.DisableCircularReferenceDetect, true); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes instanceof ServletRequestAttributes) { Class<?> rootClass = filterClass(obj); if (rootClass != null) { String[] paths = ((ServletRequestAttributes) requestAttributes).getRequest() .getParameterValues("paths"); if (paths != null && paths.length > 0) { rootClass = ClassUtils.getUserClass(rootClass); Map<Class<?>, PropertyPreFilter> propertyFilters = new HashMap<>(); pathVisit(propertyFilters, Sets.newHashSet(paths), rootClass); for (Entry<Class<?>, PropertyPreFilter> entry : propertyFilters.entrySet()) { serializer.getPropertyPreFilters().add(entry.getValue()); } } else { serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER); } } else { serializer.getPropertyFilters().add(DEFAULT_PROPERTY_FILTER); } } serializer.write(obj); writer.flush(); out.close(); }
Example 15
Source File: FastjsonSockJsMessageCodec.java From uavstack with Apache License 2.0 | 5 votes |
@Override protected char[] applyJsonQuoting(String content) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); serializer.write(content); return out.toCharArrayForSpringWebSocket(); } finally { out.close(); } }
Example 16
Source File: FastJsonObjectOutput.java From dubbox with Apache License 2.0 | 5 votes |
public void writeObject(Object obj) throws IOException { SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.config(SerializerFeature.WriteEnumUsingToString, true); serializer.write(obj); out.writeTo(writer); writer.println(); writer.flush(); }
Example 17
Source File: ObjectIdSerializer.java From xian with Apache License 2.0 | 4 votes |
@Override public void write(JSONSerializer serializer, Object valueObject, Object fieldName, Type fieldType, int features) throws IOException { ObjectId value = (ObjectId) valueObject; String id = value.toString(); serializer.write(id); }
Example 18
Source File: ResultSerializer.java From java-platform with Apache License 2.0 | 4 votes |
@Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.getWriter(); if (object == null) { out.writeNull(); return; } Result result = (Result) object; out.write('{'); Map<String, Object> extraProperties = result.getExtraProperties(); for (Entry<String, Object> entry : extraProperties.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); out.writeFieldName(key); serializer.write(value); out.write(','); } out.writeFieldName(FIELD_SUCCESS); out.write(Objects.equal(result.getCode(), Code.success)); out.write(','); out.writeFieldName(FIELD_CODE); out.writeString(result.getCode().name()); out.write(','); out.writeFieldName(FIELD_MESSAGE); out.writeString(result.getMessage()); out.write(','); out.writeFieldName(FIELD_DATA); serializer.write(result.getData()); out.write('}'); }
Example 19
Source File: CounterSerializer.java From watcher with Apache License 2.0 | 4 votes |
@Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { Counter counter = (Counter) object; serializer.write(counter.getCount()); }
Example 20
Source File: FastJsonProvider.java From metrics with Apache License 2.0 | 4 votes |
public String toJSONString(Object object, SerializeFilter filter, SerializerFeature[] features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out, fastJsonConfig.serializeConfig); if (features != null) { for (SerializerFeature feature : features) { serializer.config(feature, true); } } 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); } } serializer.write(object); return out.toString(); } finally { out.close(); } }