更新JSON格式化
增加剪切板helper
This commit is contained in:
1 parent
d3488c98f5
commit
a217a74868
7 files changed
+342
-156
No files matched your search
@@ -1,11 +1,9 @@
|
||||
using LaunchDarkly.EventSource;
|
||||
using Newtonsoft.Json;
|
||||
using OrderSupportTicket.Models;
|
||||
using OrderSupportTicket.Models.Api;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrderSupportTicket.Services;
|
||||
@@ -134,14 +132,9 @@ public sealed class EventSourceTicketRealtimeService : ITicketRealtimeService
|
||||
TicketRealtimeNotification notification;
|
||||
try
|
||||
{
|
||||
var serializer = new DataContractJsonSerializer(
|
||||
typeof(TicketRealtimeNotification));
|
||||
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
|
||||
{
|
||||
notification = (TicketRealtimeNotification)serializer.ReadObject(stream);
|
||||
}
|
||||
notification = JsonConvert.DeserializeObject<TicketRealtimeNotification>(data, TicketApiService.JsonSettings);
|
||||
}
|
||||
catch (SerializationException)
|
||||
catch (JsonException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
using AllBase;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using OrderSupportTicket.Models;
|
||||
using OrderSupportTicket.Models.Api;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using WxOrder.Framework.Commons;
|
||||
@@ -29,6 +27,11 @@ public sealed class TicketApiService : ITicketApiService
|
||||
new Regex(@"^[\p{L}\p{N}_.@-]{2,120}$", RegexOptions.Compiled);
|
||||
private static readonly Regex OperationIdRegex =
|
||||
new Regex(@"^[A-Za-z0-9_.:-]{1,128}$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
||||
internal static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
static TicketApiService()
|
||||
{
|
||||
if (EnvironmentHelper.IsTest)
|
||||
@@ -259,8 +262,8 @@ public sealed class TicketApiService : ITicketApiService
|
||||
object request)
|
||||
{
|
||||
var url = BuildUri(path);
|
||||
// 请求 DTO 使用 DataMember 驼峰字段,先序列化后走 GetHtml 的字符串重载。
|
||||
var json = Serialize(request);
|
||||
// 请求 DTO 使用 JsonProperty 驼峰字段,先序列化后走 GetHtml 的字符串重载。
|
||||
var json = JsonConvert.SerializeObject(request, JsonSettings);
|
||||
var html = await GetHtml.PostJsonAsync(url, json);
|
||||
if (string.IsNullOrWhiteSpace(html))
|
||||
throw RequestFailed("工单接口未返回数据。");
|
||||
@@ -275,9 +278,9 @@ public sealed class TicketApiService : ITicketApiService
|
||||
ApiSuccessResponse response;
|
||||
try
|
||||
{
|
||||
response = Deserialize<ApiSuccessResponse>(html);
|
||||
response = JsonConvert.DeserializeObject<ApiSuccessResponse>(html, JsonSettings);
|
||||
}
|
||||
catch (SerializationException)
|
||||
catch (JsonException)
|
||||
{
|
||||
throw RequestFailed(html);
|
||||
}
|
||||
@@ -296,12 +299,12 @@ public sealed class TicketApiService : ITicketApiService
|
||||
EnsureSuccessBody(html);
|
||||
try
|
||||
{
|
||||
var result = Deserialize<T>(html);
|
||||
var result = JsonConvert.DeserializeObject<T>(html, JsonSettings);
|
||||
if (result == null)
|
||||
throw new SerializationException("接口响应为空。");
|
||||
throw new JsonSerializationException("接口响应为空。");
|
||||
return result;
|
||||
}
|
||||
catch (SerializationException)
|
||||
catch (JsonException)
|
||||
{
|
||||
throw RequestFailed("工单接口返回了无法解析的数据。", html);
|
||||
}
|
||||
@@ -314,38 +317,19 @@ public sealed class TicketApiService : ITicketApiService
|
||||
|
||||
try
|
||||
{
|
||||
var error = Deserialize<ApiErrorResponse>(body);
|
||||
var error = JsonConvert.DeserializeObject<ApiErrorResponse>(body, JsonSettings);
|
||||
return error == null
|
||||
? null
|
||||
: error.Message
|
||||
?? error.Error
|
||||
?? error.Code;
|
||||
}
|
||||
catch (SerializationException)
|
||||
catch (JsonException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string Serialize(object value)
|
||||
{
|
||||
var serializer = new DataContractJsonSerializer(value.GetType());
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
serializer.WriteObject(stream, value);
|
||||
return Encoding.UTF8.GetString(stream.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static T Deserialize<T>(string json)
|
||||
{
|
||||
var serializer = new DataContractJsonSerializer(typeof(T));
|
||||
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json ?? string.Empty)))
|
||||
{
|
||||
return (T)serializer.ReadObject(stream);
|
||||
}
|
||||
}
|
||||
|
||||
private static string BuildUri(string path)
|
||||
{
|
||||
return ServerUrl + path;
|
||||
@@ -368,11 +352,11 @@ public sealed class TicketApiService : ITicketApiService
|
||||
|
||||
try
|
||||
{
|
||||
var error = Deserialize<ApiErrorResponse>(responseBody);
|
||||
var error = JsonConvert.DeserializeObject<ApiErrorResponse>(responseBody, JsonSettings);
|
||||
if (error != null && int.TryParse(error.Code, out var code))
|
||||
return code;
|
||||
}
|
||||
catch (SerializationException)
|
||||
catch (JsonException)
|
||||
{
|
||||
// GetHtml 在网络异常时会返回普通错误文本,此时没有 HTTP 状态码。
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user