chore: 将 json 序列化工具提出来

oneRain 2020-05-13 14:07:35 +08:00
parent 45cb4a14c3
commit 1c70bda35c
6 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace LeanCloud.Common {
public class LCJsonConverter : JsonConverter {
public override bool CanConvert(Type objectType) {
return objectType == typeof(object);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
serializer.Serialize(writer, value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
if (reader.TokenType == JsonToken.StartObject) {
var obj = new Dictionary<string, object>();
serializer.Populate(reader, obj);
return obj;
}
if (reader.TokenType == JsonToken.StartArray) {
var arr = new List<object>();
serializer.Populate(reader, arr);
return arr;
}
if (reader.TokenType == JsonToken.Integer) {
if ((long)reader.Value < int.MaxValue) {
return Convert.ToInt32(reader.Value);
}
}
return serializer.Deserialize(reader);
}
public readonly static LCJsonConverter Default = new LCJsonConverter();
}
}

View File

@ -0,0 +1,7 @@
using System;
namespace LiveQuery.Internal {
public class LCLiveQueryHeartBeat {
public LCLiveQueryHeartBeat() {
}
}
}

View File

@ -0,0 +1,7 @@
using System;
namespace LiveQuery {
public class LCQueryExtension {
public LCQueryExtension() {
}
}
}

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,9 @@
using System;
namespace LiveQueryApp {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace RealtimeApp {
/// <summary>
/// 单线程环境,用于控制台应用 await 返回
/// </summary>
public class SingleThreadSynchronizationContext : SynchronizationContext {
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> queue = new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();
public override void Post(SendOrPostCallback d, object state) {
queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
}
public void RunOnCurrentThread() {
while (queue.TryTake(out KeyValuePair<SendOrPostCallback, object> workItem, Timeout.Infinite)) {
workItem.Key(workItem.Value);
}
}
public void Complete() {
queue.CompleteAdding();
}
public static void Run(Func<Task> func) {
SynchronizationContext prevContext = Current;
try {
SingleThreadSynchronizationContext syncContext = new SingleThreadSynchronizationContext();
SetSynchronizationContext(syncContext);
Task t = func();
syncContext.RunOnCurrentThread();
t.GetAwaiter().GetResult();
} finally {
SetSynchronizationContext(prevContext);
}
}
}
}