當前位置: 首頁>>代碼示例>>Java>>正文


Java SerializerFeature類代碼示例

本文整理匯總了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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:19,代碼來源:Bug_for_smoothrat5.java

示例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();
    }
   
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:SerializeWriterTest_18.java

示例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();
    }

}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:SerializeWriterTest_19.java

示例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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:9,代碼來源:Issue1503.java

示例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);
}
 
開發者ID:liuxx001,項目名稱:bird-java,代碼行數:24,代碼來源:AbstractController.java

示例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);
}
 
開發者ID:SnailFastGo,項目名稱:ontology_setting,代碼行數:30,代碼來源:FastJsonConfiguration.java

示例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]);
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:17,代碼來源:LongTest_browserCompatible.java

示例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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:PointTest2.java

示例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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:JSONWriterTest_3.java

示例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;
}
 
開發者ID:chen0040,項目名稱:java-magento-client,代碼行數:21,代碼來源:MagentoMyCartManager.java

示例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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:ListFieldTest.java

示例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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:19,代碼來源:Bug_for_bbl.java

示例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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:17,代碼來源:WriteAsArray_list_obj_public.java

示例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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:20,代碼來源:Bug_for_smoothrat8.java

示例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;
    	} 
    }
 
開發者ID:duanyaxin,項目名稱:springboot-smart,代碼行數:23,代碼來源:UsersController.java


注:本文中的com.alibaba.fastjson.serializer.SerializerFeature類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。