public static BaseGenerator Load(JsonElement root) { BaseGenerator generator; JsonElement typeGenerator = root.GetProperty("type"); string name = root.GetProperty("name").GetString(); JsonElement sequenceElement = root.GetProperty("sequence"); int n = root.GetProperty("count").GetInt32(); double[] sequence = new double[n]; int ndx = 0; foreach (var number in sequenceElement.EnumerateArray()) { sequence[ndx] = number.GetDouble(); ndx++; } switch ((GeneratorType)typeGenerator.GetInt32()) { case GeneratorType.BASE: generator = new BaseGenerator(name, n); break; case GeneratorType.RAND: generator = new RandomGenerator(name, n); break; case GeneratorType.STEP: double step = root.GetProperty("step").GetDouble(); generator = new GeneratorWithStep(name, n, 0, step); break; default: throw new ArgumentException("invalid json"); } for (int i = 0; i < ndx; i++) { generator.Push(sequence[i]); } JsonElement generatorsElement = root.GetProperty("generators"); foreach (var generatorElement in generatorsElement.EnumerateArray()) { BaseGenerator children = BaseGenerator.Load(generatorElement); generator.Add(children); } return(generator); }
public static BaseGenerator Load(Stream stream) { if (!stream.CanRead) { throw new ArgumentException("invalid stream"); } UTF8Encoding utf8 = new UTF8Encoding(true); byte[] array = new byte[stream.Length]; stream.Read(array, 0, array.Length); string json = utf8.GetString(array); BaseGenerator generator; using (JsonDocument document = JsonDocument.Parse(json)) { JsonElement root = document.RootElement; generator = BaseGenerator.Load(root); } return(generator); }
public void Add(BaseGenerator generator) { _generators.Add(generator); }