Exemple #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jsonObject    = JObject.Load(reader);
            var samplerOption = new SamplerOptions {
                metadata = jsonObject["metadata"].ToObject <StandardMetadata>()
            };

            if (jsonObject.ContainsKey("constant"))
            {
                samplerOption.defaultSampler = jsonObject["constant"].ToObject <ConstantSampler>();
            }
            else if (jsonObject.ContainsKey("uniform"))
            {
                samplerOption.defaultSampler = jsonObject["uniform"].ToObject <UniformSampler>();
            }
            else if (jsonObject.ContainsKey("normal"))
            {
                samplerOption.defaultSampler = jsonObject["normal"].ToObject <NormalSampler>();
            }
            else
            {
                throw new KeyNotFoundException("No valid SamplerOption key type found");
            }

            return(samplerOption);
        }
        static SamplerOptions SerializeSampler(ISampler sampler)
        {
            var samplerData = new SamplerOptions();

            if (sampler is Samplers.ConstantSampler constantSampler)
            {
                samplerData.defaultSampler = new ConstantSampler
                {
                    value = constantSampler.value
                }
            }
            ;
            else if (sampler is Samplers.UniformSampler uniformSampler)
            {
                samplerData.defaultSampler = new UniformSampler
                {
                    min = uniformSampler.range.minimum,
                    max = uniformSampler.range.maximum
                }
            }
            ;
            else if (sampler is Samplers.NormalSampler normalSampler)
            {
                samplerData.defaultSampler = new NormalSampler
                {
                    min  = normalSampler.range.minimum,
                    max  = normalSampler.range.maximum,
                    mean = normalSampler.mean,
                    standardDeviation = normalSampler.standardDeviation
                }
            }
            ;
            else
            {
                throw new ArgumentException($"Invalid sampler type ({sampler.GetType()})");
            }
            return(samplerData);
        }