//инициализация public static bool TryCreate(string jsonMessage, out AmazonSesNotification amazonSesNotification) { amazonSesNotification = null; bool result = false; if (string.IsNullOrEmpty(jsonMessage)) return false; try { using (TextReader reader = new StringReader(jsonMessage)) using (var jsonRreader = new Newtonsoft.Json.JsonTextReader(reader)) { var serializer = new Newtonsoft.Json.JsonSerializer(); amazonSesNotification = serializer.Deserialize<AmazonSesNotification>(jsonRreader); } result = true; } catch (Exception ex) { } return result; }
public ActionResult Webhook(string id) { string securityId = ConfigurationManager.AppSettings["SecurityId"]; if (!String.IsNullOrEmpty(securityId)) { if (securityId != id) return new HttpStatusCodeResult((int)System.Net.HttpStatusCode.BadRequest); } // Verify the content type is json if (!this.Request.ContentType.StartsWith("application/json")) return new HttpStatusCodeResult((int)System.Net.HttpStatusCode.BadRequest); // TODO: Verify the user agent // Parse the JSON content simply to validate it as proper JSON this.Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin); var textReader = new System.IO.StreamReader(this.Request.InputStream); { Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(textReader); JObject jObject = JObject.Load(reader); } // All is OK, so seek back to the start this.Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin); // and re-read the content string messageBody = textReader.ReadToEnd(); string queueUrl = ConfigurationManager.AppSettings["SqsQueueUrl"]; if (String.IsNullOrEmpty(queueUrl)) throw new Exception("Null or empty SqsQueueUrl setting."); Amazon.SQS.AmazonSQS sqsClient; string endPointName = ConfigurationManager.AppSettings["SqsEndpoint"]; if (!String.IsNullOrEmpty(endPointName)) { Amazon.RegionEndpoint endPoint = Amazon.RegionEndpoint.GetBySystemName(endPointName); if (endPoint == null) throw new Exception("Invalid Amazon AWS endpoint name: " + endPointName); sqsClient = new Amazon.SQS.AmazonSQSClient(endPoint); } else { sqsClient = new Amazon.SQS.AmazonSQSClient(); } // Build our request var request = new Amazon.SQS.Model.SendMessageRequest() .WithMessageBody(messageBody) .WithQueueUrl(queueUrl); // Send to SQS var response = sqsClient.SendMessage(request); string messageId = response.SendMessageResult.MessageId; return new EmptyResult(); }
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(); } }
List<Show> Grab(GrabParametersBase p,ILogger logger) { var pp = (CyfraPlus.GrabParameters)p; var shows = new List<Show>(); var wr = WebRequest.Create(string.Format(urlFormat, pp.Date.ToString(DateFormat))); logger.WriteEntry(string.Format("Grabbing Cyfra+ date {0} ...", pp.Date.ToString(DateFormat)), LogType.Info); var res = (HttpWebResponse)wr.GetResponse(); const int ChannelDepth = 2; using (var sr = new StreamReader(res.GetResponseStream())) { var startDownloadTime = DateTime.Now; var data = new StringBuilder(); int blockSize = 16384; while (!sr.EndOfStream) { var buf = new char[blockSize]; var totalRead = sr.ReadBlock(buf, 0, blockSize); data.Append(buf); if (DateTime.Now - startDownloadTime > TimeSpan.FromSeconds(1)) { startDownloadTime = DateTime.Now; logger.WriteEntry(string.Format("Downloaded {0:#,##0} bytes so far", data.Length), LogType.Info); } } var r = new Newtonsoft.Json.JsonTextReader(new StringReader(data.ToString())); while (r.Read()) { r.Read(); var channelNumber = r.ReadAsInt32(); var channelName = r.ReadAsString(); r.Read(); r.Read(); while (r.Depth > ChannelDepth) { var show = new Show(); show.Channel = channelName.Trim(); var programId = r.ReadAsInt32(); show.Title = Tools.CleanupText(r.ReadAsString()); show.StartTime = new DateTime(1970, 1, 1).Add(TimeSpan.FromSeconds(r.ReadAsInt32().Value)); show.EndTime = show.StartTime.Add(TimeSpan.FromSeconds(Convert.ToDouble(r.ReadAsInt32()))); var num = r.ReadAsInt32(); shows.Add(show); var depth = r.Depth; while (r.Depth == depth) r.Read(); r.Read(); } } } return shows; }
private async Task <List <Geometry> > ParseGeoJSON(TextReader geoJSON) { List <Geometry> geoms = null; using (var jsonReader = new Newtonsoft.Json.JsonTextReader(geoJSON)) { var root = JObject.ReadFrom(jsonReader) as JObject; string type = root.Value <string>("type"); switch (type) { case "FeatureCollection": geoms = await ParseFeatureCollection(root); break; case "Feature": var geom = await ParseFeature(root); if (geom != null) { geoms = new List <Geometry>(); geoms.Add(geom); } break; case "Point": case "LineString": case "Polygon": case "MultiPoint": case "MultiLineString": case "MultiPolygon": case "GeometryCollection": var g = await ParseGeometry(root); if (g != null) { geoms = new List <Geometry>(); geoms.Add(g); } break; default: break; } } return(geoms); }
public async Task <List <TodoDetailData> > GetById(int userId) { var hc = new HttpClient(); hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var res = await hc.GetAsync(common.GetURL() + "TodoDetailDatas/" + userId).ConfigureAwait(false);; var str = await res.Content.ReadAsStringAsync(); var js = new Newtonsoft.Json.JsonSerializer(); var jr = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(str)); var item = js.Deserialize <List <TodoDetailData> >(jr); return(item); }
private static string NewtonsoftReturnStringHelper(TextReader reader) { StringBuilder sb = new StringBuilder(); var json = new Newtonsoft.Json.JsonTextReader(reader); while (json.Read()) { if (json.Value != null) { sb.Append(json.Value).Append(", "); } } return(sb.ToString()); }
} // End Sub SerializeAndIgnoreSerializableInterface public static T Deserialize <T>(System.IO.Stream s) { T retValue = default(T); using (System.IO.StreamReader reader = new System.IO.StreamReader(s, System.Text.Encoding.UTF8)) { using (Newtonsoft.Json.JsonTextReader jsonReader = new Newtonsoft.Json.JsonTextReader(reader)) { Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer(); retValue = ser.Deserialize <T>(jsonReader); } // End Using jsonReader } // End Using reader return(retValue); } // End Function Deserialize
private bool LoadConfiguration() { Configuration result = null; if (System.IO.File.Exists(_configFilePath)) { using (var fs = new FileStream(_configFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var sr = new StreamReader(fs, Encoding.UTF8)) using (var jsreader = new Newtonsoft.Json.JsonTextReader(sr)) { result = CompactJsonSerializer.Deserialize <Configuration>(jsreader); } } _Config = result; return(true); }
public static void ReadFromJson(XDocument document, TextReader render) { document.Root.RemoveAll(); var jsonReader = new Newtonsoft.Json.JsonTextReader(render); var json = Newtonsoft.Json.Linq.JObject.Load(jsonReader); var archives = json["ArchiveList"].ToArray(); foreach (var item in archives) { var elem = AddFileElement(document, item.Value <string>("ArchiveDescription")); elem.SetAttributeValue("id", item.Value <string>("ArchiveId")); elem.SetAttributeValue("size", item.Value <string>("Size")); elem.SetAttributeValue("creationDate", item.Value <string>("CreationDate")); } }
private T Deserialize <T>(Stream stream) { if (stream == null || !stream.CanRead) { return(default(T)); } using (var sr = new StreamReader(stream)) using (var jtr = new Newtonsoft.Json.JsonTextReader(sr)) { var js = new Newtonsoft.Json.JsonSerializer(); var value = js.Deserialize <T>(jtr); return(value); } }
public DogApiDog[] Scrape() { string serviceUrl = "https://api.thedogapi.com/v1/images/search?size=med&mime_types=jpg&format=json&has_breeds=true&order=ASC&page=0&limit=100"; System.Net.WebRequest req = System.Net.WebRequest.Create(serviceUrl); req.Headers.Add("content-type", "application/json"); req.Headers.Add("x-api-key", _apiKey); System.Net.WebResponse response = req.GetResponse(); System.IO.Stream s = response.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(s); Newtonsoft.Json.JsonTextReader jsonTextReader = new Newtonsoft.Json.JsonTextReader(sr); Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); return(serializer.Deserialize <DogApiDog[]>(jsonTextReader)); }
public static T Deserialize <T>(System.IO.Stream stream) { T objReturnValue = default(T); Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); using (System.IO.TextReader sr = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8)) { using (Newtonsoft.Json.JsonReader jsonTextReader = new Newtonsoft.Json.JsonTextReader(sr)) { objReturnValue = serializer.Deserialize <T>(jsonTextReader); } } return(objReturnValue); }
public static NewtonsoftToCosmosDBReader CreateFromString(string json) { if (json == null) { throw new ArgumentNullException(nameof(json)); } StringReader stringReader = new StringReader(json); Newtonsoft.Json.JsonTextReader newtonsoftReader = new Newtonsoft.Json.JsonTextReader(stringReader) { DateParseHandling = Newtonsoft.Json.DateParseHandling.None }; return(NewtonsoftToCosmosDBReader.CreateFromReader(newtonsoftReader)); }
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 static void Scrape() { string serviceUrl = "https://www.datakick.org/api/items"; //string serviceUrl = "http://www.ctabustracker.com/bustime/api/v2/getroutes?key=[APIKEY]&format=json"; System.Net.WebRequest req = System.Net.WebRequest.Create(serviceUrl); System.Net.WebResponse response = req.GetResponse(); System.IO.Stream s = response.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(s); Newtonsoft.Json.JsonTextReader jsonTextReader = new Newtonsoft.Json.JsonTextReader(sr); Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); DataKickObject[] result = serializer.Deserialize <DataKickObject[]>(jsonTextReader); //CtaResponse result = serializer.Deserialize<CtaResponse>(jsonTextReader); //TODO: I've pulled data in -- I should save it somewhere so it's readily available when my app needs it! }
protected virtual async System.Threading.Tasks.Task <ObjectResponseResult <T> > ReadObjectResponseAsync <T>( System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary <string, System.Collections.Generic.IEnumerable <string> > headers) { if (response == null || response.Content == null) { return(new ObjectResponseResult <T>(default(T), string.Empty)); } if (ReadResponseAsString) { var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false); try { var typedBody = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(responseText, JsonSerializerSettings); return(new ObjectResponseResult <T>(typedBody, responseText)); } catch (Newtonsoft.Json.JsonException exception) { var message = "Could not deserialize the response body string as " + typeof(T).FullName + "."; throw new SwaggerException(message, (int)response.StatusCode, responseText, headers, exception); } } else { try { using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) using (var streamReader = new System.IO.StreamReader(responseStream)) using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(streamReader)) { var serializer = Newtonsoft.Json.JsonSerializer.Create(JsonSerializerSettings); var typedBody = serializer.Deserialize <T>(jsonTextReader); return(new ObjectResponseResult <T>(typedBody, string.Empty)); } } catch (Newtonsoft.Json.JsonException exception) { var message = "Could not deserialize the response body stream as " + typeof(T).FullName + "."; throw new SwaggerException(message, (int)response.StatusCode, string.Empty, headers, exception); } } }
public HttpRequest(HttpListenerRequest internalRequest) { this.Arguments = new NameValueDictionary(internalRequest.QueryString); this.Cookies = new CookieDictionary(internalRequest.Cookies); this.Headers = new NameValueDictionary(internalRequest.Headers); if (internalRequest.ContentType == "application/json") { using (var textReader = new System.IO.StreamReader(internalRequest.InputStream)) { using (var jsonReader = new Newtonsoft.Json.JsonTextReader(textReader)) { var deserializer = Newtonsoft.Json.JsonSerializer.CreateDefault(); this.Json = deserializer.Deserialize(jsonReader) as JToken; } } } }
private async Task <T> Deserialize <T>(HttpResponseMessage httpResponse) { var httpContent = httpResponse.Content; byte[] contentBytes = await httpContent.ReadAsByteArrayAsync(); var encodingResolver = new EncodingResolver(); string responseMediaType = httpContent.Headers.ContentType?.MediaType ?? this.DefaultMediaType; var encoding = encodingResolver.ResolveEncoding(responseMediaType, this.DefaultEncoding); var textReader = new StreamReader(new MemoryStream(contentBytes), encoding); var jsonTextReader = new Newtonsoft.Json.JsonTextReader(textReader); var serializer = new Newtonsoft.Json.JsonSerializer(); return(serializer.Deserialize <T>(jsonTextReader)); }
private static T DeserializeJsonFromStream <T>(Stream stream) { if (stream == null || stream.CanRead == false) { return(default(T)); } stream.Position = 0; using (StreamReader sr = new StreamReader(stream)) { using (Newtonsoft.Json.JsonTextReader jtr = new Newtonsoft.Json.JsonTextReader(sr)) { Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer(); T result = js.Deserialize <T>(jtr); return(result); } } }
private static SerializedCulture GetSerializedCulture(string CultureName) { var Serialized = new Newtonsoft.Json.JsonSerializer(); using (var Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Teva.Common.Cultures.cultures." + CultureName.ToLower() + ".json")) { if (Stream == null) { return(null); } using (var StreamReader = new System.IO.StreamReader(Stream, System.Text.Encoding.UTF32)) using (var JsonTextReader = new Newtonsoft.Json.JsonTextReader(StreamReader)) { return(Serialized.Deserialize <SerializedCulture>(JsonTextReader)); } } }
public static Weather GetWeatherData(int appWidgetId) { var prefs = GetPreferences(appWidgetId); var weatherDataJson = prefs.GetString(KEY_WEATHERDATA, null); if (String.IsNullOrWhiteSpace(weatherDataJson)) { return(null); } else { using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(weatherDataJson))) { return(Weather.FromJson(jsonTextReader)); } } }
public static LocationData GetLocationData(int appWidgetId) { var prefs = GetPreferences(appWidgetId); var locDataJson = prefs.GetString(KEY_LOCATIONDATA, null); if (String.IsNullOrWhiteSpace(locDataJson)) { return(null); } else { using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(locDataJson))) { return(LocationData.FromJson(jsonTextReader)); } } }
public static UpdateInfo CheckForUpdate() { foreach (var url in MANIFEST_URLS) { try { using (var tmpfile = new Library.Utility.TempFile()) { System.Net.WebClient wc = new System.Net.WebClient(); wc.Headers.Add(System.Net.HttpRequestHeader.UserAgent, string.Format("{0} v{1}{2}", APPNAME, SelfVersion.Version, string.IsNullOrWhiteSpace(InstallID) ? "" : " -" + InstallID)); wc.Headers.Add("X-Install-ID", InstallID); wc.DownloadFile(url, tmpfile); using (var fs = System.IO.File.OpenRead(tmpfile)) using (var ss = new SignatureReadingStream(fs, SIGN_KEY)) using (var tr = new System.IO.StreamReader(ss)) using (var jr = new Newtonsoft.Json.JsonTextReader(tr)) { var update = new Newtonsoft.Json.JsonSerializer().Deserialize <UpdateInfo>(jr); if (TryParseVersion(update.Version) <= TryParseVersion(SelfVersion.Version)) { return(null); } if (string.Equals(SelfVersion.ReleaseType, "Debug", StringComparison.InvariantCultureIgnoreCase) && !string.Equals(update.ReleaseType, SelfVersion.ReleaseType, StringComparison.CurrentCultureIgnoreCase)) { return(null); } LastUpdateCheckVersion = update; return(update); } } } catch (Exception ex) { if (OnError != null) { OnError(ex); } } } return(null); }
private static async System.Threading.Tasks.Task <T> DeserializeJSON <T>( System.Net.WebSockets.WebSocket webSocket , Newtonsoft.Json.JsonSerializer serializer) { T searchArguments = default(T); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { byte[] buffer = new byte[1024 * 4]; System.Net.WebSockets.WebSocketReceiveResult result = null; do { result = await webSocket.ReceiveAsync( new System.ArraySegment <byte>(buffer) , System.Threading.CancellationToken.None ); ms.Write(buffer, 0, result.Count); } while (!result.EndOfMessage); // string json = System.Text.Encoding.UTF8.GetString(ms.ToArray()); // searchArguments = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchArguments>(json); ms.Position = 0; using (System.IO.TextReader tr = new System.IO.StreamReader(ms, System.Text.Encoding.UTF8)) { using (Newtonsoft.Json.JsonReader jsonReader = new Newtonsoft.Json.JsonTextReader(tr)) { try { searchArguments = serializer.Deserialize <T>(jsonReader); } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } } // End Using jsonReader } // End Using tr } // End Using ms return(searchArguments); } // End Task DeserializeJSON
/// <inheritdoc/> public Variant Decode(VariantValue value, BuiltInType builtinType) { if (VariantValueEx.IsNull(value)) { return(Variant.Null); } // // Sanitize json input from user // value = Sanitize(value, builtinType == BuiltInType.String); VariantValue json; if (builtinType == BuiltInType.Null || (builtinType == BuiltInType.Variant && value.IsObject)) { // // Let the decoder try and decode the json variant. // json = Serializer.FromObject(new { value }); } else { // // Give decoder a hint as to the type to use to decode. // json = Serializer.FromObject(new { value = new { Body = value, Type = (byte)builtinType } }); } // // Decode json to a real variant // using (var text = new StringReader(Serializer.SerializeToString(json))) using (var reader = new Newtonsoft.Json.JsonTextReader(text)) using (var decoder = new JsonDecoderEx(reader, Context)) { return(decoder.ReadVariant(nameof(value))); } }
internal static T ReadJson <T>(string path) { try { using (var reader = new Newtonsoft.Json.JsonTextReader(new StreamReader(File.Open(path, FileMode.Open), Encoding.UTF8))) { var serializer = new Newtonsoft.Json.JsonSerializer(); var result = serializer.Deserialize <T>(reader); return(result); } } catch { return(default(T)); } }
public string ReaderNewtonsoftReaderReturnString() { _stream.Seek(0, SeekOrigin.Begin); TextReader reader = _reader; StringBuilder sb = new StringBuilder(); var json = new Newtonsoft.Json.JsonTextReader(reader); while (json.Read()) { if (json.Value != null) { sb.Append(json.Value).Append(", "); } } return(sb.ToString()); }
public static NewtonsoftToCosmosDBReader CreateFromBuffer(ReadOnlyMemory <byte> buffer) { MemoryStream stream; if (MemoryMarshal.TryGetArray(buffer, out ArraySegment <byte> segment)) { stream = new MemoryStream(segment.Array, segment.Offset, segment.Count); } else { stream = new MemoryStream(buffer.ToArray()); } StreamReader streamReader = new StreamReader(stream, Encoding.UTF8); Newtonsoft.Json.JsonTextReader newtonsoftReader = new Newtonsoft.Json.JsonTextReader(streamReader); return(new NewtonsoftToCosmosDBReader(newtonsoftReader)); }
private static VideoInfo[] ParseVideos(string input) { Newtonsoft.Json.JsonTextReader jtr = new Newtonsoft.Json.JsonTextReader(new StringReader(input)); Dictionary <string, string> list = new Dictionary <string, string>(); List <VideoInfo> videos = new List <VideoInfo>(); bool hasFound = false; while (jtr.Read()) { if (jtr.Value != null) { string nme = (string)jtr.Value; if (jtr.Read() && jtr.Value != null) { if (jtr.ValueType == typeof(string)) { string val = (string)jtr.Value; if (val == "youtube#searchResult") { if (hasFound) { videos.Add(new VideoInfo(list)); // Console.WriteLine(); list.Clear(); } else { hasFound = true; } } else if (hasFound) { //Console.WriteLine(nme + ": " + jtr.Value); if (!list.ContainsKey(nme)) { list.Add(nme, (string)jtr.Value); } } } } } } return(videos.ToArray()); }
public Notification Parse(string data) { string msg = string.Empty; string custom = string.Empty; try { var reader = new Newtonsoft.Json.JsonTextReader (new StringReader(data)); var serializer = new Newtonsoft.Json.JsonSerializer (); var obj = serializer.Deserialize<NotificationObject> (reader); msg = obj.title; custom = obj.u; } catch (Exception ex) { MvxTrace.Error ("Failed to parse incoming data: {0}", ex.Message); } return new Notification (msg, data, custom); }
public static T ReadStream <T>(string fileName) { T p = default(T); using (System.IO.Stream s = System.IO.File.OpenRead(fileName)) { using (System.IO.StreamReader sr = new System.IO.StreamReader(s)) using (Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr)) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); // read the json from a stream // json size doesn't matter because only a small piece is read at a time from the HTTP request p = serializer.Deserialize <T>(reader); } } return(p); }
private T GetJSONData <T>(string url, Action <HttpWebRequest> setup = null) { var req = (HttpWebRequest)System.Net.WebRequest.Create(url); req.UserAgent = USER_AGENT; if (setup != null) { setup(req); } Utility.AsyncHttpRequest areq = new Utility.AsyncHttpRequest(req); using (var resp = (HttpWebResponse)areq.GetResponse()) using (var rs = areq.GetResponseStream()) using (var tr = new System.IO.StreamReader(rs)) using (var jr = new Newtonsoft.Json.JsonTextReader(tr)) return(new Newtonsoft.Json.JsonSerializer().Deserialize <T>(jr)); }
protected override void WriteRawJsonToken( JsonTokenType jsonTokenType, ReadOnlySpan <byte> rawJsonToken) { string rawJson = Encoding.UTF8.GetString(rawJsonToken); Newtonsoft.Json.JsonTextReader jsonTextReader = new Newtonsoft.Json.JsonTextReader(new StringReader(rawJson)); while (jsonTextReader.Read()) { if (jsonTokenType == JsonTokenType.FieldName) { this.writer.WritePropertyName(jsonTextReader.Value as string); } else { this.writer.WriteValue(jsonTextReader.Value); } } }
Json2List(System.IO.Stream inputStream, System.Text.Encoding enc) { if (inputStream == null) { throw new System.ArgumentNullException(nameof(inputStream)); } // End if (inputStream == null) Newtonsoft.Json.Linq.JToken jsonData = null; using (System.IO.TextReader tr = new System.IO.StreamReader(inputStream, enc)) { using (Newtonsoft.Json.JsonTextReader jsonReader = new Newtonsoft.Json.JsonTextReader(tr)) { jsonData = Newtonsoft.Json.Linq.JToken.ReadFrom(jsonReader); } // End Using jsonReader } // End Using tr return(Json2List(jsonData)); } // End Function Json2List
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType != typeof(IStaticQueryResult)) { return false; } var rdr = new Newtonsoft.Json.JsonTextReader(new StringReader(actionContext.Request.Content.ReadAsStringAsync().Result)); var json = Newtonsoft.Json.JsonSerializer.Create(new Newtonsoft.Json.JsonSerializerSettings() ); var res = json.Deserialize<StaticQueryResult>(rdr); bindingContext.Model = res; /* ValueProviderResult val = bindingContext.ValueProvider.GetValue( bindingContext.ModelName); if (val == null) { return false; } string key = val.RawValue as string; if (key == null) { bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Wrong value type"); return false; } */ /* IStaticQueryResult result; if (IStaticQueryResult.TryGetValue(key, out result) || IStaticQueryResult.TryParse(key, out result)) { bindingContext.Model = result; return true; } */ bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Cannot convert value to Location"); return false; }
/// <summary> /// 发送HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="param">请求的参数</param> /// <returns>请求结果</returns> public static string request(string url, string param) { string strURL = url + '?' + param; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); request.Method = "GET"; // 添加header request.Headers.Add("apikey", "ec3dedf550e5e6cea6c53385ed4f474a"); System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); string StrDate = ""; string strValue = ""; StreamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "\r\n"; } StringReader sr = new StringReader(strValue); Newtonsoft.Json.JsonTextReader jsonReader = new Newtonsoft.Json.JsonTextReader(sr); Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); var r = serializer.Deserialize<Resault>(jsonReader); //txtResult.Text = r.trans_result[0].dst; if (r.retData.trans_result != null) { strValue = r.retData.trans_result[0].dst; } else { strValue = ""; } //StringReader sr=new //Newtonsoft.Json.JsonConverter. return strValue; }
public static Madingley.Common.Configuration Deserialize(TextReader sr) { Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.FunctionalGroupDefinition> JsonReadFunctionalGroupDefinition = (reader) => { var ret = new Madingley.Common.FunctionalGroupDefinition(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "Definitions": ret.Definitions = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadString); break; case "Properties": ret.Properties = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadDouble); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.FunctionalGroupDefinitions> JsonReadFunctionalGroupDefinitions = (reader) => { var ret = new Madingley.Common.FunctionalGroupDefinitions(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "Data": ret.Data = Common.Reader.ReadArray(reader, JsonReadFunctionalGroupDefinition); break; case "Definitions": ret.Definitions = Common.Reader.ReadArray(reader, Common.Reader.ReadString); break; case "Properties": ret.Properties = Common.Reader.ReadArray(reader, Common.Reader.ReadString); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.ScenarioParameter> JsonReadScenarioParameter = (reader) => { var ret = new Madingley.Common.ScenarioParameter(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "ParamString": ret.ParamString = Common.Reader.ReadString(reader); break; case "ParamDouble1": ret.ParamDouble1 = Common.Reader.ReadDouble(reader); break; case "ParamDouble2": ret.ParamDouble2 = Common.Reader.ReadDouble(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, IDictionary<string, Madingley.Common.ScenarioParameter>> JsonReadKVPScenarioParameter = (reader) => { var ret = new Dictionary<string, Madingley.Common.ScenarioParameter>(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var key = Convert.ToString(reader.Value); reader.Read(); var value = JsonReadScenarioParameter(reader); ret.Add(key, value); } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.ScenarioParameters> JsonReadScenarioParameters = (reader) => { var ret = new Madingley.Common.ScenarioParameters(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "Label": ret.Label = Common.Reader.ReadString(reader); break; case "SimulationNumber": ret.SimulationNumber = Common.Reader.ReadInt(reader); break; case "Parameters": ret.Parameters = JsonReadKVPScenarioParameter(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.EcologicalParameters> JsonReadEcologicalParameters = (reader) => { var ret = new Madingley.Common.EcologicalParameters(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "Parameters": ret.Parameters = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadDouble); break; case "TimeUnits": ret.TimeUnits = Common.Reader.ReadArray(reader, Common.Reader.ReadString); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; var configuration = new Madingley.Common.Configuration(); using (var reader = new Newtonsoft.Json.JsonTextReader(sr)) { reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "GlobalModelTimeStepUnit": configuration.GlobalModelTimeStepUnit = Common.Reader.ReadString(reader); break; case "NumTimeSteps": configuration.NumTimeSteps = Common.Reader.ReadInt(reader); break; case "BurninTimeSteps": configuration.BurninTimeSteps = Common.Reader.ReadInt(reader); break; case "ImpactTimeSteps": configuration.ImpactTimeSteps = Common.Reader.ReadInt(reader); break; case "RecoveryTimeSteps": configuration.RecoveryTimeSteps = Common.Reader.ReadInt(reader); break; case "RunCellsInParallel": configuration.RunCellsInParallel = Common.Reader.ReadBoolean(reader); break; case "RunSimulationsInParallel": configuration.RunSimulationsInParallel = Common.Reader.ReadBoolean(reader); break; case "RunRealm": configuration.RunRealm = Common.Reader.ReadString(reader); break; case "DrawRandomly": configuration.DrawRandomly = Common.Reader.ReadBoolean(reader); break; case "ExtinctionThreshold": configuration.ExtinctionThreshold = Common.Reader.ReadDouble(reader); break; case "MaxNumberOfCohorts": configuration.MaxNumberOfCohorts = Common.Reader.ReadInt(reader); break; case "DispersalOnly": configuration.DispersalOnly = Common.Reader.ReadBoolean(reader); break; case "DispersalOnlyType": configuration.DispersalOnlyType = Common.Reader.ReadString(reader); break; case "PlanktonDispersalThreshold": configuration.PlanktonDispersalThreshold = Common.Reader.ReadDouble(reader); break; case "CohortFunctionalGroupDefinitions": configuration.CohortFunctionalGroupDefinitions = JsonReadFunctionalGroupDefinitions(reader); break; case "StockFunctionalGroupDefinitions": configuration.StockFunctionalGroupDefinitions = JsonReadFunctionalGroupDefinitions(reader); break; case "ImpactCellIndices": configuration.ImpactCellIndices = Common.Reader.ReadArray(reader, Common.Reader.ReadInt); break; case "ImpactAll": configuration.ImpactAll = Common.Reader.ReadBoolean(reader); break; case "ScenarioParameters": configuration.ScenarioParameters = Common.Reader.ReadArray(reader, JsonReadScenarioParameters).ToList(); break; case "ScenarioIndex": configuration.ScenarioIndex = Common.Reader.ReadInt(reader); break; case "Simulation": configuration.Simulation = Common.Reader.ReadInt(reader); break; case "EcologicalParameters": configuration.EcologicalParameters = JsonReadEcologicalParameters(reader); break; case "FileNames": configuration.FileNames = Common.Reader.ReadArray(reader, Common.Reader.ReadString).ToList(); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } } return configuration; }
public static Madingley.Common.Environment Deserialize(TextReader sr) { Func<Newtonsoft.Json.JsonTextReader, Tuple<int, int>> JsonReadFocusCell = (reader) => { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartArray); reader.Read(); var item1 = Common.Reader.ReadInt(reader); reader.Read(); var item2 = Common.Reader.ReadInt(reader); reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.EndArray); return Tuple.Create(item1, item2); }; Func<Newtonsoft.Json.JsonTextReader, IDictionary<string, double[]>> JsonReadCellEnvironment = (reader) => { var ret = new Dictionary<string, double[]>(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var key = Convert.ToString(reader.Value); reader.Read(); var value = Common.Reader.ReadArray(reader, Common.Reader.ReadDouble); ret.Add(key, value.ToArray()); } return ret; }; var environment = new Madingley.Common.Environment(); using (var reader = new Newtonsoft.Json.JsonTextReader(sr)) { reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "CellSize": environment.CellSize = Common.Reader.ReadDouble(reader); break; case "BottomLatitude": environment.BottomLatitude = Common.Reader.ReadDouble(reader); break; case "TopLatitude": environment.TopLatitude = Common.Reader.ReadDouble(reader); break; case "LeftmostLongitude": environment.LeftmostLongitude = Common.Reader.ReadDouble(reader); break; case "RightmostLongitude": environment.RightmostLongitude = Common.Reader.ReadDouble(reader); break; case "Units": environment.Units = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadString); break; case "SpecificLocations": environment.SpecificLocations = Common.Reader.ReadBoolean(reader); break; case "FocusCells": environment.FocusCells = Common.Reader.ReadArray(reader, JsonReadFocusCell).ToList(); break; case "CellEnvironment": environment.CellEnvironment = Common.Reader.ReadArray(reader, JsonReadCellEnvironment).ToList(); break; case "FileNames": environment.FileNames = Common.Reader.ReadArray(reader, Common.Reader.ReadString).ToList(); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } } return environment; }
public override void Reset() { if (reader != null) reader.Close(); reader = new Newtonsoft.Json.JsonTextReader(new StringReader(ParentGroup.Text.Trim())); current_start = -1; current_end = -1; }
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(); }
private void Invoke(int id) { Cnljli.Utility.HttpUtility.Request((e) => { this.BeginInvoke(() => { IsIndeterminate = false; LoadText = "网络错误,请检查网络"; }); }, (str) => { Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(str)); CommentModel<Comment> deserializedProduct = new Newtonsoft.Json.JsonSerializer().Deserialize<CommentModel<Comment>>(reader); this.BeginInvoke(() => { if (deserializedProduct.appending.Count > 0) { deserializedProduct.appending.ForEach(x => { Appending.Add(x); }); } else { HavAppend = System.Windows.Visibility.Collapsed; } Const.ParstText(string.Format("\t\t{0}", deserializedProduct.article.content)).ForEach(x => { Content.Add(x); }); deserializedProduct.list.ForEach(x => { Comment.Add(x); }); Title = deserializedProduct.article.title; Author = deserializedProduct.article.login; Create = deserializedProduct.article.Post_at; IsLoad = false; }); }, string.Format(Const.Commit, id, 1), Headers); }
private void Invoke(System.Action callback) { if (!IsLoad) { IsIndeterminate = true; IsLoad = true; LoadText = "数据加载中"; Cnljli.Utility.HttpUtility.Request((e) => { this.BeginInvoke(() => { IsIndeterminate = false; LoadText = "网络错误,请检查网络"; }); }, (str) => { Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(str)); Model<Data> deserializedProduct = new Newtonsoft.Json.JsonSerializer().Deserialize<Model<Data>>(reader); deserializedProduct.list.ForEach(x => { x.neg = x.neg.HasValue ? 0 - x.neg.Value : 0; this.BeginInvoke(() => { virtuaData.Add(x); }); }); currentPageIndex++; (callback == null ? () => { } : callback)(); this.BeginInvoke(() => { virtuaData.NotifyCollectionChanged(); IsLoad = false ; }); }, string.Format(currentUrl, currentPageIndex), Headers); } }
private WLID_FolderItem FindFolders(bool autocreate) { var folders = (m_rootfolder + '/' + m_prefix).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (folders.Length == 0) { var url = string.Format("{0}/{1}?access_token={2}", WLID_SERVER, ROOT_FOLDER_ID, Library.Utility.Uri.UrlEncode(m_oauth.AccessToken)); return m_oauth.GetJSONData<WLID_FolderItem>(url); } WLID_FolderItem cur = null; foreach (var f in folders) { var n = FindFolder(f, cur == null ? null : cur.id); if (n == null) { if (autocreate) { var url = string.Format("{0}/{1}?access_token={2}", WLID_SERVER, cur == null ? ROOT_FOLDER_ID : cur.id, Library.Utility.Uri.UrlEncode(m_oauth.AccessToken)); var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = USER_AGENT; req.Method = "POST"; var areq = new Utility.AsyncHttpRequest(req); using (var ms = new System.IO.MemoryStream()) using (var sw = new System.IO.StreamWriter(ms)) { new Newtonsoft.Json.JsonSerializer().Serialize(sw, new WLID_CreateFolderData() { name = f, description = Strings.OneDrive.AutoCreatedFolderLabel }); sw.Flush(); ms.Position = 0; req.ContentLength = ms.Length; req.ContentType = "application/json"; using (var reqs = areq.GetRequestStream()) Utility.Utility.CopyStream(ms, reqs, true, m_copybuffer); } using (var resp = (HttpWebResponse)areq.GetResponse()) using (var rs = areq.GetResponseStream()) using (var tr = new System.IO.StreamReader(rs)) using (var jr = new Newtonsoft.Json.JsonTextReader(tr)) { if ((int)resp.StatusCode < 200 || (int)resp.StatusCode > 299) throw new ProtocolViolationException(Strings.OneDrive.UnexpectedError(resp.StatusCode, resp.StatusDescription)); cur = new Newtonsoft.Json.JsonSerializer().Deserialize<WLID_FolderItem>(jr); } } else throw new FolderMissingException(Strings.OneDrive.MissingFolderError(f)); } else cur = n; } return cur; }
static void SetProjectJsonValues (string filename, string framework, out bool changed) { changed = false; bool isOpen; var file = TextFileProvider.Instance.GetTextEditorData (filename, out isOpen); JObject json; using (var tr = file.CreateReader ()) using (var jr = new Newtonsoft.Json.JsonTextReader (tr)) { json = (JObject)JToken.Load (jr); } var deps = (json ["dependencies"] as JObject); if (framework.StartsWith ("netstandard", StringComparison.Ordinal)) { if (deps == null) { deps = new JObject (); json ["dependencies"] = deps; } //make sure NETStandard.Library has the version we need if (EnsurePackageHasVersion (deps, NetStandardPackageName, NetStandardPackageVersion)) { //if we had to fix that, also add to optional Microsoft.NETCore.Portable.Compatibility EnsurePackageHasVersion (deps, NetStandardPclCompatPackageName, NetStandardPclCompatPackageVersion); changed = true; } } else { //not netstandard, remove the netstandard nuget package ref if (deps != null) { deps.Property (NetStandardPackageName)?.Remove (); deps.Property (NetStandardPclCompatPackageName)?.Remove (); } } string [] existingTargetFrameworks = null; var frameworks = (json ["frameworks"] as JObject); if (frameworks != null) { existingTargetFrameworks = frameworks.Properties ().Select (p => p.Name).ToArray (); } if (existingTargetFrameworks != null && !existingTargetFrameworks.Any(f => f == framework)) { var existingFxValue = ((JProperty) json ["frameworks"].First()).Value as JObject; json ["frameworks"] = new JObject ( new JProperty (framework, existingFxValue ?? new JObject ()) ); changed = true; } if (changed) { file.ReplaceText (0, file.Length, json.ToString ()); if (!isOpen) { file.Save (); } } }
public static void CreateUpdatePackage(System.Security.Cryptography.RSACryptoServiceProvider key, string inputfolder, string outputfolder, string manifest = null) { // Read the existing manifest UpdateInfo remoteManifest; var manifestpath = manifest ?? System.IO.Path.Combine(inputfolder, UPDATE_MANIFEST_FILENAME); using(var s = System.IO.File.OpenRead(manifestpath)) using(var sr = new System.IO.StreamReader(s)) using(var jr = new Newtonsoft.Json.JsonTextReader(sr)) remoteManifest = new Newtonsoft.Json.JsonSerializer().Deserialize<UpdateInfo>(jr); if (remoteManifest.Files == null) remoteManifest.Files = new FileEntry[0]; if (remoteManifest.ReleaseTime.Ticks == 0) remoteManifest.ReleaseTime = DateTime.UtcNow; var ignoreFiles = (from n in remoteManifest.Files where n.Ignore select n).ToArray(); var ignoreMap = ignoreFiles.ToDictionary(k => k.Path, k => "", Duplicati.Library.Utility.Utility.ClientFilenameStringComparer); remoteManifest.MD5 = null; remoteManifest.SHA256 = null; remoteManifest.Files = null; remoteManifest.UncompressedSize = 0; var localManifest = remoteManifest.Clone(); localManifest.RemoteURLS = null; inputfolder = Duplicati.Library.Utility.Utility.AppendDirSeparator(inputfolder); var baselen = inputfolder.Length; var dirsep = System.IO.Path.DirectorySeparatorChar.ToString(); ignoreMap.Add(UPDATE_MANIFEST_FILENAME, ""); var md5 = System.Security.Cryptography.MD5.Create(); var sha256 = System.Security.Cryptography.SHA256.Create(); Func<string, string> computeMD5 = (path) => { md5.Initialize(); using(var fs = System.IO.File.OpenRead(path)) return Convert.ToBase64String(md5.ComputeHash(fs)); }; Func<string, string> computeSHA256 = (path) => { sha256.Initialize(); using(var fs = System.IO.File.OpenRead(path)) return Convert.ToBase64String(sha256.ComputeHash(fs)); }; // Build a zip using (var archive_temp = new Duplicati.Library.Utility.TempFile()) { using (var zipfile = new Duplicati.Library.Compression.FileArchiveZip(archive_temp, new Dictionary<string, string>())) { Func<string, string, bool> addToArchive = (path, relpath) => { if (ignoreMap.ContainsKey(relpath)) return false; if (path.EndsWith(dirsep)) return true; using (var source = System.IO.File.OpenRead(path)) using (var target = zipfile.CreateFile(relpath, Duplicati.Library.Interface.CompressionHint.Compressible, System.IO.File.GetLastAccessTimeUtc(path))) { source.CopyTo(target); remoteManifest.UncompressedSize += source.Length; } return true; }; // Build the update manifest localManifest.Files = (from fse in Duplicati.Library.Utility.Utility.EnumerateFileSystemEntries(inputfolder) let relpath = fse.Substring(baselen) where addToArchive(fse, relpath) select new FileEntry() { Path = relpath, LastWriteTime = System.IO.File.GetLastAccessTimeUtc(fse), MD5 = fse.EndsWith(dirsep) ? null : computeMD5(fse), SHA256 = fse.EndsWith(dirsep) ? null : computeSHA256(fse) }) .Union(ignoreFiles).ToArray(); // Write a signed manifest with the files using (var ms = new System.IO.MemoryStream()) using (var sw = new System.IO.StreamWriter(ms)) { new Newtonsoft.Json.JsonSerializer().Serialize(sw, localManifest); sw.Flush(); using (var ms2 = new System.IO.MemoryStream()) { SignatureReadingStream.CreateSignedStream(ms, ms2, key); ms2.Position = 0; using (var sigfile = zipfile.CreateFile(UPDATE_MANIFEST_FILENAME, Duplicati.Library.Interface.CompressionHint.Compressible, DateTime.UtcNow)) ms2.CopyTo(sigfile); } } } remoteManifest.CompressedSize = new System.IO.FileInfo(archive_temp).Length; remoteManifest.MD5 = computeMD5(archive_temp); remoteManifest.SHA256 = computeSHA256(archive_temp); System.IO.File.Move(archive_temp, System.IO.Path.Combine(outputfolder, "package.zip")); } // Write a signed manifest for upload using(var tf = new Duplicati.Library.Utility.TempFile()) { using (var ms = new System.IO.MemoryStream()) using (var sw = new System.IO.StreamWriter(ms)) { new Newtonsoft.Json.JsonSerializer().Serialize(sw, remoteManifest); sw.Flush(); using (var fs = System.IO.File.Create(tf)) SignatureReadingStream.CreateSignedStream(ms, fs, key); } System.IO.File.Move(tf, System.IO.Path.Combine(outputfolder, UPDATE_MANIFEST_FILENAME)); } }
private void recv(string msg) { if (State == WebSockState.Connecting) { if (ci.cii == null) { ci.cii = new Dictionary<string, string>(); ci.cii.Add("nick", ci.nick); ci.cii.Add("room", ci.room); } Send(make_msg("join", ci.cii)); State = WebSockState.Connected; } else if (msg == "2::") { Send("2::"); } else { msg = msg.Substring(4); Newtonsoft.Json.JsonTextReader JTR = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(msg)); char name_type = '\0'; Dictionary<string, string> args = new Dictionary<string, string>(5); while (JTR.Read()) { switch (JTR.ValueType == typeof(string) ? (string)JTR.Value : "") { case "name": name_type = JTR.ReadAsString()[0]; break; case "args": switch (name_type) { case 'u': JTR.Read(); JTR.Read(); JTR.Read(); switch (JTR.ReadAsString()) { case "l": Dictionary<string, user_info> ui = new Dictionary<string,user_info>(8); JTR.Read(); JTR.Read(); while (JTR.Read()) { string nick = (string)JTR.Value; user_info lui = new user_info(); JTR.Read(); while (JTR.Read() && JTR.Value != null && JTR.ValueType.ToString() != "EndObject") { switch ((string)JTR.Value) { case "a": lui.admin = JTR.ReadAsInt32() == 0 ? false : true; break; case "t": lui.conn = (DateTime)JTR.ReadAsDateTime(); break; case "m": lui.mb_id = JTR.ReadAsString(); break; case "l": lui.login = JTR.ReadAsInt32() == 1 ? false : true; break; default: JTR.Read(); break; } } if(nick != null) ui.Add(nick, lui); } break; default: break; } break; case 'c': JTR.Skip(); JTR.Skip(); while (JTR.Read()) { args.Add((string)JTR.Value, JTR.ReadAsString()); } break; default: break; } break; default: break; } } } }
public static bool VerifyUnpackedFolder(string folder, UpdateInfo version = null) { try { UpdateInfo update; FileEntry manifest; var sha256 = System.Security.Cryptography.SHA256.Create(); var md5 = System.Security.Cryptography.MD5.Create(); using(var fs = System.IO.File.OpenRead(System.IO.Path.Combine(folder, UPDATE_MANIFEST_FILENAME))) { using(var ss = new SignatureReadingStream(fs, SIGN_KEY)) using(var tr = new System.IO.StreamReader(ss)) using(var jr = new Newtonsoft.Json.JsonTextReader(tr)) update = new Newtonsoft.Json.JsonSerializer().Deserialize<UpdateInfo>(jr); sha256.Initialize(); md5.Initialize(); fs.Position = 0; var h1 = Convert.ToBase64String(sha256.ComputeHash(fs)); fs.Position = 0; var h2 = Convert.ToBase64String(md5.ComputeHash(fs)); manifest = new FileEntry() { Path = UPDATE_MANIFEST_FILENAME, Ignore = false, LastWriteTime = update.ReleaseTime, SHA256 = h1, MD5 = h2 }; } if (version != null && (update.Displayname != version.Displayname || update.ReleaseTime != version.ReleaseTime)) throw new Exception("The found version was not the expected version"); var paths = update.Files.Where(x => !x.Ignore).ToDictionary(x => x.Path.Replace('/', System.IO.Path.DirectorySeparatorChar), Library.Utility.Utility.ClientFilenameStringComparer); paths.Add(manifest.Path, manifest); var ignores = (from x in update.Files where x.Ignore select Library.Utility.Utility.AppendDirSeparator(x.Path.Replace('/', System.IO.Path.DirectorySeparatorChar))).ToList(); folder = Library.Utility.Utility.AppendDirSeparator(folder); var baselen = folder.Length; foreach(var file in Library.Utility.Utility.EnumerateFileSystemEntries(folder)) { var relpath = file.Substring(baselen); if (string.IsNullOrWhiteSpace(relpath)) continue; FileEntry fe; if (!paths.TryGetValue(relpath, out fe)) { var ignore = false; foreach(var c in ignores) if (ignore = relpath.StartsWith(c)) break; if (ignore) continue; throw new Exception(string.Format("Found unexpected file: {0}", file)); } paths.Remove(relpath); if (fe.Path.EndsWith("/")) continue; sha256.Initialize(); md5.Initialize(); using(var fs = System.IO.File.OpenRead(file)) { if (Convert.ToBase64String(sha256.ComputeHash(fs)) != fe.SHA256) throw new Exception(string.Format("Invalid sha256 hash for file: {0}", file)); fs.Position = 0; if (Convert.ToBase64String(md5.ComputeHash(fs)) != fe.MD5) throw new Exception(string.Format("Invalid md5 hash for file: {0}", file)); } } var filteredpaths = (from p in paths where !string.IsNullOrWhiteSpace(p.Key) && !p.Key.EndsWith("/") select p.Key).ToList(); if (filteredpaths.Count == 1) throw new Exception(string.Format("Folder {0} is missing: {1}", folder, filteredpaths.First())); else if (filteredpaths.Count > 0) throw new Exception(string.Format("Folder {0} is missing {1} and {2} other file(s)", folder, filteredpaths.First(), filteredpaths.Count - 1)); return true; } catch (Exception ex) { if (OnError != null) OnError(ex); } return false; }
private static UpdateInfo ReadInstalledManifest(string folder) { var manifest = System.IO.Path.Combine(folder, UPDATE_MANIFEST_FILENAME); if (System.IO.File.Exists(manifest)) { try { using(var fs = System.IO.File.OpenRead(manifest)) using(var ss = new SignatureReadingStream(fs, SIGN_KEY)) using(var tr = new System.IO.StreamReader(ss)) using(var jr = new Newtonsoft.Json.JsonTextReader(tr)) return new Newtonsoft.Json.JsonSerializer().Deserialize<UpdateInfo>(jr); } catch (Exception ex) { if (OnError != null) OnError(ex); } } return null; }
public static Madingley.Common.ModelState Deserialize(TextReader sr) { Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.Cohort> JsonReadCohort = (reader) => { var ret = new Madingley.Common.Cohort(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "FunctionalGroupIndex": ret.FunctionalGroupIndex = Common.Reader.ReadInt(reader); break; case "BirthTimeStep": ret.BirthTimeStep = Common.Reader.ReadInt(reader); break; case "MaturityTimeStep": ret.MaturityTimeStep = Common.Reader.ReadInt(reader); break; case "IDs": ret.IDs = Common.Reader.ReadArray(reader, Common.Reader.ReadInt).ToList(); break; case "JuvenileMass": ret.JuvenileMass = Common.Reader.ReadDouble(reader); break; case "AdultMass": ret.AdultMass = Common.Reader.ReadDouble(reader); break; case "IndividualBodyMass": ret.IndividualBodyMass = Common.Reader.ReadDouble(reader); break; case "IndividualReproductivePotentialMass": ret.IndividualReproductivePotentialMass = Common.Reader.ReadDouble(reader); break; case "MaximumAchievedBodyMass": ret.MaximumAchievedBodyMass = Common.Reader.ReadDouble(reader); break; case "Abundance": ret.Abundance = Common.Reader.ReadDouble(reader); break; case "Merged": ret.Merged = Common.Reader.ReadBoolean(reader); break; case "ProportionTimeActive": ret.ProportionTimeActive = Common.Reader.ReadDouble(reader); break; case "TrophicIndex": ret.TrophicIndex = Common.Reader.ReadDouble(reader); break; case "LogOptimalPreyBodySizeRatio": ret.LogOptimalPreyBodySizeRatio = Common.Reader.ReadDouble(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.Stock> JsonReadStock = (reader) => { var ret = new Madingley.Common.Stock(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "FunctionalGroupIndex": ret.FunctionalGroupIndex = Common.Reader.ReadInt(reader); break; case "IndividualBodyMass": ret.IndividualBodyMass = Common.Reader.ReadDouble(reader); break; case "TotalBiomass": ret.TotalBiomass = Common.Reader.ReadDouble(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; Func<Newtonsoft.Json.JsonTextReader, IDictionary<string, double[]>> JsonReadCellEnvironment = (reader) => { var ret = new Dictionary<string, double[]>(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var key = Convert.ToString(reader.Value); reader.Read(); var value = Common.Reader.ReadArray(reader, Common.Reader.ReadDouble); ret.Add(key, value.ToArray()); } return ret; }; Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.GridCell> JsonReadGridCell = (reader) => { var ret = new Madingley.Common.GridCell(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "Latitude": ret.Latitude = Common.Reader.ReadDouble(reader); break; case "Longitude": ret.Longitude = Common.Reader.ReadDouble(reader); break; case "Cohorts": ret.Cohorts = Common.Reader.ReadArray(reader, r => Common.Reader.ReadArray(r, JsonReadCohort)).ToList(); break; case "Stocks": ret.Stocks = Common.Reader.ReadArray(reader, r => Common.Reader.ReadArray(r, JsonReadStock)).ToList(); break; case "Environment": ret.Environment = JsonReadCellEnvironment(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } return ret; }; var modelState = new Madingley.Common.ModelState(); using (var reader = new Newtonsoft.Json.JsonTextReader(sr)) { reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "TimestepsComplete": modelState.TimestepsComplete = Common.Reader.ReadInt(reader); break; case "GlobalDiagnosticVariables": modelState.GlobalDiagnosticVariables = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadDouble); break; case "GridCells": modelState.GridCells = Common.Reader.ReadArray(reader, JsonReadGridCell).ToList(); break; case "NextCohortID": modelState.NextCohortID = Common.Reader.ReadLong(reader); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } } return modelState; }
public void NewtonsoftJsonSerializeDeserializeBenchmark() { var serializer = new Newtonsoft.Json.JsonSerializer() { DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, }; int runs = 50000; DateTime start, stop; MessageModel msg = null; start = DateTime.UtcNow; for (int i = 0; i < runs; i++) { var writer = new StringWriter(); serializer.Serialize(writer, SimpleMessage); var reader = new StringReader(writer.ToString()); var jsonReader = new Newtonsoft.Json.JsonTextReader(reader); var dtoMsg = serializer.Deserialize<MessageDtoModelV1>(jsonReader); } stop = DateTime.UtcNow; Assert.AreEqual(ComplexMessage, msg); var total = (stop - start).TotalMilliseconds; Console.WriteLine( "NewtonsoftJsonSerialize+Deserialize(Simple): avg: {0:0.00} ms runs: {1} took: {2:0.00} ms", total / runs, runs, total ); start = DateTime.UtcNow; for (int i = 0; i < runs; i++) { var writer = new StringWriter(); serializer.Serialize(writer, ComplexMessage); var reader = new StringReader(writer.ToString()); var jsonReader = new Newtonsoft.Json.JsonTextReader(reader); var dtoMsg = serializer.Deserialize<MessageDtoModelV1>(jsonReader); } stop = DateTime.UtcNow; Assert.AreEqual(ComplexMessage, msg); total = (stop - start).TotalMilliseconds; Console.WriteLine( "NewtonsoftJsonSerialize+Deserialize(ComplexMessage): avg: {0:0.00} ms runs: {1} took: {2:0.00} ms", total / runs, runs, total ); }
public void Put(string remotename, System.IO.Stream stream) { if (stream.Length > BITS_FILE_SIZE_LIMIT) { // Get extra info for BITS var uid = UserID; var fid = FolderID.Split('.')[2]; // Create a session var url = string.Format("https://cid-{0}.users.storage.live.com/items/{1}/{2}?access_token={3}", uid, fid, Utility.Uri.UrlPathEncode(remotename), Library.Utility.Uri.UrlEncode(m_oauth.AccessToken)); var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = USER_AGENT; req.Method = "POST"; req.ContentType = "application/json"; req.Headers.Add("X-Http-Method-Override", "BITS_POST"); req.Headers.Add("BITS-Packet-Type", "Create-Session"); req.Headers.Add("BITS-Supported-Protocols", "{7df0354d-249b-430f-820d-3d2a9bef4931}"); req.ContentLength = 0; var areq = new Utility.AsyncHttpRequest(req); string sessionid; using(var resp = (HttpWebResponse)areq.GetResponse()) { var packtype = resp.Headers["BITS-Packet-Type"]; if (!packtype.Equals("Ack", StringComparison.InvariantCultureIgnoreCase)) throw new Exception(string.Format("Unable to create BITS transfer, got status: {0}", packtype)); sessionid = resp.Headers["BITS-Session-Id"]; } if (string.IsNullOrEmpty(sessionid)) throw new Exception("BITS session-id was missing"); // Session is now created, start uploading chunks var offset = 0L; var retries = 0; while (offset < stream.Length) { try { var bytesInChunk = Math.Min(BITS_CHUNK_SIZE, stream.Length - offset); req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = USER_AGENT; req.Method = "POST"; req.Headers.Add("X-Http-Method-Override", "BITS_POST"); req.Headers.Add("BITS-Packet-Type", "Fragment"); req.Headers.Add("BITS-Session-Id", sessionid); req.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", offset, offset + bytesInChunk - 1, stream.Length)); req.ContentLength = bytesInChunk; if (stream.Position != offset) stream.Position = offset; areq = new Utility.AsyncHttpRequest(req); var remaining = (int)bytesInChunk; using(var reqs = areq.GetRequestStream()) { int read; while ((read = stream.Read(m_copybuffer, 0, Math.Min(m_copybuffer.Length, remaining))) != 0) { reqs.Write(m_copybuffer, 0, read); remaining -= read; } } using(var resp = (HttpWebResponse)areq.GetResponse()) { if (resp.StatusCode != HttpStatusCode.OK) throw new WebException("Invalid partial upload response", null, WebExceptionStatus.UnknownError, resp); } offset += bytesInChunk; retries = 0; } catch (Exception ex) { var retry = false; // If we get a 5xx error, or some network issue, we retry if (ex is WebException && ((WebException)ex).Response is HttpWebResponse) { var code = (int)((HttpWebResponse)((WebException)ex).Response).StatusCode; retry = code >= 500 && code <= 599; } else if (ex is System.Net.Sockets.SocketException || ex is System.IO.IOException || ex.InnerException is System.Net.Sockets.SocketException || ex.InnerException is System.IO.IOException) { retry = true; } // Retry with exponential backoff if (retry && retries < 5) { System.Threading.Thread.Sleep(TimeSpan.FromSeconds(Math.Pow(2, retries))); retries++; } else throw; } } // Transfer completed, now commit the upload and close the session req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = USER_AGENT; req.Method = "POST"; req.Headers.Add("X-Http-Method-Override", "BITS_POST"); req.Headers.Add("BITS-Packet-Type", "Close-Session"); req.Headers.Add("BITS-Session-Id", sessionid); req.ContentLength = 0; areq = new Utility.AsyncHttpRequest(req); using(var resp = (HttpWebResponse)areq.GetResponse()) { if (resp.StatusCode != HttpStatusCode.OK) throw new Exception("Invalid partial upload commit response"); } } else { var url = string.Format("{0}/{1}/files/{2}?access_token={3}", WLID_SERVER, FolderID, Utility.Uri.UrlPathEncode(remotename), Library.Utility.Uri.UrlEncode(m_oauth.AccessToken)); var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = USER_AGENT; req.Method = "PUT"; try { req.ContentLength = stream.Length; } catch { } // Docs says not to set this ? //req.ContentType = "application/octet-stream"; var areq = new Utility.AsyncHttpRequest(req); using(var reqs = areq.GetRequestStream()) Utility.Utility.CopyStream(stream, reqs, true, m_copybuffer); using(var resp = (HttpWebResponse)areq.GetResponse()) using(var rs = areq.GetResponseStream()) using(var tr = new System.IO.StreamReader(rs)) using(var jr = new Newtonsoft.Json.JsonTextReader(tr)) { var nf = new Newtonsoft.Json.JsonSerializer().Deserialize<WLID_FolderItem>(jr); m_fileidCache[remotename] = nf.id; } } }
private Item _LoadFromDisk(string fileId) { try { string journalFilePath = ""; Item item = null; try { journalFilePath = Settings.GetJournalFilePath(fileId + ".item", false); if (!FileExists(journalFilePath)) { return null; } DateTime lastWriteTime = GetFileLastWriteTime(journalFilePath); using ( System.IO.FileStream fileStream = OpenFile(journalFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { using (var streamReader = new System.IO.StreamReader(fileStream)) { using (Newtonsoft.Json.JsonReader jsonReader = new Newtonsoft.Json.JsonTextReader(streamReader)) { var jsonSerializer = new Newtonsoft.Json.JsonSerializer(); item = (Item)jsonSerializer.Deserialize(jsonReader, typeof (Item)); } } } if (item == null || item.Status != ItemStatus.Cached) { throw new Exception("Json parsing failed"); } _AddItem(item); } catch (Exception exception) { Log.Error(exception, false, false); try { DeleteFile(journalFilePath); } catch (Exception exception2) { Log.Error(exception2, false, false); } return null; } Item[] children = null; try { journalFilePath = Settings.GetJournalFilePath(fileId + ".children", false); if (!FileExists(journalFilePath)) { return null; } using ( System.IO.FileStream fileStream = OpenFile(journalFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { using (var streamReader = new System.IO.StreamReader(fileStream)) { using (Newtonsoft.Json.JsonReader jsonReader = new Newtonsoft.Json.JsonTextReader(streamReader)) { var jsonSerializer = new Newtonsoft.Json.JsonSerializer(); children = (Item[])jsonSerializer.Deserialize(jsonReader, typeof (Item[])); } } } if (children == null) { throw new Exception("Json parsing failed"); } foreach (Item child in children) { _AddItem(child); } } catch (Exception exception) { Log.Error(exception, false, false); try { DeleteFile(journalFilePath); } catch (Exception exception2) { Log.Error(exception2, false, false); } return null; } return item; } catch (Exception exception) { Log.Error(exception, false); return null; } }
public static UpdateInfo CheckForUpdate() { foreach(var url in MANIFEST_URLS) { try { using(var tmpfile = new Library.Utility.TempFile()) { System.Net.WebClient wc = new System.Net.WebClient(); wc.Headers.Add(System.Net.HttpRequestHeader.UserAgent, string.Format("{0} v{1}{2}", APPNAME, SelfVersion.Version, string.IsNullOrWhiteSpace(InstallID) ? "" : " -" + InstallID)); wc.Headers.Add("X-Install-ID", InstallID); wc.DownloadFile(url, tmpfile); using(var fs = System.IO.File.OpenRead(tmpfile)) using(var ss = new SignatureReadingStream(fs, SIGN_KEY)) using(var tr = new System.IO.StreamReader(ss)) using(var jr = new Newtonsoft.Json.JsonTextReader(tr)) { var update = new Newtonsoft.Json.JsonSerializer().Deserialize<UpdateInfo>(jr); if (TryParseVersion(update.Version) <= TryParseVersion(SelfVersion.Version)) return null; if (string.Equals(SelfVersion.ReleaseType, "Debug", StringComparison.InvariantCultureIgnoreCase) && !string.Equals(update.ReleaseType, SelfVersion.ReleaseType, StringComparison.CurrentCultureIgnoreCase)) return null; LastUpdateCheckVersion = update; return update; } } } catch (Exception ex) { if (OnError != null) OnError(ex); } } return null; }
static ProjectFile MigrateToProjectJson (DotNetProject project) { var projectJsonName = project.BaseDirectory.Combine ("project.json"); var projectJsonFile = new ProjectFile (projectJsonName, BuildAction.None); bool isOpen = false; JObject json; ITextDocument file; if (System.IO.File.Exists (projectJsonName)) { file = TextFileProvider.Instance.GetTextEditorData (projectJsonFile.FilePath.ToString (), out isOpen); using (var tr = file.CreateReader ()) using (var jr = new Newtonsoft.Json.JsonTextReader (tr)) { json = (JObject)JToken.Load (jr); } } else { file = TextEditorFactory.CreateNewDocument (); file.FileName = projectJsonName; file.Encoding = Encoding.UTF8; json = new JObject ( new JProperty ("dependencies", new JObject ()), new JProperty ("frameworks", new JObject()) ); } var packagesConfigFile = project.GetProjectFile (project.BaseDirectory.Combine ("packages.config")); if (packagesConfigFile != null) { //NOTE: it might also be open and unsaved, but that's an unimportant edge case, ignore it var configDoc = System.Xml.Linq.XDocument.Load (packagesConfigFile.FilePath); if (configDoc.Root != null) { var deps = (json ["dependencies"] as JObject) ?? ((JObject)(json ["dependencies"] = new JObject ())); foreach (var packagelEl in configDoc.Root.Elements ("package")) { deps [(string)packagelEl.Attribute ("id")] = (string)packagelEl.Attribute ("version"); } } } var framework = GetPclProfileFullName (project.TargetFramework.Id) ?? NetStandardDefaultFramework; json ["frameworks"] = new JObject ( new JProperty (framework, new JObject()) ); file.Text = json.ToString (); if (!isOpen) { file.Save (); } project.AddFile (projectJsonFile); if (packagesConfigFile != null) { project.Files.Remove (packagesConfigFile); //we have to delete the packages.config, or the NuGet addin will try to retarget its packages FileService.DeleteFile (packagesConfigFile.FilePath); //remove the package refs nuget put in the file, project.json doesn't use those project.References.RemoveRange (project.References.Where (IsFromPackage).ToArray ()); } return projectJsonFile; }
public static UpdateInfo CheckForUpdate(ReleaseType channel = ReleaseType.Unknown) { if (channel == ReleaseType.Unknown) channel = AutoUpdateSettings.DefaultUpdateChannel; foreach(var rawurl in MANIFEST_URLS) { var url = rawurl; // Attempt to match the url to change the channel if possible // This allows overrides to the URLs for deployment of custom builds, // but does not require that they adopt the channel system var match = AutoUpdateSettings.MATCH_AUTOUPDATE_URL.Match(url); if (match.Success) { var mg = match.Groups[AutoUpdateSettings.MATCH_UPDATE_URL_CHANNEL_GROUP]; // Replace the channel name with the chosen channel url = url.Substring(0, mg.Index) + channel.ToString().ToLowerInvariant() + url.Substring(mg.Index + mg.Length); } try { using(var tmpfile = new Library.Utility.TempFile()) { System.Net.WebClient wc = new System.Net.WebClient(); wc.Headers.Add(System.Net.HttpRequestHeader.UserAgent, string.Format("{0} v{1}{2}", APPNAME, SelfVersion.Version, string.IsNullOrWhiteSpace(InstallID) ? "" : " -" + InstallID)); wc.Headers.Add("X-Install-ID", InstallID); wc.DownloadFile(url, tmpfile); using(var fs = System.IO.File.OpenRead(tmpfile)) using(var ss = new SignatureReadingStream(fs, SIGN_KEY)) using(var tr = new System.IO.StreamReader(ss)) using(var jr = new Newtonsoft.Json.JsonTextReader(tr)) { var update = new Newtonsoft.Json.JsonSerializer().Deserialize<UpdateInfo>(jr); if (TryParseVersion(update.Version) <= TryParseVersion(SelfVersion.Version)) return null; // Don't install a debug update on a release build and vice versa if (string.Equals(SelfVersion.ReleaseType, "Debug", StringComparison.InvariantCultureIgnoreCase) && !string.Equals(update.ReleaseType, SelfVersion.ReleaseType, StringComparison.CurrentCultureIgnoreCase)) return null; ReleaseType rt; if (!Enum.TryParse<ReleaseType>(update.ReleaseType, true, out rt)) rt = ReleaseType.Unknown; // If the update is too low to be considered, skip it // Should never happen, but protects against mistakes in deployment if (rt > channel) return null; LastUpdateCheckVersion = update; return update; } } } catch (Exception ex) { if (OnError != null) OnError(ex); } } return null; }
static IEnumerable<string> GetProjectJsonFrameworks (DotNetProject project) { var packagesJsonFile = project.GetProjectFile (project.BaseDirectory.Combine ("project.json")); if (packagesJsonFile == null) { return null; } var file = TextFileProvider.Instance.GetEditableTextFile (packagesJsonFile.FilePath.ToString ()); JObject json; using (var tr = file.CreateReader ()) using (var jr = new Newtonsoft.Json.JsonTextReader (tr)) { json = (JObject)JToken.Load (jr); } var frameworks = json ["frameworks"] as JObject; if (frameworks == null) return null; return frameworks.Properties ().Select (p => p.Name); }
public static int Main(string[] _args) { var args = new List<string>(_args); var opts = Duplicati.Library.Utility.CommandLineParser.ExtractOptions(args); string inputfolder; string outputfolder; string keyfile; string manifestfile; string keyfilepassword; string gpgkeyfile; string gpgpath; opts.TryGetValue("input", out inputfolder); opts.TryGetValue("output", out outputfolder); opts.TryGetValue("keyfile", out keyfile); opts.TryGetValue("manifest", out manifestfile); opts.TryGetValue("keyfile-password", out keyfilepassword); opts.TryGetValue("gpgkeyfile", out gpgkeyfile); opts.TryGetValue("gpgpath", out gpgpath); var usedoptions = new string[] { "input", "output", "keyfile", "manifest", "keyfile-password", "gpgkeyfile", "gpgpath" }; if (string.IsNullOrWhiteSpace(inputfolder)) { Console.WriteLine("Missing input folder"); return 4; } if (string.IsNullOrWhiteSpace(outputfolder)) { Console.WriteLine("Missing output folder"); return 4; } if (string.IsNullOrWhiteSpace(keyfile)) { Console.WriteLine("Missing keyfile"); return 4; } if (!System.IO.Directory.Exists(inputfolder)) { Console.WriteLine("Input folder not found"); return 4; } if (string.IsNullOrWhiteSpace(keyfilepassword)) { Console.WriteLine("Enter keyfile passphrase: "); keyfilepassword = Console.ReadLine().Trim(); } if (!System.IO.File.Exists(keyfile)) { Console.WriteLine("Keyfile not found, creating new"); var newkey = System.Security.Cryptography.RSACryptoServiceProvider.Create().ToXmlString(true); using (var enc = new Duplicati.Library.Encryption.AESEncryption(keyfilepassword, new Dictionary<string, string>())) using (var fs = System.IO.File.OpenWrite(keyfile)) using (var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(newkey))) enc.Encrypt(ms, fs); } if (!System.IO.Directory.Exists(outputfolder)) System.IO.Directory.CreateDirectory(outputfolder); var privkey = (System.Security.Cryptography.RSACryptoServiceProvider)System.Security.Cryptography.RSACryptoServiceProvider.Create(); using(var enc = new Duplicati.Library.Encryption.AESEncryption(keyfilepassword, new Dictionary<string, string>())) using(var ms = new System.IO.MemoryStream()) using(var fs = System.IO.File.OpenRead(keyfile)) { enc.Decrypt(fs, ms); ms.Position = 0; using(var sr = new System.IO.StreamReader(ms)) privkey.FromXmlString(sr.ReadToEnd()); } if (Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey == null || privkey.ToXmlString(false) != Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey.ToXmlString(false)) { Console.WriteLine("The public key in the project is not the same as the public key from the file"); Console.WriteLine("Try setting the key to: "); Console.WriteLine(privkey.ToXmlString(false)); return 5; } string gpgkeyid = null; string gpgkeypassphrase = null; if (string.IsNullOrWhiteSpace(gpgkeyfile)) { Console.WriteLine("No gpgfile, skipping GPG signature files"); } else if (!System.IO.File.Exists(gpgkeyfile)) { Console.WriteLine("Missing gpgfile"); return 6; } else { using(var enc = new Duplicati.Library.Encryption.AESEncryption(keyfilepassword, new Dictionary<string, string>())) using(var ms = new System.IO.MemoryStream()) using(var fs = System.IO.File.OpenRead(gpgkeyfile)) { enc.Decrypt(fs, ms); ms.Position = 0; // No real format, just two lines using (var sr = new System.IO.StreamReader(ms)) { var lines = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); gpgkeyid = lines[0]; gpgkeypassphrase = lines[1]; } } } Duplicati.Library.AutoUpdater.UpdateInfo updateInfo; using (var fs = System.IO.File.OpenRead(manifestfile)) using (var sr = new System.IO.StreamReader(fs)) using (var jr = new Newtonsoft.Json.JsonTextReader(sr)) updateInfo = new Newtonsoft.Json.JsonSerializer().Deserialize<Duplicati.Library.AutoUpdater.UpdateInfo>(jr); var isopts = new Dictionary<string, string>(opts, StringComparer.InvariantCultureIgnoreCase); foreach (var usedopt in usedoptions) isopts.Remove(usedopt); foreach (var k in updateInfo.GetType().GetFields()) if (isopts.ContainsKey(k.Name)) { try { //Console.WriteLine("Setting {0} to {1}", k.Name, isopts[k.Name]); if (k.FieldType == typeof(string[])) k.SetValue(updateInfo, isopts[k.Name].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); else if (k.FieldType == typeof(Version)) k.SetValue(updateInfo, new Version(isopts[k.Name])); else if (k.FieldType == typeof(int)) k.SetValue(updateInfo, int.Parse(isopts[k.Name])); else if (k.FieldType == typeof(long)) k.SetValue(updateInfo, long.Parse(isopts[k.Name])); else k.SetValue(updateInfo, isopts[k.Name]); } catch (Exception ex) { Console.WriteLine("Failed setting {0} to {1}: {2}", k.Name, isopts[k.Name], ex.Message); } isopts.Remove(k.Name); } foreach(var opt in isopts) Console.WriteLine("Warning! unused option: {0} = {1}", opt.Key, opt.Value); using (var tf = new Duplicati.Library.Utility.TempFile()) { using (var fs = System.IO.File.OpenWrite(tf)) using (var tw = new System.IO.StreamWriter(fs)) new Newtonsoft.Json.JsonSerializer().Serialize(tw, updateInfo); Duplicati.Library.AutoUpdater.UpdaterManager.CreateUpdatePackage(privkey, inputfolder, outputfolder, tf); } if (gpgkeyid != null) { gpgpath = gpgpath ?? "gpg"; var srcfile = System.IO.Path.Combine(outputfolder, "package.zip"); var proc = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = gpgpath, Arguments = string.Format("--passphrase-fd 0 --batch --yes --default-key={1} --output \"{0}.sig\" --detach-sig \"{0}\"", srcfile, gpgkeyid), RedirectStandardInput = true, UseShellExecute = false }); proc.StandardInput.WriteLine(gpgkeypassphrase); proc.WaitForExit(); proc = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = gpgpath, Arguments = string.Format("--passphrase-fd 0 --batch --yes --default-key={1} --armor --output \"{0}.sig.asc\" --detach-sig \"{0}\"", srcfile, gpgkeyid), RedirectStandardInput = true, UseShellExecute = false }); proc.StandardInput.WriteLine(gpgkeypassphrase); proc.WaitForExit(); } return 0; }
public static int Main(string[] _args) { var args = new List<string>(_args); var opts = Duplicati.Library.Utility.CommandLineParser.ExtractOptions(args); string inputfolder; string outputfolder; string keyfile; string manifestfile; string keyfilepassword; opts.TryGetValue("input", out inputfolder); opts.TryGetValue("output", out outputfolder); opts.TryGetValue("keyfile", out keyfile); opts.TryGetValue("manifest", out manifestfile); opts.TryGetValue("keyfile-password", out keyfilepassword); var usedoptions = new string[] { "input", "output", "keyfile", "manifest", "keyfile-password" }; if (string.IsNullOrWhiteSpace(inputfolder)) { Console.WriteLine("Missing input folder"); return 4; } if (string.IsNullOrWhiteSpace(outputfolder)) { Console.WriteLine("Missing output folder"); return 4; } if (string.IsNullOrWhiteSpace(keyfile)) { Console.WriteLine("Missing keyfile"); return 4; } if (!System.IO.Directory.Exists(inputfolder)) { Console.WriteLine("Input folder not found"); return 4; } if (string.IsNullOrWhiteSpace(keyfilepassword)) { Console.WriteLine("Enter keyfile passphrase: "); keyfilepassword = Console.ReadLine().Trim(); } if (!System.IO.File.Exists(keyfile)) { Console.WriteLine("Keyfile not found, creating new"); var newkey = System.Security.Cryptography.RSACryptoServiceProvider.Create().ToXmlString(true); using (var enc = new Duplicati.Library.Encryption.AESEncryption(keyfilepassword, new Dictionary<string, string>())) using (var fs = System.IO.File.OpenWrite(keyfile)) using (var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(newkey))) enc.Encrypt(ms, fs); } if (!System.IO.Directory.Exists(outputfolder)) System.IO.Directory.CreateDirectory(outputfolder); var privkey = (System.Security.Cryptography.RSACryptoServiceProvider)System.Security.Cryptography.RSACryptoServiceProvider.Create(); using(var enc = new Duplicati.Library.Encryption.AESEncryption(keyfilepassword, new Dictionary<string, string>())) using(var ms = new System.IO.MemoryStream()) using(var fs = System.IO.File.OpenRead(keyfile)) { enc.Decrypt(fs, ms); ms.Position = 0; using(var sr = new System.IO.StreamReader(ms)) privkey.FromXmlString(sr.ReadToEnd()); } if (Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey == null || privkey.ToXmlString(false) != Duplicati.Library.AutoUpdater.AutoUpdateSettings.SignKey.ToXmlString(false)) { Console.WriteLine("The public key in the project is not the same as the public key from the file"); Console.WriteLine("Try setting the key to: "); Console.WriteLine(privkey.ToXmlString(false)); return 5; } Duplicati.Library.AutoUpdater.UpdateInfo updateInfo; using (var fs = System.IO.File.OpenRead(manifestfile)) using (var sr = new System.IO.StreamReader(fs)) using (var jr = new Newtonsoft.Json.JsonTextReader(sr)) updateInfo = new Newtonsoft.Json.JsonSerializer().Deserialize<Duplicati.Library.AutoUpdater.UpdateInfo>(jr); var isopts = new Dictionary<string, string>(opts, StringComparer.InvariantCultureIgnoreCase); foreach (var usedopt in usedoptions) isopts.Remove(usedopt); foreach (var k in updateInfo.GetType().GetFields()) if (isopts.ContainsKey(k.Name)) { try { //Console.WriteLine("Setting {0} to {1}", k.Name, isopts[k.Name]); if (k.FieldType == typeof(string[])) k.SetValue(updateInfo, isopts[k.Name].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); else if (k.FieldType == typeof(Version)) k.SetValue(updateInfo, new Version(isopts[k.Name])); else if (k.FieldType == typeof(int)) k.SetValue(updateInfo, int.Parse(isopts[k.Name])); else if (k.FieldType == typeof(long)) k.SetValue(updateInfo, long.Parse(isopts[k.Name])); else k.SetValue(updateInfo, isopts[k.Name]); } catch (Exception ex) { Console.WriteLine("Failed setting {0} to {1}: {2}", k.Name, isopts[k.Name], ex.Message); } isopts.Remove(k.Name); } foreach(var opt in isopts) Console.WriteLine("Warning! unused option: {0} = {1}", opt.Key, opt.Value); using (var tf = new Duplicati.Library.Utility.TempFile()) { using (var fs = System.IO.File.OpenWrite(tf)) using (var tw = new System.IO.StreamWriter(fs)) new Newtonsoft.Json.JsonSerializer().Serialize(tw, updateInfo); Duplicati.Library.AutoUpdater.UpdaterManager.CreateUpdatePackage(privkey, inputfolder, outputfolder, tf); } return 0; }
private static void ApplyMetadata(string path, System.IO.Stream stream, bool restorePermissions) { using(var tr = new System.IO.StreamReader(stream)) using(var jr = new Newtonsoft.Json.JsonTextReader(tr)) { var metadata = new Newtonsoft.Json.JsonSerializer().Deserialize<Dictionary<string, string>>(jr); string k; long t; System.IO.FileAttributes fa; var isDirTarget = path.EndsWith(DIRSEP); var targetpath = isDirTarget ? path.Substring(0, path.Length - 1) : path; // Make the symlink first, otherwise we cannot apply metadata to it if (metadata.TryGetValue("CoreSymlinkTarget", out k)) m_systemIO.CreateSymlink(targetpath, k, isDirTarget); if (metadata.TryGetValue("CoreLastWritetime", out k) && long.TryParse(k, out t)) { if (isDirTarget) m_systemIO.DirectorySetLastWriteTimeUtc(targetpath, new DateTime(t, DateTimeKind.Utc)); else m_systemIO.FileSetLastWriteTimeUtc(targetpath, new DateTime(t, DateTimeKind.Utc)); } if (metadata.TryGetValue("CoreCreatetime", out k) && long.TryParse(k, out t)) { if (isDirTarget) m_systemIO.DirectorySetCreationTimeUtc(targetpath, new DateTime(t, DateTimeKind.Utc)); else m_systemIO.FileSetCreationTimeUtc(targetpath, new DateTime(t, DateTimeKind.Utc)); } if (metadata.TryGetValue("CoreAttributes", out k) && Enum.TryParse(k, true, out fa)) m_systemIO.SetFileAttributes(targetpath, fa); m_systemIO.SetMetadata(path, metadata, restorePermissions); } }