本文整理汇总了Java中com.alibaba.fastjson.serializer.SerializerFeature类的典型用法代码示例。如果您正苦于以下问题:Java SerializerFeature类的具体用法?Java SerializerFeature怎么用?Java SerializerFeature使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SerializerFeature类属于com.alibaba.fastjson.serializer包,在下文中一共展示了SerializerFeature类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_map
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_map() throws Exception {
Map<Object, Object> map = new LinkedHashMap<Object, Object>();
map.put(34L, "b");
map.put(12, "a");
Entity entity = new Entity();
entity.setValue(map);
String text = JSON.toJSONString(entity, SerializerFeature.WriteClassName);
System.out.println(text);
Assert.assertEquals("{\"@type\":\"com.alibaba.json.bvt.bug.Bug_for_smoothrat5$Entity\",\"value\":{\"@type\":\"java.util.LinkedHashMap\",34L:\"b\",12:\"a\"}}",
text);
Entity entity2 = JSON.parseObject(text, Entity.class);
Assert.assertEquals(map, entity2.getValue());
Assert.assertEquals(map.getClass(), entity2.getValue().getClass());
}
示例2: test_writer_1
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_writer_1() throws Exception {
SerializeWriter out = new SerializeWriter(14);
out.config(SerializerFeature.QuoteFieldNames, true);
try {
JSONSerializer serializer = new JSONSerializer(out);
VO vo = new VO();
vo.setValue("#");
serializer.write(vo);
Assert.assertEquals("{\"value\":\"#\"}", out.toString());
} finally {
out.close();
}
}
示例3: test_writer_1
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_writer_1() throws Exception {
SerializeWriter out = new SerializeWriter(14);
out.config(SerializerFeature.QuoteFieldNames, true);
out.config(SerializerFeature.UseSingleQuotes, true);
try {
JSONSerializer serializer = new JSONSerializer(out);
VO vo = new VO();
vo.getValues().add("#");
serializer.write(vo);
Assert.assertEquals("{'values':['#']}", out.toString());
} finally {
out.close();
}
}
示例4: test_for_issue
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_for_issue() throws Exception {
ParserConfig config = new ParserConfig();
config.setAutoTypeSupport(true);
Map<Long, Bean> map = new HashMap<Long, Bean>();
map.put(null, new Bean());
Map<Long, Bean> rmap = (Map<Long, Bean>) JSON.parse(JSON.toJSONString(map, SerializerFeature.WriteClassName), config);
System.out.println(rmap);
}
示例5: exceptionHandler
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
/** 异常处理 */
@ExceptionHandler(Exception.class)
public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
throws Exception {
logger.error(Constants.Exception_Head, ex);
OperationResult result=new OperationResult();
if (ex instanceof AbstractException) {
((AbstractException) ex).handler(result);
} /*else if (ex instanceof IllegalArgumentException) {
new IllegalParameterException(ex.getMessage()).handler(modelMap);
} else if (ex instanceof UnauthorizedException) {
modelMap.put("httpCode", HttpCode.FORBIDDEN.value());
modelMap.put("msg", StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.FORBIDDEN.msg()));
} */else {
result.setCode(HttpCode.INTERNAL_SERVER_ERROR.value());
String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.INTERNAL_SERVER_ERROR.msg());
result.setMessage(msg.length() > 100 ? "系统走神了,请稍候再试." : msg);
}
response.setContentType("application/json;charset=UTF-8");
logger.info(JSON.toJSONString(result));
byte[] bytes = JSON.toJSONBytes(result, SerializerFeature.DisableCircularReferenceDetect);
response.getOutputStream().write(bytes);
}
示例6: configureMessageConverters
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
/**
* 修改自定义消息转换器
* @param converters 消息转换器列表
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//调用父类的配置
super.configureMessageConverters(converters);
//创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
fastConverter.setFastJsonConfig(fastJsonConfig);
//将fastjson添加到视图消息转换器列表内
converters.add(fastConverter);
}
示例7: test_array_writer_2
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_array_writer_2() throws Exception {
Random random = new Random();
long[] values = new long[2048];
for (int i = 0; i < values.length; ++i) {
values[i] = random.nextLong();
}
StringWriter writer = new StringWriter();
JSON.writeJSONString(writer, values, SerializerFeature.BrowserCompatible);
String text = writer.toString();
long[] values_2 = JSON.parseObject(text, long[].class);
Assert.assertEquals(values_2.length, values.length);
for (int i = 0; i < values.length; ++i) {
Assert.assertEquals(values[i], values_2[i]);
}
}
示例8: test_point
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_point() throws Exception {
JSONSerializer serializer = new JSONSerializer();
Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Point.class).getClass());
Point point = new Point(3, 4);
String text = JSON.toJSONString(point, SerializerFeature.WriteClassName);
System.out.println(text);
Object obj = JSON.parse(text);
Point point2 = (Point) obj;
Assert.assertEquals(point, point2);
Point point3 = (Point) JSON.parseObject(text, Point.class);
Assert.assertEquals(point, point3);
}
示例9: test_writer
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_writer() throws Exception {
StringWriter out = new StringWriter();
JSONWriter writer = new JSONWriter(out);
writer.config(SerializerFeature.UseSingleQuotes, true);
writer.startObject();
writer.startObject();
writer.endObject();
writer.startObject();
writer.endObject();
writer.endObject();
writer.close();
Assert.assertEquals("{{}:{}}", out.toString());
}
示例10: addItemToCart
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public CartItem addItemToCart(String quoteId, CartItem item) {
Map<String, Map<String, Object>> request = new HashMap<>();
Map<String, Object> cartItem = new HashMap<>();
cartItem.put("qty", item.getQty());
cartItem.put("sku", item.getSku());
cartItem.put("quote_id", quoteId);
request.put("cartItem", cartItem);
String json = JSON.toJSONString(request, SerializerFeature.BrowserCompatible);
json = postSecure(baseUri() + "/" + relativePath + "/" + cartId + "/items", json);
if(!validate(json)){
return null;
}
CartItem saved = JSON.parseObject(json, CartItem.class);
return saved;
}
示例11: test_codec_null
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_codec_null() throws Exception {
V0 v = new V0();
SerializeConfig mapping = new SerializeConfig();
mapping.setAsmEnable(false);
String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
Assert.assertEquals("{\"value\":null}", text);
ParserConfig config = new ParserConfig();
config.setAutoTypeSupport(true);
config.setAsmEnable(false);
V0 v1 = JSON.parseObject(text, V0.class, config, JSON.DEFAULT_PARSER_FEATURE);
Assert.assertEquals(v1.getValue(), v.getValue());
}
示例12: test_bug
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_bug() throws Exception {
Map<Object, Object> params = new HashMap<Object, Object>();
params.put("msg",
"<img class=\"em\" src=\"http://ab.com/12/33.jpg\" />");
params.put("uid", "22034343");
String s001 = JSON.toJSONString(params, SerializerFeature.BrowserCompatible);
System.out.println(s001);
Map<Object, Object> params2 = (Map<Object, Object>) JSON.parse(s001);
Assert.assertEquals(params.size(), params2.size());
Assert.assertEquals(params.get("uid"), params2.get("uid"));
Assert.assertEquals(params.get("msg"), params2.get("msg"));
Assert.assertEquals(params, params2);
}
示例13: test_0
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_0() throws Exception {
VO vo = new VO();
vo.setId(123);
vo.setName("wenshao");
vo.getValues().add(new A());
String text = JSON.toJSONString(vo, SerializerFeature.BeanToArray);
Assert.assertEquals("[123,\"wenshao\",[[0]]]", text);
VO vo2 = JSON.parseObject(text, VO.class, Feature.SupportArrayToBean);
Assert.assertEquals(vo.getId(), vo2.getId());
Assert.assertEquals(vo.getName(), vo2.getName());
Assert.assertEquals(vo.getValues().size(), vo2.getValues().size());
Assert.assertEquals(vo.getValues().get(0).getClass(), vo2.getValues().get(0).getClass());
Assert.assertEquals(vo.getValues().get(0).getValue(), vo2.getValues().get(0).getValue());
}
示例14: test_set
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
public void test_set() throws Exception {
Map<Integer, Object> map = new LinkedHashMap<Integer, Object>();
map.put(1, "a");
map.put(2, "b");
Entity entity = new Entity();
entity.setValue(map);
String text = JSON.toJSONString(entity, SerializerFeature.WriteClassName);
System.out.println(text);
Assert.assertEquals("{\"@type\":\"com.alibaba.json.bvt.bug.Bug_for_smoothrat8$Entity\",\"value\":{\"@type\":\"java.util.LinkedHashMap\",1:\"a\",2:\"b\"}}",
text);
Entity entity2 = JSON.parseObject(text, Entity.class);
Assert.assertEquals(map, entity2.getValue());
Assert.assertEquals(map.getClass(), entity2.getValue().getClass());
Assert.assertEquals(Integer.class, ((Map)entity2.getValue()).keySet().iterator().next().getClass());
}
示例15: delAndAddUser
import com.alibaba.fastjson.serializer.SerializerFeature; //导入依赖的package包/类
@RequestMapping(value="/delAndAdd",method=RequestMethod.GET)
@ResponseBody
// @ResponseBody
/*public String delAndAddUser(@RequestParam("name")String name,@RequestParam("sex")String sex,
@RequestParam("age")int age,@RequestParam("phone")String phone)*/
public String delAndAddUser(String name,String sex, int age,String phone,String password){
String result = null;
ResponseBean responseBean = null;
try {
User newUser = new User(name,sex,age,phone,password);
userService.deleteAndAdd(newUser);
// responseBean = new ResponseBean(200,true,"用户删除并添加成功",null);
responseBean = new ResponseBean(200,true,"用户删除并添加成功","none");
result = JSON.toJSONString(responseBean, SerializerFeature.WriteMapNullValue);
return result;
}catch(Exception e) {
e.printStackTrace();
responseBean = new ResponseBean(200,true,"删除失败!","原因不明!");
result = JSON.toJSONString(responseBean, SerializerFeature.WriteMapNullValue);
return result;
}
}