Demo TestProject1
Leksiqq edited this page Feb 9, 2024
·
5 revisions
Attention! This article, as well as this announcement, are automatically translated from Russian.
Shown is a usage example for writing data generated programmatically using LINQ
.
The sources are here.
Here we create a sequence of several objects of an anonymous type, which we write to ZooKeeper
, then read the json file.
// Create a query sequence
Random rnd = new Random();
string[] names = ["order", "subrequest", "surprise", "image"];
string[] statuses = ["done", "pending", "running"];
var query = Enumerable.Range(0, 4).Select(i => new
{
pos = i,
name = names[rnd.Next(0, names.Length)],
longValue = rnd.NextInt64(),
doubleValue = rnd.NextDouble(),
status = statuses[rnd.Next(0, statuses.Length)],
watched = rnd.Next(0, 2) == 0,
timestamp = new DateTime(rnd.NextInt64(DateTime.MinValue.Ticks, DateTime.MaxValue.Ticks)),
});
// Ready
// Connect with the keeper
...
// Eat
ZkJsonSerializer zkJson = new()
{
ZooKeeper = zk,
};
JsonSerializerOptions options = new()
{
WriteIndented = true,
};
options.Converters.Add(zkJson);
// Serialize the sequence into a JsonElement, and deserialize it into a keeper
JsonSerializer.Deserialize<ZkStub>(JsonSerializer.SerializeToElement(query, options), options);
// Reset the factory
zkJson.Reset();
// Read from keeper
MemoryStream ms = new();
JsonSerializer.Serialize(ms, ZkStub.Instance, options);
ms.Flush();
ms.Position = 0;
Console.WriteLine(new StreamReader(ms).ReadToEnd());
Something has appeared in the keeper!
Previously: (Demo:ZkJsonDemo) Start:(Overview)