当前位置: 首页>>代码示例>>C#>>正文


C# JsonTextWriter.WriteValue方法代码示例

本文整理汇总了C#中Raven.Imports.Newtonsoft.Json.JsonTextWriter.WriteValue方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextWriter.WriteValue方法的具体用法?C# JsonTextWriter.WriteValue怎么用?C# JsonTextWriter.WriteValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Raven.Imports.Newtonsoft.Json.JsonTextWriter的用法示例。


在下文中一共展示了JsonTextWriter.WriteValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WriteIConvertable

    public void WriteIConvertable()
    {
      var sw = new StringWriter();
      JsonTextWriter writer = new JsonTextWriter(sw);
      writer.WriteValue(new ConvertibleInt(1));

      Assert.AreEqual("1", sw.ToString());
    }
开发者ID:WimVergouwe,项目名称:ravendb,代码行数:8,代码来源:JsonTextWriterTest.cs

示例2: StreamToClient

		private void StreamToClient(long id, SubscriptionActions subscriptions, Stream stream)
		{
			var sentDocuments = false;

			using (var streamWriter = new StreamWriter(stream))
			using (var writer = new JsonTextWriter(streamWriter))
			{
				var options = subscriptions.GetBatchOptions(id);

				writer.WriteStartObject();
				writer.WritePropertyName("Results");
				writer.WriteStartArray();

				using (var cts = new CancellationTokenSource())
				using (var timeout = cts.TimeoutAfter(DatabasesLandlord.SystemConfiguration.DatabaseOperationTimeout))
				{
                    Etag lastProcessedDocEtag = null;

					var batchSize = 0;
					var batchDocCount = 0;
					var hasMoreDocs = false;

					var config = subscriptions.GetSubscriptionConfig(id);
					var startEtag = config.AckEtag;
					var criteria = config.Criteria;

                    Action<JsonDocument> addDocument = doc =>
                    {
                        timeout.Delay();

                        if (options.MaxSize.HasValue && batchSize >= options.MaxSize)
                            return;

                        if (batchDocCount >= options.MaxDocCount)
                            return;

                        lastProcessedDocEtag = doc.Etag;

                        if (doc.Key.StartsWith("Raven/", StringComparison.InvariantCultureIgnoreCase))
                            return;

                        if (MatchCriteria(criteria, doc) == false)
                            return;

                        doc.ToJson().WriteTo(writer);
                        writer.WriteRaw(Environment.NewLine);

                        batchSize += doc.SerializedSizeOnDisk;
                        batchDocCount++;
                    };

                    int nextStart = 0;

					do
					{
						Database.TransactionalStorage.Batch(accessor =>
						{
							// we may be sending a LOT of documents to the user, and most 
							// of them aren't going to be relevant for other ops, so we are going to skip
							// the cache for that, to avoid filling it up very quickly
							using (DocumentCacher.SkipSettingDocumentsInDocumentCache())
							{    
                                if (!string.IsNullOrWhiteSpace(criteria.KeyStartsWith))
                                {
                                    Database.Documents.GetDocumentsWithIdStartingWith(criteria.KeyStartsWith, options.MaxDocCount - batchDocCount, startEtag, cts.Token, addDocument);
                                }
                                else
                                {
                                    Database.Documents.GetDocuments(-1, options.MaxDocCount - batchDocCount, startEtag, cts.Token, addDocument);
                                }
							}

							if (lastProcessedDocEtag == null)
								hasMoreDocs = false;
							else
							{
								var lastDocEtag = accessor.Staleness.GetMostRecentDocumentEtag();
								hasMoreDocs = EtagUtil.IsGreaterThan(lastDocEtag, lastProcessedDocEtag);

								startEtag = lastProcessedDocEtag;
							}
						});
					} while (hasMoreDocs && batchDocCount < options.MaxDocCount && (options.MaxSize.HasValue == false || batchSize < options.MaxSize));

					writer.WriteEndArray();

					if (batchDocCount > 0)
					{
						writer.WritePropertyName("LastProcessedEtag");
						writer.WriteValue(lastProcessedDocEtag.ToString());

						sentDocuments = true;
					}

					writer.WriteEndObject();
					writer.Flush();
				}
			}

			if (sentDocuments)
//.........这里部分代码省略.........
开发者ID:jrusbatch,项目名称:ravendb,代码行数:101,代码来源:SubscriptionsController.cs

示例3: StreamToClient

		private void StreamToClient(Stream stream, string startsWith, int start, int pageSize, Etag etag, string matches, int nextPageStart, string skipAfter)
		{
			using (var cts = new CancellationTokenSource())
			using (var timeout = cts.TimeoutAfter(DatabasesLandlord.SystemConfiguration.DatabaseOperationTimeout))
			using (var writer = new JsonTextWriter(new StreamWriter(stream)))
			{
				writer.WriteStartObject();
				writer.WritePropertyName("Results");
				writer.WriteStartArray();

				Database.TransactionalStorage.Batch(accessor =>
				{
					// we may be sending a LOT of documents to the user, and most 
					// of them aren't going to be relevant for other ops, so we are going to skip
					// the cache for that, to avoid filling it up very quickly
					using (DocumentCacher.SkipSettingDocumentsInDocumentCache())
					{
						if (string.IsNullOrEmpty(startsWith))
							Database.Documents.GetDocuments(start, pageSize, etag, cts.Token, doc =>
							{
								timeout.Delay();
								doc.WriteTo(writer);
                                writer.WriteRaw(Environment.NewLine);
							});
						else
						{
							var nextPageStartInternal = nextPageStart;

							Database.Documents.GetDocumentsWithIdStartingWith(startsWith, matches, null, start, pageSize, cts.Token, ref nextPageStartInternal, doc =>
							{
								timeout.Delay();
								doc.WriteTo(writer);
                                writer.WriteRaw(Environment.NewLine);
							},skipAfter: skipAfter);

							nextPageStart = nextPageStartInternal;
						}
					}
				});

				writer.WriteEndArray();
				writer.WritePropertyName("NextPageStart");
				writer.WriteValue(nextPageStart);
				writer.WriteEndObject();
				writer.Flush();
			}
		}
开发者ID:bbqchickenrobot,项目名称:ravendb,代码行数:47,代码来源:StreamsController.cs

示例4: WriteReadBoundaryDecimals

    public void WriteReadBoundaryDecimals()
    {
      StringWriter sw = new StringWriter();
      JsonTextWriter writer = new JsonTextWriter(sw);

      writer.WriteStartArray();
      writer.WriteValue(decimal.MaxValue);
      writer.WriteValue(decimal.MinValue);
      writer.WriteEndArray();

      string json = sw.ToString();

      StringReader sr = new StringReader(json);
      JsonTextReader reader = new JsonTextReader(sr);

      Assert.IsTrue(reader.Read());

      decimal? max = reader.ReadAsDecimal();
      Assert.AreEqual(decimal.MaxValue, max);

      decimal? min = reader.ReadAsDecimal();
      Assert.AreEqual(decimal.MinValue, min);

      Assert.IsTrue(reader.Read());
    }
开发者ID:WimVergouwe,项目名称:ravendb,代码行数:25,代码来源:JsonTextReaderTest.cs

示例5: WriteIndexes

 private void WriteIndexes(JsonTextWriter jsonWriter)
 {
     var indexDefinitionsBasePath = Path.Combine(baseDirectory, indexDefinitionFolder);
     var indexes = Directory.GetFiles(indexDefinitionsBasePath, "*.index");
     int currentIndexCount = 0;
     foreach (var file in indexes)
     {
         var ravenObj = RavenJObject.Parse(File.ReadAllText(file));
         jsonWriter.WriteStartObject();
         jsonWriter.WritePropertyName("name");
         jsonWriter.WriteValue(ravenObj.Value<string>("Name"));
         jsonWriter.WritePropertyName("definition");
         ravenObj.WriteTo(jsonWriter);
         jsonWriter.WriteEndObject();
         currentIndexCount++;
         ReportProgress("indexes", currentIndexCount, indexes.Count());
     }
 }
开发者ID:IdanHaim,项目名称:ravendb,代码行数:18,代码来源:StorageExporter.cs

示例6: QuoteChar

    public void QuoteChar()
    {
      StringWriter sw = new StringWriter();
      JsonTextWriter writer = new JsonTextWriter(sw);
      writer.Formatting = Formatting.Indented;
      writer.QuoteChar = '\'';

      writer.WriteStartArray();

      writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
      writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));

      writer.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
      writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc));
      writer.WriteValue(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero));

      writer.WriteValue(new byte[] { 1, 2, 3 });
      writer.WriteValue(TimeSpan.Zero);
      writer.WriteValue(new Uri("http://www.google.com/"));
      writer.WriteValue(Guid.Empty);

      writer.WriteEnd();

      Assert.AreEqual(@"[
  '2000-01-01T01:01:01Z',
  '2000-01-01T01:01:01+00:00',
  '\/Date(946688461000)\/',
  '\/Date(946688461000+0000)\/',
  'AQID',
  '00:00:00',
  'http://www.google.com/',
  '00000000-0000-0000-0000-000000000000'
]", sw.ToString());
    }
开发者ID:925coder,项目名称:ravendb,代码行数:34,代码来源:JsonTextWriterTest.cs

示例7: HtmlStringEscapeHandling

    public void HtmlStringEscapeHandling()
    {
      StringWriter sw = new StringWriter();
      JsonTextWriter writer = new JsonTextWriter(sw)
      {
        StringEscapeHandling = StringEscapeHandling.EscapeHtml
      };

      string script = @"<script type=""text/javascript"">alert('hi');</script>";

      writer.WriteValue(script);

      string json = sw.ToString();

      Assert.AreEqual(@"""\u003cscript type=\u0022text/javascript\u0022\u003ealert(\u0027hi\u0027);\u003c/script\u003e""", json);

      JsonTextReader reader = new JsonTextReader(new StringReader(json));
      
      Assert.AreEqual(script, reader.ReadAsString());

      //Console.WriteLine(HttpUtility.HtmlEncode(script));

      //System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();
      //Console.WriteLine(s.Serialize(new { html = script }));
    }
开发者ID:925coder,项目名称:ravendb,代码行数:25,代码来源:JsonTextWriterTest.cs

示例8: Path

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

      string text = "Hello world.";
      byte[] data = Encoding.UTF8.GetBytes(text);

      using (JsonTextWriter writer = new JsonTextWriter(sw))
      {
        writer.Formatting = Formatting.Indented;

        writer.WriteStartArray();
        Assert.AreEqual("", writer.Path);
        writer.WriteStartObject();
        Assert.AreEqual("[0]", writer.Path);
        writer.WritePropertyName("Property1");
        Assert.AreEqual("[0].Property1", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1", writer.Path);
        writer.WriteValue(1);
        Assert.AreEqual("[0].Property1[0]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1[1]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1[1][0]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1[1][0][0]", writer.Path);
        writer.WriteEndObject();
        Assert.AreEqual("[0]", writer.Path);
        writer.WriteStartObject();
        Assert.AreEqual("[1]", writer.Path);
        writer.WritePropertyName("Property2");
        Assert.AreEqual("[1].Property2", writer.Path);
        writer.WriteStartConstructor("Constructor1");
        Assert.AreEqual("[1].Property2", writer.Path);
        writer.WriteNull();
        Assert.AreEqual("[1].Property2[0]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[1].Property2[1]", writer.Path);
        writer.WriteValue(1);
        Assert.AreEqual("[1].Property2[1][0]", writer.Path);
        writer.WriteEnd();
        Assert.AreEqual("[1].Property2[1]", writer.Path);
        writer.WriteEndObject();
        Assert.AreEqual("[1]", writer.Path);
        writer.WriteEndArray();
        Assert.AreEqual("", writer.Path);
      }

      Assert.AreEqual(@"[
  {
    ""Property1"": [
      1,
      [
        [
          []
        ]
      ]
    ]
  },
  {
    ""Property2"": new Constructor1(
      null,
      [
        1
      ]
    )
  }
]", sb.ToString());
    }
开发者ID:925coder,项目名称:ravendb,代码行数:71,代码来源:JsonTextWriterTest.cs

示例9: State

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

      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
      {
        Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);

        jsonWriter.WriteStartObject();
        Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
        Assert.AreEqual("", jsonWriter.Path);

        jsonWriter.WritePropertyName("CPU");
        Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
        Assert.AreEqual("CPU", jsonWriter.Path);

        jsonWriter.WriteValue("Intel");
        Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
        Assert.AreEqual("CPU", jsonWriter.Path);

        jsonWriter.WritePropertyName("Drives");
        Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
        Assert.AreEqual("Drives", jsonWriter.Path);

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

        jsonWriter.WriteValue("DVD read/writer");
        Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
        Assert.AreEqual("Drives[0]", jsonWriter.Path);

        jsonWriter.WriteEnd();
        Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
        Assert.AreEqual("Drives", jsonWriter.Path);

        jsonWriter.WriteEndObject();
        Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
        Assert.AreEqual("", jsonWriter.Path);
      }
    }
开发者ID:925coder,项目名称:ravendb,代码行数:41,代码来源:JsonTextWriterTest.cs

示例10: Indenting

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

      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
      {
        jsonWriter.Formatting = Formatting.Indented;

        jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("CPU");
        jsonWriter.WriteValue("Intel");
        jsonWriter.WritePropertyName("PSU");
        jsonWriter.WriteValue("500W");
        jsonWriter.WritePropertyName("Drives");
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue("DVD read/writer");
        jsonWriter.WriteComment("(broken)");
        jsonWriter.WriteValue("500 gigabyte hard drive");
        jsonWriter.WriteValue("200 gigabype hard drive");
        jsonWriter.WriteEnd();
        jsonWriter.WriteEndObject();
        Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
      }

      // {
      //   "CPU": "Intel",
      //   "PSU": "500W",
      //   "Drives": [
      //     "DVD read/writer"
      //     /*(broken)*/,
      //     "500 gigabyte hard drive",
      //     "200 gigabype hard drive"
      //   ]
      // }

      string expected = @"{
  ""CPU"": ""Intel"",
  ""PSU"": ""500W"",
  ""Drives"": [
    ""DVD read/writer""
    /*(broken)*/,
    ""500 gigabyte hard drive"",
    ""200 gigabype hard drive""
  ]
}";
      string result = sb.ToString();

      Assert.AreEqual(expected, result);
    }
开发者ID:925coder,项目名称:ravendb,代码行数:50,代码来源:JsonTextWriterTest.cs

示例11: CloseWithRemainingContent

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

      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
      {
        jsonWriter.Formatting = Formatting.Indented;

        jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("CPU");
        jsonWriter.WriteValue("Intel");
        jsonWriter.WritePropertyName("PSU");
        jsonWriter.WriteValue("500W");
        jsonWriter.WritePropertyName("Drives");
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue("DVD read/writer");
        jsonWriter.WriteComment("(broken)");
        jsonWriter.WriteValue("500 gigabyte hard drive");
        jsonWriter.WriteValue("200 gigabype hard drive");
        jsonWriter.Close();
      }

      string expected = @"{
  ""CPU"": ""Intel"",
  ""PSU"": ""500W"",
  ""Drives"": [
    ""DVD read/writer""
    /*(broken)*/,
    ""500 gigabyte hard drive"",
    ""200 gigabype hard drive""
  ]
}";
      string result = sb.ToString();

      Assert.AreEqual(expected, result);
    }
开发者ID:925coder,项目名称:ravendb,代码行数:37,代码来源:JsonTextWriterTest.cs

示例12: StringEscaping

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

      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
      {
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue(@"""These pretzels are making me thirsty!""");
        jsonWriter.WriteValue("Jeff's house was burninated.");
        jsonWriter.WriteValue(@"1. You don't talk about fight club.
2. You don't talk about fight club.");
        jsonWriter.WriteValue("35% of\t statistics\n are made\r up.");
        jsonWriter.WriteEndArray();
      }

      string expected = @"[""\""These pretzels are making me thirsty!\"""",""Jeff's house was burninated."",""1. You don't talk about fight club.\r\n2. You don't talk about fight club."",""35% of\t statistics\n are made\r up.""]";
      string result = sb.ToString();

      Console.WriteLine("StringEscaping");
      Console.WriteLine(result);

      Assert.AreEqual(expected, result);
    }
开发者ID:925coder,项目名称:ravendb,代码行数:24,代码来源:JsonTextWriterTest.cs

示例13: WriteValueObjectWithUnsupportedValue

 public void WriteValueObjectWithUnsupportedValue()
 {
   ExceptionAssert.Throws<JsonWriterException>(
     @"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation. Path ''.",
     () =>
     {
       StringWriter sw = new StringWriter();
       using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
       {
         jsonWriter.WriteStartArray();
         jsonWriter.WriteValue(new Version(1, 1, 1, 1));
         jsonWriter.WriteEndArray();
       }
     });
 }
开发者ID:925coder,项目名称:ravendb,代码行数:15,代码来源:JsonTextWriterTest.cs

示例14: WriteValueObjectWithNullable

    public void WriteValueObjectWithNullable()
    {
      StringWriter sw = new StringWriter();
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
      {
        char? value = 'c';

        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue((object)value);
        jsonWriter.WriteEndArray();
      }

      string json = sw.ToString();
      string expected = @"[""c""]";

      Assert.AreEqual(expected, json);
    }
开发者ID:925coder,项目名称:ravendb,代码行数:17,代码来源:JsonTextWriterTest.cs

示例15: NullableValueFormatting

    public void NullableValueFormatting()
    {
      StringWriter sw = new StringWriter();
      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
      {
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue((char?)null);
        jsonWriter.WriteValue((char?)'c');
        jsonWriter.WriteValue((bool?)null);
        jsonWriter.WriteValue((bool?)true);
        jsonWriter.WriteValue((byte?)null);
        jsonWriter.WriteValue((byte?)1);
        jsonWriter.WriteValue((sbyte?)null);
        jsonWriter.WriteValue((sbyte?)1);
        jsonWriter.WriteValue((short?)null);
        jsonWriter.WriteValue((short?)1);
        jsonWriter.WriteValue((ushort?)null);
        jsonWriter.WriteValue((ushort?)1);
        jsonWriter.WriteValue((int?)null);
        jsonWriter.WriteValue((int?)1);
        jsonWriter.WriteValue((uint?)null);
        jsonWriter.WriteValue((uint?)1);
        jsonWriter.WriteValue((long?)null);
        jsonWriter.WriteValue((long?)1);
        jsonWriter.WriteValue((ulong?)null);
        jsonWriter.WriteValue((ulong?)1);
        jsonWriter.WriteValue((double?)null);
        jsonWriter.WriteValue((double?)1.1);
        jsonWriter.WriteValue((float?)null);
        jsonWriter.WriteValue((float?)1.1);
        jsonWriter.WriteValue((decimal?)null);
        jsonWriter.WriteValue((decimal?)1.1m);
        jsonWriter.WriteValue((DateTime?)null);
        jsonWriter.WriteValue((DateTime?)new DateTime(JsonConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc));
#if !PocketPC && !NET20
        jsonWriter.WriteValue((DateTimeOffset?)null);
        jsonWriter.WriteValue((DateTimeOffset?)new DateTimeOffset(JsonConvert.InitialJavaScriptDateTicks, TimeSpan.Zero));
#endif
        jsonWriter.WriteEndArray();
      }

      string json = sw.ToString();
      string expected;

#if !PocketPC && !NET20
      expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""1970-01-01T00:00:00Z"",null,""1970-01-01T00:00:00+00:00""]";
#else
      expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""1970-01-01T00:00:00Z""]";
#endif

      Assert.AreEqual(expected, json);
    }
开发者ID:925coder,项目名称:ravendb,代码行数:52,代码来源:JsonTextWriterTest.cs


注:本文中的Raven.Imports.Newtonsoft.Json.JsonTextWriter.WriteValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。