-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6b3c5f
commit 334d670
Showing
8 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Haceau.JSON | ||
{ | ||
public static class WriteJSON | ||
{ | ||
/// <summary> | ||
/// 将对象转换为字符串 | ||
/// </summary> | ||
/// <param name="obj">对象</param> | ||
/// <returns>字符串</returns> | ||
public static string Write(object obj) => | ||
obj.GetType() == typeof(List<object>) ? NextArrayToken((List<object>)obj) : NextObjectToken((Dictionary<string, object>)obj); | ||
|
||
/// <summary> | ||
/// 获取数组的下一个token | ||
/// </summary> | ||
/// <returns>token</returns> | ||
private static string NextArrayToken(List<object> array) | ||
{ | ||
string result = "["; | ||
foreach (var item in array) | ||
{ | ||
if ((item == null ? typeof(Nullable) : item.GetType()) == typeof(Dictionary<string, object>)) | ||
result += NextObjectToken((Dictionary<string, object>)item); | ||
else if ((item == null ? typeof(Nullable) : item.GetType()) == typeof(string)) | ||
result += '"' + (string)item + '"'; | ||
else | ||
result += (item ?? "null"); | ||
result += ","; | ||
} | ||
result = result.Remove(result.ToArray().Length - 1); | ||
result += "]"; | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// 获取对象的下一个token | ||
/// </summary> | ||
/// <returns>token</returns> | ||
private static string NextObjectToken(Dictionary<string, object> obj) | ||
{ | ||
string result = "{"; | ||
foreach (var item in obj) | ||
{ | ||
if ((item.Value == null ? typeof(Nullable) : item.Value.GetType()) == typeof(List<object>)) | ||
result += '"' + item.Key + '"' + ":" + NextArrayToken((List<object>)item.Value); | ||
else if ((item.Value == null ? typeof(Nullable) : item.Value.GetType()) == typeof(string)) | ||
result += '"' + item.Key + '"' + ":" + '"' + item.Value + '"'; | ||
else | ||
result += '"' + item.Key + '"' + ":" + (item.Value ?? "null"); | ||
result += ","; | ||
} | ||
result = result.Remove(result.ToArray().Length - 1); | ||
result += "}"; | ||
return result; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Haceau.JSON/obj/Debug/netstandard2.0/Haceau.JSON.AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// 此代码由工具生成。 | ||
// 运行时版本:4.0.30319.42000 | ||
// | ||
// 对此文件的更改可能会导致不正确的行为,并且如果 | ||
// 重新生成代码,这些更改将会丢失。 | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System; | ||
using System.Reflection; | ||
|
||
[assembly: System.Reflection.AssemblyCompanyAttribute("辰落火辉Haceau")] | ||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] | ||
[assembly: System.Reflection.AssemblyCopyrightAttribute("MIT")] | ||
[assembly: System.Reflection.AssemblyDescriptionAttribute("一个简易的JSON解析器。")] | ||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] | ||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] | ||
[assembly: System.Reflection.AssemblyProductAttribute("Haceau.JSON")] | ||
[assembly: System.Reflection.AssemblyTitleAttribute("Haceau.JSON")] | ||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] | ||
|
||
// 由 MSBuild WriteCodeFragment 类生成。 | ||
|
1 change: 1 addition & 0 deletions
1
src/Haceau.JSON/obj/Debug/netstandard2.0/Haceau.JSON.AssemblyInfoInputs.cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cdfb31be8322d91cbe03056c46dc534f6408342b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Haceau.JSON" Version="1.0.0" /> | ||
<ProjectReference Include="..\Haceau.JSON\Haceau.JSON.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |