XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Collections;
using XFEExtension.NetCore.XFETransform.ObjectInfoAnalyzer;

namespace XFEExtension.NetCore.XFETransform.StringConverter;

/// <summary>
/// Json转换器
/// </summary>
public class JsonTransformer : StringConverter
{
    /// <summary>
    /// 输出Json对象信息
    /// </summary>
    /// <param name="objectInfo"></param>
    /// <returns></returns>
    public override string OutPutObject(IObjectInfo objectInfo)
    {
        var outPutString = string.Empty;
        if (objectInfo.IsBasicType || objectInfo.ObjectPlace == ObjectPlace.Enum)
        {
            if (IsEnumerableMember(objectInfo))
            {
                switch (objectInfo.Value)
                {
                    case null:
                        outPutString += "null";
                        break;
                    case string:
                        outPutString += $"\"{objectInfo.Value}\"";
                        break;
                    default:
                        {
                            if (objectInfo.ObjectPlace == ObjectPlace.Enum)
                                outPutString += $"{(int)objectInfo.Value}";
                            else if (objectInfo.Value is bool)
                                outPutString += $"{objectInfo.Value.ToString()?.ToLower()}";
                            else if (objectInfo.Type == typeof(int) || objectInfo.Type == typeof(double) || objectInfo.Type == typeof(float) || objectInfo.Type == typeof(decimal) || objectInfo.Type == typeof(long) || objectInfo.Type == typeof(short) || objectInfo.Type == typeof(byte) || objectInfo.Type == typeof(nint) || objectInfo.Type == typeof(uint) || objectInfo.Type == typeof(ulong) || objectInfo.Type == typeof(ushort) || objectInfo.Type == typeof(sbyte))
                                outPutString += objectInfo.Value;
                            else
                                outPutString += $"\"{objectInfo.Value}\"";
                            break;
                        }
                }
            }
            else
            {
                switch (objectInfo.Value)
                {
                    case null:
                        outPutString += $"\"{objectInfo.Name}\":null";
                        break;
                    case string:
                        outPutString += $"\"{objectInfo.Name}\":\"{objectInfo.Value}\"";
                        break;
                    default:
                        {
                            if (objectInfo.ObjectPlace == ObjectPlace.Enum)
                                outPutString += $"\"{objectInfo.Name}\":{(int)objectInfo.Value}";
                            else if (objectInfo.Value is bool)
                                outPutString += $"\"{objectInfo.Name}\":{objectInfo.Value.ToString()?.ToLower()}";
                            else if (objectInfo.Type == typeof(int) || objectInfo.Type == typeof(double) || objectInfo.Type == typeof(float) || objectInfo.Type == typeof(decimal) || objectInfo.Type == typeof(long) || objectInfo.Type == typeof(short) || objectInfo.Type == typeof(byte) || objectInfo.Type == typeof(nint) || objectInfo.Type == typeof(uint) || objectInfo.Type == typeof(ulong) || objectInfo.Type == typeof(ushort) || objectInfo.Type == typeof(sbyte))
                                outPutString += $"\"{objectInfo.Name}\":{objectInfo.Value}";
                            else
                                outPutString += $"\"{objectInfo.Name}\":\"{objectInfo.Value}\"";
                            break;
                        }
                }
            }
        }
        else
        {
            if (objectInfo.Layer == 0)
            {
                if (objectInfo.IsArray)
                {
                    outPutString += objectInfo.SubObjects is not null ? $"""
                    [{OutPutSubObjects(objectInfo.SubObjects)}]
                    """ : "null";
                }
                else
                {
                    outPutString += objectInfo.SubObjects is not null ? $$"""
                    {{{OutPutSubObjects(objectInfo.SubObjects)}}}
                    """ : "null";
                }
            }
            else
            {
                if (objectInfo.SubObjects is not null)
                    outPutString += OutPutSubObjects(objectInfo.SubObjects);
            }
        }
        return outPutString;
    }
    /// <summary>
    /// 输出子对象的Json信息
    /// </summary>
    /// <param name="subObjects"></param>
    /// <returns></returns>
    public override string OutPutSubObjects(ISubObjects subObjects)
    {
        var outString = string.Empty;
        for (var i = 0; i < subObjects.Count; i++)
        {
            var obj = subObjects[i];
            var currentConnectString = i == subObjects.Count - 1 ? "" : ",";
            if (obj.IsBasicType || obj.ObjectPlace == ObjectPlace.Enum)
            {
                outString += obj.OutPutObject();
                outString += currentConnectString;
            }
            else
            {
                if (IsEnumerableClassMember(obj))
                    outString += obj.Value is not null ? $"""
                     "{obj.Name}": [{OutPutObject(obj)}]{currentConnectString}
                     """ : "null";
                else if (IsEnumerableMember(obj))
                    if (obj.Value is not null && (obj.Type!.IsAssignableTo(typeof(IEnumerable)) || obj.Type.IsAssignableTo(typeof(Array))))
                        outString += obj.Value is not null ? $"""
                     [{OutPutObject(obj)}]{currentConnectString}
                     """ : "null";
                    else
                        outString += obj.Value is not null ? $$"""
                     {{{OutPutObject(obj)}}}{{currentConnectString}}
                     """ : "null";
                else
                    outString += obj.Value is not null ? $$"""
                     "{{obj.Name}}": {{{OutPutObject(obj)}}}{{currentConnectString}}
                     """ : "null";
            }
        }
        if (outString.Length > 0 && outString[^1] == '\n')
        {
            outString = outString[..^1];
        }
        return outString;
    }

    internal static bool IsEnumerable(IObjectInfo objectInfo) => objectInfo.ObjectPlace is ObjectPlace.Array or ObjectPlace.List;
    internal static bool IsEnumerableMember(IObjectInfo objectInfo) => objectInfo.ObjectPlace is ObjectPlace.ArrayMember or ObjectPlace.ListMember;
    internal static bool IsEnumerableClassMember(IObjectInfo objectInfo) => objectInfo.Value is not null && objectInfo.ObjectPlace == ObjectPlace.Property && (objectInfo.Type!.IsAssignableTo(typeof(IEnumerable)) || objectInfo.Type.IsAssignableTo(typeof(Array)));
}