/// <summary> /// Flushes remaining data to the file and then manually writes JSON tags to close the file out /// </summary> internal override async Task FlushWriter() { if (!FileCreated) { return; } if (Queue.Count > 0) { await WriteData(); } var meta = new MetaTag { Count = Count, CollectionMethods = (long)_context.ResolvedCollectionMethods, DataType = DataType, Version = DataVersion }; await _jsonWriter.FlushAsync(); await _jsonWriter.WriteEndArrayAsync(); await _jsonWriter.WritePropertyNameAsync("meta"); await _jsonWriter.WriteRawValueAsync(JsonConvert.SerializeObject(meta, PrettyPrint)); await _jsonWriter.FlushAsync(); await _jsonWriter.CloseAsync(); }
public async void WriteDefault(Action <bool> callback) { Config tempCfg = new Config(); JsonSerializer js = new JsonSerializer(); js.Formatting = Formatting.Indented; using (StreamWriter sw = new StreamWriter(Properties.Settings.Default.configFile)) using (JsonWriter jsw = new JsonTextWriter(sw)) { js.Serialize(jsw, tempCfg); await jsw.CloseAsync(); callback(true); } }
private async Task SendResponse(HttpContext httpContext, JsonRpcResponse response) { var jObject = JObject.FromObject(response, _server.Serializer); // append jsonrpc constant if enabled if (_options.UseJsonRpcConstant) { jObject.AddFirst(new JProperty("jsonrpc", "2.0")); } // ensure that respose has either result or error property var errorProperty = jObject.Property("error"); if (jObject.Property("result") == null && errorProperty == null) { jObject.Add("result", null); } // remove error data if disallowed if (!_options.AllowErrorData && errorProperty?.Value is JObject errorObject) { errorObject.Property("data", StringComparison.OrdinalIgnoreCase)?.Remove(); } httpContext.Response.StatusCode = 200; httpContext.Response.ContentType = "application/json"; // todo: move to System.Text.Json await using (var tempStream = new MemoryStream()) { await using (var tempStreamWriter = new StreamWriter(tempStream, leaveOpen: true)) using (var jsonWriter = new JsonTextWriter(tempStreamWriter)) { await jObject.WriteToAsync(jsonWriter); await jsonWriter.FlushAsync(); await jsonWriter.CloseAsync(); } tempStream.Seek(0, SeekOrigin.Begin); await tempStream.CopyToAsync(httpContext.Response.Body); } }
/// <summary> /// Saves a User's Token to a file /// </summary> /// <param name="token">A User's Identity</param> public static async void SaveEmail(string email) { File.Delete(_filename); //Create StringBuilder and StringWriter StreamWriter sw = new StreamWriter(_filename); //Using the Writer do..... using (JsonWriter writer = new JsonTextWriter(sw)) { //Set Formatting and Wtire necessary information writer.Formatting = Formatting.Indented; await writer.WriteStartObjectAsync(); await writer.WritePropertyNameAsync("Email"); await writer.WriteValueAsync(email); await writer.WriteEndObjectAsync(); await writer.CloseAsync(); } }