當前位置: 首頁>>代碼示例>>C#>>正文


C# JsonTextWriter.WriteToken方法代碼示例

本文整理匯總了C#中Newtonsoft.Json.JsonTextWriter.WriteToken方法的典型用法代碼示例。如果您正苦於以下問題:C# JsonTextWriter.WriteToken方法的具體用法?C# JsonTextWriter.WriteToken怎麽用?C# JsonTextWriter.WriteToken使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Newtonsoft.Json.JsonTextWriter的用法示例。


在下文中一共展示了JsonTextWriter.WriteToken方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ReadJsonAsStringArray

        protected virtual object ReadJsonAsStringArray(JsonReader reader)
        {
            if (reader.TokenType != JsonToken.StartArray)
            {
                reader.Skip();
                return null;
            }

            var rowValues = new List<string>();
            var valueStartDepth = reader.Depth;
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                using (var jw = new JsonTextWriter(sw))
                {
                    while (reader.Read() && !(reader.TokenType == JsonToken.EndArray && reader.Depth == valueStartDepth))
                    {
                        jw.WriteToken(reader, true);
                        rowValues.Add(sb.ToString());
                        sb.Clear();
                    }
                }
            }

            return rowValues.ToArray();
        }
開發者ID:joaoportela,項目名稱:mycouch,代碼行數:27,代碼來源:MultiTypeDeserializationJsonConverter.cs

示例2: Create

        /// <summary>
        /// Creates an instance of <see cref="JRaw"/> with the content of the reader's current token.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns>An instance of <see cref="JRaw"/> with the content of the reader's current token.</returns>
        public static JRaw Create(JsonReader reader)
        {
            using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
            using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.WriteToken(reader);

                return new JRaw(sw.ToString());
            }
        }
開發者ID:HaKDMoDz,項目名稱:eStd,代碼行數:15,代碼來源:JRaw.cs

示例3: Create

 public static JRaw Create(JsonReader reader)
 {
   using (StringWriter stringWriter = new StringWriter((IFormatProvider) CultureInfo.InvariantCulture))
   {
     using (JsonTextWriter jsonTextWriter = new JsonTextWriter((TextWriter) stringWriter))
     {
       jsonTextWriter.WriteToken(reader);
       return new JRaw((object) stringWriter.ToString());
     }
   }
 }
開發者ID:Zeludon,項目名稱:FEZ,代碼行數:11,代碼來源:JRaw.cs

示例4: formatButton_Click

        private void formatButton_Click(object sender, RoutedEventArgs e)
        {
            using (var stringReader = new StringReader(textBox.Text))
            using (var stringWriter = new StringWriter())
            {
                var jsonReader = new JsonTextReader(stringReader);
                var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented };
                jsonWriter.WriteToken(jsonReader);
                textBox.Text = stringWriter.ToString();
            }

        }
開發者ID:robert-impey,項目名稱:CodingExperiments,代碼行數:12,代碼來源:MainWindow.xaml.cs

示例5: ReadJsonAsString

        protected virtual object ReadJsonAsString(JsonReader reader)
        {
            var sb = new StringBuilder();

            using (var wo = new StringWriter(sb))
            {
                using (var w = new JsonTextWriter(wo))
                {
                    w.WriteToken(reader, true);
                }
            }

            return sb.ToString();
        }
開發者ID:joaoportela,項目名稱:mycouch,代碼行數:14,代碼來源:MultiTypeDeserializationJsonConverter.cs

示例6: GetInnerJson

        public static string GetInnerJson(this JsonReader jsonReader)
        {
            if (jsonReader == null)
                throw new ArgumentNullException("jsonReader");

            if (jsonReader.TokenType == JsonToken.PropertyName)
                jsonReader.Read();

            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            using (var jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.WriteToken(jsonReader);
            }

            return sb.ToString();
        }
開發者ID:haoasqui,項目名稱:ONLYOFFICE-Server,代碼行數:18,代碼來源:Extensions.cs

示例7: Import

        static void Import(AzureDataCatalog td, string exportedCatalogFilePath)
        {
            int totalAssetsImportSucceeded = 0;
            int totalAssetsImportFailed = 0;

            System.IO.StreamReader sr = new StreamReader(exportedCatalogFilePath);
            JsonTextReader reader = new JsonTextReader(sr);

            StringWriter sw = new StringWriter(new StringBuilder());

            JsonTextWriter jtw = new JsonTextWriter(sw);

            reader.Read();
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new Exception("Invalid Json. Expected StartObject");
            }

            reader.Read();
            if ((reader.TokenType != JsonToken.PropertyName) || (!reader.Value.ToString().Equals("catalog")))
            {
                throw new Exception("Invalid Json. Expected catalog array");
            }

            reader.Read();
            if (reader.TokenType != JsonToken.StartArray)
            {
                throw new Exception("Invalid Json. Expected StartArray");
            }

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndArray)
                    break;

                jtw.WriteToken(reader);

                JObject asset = JObject.Parse(sw.ToString());

                string id = asset["id"].ToString();
                asset.Remove("id");
                string[] idInfo = id.Split(new char[] { '/' });
                string newid;

                string UpdateResponse = td.Update(asset.ToString(), idInfo[idInfo.Length - 2], out newid);

                if ((UpdateResponse != null) && (!string.IsNullOrEmpty(newid)))
                {
                    totalAssetsImportSucceeded++;

                    if (totalAssetsImportSucceeded % 50 == 0)
                    {
                        System.Console.WriteLine(totalAssetsImportSucceeded + "Assets Imported Succesfully");
                    }
                }
                else
                {
                    totalAssetsImportFailed++;
                }

                //reset local variables for next iteration
                sw = new StringWriter(new StringBuilder());
                jtw = new JsonTextWriter(sw);

            }

            Console.WriteLine("Total Imported Success: " + totalAssetsImportSucceeded);
            Console.WriteLine("Total Imported Failed: " + totalAssetsImportFailed);
        }
開發者ID:Azure-Samples,項目名稱:data-catalog-dotnet-import-export,代碼行數:69,代碼來源:Program.cs

示例8: WriteTokenDirect_BadValue

        public void WriteTokenDirect_BadValue()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.WriteToken(JsonToken.StartArray);

                ExceptionAssert.Throws<FormatException>(() => { jsonWriter.WriteToken(JsonToken.Integer, "three"); }, "Input string was not in a correct format.");

                ExceptionAssert.Throws<ArgumentNullException>(() => { jsonWriter.WriteToken(JsonToken.Integer); }, @"Value cannot be null.
Parameter name: value");
            }
        }
開發者ID:Dylan-Jang,項目名稱:Newtonsoft.Json,代碼行數:15,代碼來源:JsonTextWriterTest.cs

示例9: WriteTokenDirect

        public void WriteTokenDirect()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.WriteToken(JsonToken.StartArray);
                jsonWriter.WriteToken(JsonToken.Integer, 1);
                jsonWriter.WriteToken(JsonToken.StartObject);
                jsonWriter.WriteToken(JsonToken.PropertyName, "string");
                jsonWriter.WriteToken(JsonToken.Integer, int.MaxValue);
                jsonWriter.WriteToken(JsonToken.EndObject);
                jsonWriter.WriteToken(JsonToken.EndArray);
            }

            Assert.AreEqual(@"[1,{""string"":2147483647}]", sb.ToString());
        }
開發者ID:Dylan-Jang,項目名稱:Newtonsoft.Json,代碼行數:18,代碼來源:JsonTextWriterTest.cs

示例10: JsonPrettify

 private static String JsonPrettify(string json)
 {
     using (var stringReader = new StringReader (json))
     using (var stringWriter = new StringWriter ())
     using (var jsonReader = new JsonTextReader (stringReader))
     using (var jsonWriter = new JsonTextWriter (stringWriter) { Formatting = Formatting.Indented }) {
         jsonWriter.WriteToken (jsonReader);
         return stringWriter.ToString ();
     }
 }
開發者ID:exzeo,項目名稱:webhooks-consumer-csharp-sample,代碼行數:10,代碼來源:SampleJsonProcessor.cs

示例11: DumpCollection

        private async Task DumpCollection(Uri collection, IReliableServiceQuery proxy, JsonTextWriter jsonWriter)
        {
            Log($"Dumping collection {collection}");

            var collectionData = await proxy.GetCollectionData(collection);

            jsonWriter.WriteStartArray();

            foreach (var item in collectionData)
            {
                using (var reader = new JsonTextReader(new StringReader(item)))
                {
                    jsonWriter.WriteToken(reader);
                }
            }

            jsonWriter.WriteEndArray();
        }
開發者ID:aelij,項目名稱:samples-servicefabric-reliablecollectionquery,代碼行數:18,代碼來源:Program.cs

示例12: TokenTypeOutOfRange

        public void TokenTypeOutOfRange()
        {
            using (JsonWriter jsonWriter = new JsonTextWriter(new StringWriter()))
            {
                ArgumentOutOfRangeException ex = ExceptionAssert.Throws<ArgumentOutOfRangeException>(() => jsonWriter.WriteToken((JsonToken)int.MinValue));
                Assert.AreEqual("token", ex.ParamName);

                ex = ExceptionAssert.Throws<ArgumentOutOfRangeException>(() => jsonWriter.WriteToken((JsonToken)int.MinValue, "test"));
                Assert.AreEqual("token", ex.ParamName);
            }
        }
開發者ID:janky-h,項目名稱:Newtonsoft.Json,代碼行數:11,代碼來源:JsonTextWriterTest.cs

示例13: WriteTokenNullCheck

 public void WriteTokenNullCheck()
 {
     using (JsonWriter jsonWriter = new JsonTextWriter(new StringWriter()))
     {
         ExceptionAssert.Throws<ArgumentNullException>(() => { jsonWriter.WriteToken(null); });
         ExceptionAssert.Throws<ArgumentNullException>(() => { jsonWriter.WriteToken(null, true); });
     }
 }
開發者ID:janky-h,項目名稱:Newtonsoft.Json,代碼行數:8,代碼來源:JsonTextWriterTest.cs

示例14: WriteComments

        public void WriteComments()
        {
            string json = @"//comment*//*hi*/
{//comment
Name://comment
true//comment after true" + StringUtils.CarriageReturn + @"
,//comment after comma" + StringUtils.CarriageReturnLineFeed + @"
""ExpiryDate""://comment" + StringUtils.LineFeed + @"
new
" + StringUtils.LineFeed +
                  @"Constructor
(//comment
null//comment
),
        ""Price"": 3.99,
        ""Sizes"": //comment
[//comment

          ""Small""//comment
]//comment
}//comment 
//comment 1 ";

            JsonTextReader r = new JsonTextReader(new StringReader(json));

            StringWriter sw = new StringWriter();
            JsonTextWriter w = new JsonTextWriter(sw);
            w.Formatting = Formatting.Indented;

            w.WriteToken(r, true);

            StringAssert.AreEqual(@"/*comment*//*hi*/*/{/*comment*/
  ""Name"": /*comment*/ true/*comment after true*//*comment after comma*/,
  ""ExpiryDate"": /*comment*/ new Constructor(
    /*comment*/,
    null
    /*comment*/
  ),
  ""Price"": 3.99,
  ""Sizes"": /*comment*/ [
    /*comment*/
    ""Small""
    /*comment*/
  ]/*comment*/
}/*comment *//*comment 1 */", sw.ToString());
        }
開發者ID:ZhouAnPing,項目名稱:Mail,代碼行數:46,代碼來源:JsonTextWriterTest.cs

示例15: TransformToSDataPayload

        private async Task TransformToSDataPayload(Stream targetStream)
        {
            using (var oContentStream = await originalContent.ReadAsStreamAsync())
            {
                JsonReader reader = null;
                JsonWriter outStream = null;
                int level = -1;
                try
                {
                    reader = new JsonTextReader(new StreamReader(oContentStream));
                    outStream = new JsonTextWriter(new StreamWriter(targetStream));

                    int respContentType = GetContentType();
                    bool searchForCollection = respContentType == ContentOdataCollection
                                               || respContentType == ContentApiCollection;

                    while (reader.Read())
                    {
                        if (searchForCollection && level == 0 && reader.TokenType == JsonToken.PropertyName)
                        {
                            if ((string)reader.Value == ParamCollectionNameApi || (string)reader.Value == ParamCollectionNameOdata)
                            {
                                WritePagingProperties(outStream);
                                outStream.WritePropertyName(ParamCollectionNameSdata);
                            }
                            else if ((string)reader.Value == ParamTotalresultOdata)
                                outStream.WritePropertyName(ParamTotalresultSdata);
                            else
                                outStream.WriteToken(reader, false);
                        }
                        else if (reader.TokenType == JsonToken.PropertyName)
                            outStream.WritePropertyName(CallOptionalMaps((string)reader.Value));
                        else
                        {
                            if (reader.TokenType == JsonToken.StartObject)
                                level++;
                            else if (reader.TokenType == JsonToken.EndObject)
                                level--;
                            outStream.WriteToken(reader, false);
                        }
                    }
                    outStream.Flush();
                }
                catch (Exception e)
                {
                    // TODO can we get the user and tenant context from here?
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                    if (outStream != null)
                        outStream.Close();

                }
            }
        }
開發者ID:jaretts,項目名稱:ODataSDataDemo,代碼行數:57,代碼來源:SDataContent.cs


注:本文中的Newtonsoft.Json.JsonTextWriter.WriteToken方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。