Пример #1
0
        public async ValueTask WriteAsync(ShapeStoreState shapeState, StreamWriter target)
        {
            JObject rawObject = new JObject
            {
                [ID_KEY] = shapeState.Id
            };

            foreach ((string propName, object value) in shapeState.Props)
            {
                rawObject[propName] = JToken.FromObject(value, serializer);
            }

            serializer.Serialize(target, rawObject);

            target.WriteLine();
            target.WriteLine(BLOK_SEPARATOR);

            await target.FlushAsync();
        }
Пример #2
0
        public async ValueTask <ShapeStoreState> ReadAsync(TextReader source, Dictionary <int, Dictionary <string, Type> > shapesInfo)
        {
            return(await Task.Run(() =>
            {
                var json = ReadBlock(source);

                //достигнуто дно ридера
                if (string.IsNullOrWhiteSpace(json))
                {
                    return null;
                }

                JObject rawObject = JObject.Parse(json);

                var result = new ShapeStoreState
                {
                    Id = rawObject[ID_KEY].ToObject <int>(),
                };

                if (!shapesInfo.ContainsKey(result.Id))
                {
                    throw new ArgumentException($"Unknown shape id = {result.Id}");
                }

                var propsInfo = shapesInfo[result.Id];


                foreach ((string propName, Type propType) in propsInfo)
                {
                    var token = rawObject[propName];

                    if (token != null)
                    {
                        result.Props[propName] = token.ToObject(propType, serializer);
                    }
                }

                return result;
            }));
        }