惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 于斯人也

JsonHelper MergeJsonTemplate Add Indexer to DynamicJson Etw EventSourceProvider_EventsProducer.cs OopConsoleTraceEventListenerMonitor_TraceControllerEventsConsumer.cs ConcurrentAsyncQueue 2014-09-07 KeyedPriorityQueue JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to <T> object , XmlToJson/JsonToXml, QuoteName by using JToken Path. Udp SocketAsyncEventArgs SocketAsyncDataHandler C#: using JsonReader avoid Deserialize Json to dynamic XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate SharePoint 2013 Apps TokenHelper SharePointContext OAuth Provider-Hosted App (抄袭,测试 csc.rsp 用) nuget packages batch install csc.rsp Nuget MVC/WebAPI、SignalR、Rx、Json、EntityFramework、OAuth、Spatial SQL 2012 Group By Rollup, Grouping SQL 2012 Restore HeaderOnly EasyPerformanceCounterHelper 并发异步处理队列 .NET 4.5+ (改进性能计数器) 2013-11-16 CodeTimerPerformance EasyPerformanceCounterHelper .NET 4.5 csc.rsp Nuget MVC/WebAPI 5.0、SignalR 2.0、Rx、Json、Azure、EntityFramework、OAuth、Spatial MEF Parts Sample
WorkFlowHelper
于斯人也 · 2015-12-21 · via 博客园 - 于斯人也

/*
# Microshaoft
/r:System.Xaml.dll
/r:System.Activities.dll
/r:System.Activities.DurableInstancing.dll
/r:System.Runtime.DurableInstancing.dll
/r:"D:\Microshaoft.Nuget.Packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll"
*/
namespace Microshaoft
{
    using Newtonsoft.Json.Linq;
    using System;
    using System.Activities;
    using System.Activities.Tracking;
    using System.Activities.XamlIntegration;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Xaml;
    using System.Xml;
    using System.Runtime.DurableInstancing;
    public static class WorkFlowHelper
    {
        public static WorkflowApplication CreateWorkflowApplication
                                            (
                                                string xaml
                                                , string localAssemblyFilePath = null
                                                , Func<InstanceStore> onPersistProcessFunc = null
                                            )
        {
            var activity = XamlToActivity
                                (
                                    xaml
                                    , localAssemblyFilePath
                                );
            WorkflowApplication workflowApplication = new WorkflowApplication(activity);
            if (onPersistProcessFunc != null)
            {
                workflowApplication.InstanceStore = onPersistProcessFunc();
            }
            return workflowApplication;
        }
        public static Activity XamlToActivity
                                    (
                                        string xaml
                                        , string localAssemblyFilePath = null
                                    )
        {
            Assembly localAssembly = null;
            if (string.IsNullOrEmpty(localAssemblyFilePath))
            {
                localAssembly = Assembly
                                    .GetExecutingAssembly();
            }
            else
            {
                localAssembly = Assembly
                                    .LoadFrom(localAssemblyFilePath);
            }
            var stringReader = new StringReader(xaml);
            var xmlReader = XmlReader.Create(stringReader);
            var xamlXmlReader = new XamlXmlReader
                                                (
                                                    xmlReader
                                                    , new XamlXmlReaderSettings()
                                                        {
                                                            LocalAssembly = localAssembly
                                                        }
                                                );
            var xamlReader = ActivityXamlServices
                                        .CreateReader
                                            (
                                                xamlXmlReader
                                            );
            var activity = ActivityXamlServices
                                .Load
                                    (
                                        xamlReader
                                        , new ActivityXamlServicesSettings()
                                        {
                                            CompileExpressions = true
                                        }
                                    );
            return activity;
        }
        public static TrackingProfile GetTrackingProfileFromJson
            (
                string json
                , bool isArray = false
            )
        {
            TrackingProfile trackingProfile = null;
            var trackingQueries = GetTrackingQueriesFromJson(json, isArray);
            if (trackingQueries != null)
            {
                foreach (var trackingQuery in trackingQueries)
                {
                    if (trackingProfile == null)
                    {
                        trackingProfile = new TrackingProfile();
                    }
                    trackingProfile
                            .Queries
                            .Add(trackingQuery);
                }
            }
            return trackingProfile;
        }
        public static TrackingParticipant GetTrackingParticipantFromJson<TTrackingParticipant>
                                                    (
                                                        string json
                                                        , bool isArray = false
                                                    )
            where TTrackingParticipant : TrackingParticipant, new()
        {
            TrackingParticipant trackingParticipant = null;
            TrackingProfile trackingProfile
                    = GetTrackingProfileFromJson(json, isArray);
            if (trackingProfile != null)
            {
                trackingParticipant = new TTrackingParticipant();
                trackingParticipant.TrackingProfile = trackingProfile;
            }
            return trackingParticipant;
        }
        public static IEnumerable<TrackingQuery> GetTrackingQueriesFromJson
                                                        (
                                                            string json
                                                            , bool isArray = false
                                                        )
        {
            IEnumerable<TrackingQuery> r = null;
            if (isArray)
            {
                //闭包
                var key = string.Empty;
                r = JsonHelper
                        .DeserializeToFromDictionary<string, JObject[], JObject[]>
                                (
                                    json
                                    , (x, y) =>
                                    {
                                        //闭包
                                        key = x;
                                        return y;
                                    }
                                )
                                .SelectMany
                                    (
                                        (x) =>
                                        {
                                            return x;
                                        }
                                    )
                                .Select
                                    (
                                        (x) =>
                                        {
                                            //闭包
                                            return
                                                GetTrackingQuery(key, x);
                                        }
                                    );
            }
            else
            {
                r = JsonHelper
                        .DeserializeToFromDictionary<string, JObject, TrackingQuery>
                            (
                                json
                                , (x, y) =>
                                {
                                    return GetTrackingQuery(x, y);
                                }
                            );
            }
            return r;
        }
        public static TrackingQuery GetTrackingQuery(string queryName, JObject jObject)
        {
            var json = jObject.ToString();
            return
                GetTrackingQuery
                        (
                            queryName
                            , json
                        );
        }
        public static TrackingQuery GetTrackingQuery(string queryName, string json)
        {
            TrackingQuery r = null;
            if (string.Compare(queryName, "WorkflowInstanceQuery", true) == 0)
            {
                r = JsonHelper
                            .DeserializeByJTokenPath<WorkflowInstanceQuery>
                                (
                                    json
                                );
            }
            else if (string.Compare(queryName, "ActivityStateQuery", true) == 0)
            {
                r = JsonHelper
                            .DeserializeByJTokenPath<ActivityStateQuery>
                                (
                                    json
                                );
            }
            else if (string.Compare(queryName, "CustomTrackingQuery", true) == 0)
            {
                r = JsonHelper
                            .DeserializeByJTokenPath<CustomTrackingQuery>
                                (
                                    json
                                );
            }
            else if (string.Compare(queryName, "FaultPropagationQuery", true) == 0)
            {
                r = JsonHelper
                            .DeserializeByJTokenPath<FaultPropagationQuery>
                                (
                                    json
                                );
            }
            else if (string.Compare(queryName, "BookmarkResumptionQuery", true) == 0)
            {
                r = JsonHelper
                            .DeserializeByJTokenPath<BookmarkResumptionQuery>
                                (
                                    json
                                );
            }
            else if (string.Compare(queryName, "ActivityScheduledQuery", true) == 0)
            {
                r = JsonHelper
                            .DeserializeByJTokenPath<ActivityScheduledQuery>
                                (
                                    json
                                );
            }
            else if (string.Compare(queryName, "CancelRequestedQuery", true) == 0)
            {
                r = JsonHelper
                            .DeserializeByJTokenPath<CancelRequestedQuery>
                                (
                                    json
                                );
            }
            return r;
        }
    }
}
namespace Microshaoft
{
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    using System.Collections.Generic;
    public static class JsonHelper
    {
        public static string XmlToJson
                                (
                                    string xml
                                    , Newtonsoft
                                            .Json
                                            .Formatting formatting
                                                            = Newtonsoft
                                                                    .Json
                                                                    .Formatting
                                                                    .Indented
                                    , bool needKeyQuote = false
                                )
        {
            XNode xElement;
            xElement = XElement.Parse(xml).Elements().First();
            string json = string.Empty;
            using (var stringWriter = new StringWriter())
            {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter))
                {
                    jsonTextWriter.Formatting = formatting;
                    jsonTextWriter.QuoteName = needKeyQuote;
                    var jsonSerializer = new JsonSerializer();
                    jsonSerializer.Serialize(jsonTextWriter, xElement);
                    json = stringWriter.ToString();
                }
            }
            return json;
        }
        public static string JsonToXml
                        (
                            string json
                            , bool needRoot = false
                            , string defaultDeserializeRootElementName = "root"
                        )
        {
            if (needRoot)
            {
                json = string.Format
                                (
                                    @"{{ {1}{0}{2} }}"
                                    , " : "
                                    , defaultDeserializeRootElementName
                                    , json
                                );
            }
            //XmlDocument xmlDocument = JsonConvert.DeserializeXmlNode(json, defaultDeserializeRootElementName);
            var xDocument = JsonConvert
                                    .DeserializeXNode
                                        (
                                            json
                                            , defaultDeserializeRootElementName
                                        );
            var xml = xDocument
                            .Elements()
                            .First()
                            .ToString();
            return xml;
        }
        public static T DeserializeByJTokenPath<T>
            (
                string json
                , string jTokenPath = null //string.Empty
            )
        {
            var jObject = JObject.Parse(json);
            var jsonSerializer = new JsonSerializer();
            if (string.IsNullOrEmpty(jTokenPath))
            {
                jTokenPath = string.Empty;
            }
            var jToken = jObject.SelectToken(jTokenPath);
            using (var jsonReader = jToken.CreateReader())
            {
                return
                    jsonSerializer
                        .Deserialize<T>(jsonReader);
            }
        }
        public static string Serialize
                                (
                                    object target
                                    , bool formattingIndented = false
                                    , bool keyQuoteName = false
                                )
        {
            string json = string.Empty;
            using (StringWriter stringWriter = new StringWriter())
            {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter))
                {
                    jsonTextWriter.QuoteName = keyQuoteName;
                    jsonTextWriter.Formatting = (formattingIndented ? Formatting.Indented : Formatting.None);
                    var jsonSerializer = new JsonSerializer();
                    jsonSerializer.Serialize(jsonTextWriter, target);
                    json = stringWriter.ToString();
                }
            }
            return json;
        }
        public static void ReadJsonPathsValuesAsStrings
                            (
                                string json
                                , string[] jsonPaths
                                , Func<string, string, bool> onReadedOncePathStringValueProcesssFunc = null
                            )
        {
            using (var stringReader = new StringReader(json))
            {
                using (var jsonReader = new JsonTextReader(stringReader))
                {
                    bool breakAndReturn = false;
                    while
                        (
                            jsonReader.Read()
                            &&
                            !breakAndReturn
                        )
                    {
                        foreach (var x in jsonPaths)
                        {
                            if (x == jsonReader.Path)
                            {
                                if (onReadedOncePathStringValueProcesssFunc != null)
                                {
                                    var s = jsonReader.ReadAsString();
                                    breakAndReturn
                                            = onReadedOncePathStringValueProcesssFunc
                                                    (
                                                        x
                                                        , s
                                                    );
                                    if (breakAndReturn)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public static IEnumerable<TElement> 
                            DeserializeToFromDictionary<TKey, TValue, TElement>
                                        (
                                            string json
                                            , Func<TKey, TValue, TElement> OnOneElementProcessFunc
                                        )
        {
            //IEnumerable<TElement> r = default(IEnumerable<TElement>);
            return
                    DeserializeByJTokenPath<Dictionary<TKey, TValue>>(json)
                        .Select
                            (
                                (x) =>
                                {
                                    var rr = OnOneElementProcessFunc(x.Key, x.Value);
                                    return rr;
                                }
                            );
            //return r;
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Activities.Tracking;
    public class CommonTrackingParticipant : TrackingParticipant
    {
        public Func<TrackingRecord, TimeSpan, bool> OnTrackingRecordReceived;
        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
            var r = false;
            if (OnTrackingRecordReceived != null)
            {
                r = OnTrackingRecordReceived(record, timeout);
            }
        }
    }
}