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 StateAsync()
        {
            using (JsonWriter jsonWriter = new JTokenWriter())
            {
                Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);

                await jsonWriter.WriteStartObjectAsync();

                Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);

                await jsonWriter.WritePropertyNameAsync("CPU");

                Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);

                await jsonWriter.WriteValueAsync("Intel");

                Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);

                await jsonWriter.WritePropertyNameAsync("Drives");

                Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);

                await jsonWriter.WriteStartArrayAsync();

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);

                await jsonWriter.WriteValueAsync("DVD read/writer");

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);

#if !PORTABLE || NETSTANDARD1_1
                await jsonWriter.WriteValueAsync(new BigInteger(123));

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
#endif

                await jsonWriter.WriteValueAsync(new byte[0]);

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);

                await jsonWriter.WriteEndAsync();

                Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);

                await jsonWriter.WriteEndObjectAsync();

                Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
            }
        }
        public async Task WriteTokenWithPropertyParentAsync()
        {
            JValue v = new JValue(1);

            JTokenWriter writer = new JTokenWriter();

            await writer.WriteStartObjectAsync();

            await writer.WritePropertyNameAsync("Prop1");

            await writer.WriteTokenAsync(v.CreateReader());

            Assert.AreEqual(WriteState.Object, writer.WriteState);

            await writer.WriteEndObjectAsync();

            StringAssert.AreEqual(@"{
  ""Prop1"": 1
}", writer.Token.ToString());
        }
        public async Task CurrentTokenAsync()
        {
            using (JTokenWriter jsonWriter = new JTokenWriter())
            {
                Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
                Assert.AreEqual(null, jsonWriter.CurrentToken);

                await jsonWriter.WriteStartObjectAsync();

                Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
                Assert.AreEqual(jsonWriter.Token, jsonWriter.CurrentToken);

                JObject o = (JObject)jsonWriter.Token;

                await jsonWriter.WritePropertyNameAsync("CPU");

                Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
                Assert.AreEqual(o.Property("CPU"), jsonWriter.CurrentToken);

                await jsonWriter.WriteValueAsync("Intel");

                Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
                Assert.AreEqual(o["CPU"], jsonWriter.CurrentToken);

                await jsonWriter.WritePropertyNameAsync("Drives");

                Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
                Assert.AreEqual(o.Property("Drives"), jsonWriter.CurrentToken);

                await jsonWriter.WriteStartArrayAsync();

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
                Assert.AreEqual(o["Drives"], jsonWriter.CurrentToken);

                JArray a = (JArray)jsonWriter.CurrentToken;

                await jsonWriter.WriteValueAsync("DVD read/writer");

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
                Assert.AreEqual(a[a.Count - 1], jsonWriter.CurrentToken);

#if !PORTABLE || NETSTANDARD1_1
                await jsonWriter.WriteValueAsync(new BigInteger(123));

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
                Assert.AreEqual(a[a.Count - 1], jsonWriter.CurrentToken);
#endif

                await jsonWriter.WriteValueAsync(new byte[0]);

                Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
                Assert.AreEqual(a[a.Count - 1], jsonWriter.CurrentToken);

                await jsonWriter.WriteEndAsync();

                Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
                Assert.AreEqual(a, jsonWriter.CurrentToken);

                await jsonWriter.WriteEndObjectAsync();

                Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
                Assert.AreEqual(o, jsonWriter.CurrentToken);
            }
        }
Exemple #5
0
        /// <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);
                }
            }
        }