当前位置: 首页>>代码示例>>Java>>正文


Java JSON.toJSONString方法代码示例

本文整理汇总了Java中com.alibaba.fastjson.JSON.toJSONString方法的典型用法代码示例。如果您正苦于以下问题:Java JSON.toJSONString方法的具体用法?Java JSON.toJSONString怎么用?Java JSON.toJSONString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.alibaba.fastjson.JSON的用法示例。


在下文中一共展示了JSON.toJSONString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test_1

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_1() throws Exception {
    Model[] array = new Model[2048];
    for (int i = 0; i < array.length; ++i) {
        array[i] = new Model();
        array[i].value = Type.A;
    }

    String text = JSON.toJSONString(array);

    Model[] array2 = JSON.parseObject(text, Model[].class);

    Assert.assertEquals(array.length, array2.length);
    for (int i = 0; i < array.length; ++i) {
        Assert.assertEquals(array[i].value, array2[i].value);
    }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:17,代码来源:EnumFieldTest3_private.java

示例2: test_0

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_0() throws Exception {
    Model model = new Model();

    String text = JSON.toJSONString(model, SerializerFeature.BeanToArray);
    Assert.assertEquals("[null]", text);

    Model model2 = JSON.parseObject(text, Model.class, Feature.SupportArrayToBean);
    Assert.assertNull(model2.name);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:10,代码来源:WriteAsArray_string.java

示例3: test_0

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_0() throws Exception {
    VO vo = new VO();
    vo.id = 100;
    
    String text = JSON.toJSONString(vo, SerializerFeature.WriteNonStringValueAsString);
     Assert.assertEquals("{\"id\":\"100\"}", text);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:WriteNonStringValueAsStringTestByteField.java

示例4: test_codec_null_2

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_codec_null_2() throws Exception {
    V0 v = JSON.parseObject("{\"value\":[1,2]}", V0.class);

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
    Assert.assertEquals("{\"value\":[1,2]}", text);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:10,代码来源:AtomicLongArrayFieldTest.java

示例5: test_1

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(true);
    
    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":\"\"}", text);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:FeaturesTest4.java

示例6: test_create

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_create() throws Exception {
    Entity entity = new Entity(123.45D, "菜姐");
    String text = JSON.toJSONString(entity);
    
    Entity entity2 = JSON.parseObject(text, Entity.class);
    Assert.assertTrue(entity.getId() == entity2.getId());
    Assert.assertEquals(entity.getName(), entity2.getName());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:9,代码来源:JSONCreatorTest_double.java

示例7: run

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
@Override
public void run() {
    LOG.info("MessageProcesserThread process message start...");
    try {
        byte [] body=message.getBody();
        if(null==body){
            return;
        }
        String bodyStr = new String(body,"UTF-8");

        //消息数据转化为ElvesMqMessage,验证数据结构是否符合标准
        ElvesMqMessage elvesMsg = ElvesMqMessage.getInstance(bodyStr);
        if(null==elvesMsg){
            LOG.error("Message structure error!");
            return;
        }

        //elves consumer 服务类进行消费消息
        Object serviceBean=SpringUtil.getBean(this.SERVICE_NAME);
        Class clazz = serviceBean.getClass();
        Method method = clazz.getDeclaredMethod(elvesMsg.getAction(),Map.class);
        Object result = method.invoke(serviceBean,elvesMsg.getMqbody());

        //消息处理结果为null ,或者cast类型的消息,后续不做处理
        if(result==null){
            return;
        }
        Map<String,Object> back =new HashMap<String,Object>();
        back.put("mqkey",elvesMsg.getToModule()+"."+elvesMsg.getFromModule());
        back.put("mqflag","cast");
        back.put("mqbody",result);
        String responseMsg =JSON.toJSONString(back);
        LOG.info("Response msg :"+responseMsg);
        messageProducer.reply(message.getMessageProperties().getReplyTo(),responseMsg);
    } catch (Exception e) {
        LOG.error(ExceptionUtil.getStackTraceAsString(e));
    }
    LOG.info("MessageProcesserThread process message end.");
}
 
开发者ID:elves-project,项目名称:scheduler,代码行数:40,代码来源:MessageProcesserThread.java

示例8: test_ref

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_ref() throws Exception {
    Department tech = new Department(1, "技术部");
    tech.setRoot(tech);
    
    {
        Department pt = new Department(2, "平台技术部");
        pt.setParent(tech);
        pt.setRoot(tech);
        tech.getChildren().add(pt);
        {
            Department sysbase = new Department(3, "系统基础");
            sysbase.setParent(pt);
            sysbase.setRoot(tech);
            pt.getChildren().add(sysbase);
        }
    }
    {
        Department cn = new Department(4, "中文站技术部");
        cn.setParent(tech);
        cn.setRoot(tech);
        tech.getChildren().add(cn);
    }
    
    {
        //JSON.toJSONString(tech);
    }
    
    {
        String prettyText = JSON.toJSONString(tech, SerializerFeature.PrettyFormat);
        System.out.println(prettyText);
    
        String text = JSON.toJSONString(tech);
        Department dept = JSON.parseObject(text, Department.class);
        Assert.assertTrue(dept == dept.getRoot());
        
        System.out.println(JSON.toJSONString(dept, SerializerFeature.PrettyFormat));
    }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:39,代码来源:RefTest11.java

示例9: test_color

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_color() 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);

    Point point2 = JSON.parseObject(text, Point.class);

    Assert.assertEquals(point, point2);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:12,代码来源:PointTest.java

示例10: test_for_mtop

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_mtop() throws Exception {
    P0 p = new P0();
    p.model = new Model();

    ContextValueFilter valueFilter = new ContextValueFilter() {

        @Override
        public Object process(BeanContext context, Object object, String name, Object value) {


            if (value instanceof Model) {
                Assert.assertEquals(P0.class, context.getBeanClass());
                Assert.assertNotNull(context.getField());
                Assert.assertNotNull(context.getMethod());
                Assert.assertEquals("model", context.getName());
                Assert.assertEquals(Model.class, context.getFieldClass());
                Assert.assertEquals(Model.class, context.getFieldType());
                Assert.assertEquals(SerializerFeature.WriteMapNullValue.mask, context.getFeatures());
                
                Field field = context.getField();
                Assert.assertNotNull(field.getAnnotation(UrlIdentify.class));
                Assert.assertNotNull(context.getAnnation(UrlIdentify.class));
                
                return value;
            }

            return value;
        }
    };

    JSON.toJSONString(p, valueFilter);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:33,代码来源:MTopTest.java

示例11: test_format

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_format() throws Exception {
    Model model = new Model();
    model.value = 123.45678D;

    String str = JSON.toJSONString(model);
    assertEquals("{\"value\":123.46}", str);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:DoubleFormatTest2.java

示例12: test_for_issue

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_for_issue() throws Exception {
    LocalDateTime dateTime = LocalDateTime.of(2016, 5, 6, 9, 3, 16);
    VO vo = new VO();
    vo.setDate(dateTime);
    
    String text = JSON.toJSONString(vo);
    Assert.assertEquals("{\"date\":\"2016-05-06 09:03:16\"}", text);
    
    VO vo1 = JSON.parseObject(text, VO.class);
    
    Assert.assertEquals(vo.getDate(), vo1.getDate());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:13,代码来源:LocalDateTimeTest4.java

示例13: test_empty

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_empty() throws Exception {
    Health vo = new Health();
    vo.id = 123;
    vo.border = 234;

    String text = JSON.toJSONString(vo);
    Assert.assertEquals("{\"border\":234,\"id\":123}", text);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:9,代码来源:JSONFieldTest_unwrapped_4.java

示例14: test_array

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_array() throws Exception {
    List<Entity> list = new ArrayList<Entity>();
    list.add(new Entity(123, "aaa"));
    list.add(new Entity(234, "bbb"));
    list.add(new Entity(3, "ccc"));
    String text = JSON.toJSONString(list, SerializerFeature.PrettyFormat, SerializerFeature.UseSingleQuotes);
    Assert.assertEquals("[\n\t{\n\t\t'id':123,\n\t\t'name':'aaa'\n\t},\n\t{\n\t\t'id':234,\n\t\t'name':'bbb'\n\t},\n\t{\n\t\t'id':3,\n\t\t'name':'ccc'\n\t}\n]", text);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:9,代码来源:ArrayListTest.java

示例15: test_unicode

import com.alibaba.fastjson.JSON; //导入方法依赖的package包/类
public void test_unicode() throws Exception {
    String text = JSON.toJSONString(Collections.singletonMap("v", "\u0018"));
    System.out.println(text);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:5,代码来源:UnicodeTest.java


注:本文中的com.alibaba.fastjson.JSON.toJSONString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。