Skip to content

Commit

Permalink
version 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DebugST committed Aug 5, 2024
1 parent e0d036f commit 6cb300a
Show file tree
Hide file tree
Showing 64 changed files with 9,765 additions and 5,634 deletions.
157 changes: 101 additions & 56 deletions Src/STLib.Json.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,119 @@
using System.Data;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using STLib.Json;

namespace STLib.Json.Test
{
internal class Program
public class UserInfo
{
[STJson(STJsonSerilizaMode.All)]
public class Student
{
[STJsonProperty("test_name")]
public string Name;
[STJsonProperty]
public int Age;
public Gender Gender;
[STJsonProperty] // optional
public List<string> Hobby;
}
public string Name { get; set; }
public string Github { get; set; }
public string[] Language { get; set; }
public Address Address { get; set; }
}

public enum Gender
{
Male, Female
}
public class Address
{
public string Country { get; set; }
public string Province { get; set; }
public string City { get; set; }
}

static void Main(string[] args) {
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("age", typeof(int));
dt.Columns.Add("is_boy", typeof(bool));
for (int i = 0; i < 10; i++) {
DataRow dr = dt.NewRow();
dr["name"] = "test";
dr["age"] = 10;
dr["is_boy"] = i % 2 == 0;
dt.Rows.Add(dr);
}
internal class Program
{
static void Main(string[] args)
{
//var obj_test = STJsonTestObject.CreateTestObject();
var type = typeof(UserInfo);
var obj_test = new UserInfo()
{
Name = "DebugST",
Github = "https://github.com/DebugST",
Language = new string[] { "C#", "JS", "..." },
Address = new Address()
{
Country = "China",
Province = "GuangDong",
City = "ShenZhen"
}
};
string str_json, str_temp;
Console.WriteLine("========================================");
Console.WriteLine(STJson.Serialize(obj_test));
Console.WriteLine("========================================");

var str = STJson.Serialize(dt, 4);
Console.WriteLine(str);
Console.WriteLine("[CHECK_IS_SAME]");

var stu = new Student() {
Name = "Tom",
Age = 20,
Hobby = new List<string>() { "sing", "dance" }
};
object obj = STJsonTestObject.CreateTestObject();
Console.WriteLine(STJson.Serialize(obj, 4));
str_json = JsonConvert.SerializeObject(obj_test);
str_temp = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<UserInfo>(str_json));
Console.WriteLine("Newtonsoft : " + (str_temp == str_json));

str = STJson.Serialize(stu, 4);
Console.WriteLine(str);
stu = STJson.Deserialize<Student>(str);
str_json = STJson.Serialize(obj_test);
str_temp = STJson.Serialize(STJson.Deserialize<UserInfo>(str_json));
Console.WriteLine("STJson : " + (str_temp == str_json));

Console.WriteLine(STJson.Serialize(new System.Drawing.Rectangle(10, 10, 100, 100)));
var sw = new System.Diagnostics.Stopwatch();

Console.WriteLine(STJson.FromObject(stu).Select("..").ToString(4));
int n_counter = 50000;
System.Threading.Thread.Sleep(5000);
Console.WriteLine("========================================");
Console.WriteLine("[Deserialize To Linq] - " + n_counter);
sw.Reset();
sw.Start();
for (int i = 0; i < n_counter; i++) {
JsonConvert.DeserializeObject<JObject>(str_json);
}
sw.Stop();
Console.WriteLine("Newstonoft : " + sw.ElapsedMilliseconds);

//str = STJson.Serialize(obj, 4);
//var sw = new System.Diagnostics.Stopwatch();
//int nCount = 10000;
sw.Reset();
sw.Start();
for (int i = 0; i < n_counter; i++) {
STJson.Deserialize(str_json);
}
sw.Stop();
Console.WriteLine("STJson : " + sw.ElapsedMilliseconds);
// ====================================================================================================
System.Threading.Thread.Sleep(5000);
Console.WriteLine("========================================");
Console.WriteLine("[Deserialize To object] - " + n_counter);
sw.Reset();
sw.Start();
for (int i = 0; i < n_counter; i++) {
JsonConvert.DeserializeObject<UserInfo>(str_json);
}
sw.Stop();
Console.WriteLine("Newstonoft : " + sw.ElapsedMilliseconds);

//while (true) {
// Console.ReadKey();
// Console.WriteLine("===");
sw.Reset();
sw.Start();
for (int i = 0; i < n_counter; i++) {
STJson.Deserialize<UserInfo>(str_json);
}
sw.Stop();
Console.WriteLine("STJson : " + sw.ElapsedMilliseconds);
// ====================================================================================================
System.Threading.Thread.Sleep(5000);
Console.WriteLine("========================================");
Console.WriteLine("[Serialize] - " + n_counter);
sw.Reset();
sw.Start();
for (int i = 0; i < n_counter; i++) {
JsonConvert.SerializeObject(obj_test);
}
sw.Stop();
Console.WriteLine("Newstonoft : " + sw.ElapsedMilliseconds);

// sw.Restart();
// for (int i = 0; i < nCount; i++) {
sw.Reset();
sw.Start();
for (int i = 0; i < n_counter; i++) {
STJson.Serialize(obj_test);
}
sw.Stop();
Console.WriteLine("STJson : " + sw.ElapsedMilliseconds);

// var aaa = STJson.Deserialize<TestObject>(str);
// }
// sw.Stop();
// Console.WriteLine(sw.ElapsedMilliseconds);
//}
Console.ReadKey();
}
}
}
5 changes: 1 addition & 4 deletions Src/STLib.Json.Test/STJsonTestObject.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STLib.Json.Test
{
public enum TestObjectEnum { Enum1, Enum2, Enum3 }

[STJson(STJsonSerilizaMode.All)]
[STJson(STJsonSerializeMode.All)]
public class TestObject
{
private string m_private_string = "private_string_obj";
Expand Down
10 changes: 6 additions & 4 deletions Src/STLib.Json.Test/STLib.Json.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFramework>net8.0</TargetFramework>
<Title>STJson</Title>
<Authors>DebugST</Authors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\STLib.Json\STLib.Json.csproj" />
</ItemGroup>
Expand Down
15 changes: 12 additions & 3 deletions Src/STLib.Json.sln
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
# Visual Studio 15
VisualStudioVersion = 15.0.34930.48
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "STLib.Json", "STLib.Json\STLib.Json.csproj", "{151B52D3-E5D0-4877-8A84-FF7764F270DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "STLib.Json.Test", "STLib.Json.Test\STLib.Json.Test.csproj", "{E9423C17-AF9B-46F4-BAE6-0D440F9D7095}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "STLib.Json.Test", "STLib.Json.Test\STLib.Json.Test.csproj", "{E9423C17-AF9B-46F4-BAE6-0D440F9D7095}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Expand All @@ -28,4 +31,10 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C0D3E6E9-5F55-4EC6-8578-F30C609492E6}
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
Loading

0 comments on commit 6cb300a

Please sign in to comment.