|
1 | | -using GameFrameX.Foundation.Json; |
2 | | -using GameFrameX.Foundation.Logger; |
3 | | - |
4 | | -namespace GameFrameX.Foundation.Http.Normalization; |
5 | | - |
6 | | -/// <summary> |
7 | | -/// 提供用于处理HTTP JSON结果的扩展方法。 |
8 | | -/// </summary> |
9 | | -public static class HttpJsonResultHelper |
10 | | -{ |
11 | | - /// <summary> |
12 | | - /// 将JSON字符串转换为HttpJsonResultData对象 |
13 | | - /// </summary> |
14 | | - /// <typeparam name="T">泛型参数T,表示要反序列化的目标类型,必须是引用类型且包含无参构造函数</typeparam> |
15 | | - /// <param name="jsonResult">需要转换的JSON字符串</param> |
16 | | - /// <returns>返回转换后的HttpJsonResultData对象,包含反序列化结果和状态信息</returns> |
17 | | - /// <remarks> |
18 | | - /// 该方法会: |
19 | | - /// 1. 尝试将JSON字符串反序列化为HttpJsonResult对象 |
20 | | - /// 2. 根据响应码(Code)判断请求是否成功(IsSuccess属性自动根据Code==0计算) |
21 | | - /// 3. 如果成功(Code=0),则将Data字段反序列化为泛型类型T |
22 | | - /// 4. 如果失败,则保留错误信息,Data字段为默认值 |
23 | | - /// </remarks> |
24 | | - public static HttpJsonResultData<T> ToHttpJsonResultData<T>(this string jsonResult) where T : class, new() |
25 | | - { |
26 | | - // 创建默认结果对象,Code初始化为失败状态 |
27 | | - HttpJsonResultData<T> resultData = new() |
28 | | - { |
29 | | - Code = HttpJsonResultConstants.FailCode, |
30 | | - }; |
31 | | - try |
32 | | - { |
33 | | - // 反序列化JSON字符串为HttpJsonResult对象 |
34 | | - var httpJsonResult = JsonHelper.Deserialize<HttpJsonResult>(jsonResult); |
35 | | - // 检查响应码是否表示成功 |
36 | | - if (httpJsonResult.Code != HttpJsonResultConstants.SuccessCode) |
37 | | - { |
38 | | - resultData.Code = httpJsonResult.Code; |
39 | | - resultData.Message = httpJsonResult.Message; |
40 | | - return resultData; // 返回失败结果 |
41 | | - } |
42 | | - |
43 | | - // 设置成功状态(IsSuccess会自动根据Code==0返回true) |
44 | | - resultData.Code = HttpJsonResultConstants.SuccessCode; |
45 | | - // 反序列化数据部分,如果数据为空则返回类型T的默认实例 |
46 | | - resultData.Data = string.IsNullOrEmpty(httpJsonResult.Data) ? new T() : JsonHelper.Deserialize<T>(httpJsonResult.Data); |
47 | | - } |
48 | | - catch (Exception e) |
49 | | - { |
50 | | - // 捕获并输出异常信息 |
51 | | - LogHelper.Fatal(e, "JSON Deserialize Error: {ErrorMessage}", e.Message); |
52 | | - } |
53 | | - |
54 | | - return resultData; // 返回结果数据 |
55 | | - } |
56 | | -} |
| 1 | +// ========================================================================================== |
| 2 | +// GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利 |
| 3 | +// GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights |
| 4 | +// 均受中华人民共和国及相关国际法律法规保护。 |
| 5 | +// are protected by the laws of the People's Republic of China and relevant international regulations. |
| 6 | +// |
| 7 | +// 使用本项目须严格遵守相应法律法规及开源许可证之规定。 |
| 8 | +// Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses. |
| 9 | +// |
| 10 | +// 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发, |
| 11 | +// This project is dual-licensed under the MIT License and Apache License 2.0, |
| 12 | +// 完整许可证文本请参见源代码根目录下的 LICENSE 文件。 |
| 13 | +// please refer to the LICENSE file in the root directory of the source code for the full license text. |
| 14 | +// |
| 15 | +// 禁止利用本项目实施任何危害国家安全、破坏社会秩序、 |
| 16 | +// It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order, |
| 17 | +// 侵犯他人合法权益等法律法规所禁止的行为! |
| 18 | +// or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations! |
| 19 | +// 因基于本项目二次开发所产生的一切法律纠纷与责任, |
| 20 | +// Any legal disputes and liabilities arising from secondary development based on this project |
| 21 | +// 本项目组织与贡献者概不承担。 |
| 22 | +// shall be borne solely by the developer; the project organization and contributors assume no responsibility. |
| 23 | +// |
| 24 | +// GitHub 仓库:https://github.com/GameFrameX |
| 25 | +// GitHub Repository: https://github.com/GameFrameX |
| 26 | +// Gitee 仓库:https://gitee.com/GameFrameX |
| 27 | +// Gitee Repository: https://gitee.com/GameFrameX |
| 28 | +// CNB 仓库:https://cnb.cool/GameFrameX |
| 29 | +// CNB Repository: https://cnb.cool/GameFrameX |
| 30 | +// 官方文档:https://gameframex.doc.alianblank.com/ |
| 31 | +// Official Documentation: https://gameframex.doc.alianblank.com/ |
| 32 | +// ========================================================================================== |
| 33 | + |
| 34 | +using GameFrameX.Foundation.Json; |
| 35 | +using GameFrameX.Foundation.Logger; |
| 36 | + |
| 37 | +namespace GameFrameX.Foundation.Http.Normalization; |
| 38 | + |
| 39 | +/// <summary> |
| 40 | +/// 提供用于处理HTTP JSON结果的扩展方法。 |
| 41 | +/// </summary> |
| 42 | +public static class HttpJsonResultHelper |
| 43 | +{ |
| 44 | + /// <summary> |
| 45 | + /// 将JSON字符串转换为HttpJsonResultData对象 |
| 46 | + /// </summary> |
| 47 | + /// <typeparam name="T">泛型参数T,表示要反序列化的目标类型,必须是引用类型且包含无参构造函数</typeparam> |
| 48 | + /// <param name="jsonResult">需要转换的JSON字符串</param> |
| 49 | + /// <returns>返回转换后的HttpJsonResultData对象,包含反序列化结果和状态信息</returns> |
| 50 | + /// <remarks> |
| 51 | + /// 该方法会: |
| 52 | + /// 1. 尝试将JSON字符串反序列化为HttpJsonResult对象 |
| 53 | + /// 2. 根据响应码(Code)判断请求是否成功(IsSuccess属性自动根据Code==0计算) |
| 54 | + /// 3. 如果成功(Code=0),则将Data字段反序列化为泛型类型T |
| 55 | + /// 4. 如果失败,则保留错误信息,Data字段为默认值 |
| 56 | + /// </remarks> |
| 57 | + public static HttpJsonResultData<T> ToHttpJsonResultData<T>(this string jsonResult) where T : class, new() |
| 58 | + { |
| 59 | + // 创建默认结果对象,Code初始化为失败状态 |
| 60 | + HttpJsonResultData<T> resultData = new() |
| 61 | + { |
| 62 | + Code = HttpJsonResultConstants.FailCode, |
| 63 | + }; |
| 64 | + try |
| 65 | + { |
| 66 | + // 反序列化JSON字符串为HttpJsonResult对象 |
| 67 | + var httpJsonResult = JsonHelper.Deserialize<HttpJsonResult>(jsonResult); |
| 68 | + // 检查响应码是否表示成功 |
| 69 | + if (httpJsonResult.Code != HttpJsonResultConstants.SuccessCode) |
| 70 | + { |
| 71 | + resultData.Code = httpJsonResult.Code; |
| 72 | + resultData.Message = httpJsonResult.Message; |
| 73 | + return resultData; // 返回失败结果 |
| 74 | + } |
| 75 | + |
| 76 | + // 设置成功状态(IsSuccess会自动根据Code==0返回true) |
| 77 | + resultData.Code = HttpJsonResultConstants.SuccessCode; |
| 78 | + // 反序列化数据部分,如果数据为空则返回类型T的默认实例 |
| 79 | + resultData.Data = string.IsNullOrEmpty(httpJsonResult.Data) ? new T() : JsonHelper.Deserialize<T>(httpJsonResult.Data); |
| 80 | + } |
| 81 | + catch (Exception e) |
| 82 | + { |
| 83 | + // 捕获并输出异常信息 |
| 84 | + LogHelper.Fatal(e, "JSON Deserialize Error: {ErrorMessage}", e.Message); |
| 85 | + } |
| 86 | + |
| 87 | + return resultData; // 返回结果数据 |
| 88 | + } |
| 89 | +} |
0 commit comments