JSONBuilder - C#、 java扩展版

时间:2013-06-28 12:16:03
【文件属性】:

文件名称:JSONBuilder - C#、 java扩展版

文件大小:61KB

文件格式:ZIP

更新时间:2013-06-28 12:16:03

JSONBuilder C# Java 源代码

比较简单的JSONBuilder:(.net 2.0, jdk1.5) - 只有两个文件:JSONBuilder(.cs,.java) , JSONBuilderDelegates(.cs,.java) - 不用考虑对象嵌套输出格式的匹配问题 - 自动字符串转义 - 支持常见数据类型、以及常用的数据结构如: 任意数组(Array),IMap,IList,IEmutable等 - 支持任意扩展,通过注册自定义类型的转换方法(参看JSONBuilderTest(.cs,.java) 和 JSONBuilderDelegates(.cs,.java))可支持任意类型的json字符串转换 - 支持自定义的包含 public string toJSON() 的对象的输出(忽略大小写) - 带有测试的VS2005和Eclispe完整项目 - 版权:可任意使用、分发、改良(只要包含原作者信息) // 转换回调函数接口 --> JSONBuilderDelegates.cs public delegate string JSONBuilderDelegates(object value, bool useSingleQuote); 或 --> JSONBuilderDelegates.java public static String JSONBuilderDelegates(Object value, boolean useSingleQuote); //-->JSONBuilderTest.cs (JSONBuilderTest.java) 比如 .net: //demo custom class public class CustomClass { public string name = "Hu Changwei"; public string nickName = "koqiui"; public string email = "koqiui@163.com"; public string gender = "male"; public bool married = true; public DateTime birthDate = new DateTime(1978, 5, 21); } //demo custom json convertor public static string fromCustomClass(object value, bool useSingleQuote) { if (value == null) { return JSONBuilder.NullStr; } if (value.GetType() == typeof(CustomClass)) { CustomClass objValue = value as CustomClass; JSONBuilder jb = new JSONBuilder(useSingleQuote); jb.startObject(); // jb.add("Author", objValue.name); jb.add("nickname", objValue.nickName); jb.add("email", objValue.email); jb.add("married", objValue.married); jb.add("birthdate", objValue.birthDate); // jb.endObject(); return jb.toJSON(); } return JSONBuilder.NullStr; } [Test] public void test_customConvertor() { JSONBuilder.setJSONConvertor(typeof(CustomClass), new ToJSONDelegate(fromCustomClass)); JSONBuilder jb = new JSONBuilder(); jb.addValue(new CustomClass()); Console.WriteLine(jb.toJSON()); }


【文件预览】:
dotNet
----JSONBuilder-by-koqiui.zip(56KB)
java
----JSONBuilder-by-koqiui.zip(7KB)

网友评论

  • 貌似不好用,没搞出来