Skip to content

Commit ebc9268

Browse files
committed
添加 Json操作类 及 单元测试
1 parent f97663c commit ebc9268

File tree

6 files changed

+195
-3
lines changed

6 files changed

+195
-3
lines changed

readme.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Util应用框架面向的是*架构初学者*,但不是.net初学者。
105105

106106
对于简单的操作,你可以运行和阅读相应单元测试代码,即可了解它们如何使用。对于复杂操作,一方面看单元测试,另外我们会编写相关文档以描述调用方式和工作机制。
107107

108+
Util暂时未发布到nuget,待成熟后发布。
109+
108110
## 开发环境
109111

110112
以下是我们在项目开发和部署时使用的工具和组件,这个列表会经常更新。
@@ -199,7 +201,7 @@ Util应用框架技术顾问团队负责对API易用性,代码健壮性,设
199201

200202
> 如果你在阅读或使用Util中任意一个代码片断时发现Bug,或有更佳实现方式,请通知我们。
201203
202-
> 为了保持代码简单,目前很多代码只建立了一个基础结构,我在有需求的时候再添加特性,如果你发现无法满足你的需求,请通知我们。
204+
> 为了保持代码简单,目前很多功能只建立了基本结构,细节特性未进行迁移,在后续需要时进行添加,如果你发现某个类无法满足你的需求,请通知我们。
203205
204206
> 你可以通过github的Issue或Pull Request向我们提交问题和代码,如果你更喜欢使用QQ进行交流,请加入我们的交流QQ群。
205207
@@ -266,6 +268,10 @@ https://github.com/dotnetcore/util/
266268

267269
## 引用的第三方框架
268270

271+
> 未指明版本号,将使用最新版本。
272+
273+
- Newtonsoft.Json
274+
269275
## 更新计划
270276

271277
> 不打算提供精确的更新计划,以免无法兑现。
@@ -274,7 +280,11 @@ https://github.com/dotnetcore/util/
274280
275281
- 公共操作类(工具类)
276282
- 类型转换操作 [已发布]
283+
- Json操作 - 基于Newtonsoft.Json [已发布]
284+
- 对象映射操作 - 基于AutoMapper [已完成]
285+
- Ioc操作 - 基于Autofac [已完成]
277286

278287
## 更新列表
279288

280-
- 2017年7月6日,更新了类型转换操作类,类型转换扩展方法,类型转换单元测试。
289+
- 2017年7月6日,更新了类型转换操作类,类型转换扩展方法,类型转换单元测试。
290+
- 2017年7月14日,更新了Json操作类,Json操作单元测试。

src/Util/01-Util.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
<AssemblyName>Util</AssemblyName>
66
<RootNamespace>Util</RootNamespace>
77
<Authors>何镇汐</Authors>
8-
<Description>Utill应用框架</Description>
8+
<Description>Utill应用框架核心库</Description>
99
<Version>1.0.0</Version>
1010
<Product>Utill应用框架核心库</Product>
1111
<AssemblyVersion>1.0.0.0</AssemblyVersion>
1212
<FileVersion>1.0.0.0</FileVersion>
13+
<Copyright>何镇汐</Copyright>
14+
<PackageReleaseNotes>Util应用框架以MIT开源发布,可随意使用</PackageReleaseNotes>
1315
</PropertyGroup>
1416

1517
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

src/Util/Helpers/Json.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Util.Helpers {
4+
/// <summary>
5+
/// Json操作
6+
/// </summary>
7+
public static class Json {
8+
/// <summary>
9+
/// 将Json字符串转换为对象
10+
/// </summary>
11+
/// <param name="json">Json字符串</param>
12+
public static T ToObject<T>( string json ) {
13+
if ( string.IsNullOrWhiteSpace( json ) )
14+
return default(T);
15+
return JsonConvert.DeserializeObject<T>( json );
16+
}
17+
18+
/// <summary>
19+
/// 将对象转换为Json字符串
20+
/// </summary>
21+
/// <param name="target">目标对象</param>
22+
/// <param name="isConvertToSingleQuotes">是否将双引号转成单引号</param>
23+
public static string ToJson( object target,bool isConvertToSingleQuotes = false ) {
24+
if ( target == null )
25+
return "{}";
26+
var result = JsonConvert.SerializeObject( target );
27+
if ( isConvertToSingleQuotes )
28+
result = result.Replace( "\"", "'" );
29+
return result;
30+
}
31+
}
32+
}

test/Util.Tests/Helpers/JsonTest.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Text;
2+
using Newtonsoft.Json;
3+
using Util.Helpers;
4+
using Util.Tests.Samples;
5+
using Util.Tests.XUnitHelpers;
6+
using Xunit;
7+
8+
namespace Util.Tests.Helpers {
9+
/// <summary>
10+
/// Json测试
11+
/// </summary>
12+
public class JsonTest {
13+
/// <summary>
14+
/// 测试循环引用序列化
15+
/// </summary>
16+
[Fact]
17+
public void TestLoop() {
18+
A a = new A { Name = "a" };
19+
B b = new B { Name = "b" };
20+
C c = new C { Name = "c" };
21+
a.B = b;
22+
b.C = c;
23+
c.A = a;
24+
AssertHelper.Throws<JsonSerializationException>( () => Json.ToJson( c ) );
25+
}
26+
27+
/// <summary>
28+
/// 转成Json,验证空
29+
/// </summary>
30+
[Fact]
31+
public void TestToJson_Null() {
32+
Assert.Equal( "{}", Json.ToJson( null ) );
33+
}
34+
35+
/// <summary>
36+
/// 测试转成Json
37+
/// </summary>
38+
[Fact]
39+
public void TestToJson() {
40+
var result = new StringBuilder();
41+
result.Append( "{" );
42+
result.Append( "\"Name\":\"a\"," );
43+
result.Append( "\"nickname\":\"b\"," );
44+
result.Append( "\"Value\":null," );
45+
result.Append( "\"Date\":\"2012/1/1 0:00:00\"," );
46+
result.Append( "\"Age\":1," );
47+
result.Append( "\"isShow\":true" );
48+
result.Append( "}" );
49+
Assert.Equal( result.ToString(), Json.ToJson( JsonTestSample.Create() ) );
50+
}
51+
52+
/// <summary>
53+
/// 测试转成对象
54+
/// </summary>
55+
[Fact]
56+
public void TestToObject() {
57+
var customer = Json.ToObject<JsonTestSample>( $"{{\"Name\":\"a\"}}" );
58+
Assert.Equal( "a", customer.Name );
59+
}
60+
}
61+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace Util.Tests.Samples {
2+
public class A {
3+
public string Name { get; set; }
4+
public B B { get; set; }
5+
}
6+
7+
public class B {
8+
public string Name { get; set; }
9+
public C C { get; set; }
10+
}
11+
12+
public class C {
13+
public string Name { get; set; }
14+
public A A { get; set; }
15+
}
16+
17+
/// <summary>
18+
/// Json测试样例
19+
/// </summary>
20+
public class JsonTestSample {
21+
/// <summary>
22+
/// 名称,测试公共属性,且首字母大写
23+
/// </summary>
24+
public string Name { get; set; }
25+
/// <summary>
26+
/// 私有属性,应被忽略
27+
/// </summary>
28+
private string A { get; set; }
29+
/// <summary>
30+
/// 昵称,用来测试小写
31+
/// </summary>
32+
public string nickname { get; set; }
33+
/// <summary>
34+
/// 测试null
35+
/// </summary>
36+
public object Value { get; set; }
37+
/// <summary>
38+
/// 日期
39+
/// </summary>
40+
public string Date { get; set; }
41+
/// <summary>
42+
/// 测试整型,不添加引号
43+
/// </summary>
44+
public int Age { get; set; }
45+
/// <summary>
46+
/// 测试布尔型
47+
/// </summary>
48+
public bool isShow { get; set; }
49+
50+
/// <summary>
51+
/// 创建客户
52+
/// </summary>
53+
public static JsonTestSample Create() {
54+
return new JsonTestSample() {
55+
Name = "a",
56+
A = "1",
57+
nickname = "b",
58+
Value = null,
59+
Date = Util.Helpers.Convert.ToDate( "2012-1-1" ).ToString(),
60+
Age = 1,
61+
isShow = true
62+
};
63+
}
64+
}
65+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace Util.Tests.XUnitHelpers {
5+
/// <summary>
6+
/// 断言操作
7+
/// </summary>
8+
public class AssertHelper {
9+
/// <summary>
10+
/// 抛出异常,并从异常消息中搜索特定关键字
11+
/// </summary>
12+
/// <typeparam name="TException">异常类型</typeparam>
13+
/// <param name="action">操作</param>
14+
/// <param name="keyword">关键字</param>
15+
public static TException Throws<TException>( Action action,string keyword = "" ) where TException : Exception {
16+
var exception = Assert.Throws<TException>( action );
17+
if( !string.IsNullOrWhiteSpace(keyword) )
18+
Assert.Contains( keyword, exception.Message );
19+
return exception;
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)