Skip to content

Commit

Permalink
Write Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Haceau-Zoac committed Jul 29, 2020
1 parent d6b3c5f commit 334d670
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ Haceau.JSON是一个类库,用于解析JSON字符串并返回Dictionary\<strin
|完成版本|内容|
|---|---|
|1.1.0|导出JSON字符串|
|1.0.x|长期维护|
|1.1.x|长期维护|
Binary file added src/.vs/Haceau.JSON/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file added src/.vs/Haceau.JSON/v16/.suo
Binary file not shown.
61 changes: 61 additions & 0 deletions src/Haceau.JSON/WriteJSON.cs
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;
}
}
}
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 类生成。

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cdfb31be8322d91cbe03056c46dc534f6408342b
6 changes: 5 additions & 1 deletion src/test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection.Metadata;

namespace Haceau.JSON.test
{
Expand All @@ -23,7 +24,6 @@ static void JSON1()
string JSONString = "{\"Array\": [\"字符串\", false], \"number\": 123, \"double\": 999.888, \"dgfsrfe\": null}";
Console.WriteLine($"Input: {JSONString}");
ReadJSON readJSON = new ReadJSON();

Stopwatch sw = new Stopwatch();
sw.Start();
readJSON.Parser(JSONString);
Expand All @@ -43,6 +43,7 @@ static void JSON1()
Console.WriteLine($"\t\"double\":{obj["double"]},");
Console.WriteLine($"\t\"dgfsrfe\":{obj["dgfsrfe"] ?? "null"}");
Console.WriteLine("}");
Console.WriteLine($"转换为字符串:{WriteJSON.Write(obj)}");

sw.Start();
for (int i = 0; i < 100; ++i)
Expand Down Expand Up @@ -73,6 +74,7 @@ static void JSON2()
Console.WriteLine($"\t\"number\":{obj["number"]},");
Console.WriteLine($"\t\"dgfsrfe\":{obj["dgfsrfe"] ?? "null"}");
Console.WriteLine("}");
Console.WriteLine($"转换为字符串:{WriteJSON.Write(obj)}");
sw.Start();
for (int i = 0; i < 100; ++i)
readJSON.Parser(JSONString);
Expand All @@ -98,6 +100,7 @@ static void JSON3()
Console.WriteLine("[");
Console.WriteLine($"\t{obj[0]}");
Console.WriteLine("]");
Console.WriteLine($"转换为字符串:{WriteJSON.Write(obj)}");
sw.Start();
for (int i = 0; i < 100; ++i)
readJSON.Parser(JSONString);
Expand All @@ -123,6 +126,7 @@ static void JSON4()
Console.WriteLine("{");
Console.WriteLine($"\t\"st\tring\":\"{obj["st\tring"]}\"");
Console.WriteLine("}");
Console.WriteLine($"转换为字符串:{WriteJSON.Write(obj)}");
sw.Start();
for (int i = 0; i < 100; ++i)
readJSON.Parser(JSONString);
Expand Down
4 changes: 2 additions & 2 deletions src/test/test.csproj
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>

0 comments on commit 334d670

Please sign in to comment.