Skip to content

Commit

Permalink
1.11.7 支持序列化参数/null
Browse files Browse the repository at this point in the history
支持`/null`参数
允许在序列化时保持空List不为null
允许在序列化时为null参数指定为null
  • Loading branch information
LorisYounger committed Jun 26, 2024
1 parent f489f98 commit a3ee2ea
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 10 deletions.
33 changes: 30 additions & 3 deletions LinePutScript/Converter/LPSConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ public static bool GetObjectIsString(ConvertType type)
public static string GetObjectString(object? value, ConvertType type = ConvertType.Default, LineAttribute? att = null, bool convertNoneLineAttribute = false)
{
//如果为null储存空
if (value == null || att?.Ignore == true)
if (value == null)
{
return "/null";
}
else if (att?.Ignore == true)
{
return "";
}
Expand Down Expand Up @@ -459,8 +463,16 @@ public static string GetObjectString(object? value, ConvertType type = ConvertTy
TLine t = new TLine();
t.Name = name;
//如果为null储存空
if (value == null || att?.Ignore == true)
if (value == null)
{
t.info = "/null";
return t;
}
else if (att?.Ignore == true)
{
return t;
}

ConvertType Type = type == ConvertType.Default ? GetObjectConvertType(value.GetType(), att) : type;
switch (Type)
{
Expand Down Expand Up @@ -541,7 +553,7 @@ public static string GetObjectString(object? value, ConvertType type = ConvertTy
/// <param name="convertNoneLineAttribute">是否转换不带LineAttribute的类</param>
public static object? GetStringObject(string value, Type type, ConvertType convtype = ConvertType.Default, LineAttribute? att = null, bool? convertNoneLineAttribute = null)
{
if(att?.Ignore == true)
if (att?.Ignore == true)
return null;
convertNoneLineAttribute = att?.ConvertNoneLineAttribute ?? convertNoneLineAttribute;
if (value == "")
Expand All @@ -550,6 +562,21 @@ public static string GetObjectString(object? value, ConvertType type = ConvertTy
{
return Activator.CreateInstance(type);
}
else if (type.IsArray)
{
return Array.CreateInstance(type.GetElementType(), 0);
}
else if (type.IsClass)
{
return Activator.CreateInstance(type);
}
else
{
return null;
}
}
else if (value == "/null")
{
return null;
}
ConvertType ct = convtype == ConvertType.Default ? GetObjectConvertType(type, att) : convtype;
Expand Down
2 changes: 2 additions & 0 deletions LinePutScript/Converter/LineAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public LineAttribute(ConvertType type = ConvertType.Default, Type? converter = n
{
T t = new T();
t.Name = ln;
t.info = "/null";
return t;
}
//自动判断
Expand Down Expand Up @@ -125,6 +126,7 @@ public LineAttribute(ConvertType type = ConvertType.Default, Type? converter = n
{
T t = new T();
t.Name = name;
t.info = "/null";
return t;
}
//自动判断
Expand Down
2 changes: 1 addition & 1 deletion LinePutScript/Core/LinePutScript.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<ImplicitUsings>disable</ImplicitUsings>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
Expand Down
124 changes: 123 additions & 1 deletion TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Numerics;
Expand Down Expand Up @@ -139,6 +140,7 @@ static void Text()
intdict = new Dictionary<int, int>() { { 1, 2 }, { 3, 4 }, { 5, 6 } },
}
};
//Console.WriteLine(LPSConvert.SerializeObject(tc).ToString());
Console.WriteLine("CV测试1:\t" + LPSConvert.SerializeObject(tc).ToString().Equals(Properties.Resources.test4.Replace("\r", "")));
#pragma warning disable CS8604 // 引用类型参数可能为 null。
Console.WriteLine("CV测试2:\t" + LPSConvert.SerializeObject(
Expand All @@ -164,6 +166,12 @@ static void Text()
var l = new Line("food:|type#Drink:|name#ab钙奶:|price#6.5:|desc#健康美味,经济实惠:|Exp#5:|Strength#5:|StrengthDrink#4:|StrengthFood#5:|Health#1:|Feeling#1:|");
Console.WriteLine("CV测试3.4:\t" + (LPSConvert.DeserializeObject<testigcase>(l).StrengthDrink == 4));

l = new Line("BetterBuyData:|LastDiscont#0:|Discont:|ScheduleBuyItems:|BuyHistory#/null:|");
var bbd = LPSConvert.DeserializeObject<BetterBuyData>(l);
Console.WriteLine("CV测试3.5:\t" + (bbd.ScheduleBuyItems != null));
Console.WriteLine("CV测试3.6:\t" + (bbd.BuyHistory == null));


lps["ABC"].info = "a\\nb,c";
lps["ABC"].Info = lps["ABC"].Info.Replace(@"\n", "\n").Replace(@"\r", "\r"); ;
Console.WriteLine("BS测试1:\t" + (lps["ABC"].GetString() == "a\nb,c"));
Expand Down Expand Up @@ -223,7 +231,7 @@ static void Text()
Console.WriteLine("VA测试7:\t" + (g3.Name == "workone").ToString());
Console.WriteLine("VA测试8:\t" + (g3.Type == GraphInfo.GraphType.Work).ToString());
Console.WriteLine("VA测试9:\t" + (g3.Animat == GraphInfo.AnimatType.A_Start).ToString());

}
#pragma warning restore CS8602 // 解引用可能出现空引用。
#pragma warning restore CS8604 // 引用类型参数可能为 null。
Expand Down Expand Up @@ -828,7 +836,121 @@ public enum AnimatType
/// </summary>
public GraphType Type { get; set; } = GraphType.Common;
}
public class BetterBuyData
{
/// <summary>
/// 上次折扣时间
/// </summary>
[Line] public DateTime LastDiscont = DateTime.MinValue;

/// <summary>
/// 折扣数据
/// </summary>
[Line] public Dictionary<string, int> Discont = new Dictionary<string, int>();

/// <summary>
/// 定时购买的商品
/// </summary>
[Line] public ObservableCollection<ScheduleBuyItem> ScheduleBuyItems = new ObservableCollection<ScheduleBuyItem>();

/// <summary>
/// 购买历史
/// </summary>
[Line] public ObservableCollection<HistoryBuyItem> BuyHistory = new ObservableCollection<HistoryBuyItem>();

/// <summary>
/// 定时购买
/// </summary>
public class ScheduleBuyItem
{
/// <summary>
/// 更好买物品
/// </summary>
[Line] public string SalabilityItemName { get; set; }

/// <summary>
/// 数量
/// </summary>
[Line] public int Quantity { get; set; }

/// <summary>
/// 频次
/// </summary>
public enum BuyFrequency
{
None = 0,
EveryDay = 1,
EveryWeek = 7,
EveryMonth = 30,
}
/// <summary>
/// 购买频率
/// </summary>
[Line] public BuyFrequency Frequency { get; set; }

public int Frequency_id
{
get
{
switch (Frequency)
{
default:
case BuyFrequency.None:
return 0;
case BuyFrequency.EveryDay:
return 1;
case BuyFrequency.EveryWeek:
return 2;
case BuyFrequency.EveryMonth:
return 3;
}
}
set
{
switch (value)
{
default:
case 0:
Frequency = BuyFrequency.None;
break;
case 1:
Frequency = BuyFrequency.EveryDay;
break;
case 2:
Frequency = BuyFrequency.EveryWeek;
break;
case 3:
Frequency = BuyFrequency.EveryMonth;
break;
}
}
}
}
/// <summary>
/// 购买历史
/// </summary>
public class HistoryBuyItem
{
/// <summary>
/// 更好买物品
/// </summary>
[Line] public string SalabilityItemName { get; set; }
/// <summary>
/// 数量
/// </summary>
[Line] public int Quantity { get; set; }

/// <summary>
/// 金额
/// </summary>
[Line] public double Amount { get; set; }

/// <summary>
/// 购买日期
/// </summary>
[Line] public DateTime BuyTime { get; set; }
}
}
/// <summary>
/// 多人模式传输的消息
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions TestConsole/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions TestConsole/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ now#637994530277341822:|</value>
<value>getsetint#10:|
getsetDateTime#693937152000000000:|
intlistgetset#20,40,60,80,100:|
stringlistgetset#a:/!|a,b/idb,c/!|/!|c:|
stringlistgetset#a:/!!/!|a,b/!idb,c/!!/!|/!!/!|c:|
auto#test:|
pubstr#pubtest:|
db#3.1415926:|
db2#3141592600:|
tc:|getsetint#0:|getsetDateTime#694039104000000000:|intlistgetset#11,45,14,19,19:|stringlistgetset:|auto#test:|pubstr#substring:|db#0:|db2#0:|tc:|pubint#0:|intlist:|intdict#1=2/n3=4/n5=6:|
tc:|getsetint#0:|getsetDateTime#694039104000000000:|intlistgetset#11,45,14,19,19:|stringlistgetset#/null:|auto#test:|pubstr#substring:|db#0:|db2#0:|tc#/null:|pubint#0:|intlist#/null:|intdict#1=2/n3=4/n5=6:|
pubint#20:|
intlist#10,20,30,40,50:|
intdict:|1#2:|3#4:|5#6:|</value>
Expand Down
2 changes: 1 addition & 1 deletion TestConsole/TestConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\LinePutScript\Standard\LinePutScript.Standard.csproj" />
<ProjectReference Include="..\LinePutScript\Core\LinePutScript.Core.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit a3ee2ea

Please sign in to comment.