添加项目文件。

This commit is contained in:
zgf committed 2023-07-25 19:20:18 +08:00
1 parent 05e131ca4e
commit 514d6f5255
1162 files changed
+81255

No files matched your search

@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core.Json
{
/// <summary>
/// StringIgnoreLTGTConvert
/// 忽略客户端提交的 &lt;及&gt;字符
/// </summary>
public class BodyConverter : JsonConverter<PostedBody>
{
public override PostedBody Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rv = new PostedBody();
rv.ProNames = new List<string>();
List<string> prefix = new List<string>();
int depth = 0;
string lastObjecName = "";
int insideArray = 0;
JsonTokenType lastToken = JsonTokenType.Null;
while (true)
{
if (reader.TokenType == JsonTokenType.StartArray)
{
insideArray++;
depth++;
prefix.Add(lastObjecName + "[0]");
}
if (reader.TokenType == JsonTokenType.EndArray)
{
insideArray--;
depth--;
prefix.RemoveAt(prefix.Count - 1);
}
if (reader.TokenType == JsonTokenType.StartObject)
{
if (insideArray == 0)
{
depth++;
prefix.Add(lastObjecName);
if (rv.ProNames.Count > 0)
{
rv.ProNames.RemoveAt(rv.ProNames.Count - 1);
}
}
else
{
if (lastToken != JsonTokenType.StartArray)
{
reader.TrySkip();
reader.Read();
continue;
}
}
}
if (reader.TokenType == JsonTokenType.PropertyName)
{
var pname = reader.GetString();
lastObjecName = pname;
var p = prefix.Take(depth).ToSepratedString(seperator: ".");
if (string.IsNullOrEmpty(p) == false)
{
pname = p + "." + pname;
}
if (rv.ProNames.Contains(pname) == false)
{
rv.ProNames.Add(pname);
}
}
if (reader.TokenType == JsonTokenType.EndObject)
{
if (insideArray == 0)
{
depth--;
prefix.RemoveAt(prefix.Count - 1);
}
if (reader.IsFinalBlock == true && reader.CurrentDepth == 0)
{
reader.Read();
break;
}
}
lastToken = reader.TokenType;
reader.Read();
}
return rv;
}
public override void Write(Utf8JsonWriter writer, PostedBody value, JsonSerializerOptions options)
{
return;
}
}
public class PostedBody
{
public List<string> ProNames { get; set; }
}
}
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using NPOI.SS.Formula.Functions;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core.Json
{
public class BoolStringConverter :
JsonConverter<bool>
{
public override bool Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
JsonTokenType token = reader.TokenType;
if (token == JsonTokenType.String)
{
var s = reader.GetString() ?? "";
if (s.ToLower() == "true")
{
return true;
}
else if (s.ToLower() == "false")
{
return false;
}
else
{
return false;
}
}
else if (token == JsonTokenType.True || token == JsonTokenType.False)
{
return reader.GetBoolean();
}
else
{
return false;
}
}
public override void Write(
Utf8JsonWriter writer,
bool data,
JsonSerializerOptions options)
{
writer.WriteBooleanValue(data);
}
}
}
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using WalkingTec.Mvvm.Core;
namespace WalkingTec.Mvvm.Core.Json
{
public class DateRangeConverter : JsonConverter<DateRange>
{
public override DateRange Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
try
{
if (reader.TokenType == JsonTokenType.StartArray)
{
reader.Read();
string[] ds = new string[2];
ds[0] = reader.GetString();
reader.Read();
ds[1] = reader.GetString();
reader.Read();
if (DateRange.TryParse(ds, out var dateRange))
{
return dateRange;
}
else
{
return null;
}
}
}
catch (Exception)
{
}
return null;
}
public override void Write(Utf8JsonWriter writer, DateRange value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else
{
//writer.WriteStringValue(JsonSerializer.Serialize(value),);
writer.WriteStartArray();
writer.WriteStringValue(value.GetStartTime().ToString());
writer.WriteStringValue(value.GetEndTime().ToString());
writer.WriteEndArray();
}
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace WalkingTec.Mvvm.Core.Json
{
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out DateTime date))
return date;
}
return reader.GetDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core.Json
{
public class DynamicDataConverter : JsonConverter<DynamicData>
{
public override DynamicData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
try
{
DynamicData rv = new DynamicData();
rv.Fields = new Dictionary<string, object>();
string currentkey = "";
//object currentvalue;
int level = 0;
while (true)
{
if(reader.TokenType == JsonTokenType.StartObject) {
if(level > 0)
{
var inner = JsonSerializer.Deserialize<DynamicData>(ref reader, options);
rv.Fields.Add(currentkey, inner);
}
level++;
}
if(reader.TokenType == JsonTokenType.EndObject)
{
level--;
}
if (reader.TokenType == JsonTokenType.PropertyName)
{
currentkey = reader.GetString();
}
if(reader.TokenType == JsonTokenType.String || reader.TokenType == JsonTokenType.Number || reader.TokenType == JsonTokenType.True || reader.TokenType == JsonTokenType.False || reader.TokenType == JsonTokenType.Null)
{
var val = JsonSerializer.Deserialize<object>(ref reader,options);
rv.Fields.Add(currentkey, val);
}
if (reader.IsFinalBlock && level == 0)
{
//reader.Read();
break;
}
reader.Read();
}
return rv;
}
catch
{
return null;
}
}
public override void Write(Utf8JsonWriter writer, DynamicData value, JsonSerializerOptions options)
{
if (value == null || value.Fields == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStartObject();
foreach (var item in value.Fields)
{
if(item.Value == null)
{
if (options.DefaultIgnoreCondition == JsonIgnoreCondition.Never)
{
writer.WriteNull(item.Key);
}
}
else
{
writer.WritePropertyName(item.Key);
JsonSerializer.Serialize(writer, item.Value, options);
}
}
writer.WriteEndObject();
}
}
}
}
@@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Text.Unicode;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core.Json
{
public class PocoConverter : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
return typeof(TopBasePoco).IsAssignableFrom(typeToConvert);
}
public override JsonConverter CreateConverter(
Type type,
JsonSerializerOptions options)
{
var temp = CloneOptions(options);
foreach (var item in options.Converters)
{
if(item.GetType() != typeof(PocoConverter))
{
temp.Converters.Add(item);
}
}
JsonConverter converter = (JsonConverter)Activator.CreateInstance(
typeof(PocoConverterInner<>).MakeGenericType(
new Type[] { type }),
BindingFlags.Instance | BindingFlags.Public,
binder: null,
args: new object[] { temp },
culture: null);
return converter;
}
private JsonSerializerOptions CloneOptions(JsonSerializerOptions op)
{
JsonSerializerOptions rv = new JsonSerializerOptions();
rv.PropertyNamingPolicy = op.PropertyNamingPolicy;
rv.AllowTrailingCommas = op.AllowTrailingCommas;
rv.DefaultBufferSize = op.DefaultBufferSize;
rv.DefaultIgnoreCondition = op.DefaultIgnoreCondition;
rv.DictionaryKeyPolicy = op.DictionaryKeyPolicy;
rv.Encoder = op.Encoder;
rv.IgnoreReadOnlyFields = op.IgnoreReadOnlyFields;
rv.IgnoreReadOnlyProperties = op.IgnoreReadOnlyProperties;
rv.IncludeFields = op.IncludeFields;
rv.MaxDepth = op.MaxDepth;
rv.NumberHandling = op.NumberHandling;
rv.ReadCommentHandling = op.ReadCommentHandling;
rv.ReferenceHandler = op.ReferenceHandler;
rv.WriteIndented = op.WriteIndented;
return rv;
}
private class PocoConverterInner<T> :
JsonConverter<T> where T : TopBasePoco
{
protected readonly JsonSerializerOptions _options;
public PocoConverterInner(JsonSerializerOptions options)
{
_options = options;
}
public override T Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<T>(ref reader, _options);
}
public override void Write(
Utf8JsonWriter writer,
T data,
JsonSerializerOptions options)
{
var _datacache = new Dictionary<string, int>();
RemoveCycleReference(data, _datacache);
JsonSerializer.Serialize(writer, data, typeof(T), _options);
}
private void RemoveCycleReference(object Entity, Dictionary<string, int> datacache)
{
var pros = Entity.GetType().GetAllProperties();
var mainkey = Entity.GetType().FullName + (Entity as TopBasePoco).GetID();
datacache.TryAdd(mainkey, 1);
foreach (var pro in pros)
{
if (typeof(TopBasePoco).IsAssignableFrom(pro.PropertyType))
{
var subentity = pro.GetValue(Entity) as TopBasePoco;
string key = pro.PropertyType.FullName + subentity?.GetID() ?? "";
if (subentity != null && datacache.ContainsKey(key) == false)
{
RemoveCycleReference(subentity, datacache);
}
else
{
pro.SetValue(Entity,null);
}
}
//找到类型为List<xxx>的字段
if (pro.PropertyType.GenericTypeArguments.Count() > 0)
{
//获取xxx的类型
var ftype = pro.PropertyType.GenericTypeArguments.First();
//如果xxx继承自TopBasePoco
if (ftype.IsSubclassOf(typeof(TopBasePoco)))
{
//界面传过来的子表数据
if (pro.GetValue(Entity) is IEnumerable<TopBasePoco> list && list.Count() > 0)
{
bool found = false;
foreach (var newitem in list)
{
if (newitem != null)
{
string subkey = ftype.FullName + newitem?.GetID() ?? "";
if (datacache.ContainsKey(subkey) == false || (Entity is TreePoco && pro.Name == "Children"))
{
RemoveCycleReference(newitem, datacache.ToDictionary(x=>x.Key,x=>x.Value));
found = true;
}
else
{
found = false;
break;
}
}
}
if(found == false)
{
pro.SetValue(Entity, null);
}
}
}
}
}
}
}
}
}
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using NPOI.SS.Formula.Functions;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core.Json
{
public class RawStringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
try
{
if (reader.TokenType == JsonTokenType.String)
{
string rv = reader.GetString();
return rv;
}
}
catch (Exception)
{
}
return null;
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else
{
var txt = JsonEncodedText.Encode($"_raw_{value}_raw_", JavaScriptEncoder.UnsafeRelaxedJsonEscaping);
writer.WriteStringValue(txt);
}
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace System.Text.Json.Serialization
{
public class JsonStringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
return null;
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value);
}
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace System.Text.Json.Serialization
{
/// <summary>
/// StringIgnoreLTGTConvert
/// 忽略客户端提交的 &lt;及&gt;字符
/// </summary>
public class StringIgnoreLTGTConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString().Replace("<", string.Empty).Replace(">", string.Empty);
}
return null;
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value);
}
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using NPOI.SS.Formula.Functions;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core.Json
{
public class TypeConverter : JsonConverter<Type>
{
public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return null;
}
public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options)
{
writer.WriteNullValue();
}
}
}