private void Load() { string confFile = Path.Combine(this._ConfigurationPath, this._ConfigurationFile); StreamReader sR = null; Newtonsoft.Json.JsonReader jsonReader = null; try { sR = new StreamReader(confFile, Encoding.UTF8); jsonReader = new Newtonsoft.Json.JsonTextReader(sR); Newtonsoft.Json.JsonSerializer jsonSerializer = new Newtonsoft.Json.JsonSerializer(); this.Configuration = jsonSerializer.Deserialize <Xeora>(jsonReader); } catch (Exception ex) { throw new ConfigurationWrongException(ex); } finally { if (jsonReader != null) { jsonReader.Close(); } if (sR != null) { sR.Close(); } } }
public void Deserialize(OperationDescription operation, Dictionary <string, int> parameterNames, Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException( "Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(rawBody)) using (var sr = new StreamReader(ms)) { var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (parameterNames.ContainsKey(parameterName)) { int parameterIndex = parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); } }
public void Deserialize(OperationDescription operation, Dictionary<string, int> parameterNames, Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException( "Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(rawBody)) using (var sr = new StreamReader(ms)) { var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (parameterNames.ContainsKey(parameterName)) { int parameterIndex = parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); } }
public Form1() { InitializeComponent(); string jsonFilePath = ""; AppSettingsReader asr = new AppSettingsReader(); string previousName = ""; getConfigValue <string>(asr, "DefinitionFile", ref jsonFilePath, string.Empty); if (!string.IsNullOrEmpty(jsonFilePath) && File.Exists(jsonFilePath)) { this.lblCurrentFile.Text = jsonFilePath; using (TextReader tr = new StreamReader(jsonFilePath)) { Newtonsoft.Json.JsonTextReader rdr = new Newtonsoft.Json.JsonTextReader(tr); while (rdr.Read()) { // Debug.WriteLine($"{rdr.ReadAsString()}"); // reference: https://www.newtonsoft.com/json/help/html/ReadJsonWithJsonTextReader.htm if (rdr.Value != null) { Debug.WriteLine("Token: {0}, Value: {1}", rdr.TokenType, rdr.Value); if (rdr.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { if (rdr.Value.ToString().CompareTo("title") == 0) { previousName = "title"; } else if (rdr.Value.ToString().CompareTo("questions") == 0) { previousName = "questions"; } else { // save the setting if (previousName.CompareTo("title") == 0) { this.Text = rdr.Value.ToString(); } else { questions.Add(rdr.Value.ToString()); } } } else if (rdr.TokenType == Newtonsoft.Json.JsonToken.String) { answers.Add(rdr.Value.ToString()); } } } rdr.Close(); ShowQuestion(1); } } }
static JObject LoadConfig() { string config = Path.Combine(AppContext.BaseDirectory, "ServerConfig.json"); //we're using this for our config file -- wouldn't do this for production if (File.Exists(config)) { Newtonsoft.Json.JsonTextReader rdr = new Newtonsoft.Json.JsonTextReader(File.OpenText(config)); //read it in for use if (rdr.Read()) { JObject o = JToken.Load(rdr) as JObject; rdr.Close(); return(o); } } return(null); }
public object MtJsonParse(ScriptThread thread, object[] arguments) { var ret = new MtResult(); var arg0 = arguments[0] as MtResult; arg0.GetValue((o) => { var s = o.Value == null ? "null" : o.Value.ToString(); var reader = new Newtonsoft.Json.JsonTextReader(new StringReader(s)); Action readStuff = null; readStuff = () => { var hasStuff = reader.Read(); if (hasStuff) { #if DEBUG && !SILVERLIGHT Debug.Print("Token: {0}", reader.Value); Debug.Print("Value Type: {0}", reader.ValueType); Debug.Print("Token Type: {0}", reader.TokenType); #endif readStuff(); } else { reader.Close(); ret.SetValue(MtObject.True); } }; readStuff(); }); return(ret); }
public void DeserializeRequest(Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty) == null || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); var rawBody = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(rawBody)) { using (var sr = new StreamReader(ms)) { var serializer = NewtonsoftJsonSettings.GetSerializer(); if (parameters.Length == 1) { if (m_operation.Messages[0].Body.Parts[0].Type == typeof(string)) { var str = sr.ReadToEnd(); var queryString = HttpUtility.ParseQueryString(str); if (queryString.AllKeys.Contains(m_operation.Messages[0].Body.Parts[0].Name)) { parameters[0] = Convert.ChangeType(queryString[m_operation.Messages[0].Body.Parts[0].Name], m_operation.Messages[0].Body.Parts[0].Type); } } else { parameters[0] = serializer.Deserialize(sr, m_operation.Messages[0].Body.Parts[0].Type); } } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; if (parameterName == null) { throw new InvalidOperationException("The object contained a parameter, however the value was null."); } reader.Read(); if (this.m_parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.m_parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.m_operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } } } }
public void DeserializeRequest(Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); var rawBody = bodyReader.ReadContentAsBase64(); var ms = new MemoryStream(rawBody); var sr = new StreamReader(ms); var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (parameterNames.ContainsKey(parameterName)) { var parameterIndex = parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } //Attach parameters retrieved from the Uri var templateMatchResults = (UriTemplateMatch)message.Properties["UriTemplateMatchResults"]; foreach (var parameterName in parameterNames.Where(parameterName => parameters[parameterName.Value] == null)) { if(templateMatchResults.BoundVariables.AllKeys.Contains(parameterName.Key.ToUpper())) parameters[parameterName.Value] = templateMatchResults.BoundVariables[parameterName.Key.ToUpper()]; } reader.Close(); } sr.Close(); ms.Close(); }
public void DeserializeRequest(Message message, object[] parameters) { //如果是上传的content-type,则不作json处理 var headers = ((HttpRequestMessageProperty)(message.Properties[HttpRequestMessageProperty.Name])).Headers; string contenttype = headers["Content-type"]; string contentLength = headers["Content-Length"]; if (contenttype != null && contenttype.StartsWith("multipart/form-data")) { //获得附件分割边界字符串 string boundary = contenttype.Substring(contenttype.IndexOf("boundary=") + "boundary=".Length); int len = int.Parse(contentLength); //获得方法的参数 var paramts = operation.SyncMethod.GetParameters(); int streamtypeIndx = -1; int bodyheaderlen = 0; //找到Stream类型的参数 for (streamtypeIndx = 0; streamtypeIndx < paramts.Length && streamtypeIndx < parameters.Length; streamtypeIndx++) { if (paramts[streamtypeIndx].ParameterType == typeof(Stream)) { var stream = message.GetBody <Stream>(); //定位到第一个0D0A0D0A) //sb= new StringBuilder(512); int datimes = 0;//回车换行次数 int c = 0; while (datimes != 4) { c = stream.ReadByte(); if (c == -1) { break; } if (c == 0x0d && (datimes == 0 || datimes == 2)) { datimes++; } else if (c == 0x0a && (datimes == 1 || datimes == 3)) { datimes++; } else { datimes = 0; } bodyheaderlen++; } if (c == -1) { continue; } //计算实际附件大小 int fileLength = len - bodyheaderlen - boundary.Length - 6; int remain = fileLength; MemoryStream filestream = new MemoryStream(fileLength); byte[] buffer = new byte[8192]; int readed = 0; while (remain > 0) { readed = stream.Read(buffer, 0, remain > 8192 ? 8192 : remain); remain -= readed; filestream.Write(buffer, 0, readed); } stream.Close(); filestream.Seek(0, SeekOrigin.Begin); //MemoryStream stream = new MemoryStream(message.GetReaderAtBodyContents().ReadElementContentAsBinHex()); parameters[streamtypeIndx] = filestream; } else { parameters[streamtypeIndx] = headers[paramts[streamtypeIndx].Name]; } } return; } object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents(); //begin 张辽阔 2017-01-06 添加 bodyReader.Quotas.MaxArrayLength = 163840 * 2; //end 张辽阔 2017-01-06 添加 bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); MemoryStream ms = new MemoryStream(rawBody); StreamReader sr = new StreamReader(ms); Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer() { DateFormatString = "yyyy-MM-dd HH:mm:ss" }; if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { string parameterName = reader.Value as string; reader.Read(); if (this.parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); }
public void DeserializeRequest(Message message, object[] parameters) { byte[] rawBody = null; HttpRequestMessageProperty request = null; foreach (var value in message.Properties.Values) { if (value is HttpRequestMessageProperty) { request = value as HttpRequestMessageProperty; break; } } //GET Method if (request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase)) { var req = request.QueryString; var reqDecode = HttpUtility.UrlDecode(req); if (string.IsNullOrEmpty(reqDecode)) { throw new InvalidOperationException("Incoming message must have json parameter"); } rawBody = System.Text.Encoding.UTF8.GetBytes(reqDecode); } //POST Method if (request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase)) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); rawBody = bodyReader.ReadContentAsBase64(); } var ms = new MemoryStream(rawBody); var sr = new StreamReader(ms); var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare if (operation.Messages[0].Body.Parts[0].Type == typeof(string)) { parameters[0] = Encoding.UTF8.GetString(rawBody); } else { parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (this.parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); }
public void DeserializeRequest(Message message, object[] parameters) { try { string ip = null; string via = null; string userAgent = null; object xxx; if (message.Properties.TryGetValue(RemoteEndpointMessageProperty.Name, out xxx)) { var endpoint = xxx as RemoteEndpointMessageProperty; if (endpoint != null) { ip = endpoint.Address; } } if (message.Properties.Via != null) { via = message.Properties.Via.ToString(); } if (message.Properties.TryGetValue(HttpRequestMessageProperty.Name, out xxx)) { var request = xxx as HttpRequestMessageProperty; if (request != null) { userAgent = request.Headers[HttpRequestHeader.UserAgent]; } } _applog.InfoFormat("{0}: {1} {2} {3}", ip, via, operation.Name, userAgent); if (traceSource.Switch.ShouldTrace(TraceEventType.Information)) { traceSource.TraceEvent(TraceEventType.Information, 1006, string.Format("{0}: {1} {2} {3}", ip, via, operation.Name, userAgent)); } //message.Properties.Keys.ToList().ForEach(it => _log.InfoFormat("{0}={1}", it, message.Properties[it])); //message.Headers.ToList().ForEach(it => _log.InfoFormat("{0}={1} {2} {3}", it.Name, it.Namespace,it.Actor, message.Headers.GetHeader<string>(it.Name, it.Namespace))); } catch (Exception ex) { _log.InfoFormat("{0}: error {1}", MethodBase.GetCurrentMethod().Name, ex); //traceSource.TraceEvent(TraceEventType.Error, 1000, ex); if (traceSource.Switch.ShouldTrace(TraceEventType.Warning)) { traceSource.TraceEvent(TraceEventType.Warning, 1007, ex.Message); } } object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); MemoryStream ms = new MemoryStream(rawBody); StreamReader sr = new StreamReader(ms); try { Newtonsoft.Json.JsonSerializer serializer = endpoint.NewtonsoftSettings().JsonSerializer; if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { string parameterName = reader.Value as string; reader.Read(); if (this.parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } if (traceSource.Switch.ShouldTrace(TraceEventType.Information)) { traceSource.TraceEvent(TraceEventType.Information, 1002, System.Text.Encoding.UTF8.GetString(rawBody)); } sr.Close(); ms.Close(); } catch { traceSource.TraceEvent(TraceEventType.Error, 1001, System.Text.Encoding.UTF8.GetString(rawBody)); throw; } }
public static UgoiraPlayer Create(Stream sourceStream, EventHandler <UgoiraReadingEventArgs> reportProgress) { string illustTitle = string.Empty, illustUsername = string.Empty; Dictionary <string, BitmapImage> framelist = new Dictionary <string, BitmapImage>(StringComparer.OrdinalIgnoreCase); Dictionary <string, FrameHeader> headerlist = new Dictionary <string, FrameHeader>(StringComparer.OrdinalIgnoreCase); using (UgoiraReader reader = new UgoiraReader(sourceStream)) { UgoiraReadingEventArgs eventa = null; if (reportProgress != null) { eventa = new UgoiraReadingEventArgs(reader.Entries.Length); } for (int i = 0; i < reader.Entries.Length; i++) { if (eventa != null) { eventa.SetCurrent(i + 1); } using (IO.RecyclableMemoryStream rms = new IO.RecyclableMemoryStream(reader[i].Filename, Convert.ToInt32(reader[i].Filesize))) { reader[i].Extract(rms); rms.Position = 0; switch (reader[i].Type) { case EntryType.Metadata: JObject jo; using (StreamReader sr = new StreamReader(rms)) using (Newtonsoft.Json.JsonTextReader jr = new Newtonsoft.Json.JsonTextReader(sr)) { jr.CloseInput = true; jo = JObject.Load(jr); jr.Close(); } if (jo != null) { illustTitle = jo.Value <object>("illustTitle").ToString(); illustUsername = jo.Value <object>("userName").ToString(); JToken jt = jo.SelectToken("ugokuIllustData"); if (jt != null) { jt = jo.SelectToken("frames"); if (jt != null) { string currentfilename; foreach (JToken token in jt.Children()) { currentfilename = token.Value <object>("file").ToString(); headerlist.Add(currentfilename, new FrameHeader(currentfilename, token.Value <int>("delay"))); } } } } break; case EntryType.Image: framelist.Add(reader[i].Filename, QuickMethods.CreateBitmapImage(rms, true)); break; } } if (eventa != null) { reportProgress?.Invoke(reader, eventa); if (eventa.Cancel) { framelist.Clear(); headerlist.Clear(); return(null); } } } } return(new UgoiraPlayer(MakeFrames(framelist, headerlist, true))); }