public async Task WriteTokenWithParentAsync() { JObject o = new JObject { ["prop1"] = new JArray(1), ["prop2"] = 1 }; JTokenWriter writer = new JTokenWriter(); await writer.WriteStartArrayAsync(); await writer.WriteTokenAsync(o.CreateReader()); Assert.AreEqual(WriteState.Array, writer.WriteState); await writer.WriteEndArrayAsync(); Console.WriteLine(writer.Token.ToString()); StringAssert.AreEqual(@"[ { ""prop1"": [ 1 ], ""prop2"": 1 } ]", writer.Token.ToString()); }
public async Task WriteDuplicatePropertyNameAsync() { JTokenWriter writer = new JTokenWriter(); await writer.WriteStartObjectAsync(); await writer.WritePropertyNameAsync("prop1"); await writer.WriteStartObjectAsync(); await writer.WriteEndObjectAsync(); await writer.WritePropertyNameAsync("prop1"); await writer.WriteStartArrayAsync(); await writer.WriteEndArrayAsync(); await writer.WriteEndObjectAsync(); StringAssert.AreEqual( @"{ ""prop1"": [] }", writer.Token.ToString() ); }
public async Task ValueFormattingAsync() { byte[] data = Encoding.UTF8.GetBytes("Hello world."); JToken root; using (JTokenWriter jsonWriter = new JTokenWriter()) { await jsonWriter.WriteStartArrayAsync(); await jsonWriter.WriteValueAsync('@'); await jsonWriter.WriteValueAsync("\r\n\t\f\b?{\\r\\n\"\'"); await jsonWriter.WriteValueAsync(true); await jsonWriter.WriteValueAsync(10); await jsonWriter.WriteValueAsync(10.99); await jsonWriter.WriteValueAsync(0.99); await jsonWriter.WriteValueAsync(0.000000000000000001d); await jsonWriter.WriteValueAsync(0.000000000000000001m); await jsonWriter.WriteValueAsync((string)null); await jsonWriter.WriteValueAsync("This is a string."); await jsonWriter.WriteNullAsync(); await jsonWriter.WriteUndefinedAsync(); await jsonWriter.WriteValueAsync(data); await jsonWriter.WriteEndArrayAsync(); root = jsonWriter.Token; } CustomAssert.IsInstanceOfType(typeof(JArray), root); Assert.AreEqual(13, root.Children().Count()); Assert.AreEqual("@", (string)root[0]); Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", (string)root[1]); Assert.AreEqual(true, (bool)root[2]); Assert.AreEqual(10, (int)root[3]); Assert.AreEqual(10.99, (double)root[4]); Assert.AreEqual(0.99, (double)root[5]); Assert.AreEqual(0.000000000000000001d, (double)root[6]); Assert.AreEqual(0.000000000000000001m, (decimal)root[7]); Assert.AreEqual(null, (string)root[8]); Assert.AreEqual("This is a string.", (string)root[9]); Assert.AreEqual(null, ((JValue)root[10]).Value); Assert.AreEqual(null, ((JValue)root[11]).Value); Assert.AreEqual(data, (byte[])root[12]); }
public async Task WriteCommentAsync() { JTokenWriter writer = new JTokenWriter(); await writer.WriteStartArrayAsync(); await writer.WriteCommentAsync("fail"); await writer.WriteEndArrayAsync(); StringAssert.AreEqual(@"[ /*fail*/]", writer.Token.ToString()); }
public async Task WriteRawValueAsync() { JTokenWriter writer = new JTokenWriter(); await writer.WriteStartArrayAsync(); await writer.WriteRawValueAsync("fail"); await writer.WriteRawValueAsync("fail"); await writer.WriteEndArrayAsync(); StringAssert.AreEqual(@"[ fail, fail ]", writer.Token.ToString()); }
public async Task WriteValueTokenWithParentAsync() { JValue v = new JValue(1); JTokenWriter writer = new JTokenWriter(); await writer.WriteStartArrayAsync(); await writer.WriteTokenAsync(v.CreateReader()); Assert.AreEqual(WriteState.Array, writer.WriteState); await writer.WriteEndArrayAsync(); StringAssert.AreEqual(@"[ 1 ]", writer.Token.ToString()); }
public async Task WriteRawAsync() { JTokenWriter writer = new JTokenWriter(); await writer.WriteStartArrayAsync(); await writer.WriteRawAsync("fail"); await writer.WriteRawAsync("fail"); await writer.WriteEndArrayAsync(); // this is a bug. See non-async equivalent test. StringAssert.AreEqual(@"[ fail, fail ]", writer.Token.ToString()); }
public async Task WriteBigIntegerAsync() { JTokenWriter writer = new JTokenWriter(); await writer.WriteStartArrayAsync(); await writer.WriteValueAsync(new BigInteger(123)); await writer.WriteEndArrayAsync(); JValue i = (JValue)writer.Token[0]; Assert.AreEqual(new BigInteger(123), i.Value); Assert.AreEqual(JTokenType.Integer, i.Type); StringAssert.AreEqual(@"[ 123 ]", writer.Token.ToString()); }
public async Task WriteEmptyTokenAsync() { JObject o = new JObject(); JsonReader reader = o.CreateReader(); while (reader.Read()) { } JTokenWriter writer = new JTokenWriter(); await writer.WriteStartArrayAsync(); await writer.WriteTokenAsync(reader); Assert.AreEqual(WriteState.Array, writer.WriteState); await writer.WriteEndArrayAsync(); StringAssert.AreEqual(@"[]", writer.Token.ToString()); }
/// <summary> /// Write query results to json string or file. /// </summary> /// <param name="command"></param> /// <param name="queryOutput"></param> /// <param name="cancellationToken"></param> /// <returns>JToken</returns> public static async Task <object> MultiQueryToJsonAsync(this OracleCommand command, QueryOutputProperties queryOutput, CancellationToken cancellationToken) { using (var reader = await command.ExecuteReaderAsync(cancellationToken) as OracleDataReader) { var culture = string.IsNullOrWhiteSpace(queryOutput.JsonOutput.CultureInfo) ? CultureInfo.InvariantCulture : new CultureInfo(queryOutput.JsonOutput.CultureInfo); // UTF-8 as default encoding. var encoding = string.IsNullOrWhiteSpace(queryOutput.OutputFile?.Encoding) ? Encoding.UTF8 : Encoding.GetEncoding(queryOutput.OutputFile.Encoding); // Create json result. using (var writer = new JTokenWriter() as JsonWriter) { writer.Formatting = Newtonsoft.Json.Formatting.Indented; writer.Culture = culture; // Start array. await writer.WriteStartArrayAsync(cancellationToken); while (reader.Read()) { // Start row object. await writer.WriteStartObjectAsync(cancellationToken); for (var i = 0; i < reader.FieldCount; i++) { // Add row element name. await writer.WritePropertyNameAsync(reader.GetName(i), cancellationToken); // Add row element value. switch (reader.GetDataTypeName(i)) { case "Decimal": // FCOM-204 fix; proper handling of decimal values and NULL values in decimal type fields. var v = reader.GetOracleDecimal(i); var FieldValue = OracleDecimal.SetPrecision(v, 28); if (!FieldValue.IsNull) { await writer.WriteValueAsync((decimal)FieldValue, cancellationToken); } else { await writer.WriteValueAsync(string.Empty, cancellationToken); } break; default: await writer.WriteValueAsync(reader.GetValue(i) ?? string.Empty, cancellationToken); break; } cancellationToken.ThrowIfCancellationRequested(); } // End row object. await writer.WriteEndObjectAsync(cancellationToken); cancellationToken.ThrowIfCancellationRequested(); } // End array. await writer.WriteEndArrayAsync(cancellationToken); return(((JTokenWriter)writer).Token); } } }