本文整理汇总了C#中Newtonsoft.Json.Linq.JArray.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:C# JArray.WriteTo方法的具体用法?C# JArray.WriteTo怎么用?C# JArray.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Linq.JArray
的用法示例。
在下文中一共展示了JArray.WriteTo方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JArray array = new JArray();
IList list = (IList)value;
if (list.Count > 0)
{
JArray keys = new JArray();
JObject first = JObject.FromObject(list[0], serializer);
foreach (JProperty prop in first.Properties())
{
keys.Add(new JValue(prop.Name));
}
array.Add(keys);
foreach (object item in list)
{
JObject obj = JObject.FromObject(item, serializer);
JArray itemValues = new JArray();
foreach (JProperty prop in obj.Properties())
{
itemValues.Add(prop.Value);
}
array.Add(itemValues);
}
}
array.WriteTo(writer);
}
示例2: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
ConverterPrecedenceClass c = (ConverterPrecedenceClass)value;
JToken j = new JArray(ConverterType, c.TestValue);
j.WriteTo(writer);
}
示例3: TryReplicatingData
private bool TryReplicatingData(string destination, JArray jsonDocuments)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(destination + "/replication/replicate?from=" + docDb.Configuration.ServerUrl);
request.UseDefaultCredentials = true;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Method = "POST";
using (var stream = request.GetRequestStream())
using (var streamWriter = new StreamWriter(stream))
{
jsonDocuments.WriteTo(new JsonTextWriter(streamWriter));
streamWriter.Flush();
stream.Flush();
}
using (request.GetResponse())
{
log.InfoFormat("Replicated {0} to {1}", jsonDocuments.Count, destination);
}
return true;
}
catch (WebException e)
{
var response = e.Response as HttpWebResponse;
if (response != null)
{
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var error = streamReader.ReadToEnd();
log.Warn("Replication to " + destination + " had failed\r\n" + error, e);
}
}
else
{
log.Warn("Replication to " + destination + " had failed", e);
}
return false;
}
catch (Exception e)
{
log.Warn("Replication to " + destination + " had failed", e);
return false;
}
}
示例4: FlushBatch
private static void FlushBatch(string instanceUrl, List<JObject> batch)
{
var sw = Stopwatch.StartNew();
long size;
using (var webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "application/json; charset=utf-8");
webClient.UseDefaultCredentials = true;
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
using (var stream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
using (var jsonTextWriter = new JsonTextWriter(streamWriter))
{
var commands = new JArray();
foreach (var doc in batch)
{
var metadata = doc.Value<JObject>("@metadata");
doc.Remove("@metadata");
commands.Add(new JObject(
new JProperty("Method", "PUT"),
new JProperty("Document", doc),
new JProperty("Metadata", metadata),
new JProperty("Key", metadata.Value<string>("@id"))
));
}
commands.WriteTo(jsonTextWriter);
jsonTextWriter.Flush();
streamWriter.Flush();
stream.Flush();
size = stream.Length;
using (var netStream = webClient.OpenWrite(instanceUrl + "bulk_docs", "POST"))
{
stream.WriteTo(netStream);
netStream.Flush();
}
}
}
}
Console.WriteLine("Wrote {0} documents [{1:#,#} kb] in {2:#,#} ms",
batch.Count, Math.Round((double)size/1024, 2), sw.ElapsedMilliseconds);
batch.Clear();
}
示例5: TryReplicationDocuments
private bool TryReplicationDocuments(string destination, JArray jsonDocuments)
{
try
{
log.DebugFormat("Starting to replicate {0} documents to {1}", jsonDocuments.Count, destination);
var request = (HttpWebRequest)WebRequest.Create(destination + "/replication/replicateDocs?from=" + UrlEncodedServerUrl());
request.UseDefaultCredentials = true;
request.ContentType = "application/json; charset=utf-8";
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Method = "POST";
using (var stream = request.GetRequestStream())
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
{
jsonDocuments.WriteTo(new JsonTextWriter(streamWriter));
streamWriter.Flush();
stream.Flush();
}
using (request.GetResponse())
{
log.InfoFormat("Replicated {0} documents to {1}", jsonDocuments.Count, destination);
}
return true;
}
catch (WebException e)
{
var response = e.Response as HttpWebResponse;
if (response != null)
{
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var error = streamReader.ReadToEnd();
log.Warn("Replication to " + destination + " had failed\r\n" + error, e);
}
}
else
{
log.Warn("Replication to " + destination + " had failed", e);
}
return false;
}
catch (Exception e)
{
log.Warn("Replication to " + destination + " had failed", e);
return false;
}
}
示例6: FlushBatch
private static void FlushBatch(string instanceUrl, List<JObject> batch)
{
using (var webClient = new WebClient())
{
webClient.UseDefaultCredentials = true;
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
using (var stream = webClient.OpenWrite(instanceUrl + "bulk_docs", "POST"))
using (var streamWriter = new StreamWriter(stream))
using (var jsonTextWriter = new JsonTextWriter(streamWriter))
{
var commands = new JArray();
foreach (var doc in batch)
{
var metadata = doc.Value<JObject>("@metadata");
doc.Remove("@metadata");
commands.Add(new JObject(
new JProperty("Method", "PUT"),
new JProperty("Document", doc),
new JProperty("Metadata", metadata),
new JProperty("Key", metadata.Value<string>("@id"))
));
}
commands.WriteTo(jsonTextWriter);
jsonTextWriter.Flush();
streamWriter.Flush();
}
}
batch.Clear();
}
示例7: WriteCommands
private static void WriteCommands(IEnumerable<Command> cmds, Stream log)
{
var array = new JArray();
foreach (var command in cmds)
{
var cmd = new JObject
{
{"type", (byte) command.Type},
{"key", command.Key},
{"dicId", command.DictionaryId}
};
if (command.Type == CommandType.Put)
{
cmd.Add("position", command.Position);
cmd.Add("size", command.Size);
}
array.Add(cmd);
}
array.WriteTo(new BsonWriter(log));
}