序列化and反序列化数据从未如此简单(ReflectyIo)

2019-05-23  本文已影响0人  LightSun

先来看一个 json的sample

       @XmlElement("info")
       public class Info {
             private String addr;
             private String phone;
        }
        Info info = new Info();
        info.setAddr("addr1");
        info.setPhone("12345");

        ReflectyIo io = new ReflectyIo()
                .json()  
                .type(Info.class)
                .build();
        StringWriter writer = new StringWriter();
        io.write(writer, info);  
        System.out.println(writer.toString());        
 //------------  json ouput is:
        {"addr":"addr1","phone":"12345"}
//------------  

再看 一个xml的sample

        ReflectyIo io = new ReflectyIo()
                .xml()  
                .type(Info.class)
                .build();
        StringWriter writer = new StringWriter();
        io.write(writer, info);
        System.out.println(writer.toString());          
 //------------  xml ouput is:
       <info addr="addr1" phone="12345"/>
//------------  

还不够?再看yaml的sample

        ReflectyIo io = new ReflectyIo()
                .yaml()  
                .type(Info.class)
                .build();
        StringWriter writer = new StringWriter();
        io.write(writer, info);          
        System.out.println(writer.toString());
 //------------  xml ouput is:
    addr: addr1
     phone: 12345
//------------  

如果喜欢这样的框架。请移步[ReflectyIo](https://github.com/LightSun/ReflectyIO

)
如果这些格式都不满足你。 那么请尝试自定义插件吧。ReflectyPlugin

上一篇下一篇

猜你喜欢

热点阅读