Exemplo n.º 1
0
        private void DeserializeAndApplyChangeValue(Change change, DeserializingContext context)
        {
            var path = change.Path.Split('/');

            if (path[1].Length == 0)
            {
                RootChange(change, context);
                return;
            }
            var handler = _rootHandler;

            for (int q = 1; q < path.Length - 1; q++)
            {
                if (handler.TryGetValue(path[q], out var obj))
                {
                    handler = ((IEntityHandlerProvider)obj).GetHandler();
                }
                else
                {
                    change.Ignored = true;
                    DeserializeIgnoredValue(context, change, "Path not reachable");
                    Log.Warning($"Change '{change.Path}:{change.Type}' is ignored");
                    return;
                }
            }

            change.Key = path[path.Length - 1];
            handler.DeserializeAndApplyChangeValue(change, context);
            change.Handler = handler;
        }
Exemplo n.º 2
0
        private Change DeserializeAndApplyChange(DeserializingContext context)
        {
            var result = new Change();

            while (context.Reader.Read() && context.Reader.TokenType != JsonToken.EndObject)
            {
                if (!(context.Reader.Value is string))
                {
                    Log.Error($"Unexpected serialization '{context.Reader.Value?.GetType()}=context.Reader.Value'");
                }
                string property = (string)context.Reader.Value;
                context.Reader.Read();

                switch (property)
                {
                case "op":
                    result.Type = (ChangeType)Enum.Parse(typeof(ChangeType), (string)context.Reader.Value, true);
                    break;

                case "path":
                    result.Path = (string)context.Reader.Value;
                    if (result.Type == ChangeType.Remove)
                    {
                        DeserializeAndApplyChangeValue(result, context);
                    }
                    break;

                case "value":
                    DeserializeAndApplyChangeValue(result, context);
                    break;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        private void RootChange(Change change, DeserializingContext context)
        {
            _root        = DeserializeAndApplyReplace(null, "", _root, _configurations[_rootHandler.ProxyType], change, context);
            _rootHandler = ((IEntityHandlerProvider)_root).GetHandler();

            RootChanged?.Invoke(this, EventArgs.Empty);
        }
Exemplo n.º 4
0
 public List <Change> ApplyChanges(ModelClient sourceClient, string changesData)
 {
     lock (this)
     {
         var           otherClients = _clients.Where(s => s != sourceClient).ToList();
         List <Change> changes      = new List <Change>();
         using (var context = DeserializingContext.FromString(changesData, _serializer))
         {
             context.Reader.Read();
             while (context.Reader.Read() && context.Reader.TokenType != JsonToken.EndArray)
             {
                 var change = DeserializeAndApplyChange(context);
                 if (!change.Ignored) // ignored change due to unsync
                 {
                     Subscriptions.NotifyChange(sourceClient, change.Type, change.Handler, change.Key, change.Value);
                     if (change.OldValue != null)
                     {
                         DetachOldItem(sourceClient, change.OldValue);
                     }
                     if (change.Value != null)
                     {
                         AttachNewItem(sourceClient, change.Value);
                     }
                     otherClients.ForEach(client => AddChange(client, change.Type, change.Path, change.Handler, change.Key, change.Value));
                     changes.Add(change);
                 }
             }
         }
         return(changes);
     }
 }
Exemplo n.º 5
0
 public object Deserialize(Type type, string str)
 {
     using (var context = DeserializingContext.FromString(str, _serializer))
     {
         context.Reader.Read();
         return(DeserializeMemeberValue(_configurations[type], null, null, context));
     }
 }
Exemplo n.º 6
0
 public static DeserializingContext FromString(string content, JsonSerializer serializer)
 {
     var result = new DeserializingContext();
     result._textReader = new StringReader(content);
     result.Reader = new JsonTextReader(result._textReader);
     result.Serializer = serializer;
     return result;
 }
Exemplo n.º 7
0
 public void Deserialize(DeserializingContext context)
 {
     while (context.Reader.Read() && context.Reader.TokenType != Newtonsoft.Json.JsonToken.EndObject)
     {
         string key = (string)context.Reader.Value;
         context.Reader.Read();
         var o = _manager.DeserializeMemeberValue(_typeConfig.ValueType, this, key, context);
         _inner.Add(key, o);
     }
 }
Exemplo n.º 8
0
        public void DeserializeIgnoredValue(DeserializingContext context, Change change, string reason)
        {
            var res = context.Serializer.Deserialize(context.Reader);

            if (change != null)
            {
                Log.Warning($"Deserializing ignored value {change.Path}={res} ({change.Type}), reason:{reason}");
            }
            else
            {
                Log.Warning($"Deserializing ignored value, reason:{reason}");
            }
        }
        public void DeserializeAndApplyChangeValue(Change change, DeserializingContext context)
        {
            var property = change.Key;

            if (_values.TryGetValue(property, out var value))
            {
                switch (change.Type)
                {
                case ChangeType.Add:
                    _manager.DeserializeIgnoredValue(context, change, "Unsync on add");
                    break;

                case ChangeType.Remove:
                    _manager.DeserializeAndApplyRemove(value, change);
                    _values.Remove(property);
                    _propertyChangedHandler?.Invoke(Proxy, new PropertyChangedEventArgs(property));
                    break;

                case ChangeType.Replace:
                    _values[property] = _manager.DeserializeAndApplyReplace(this, property, value, _typeConfig.Members[property].TypeConfiguration, change, context);
                    _propertyChangedHandler?.Invoke(Proxy, new PropertyChangedEventArgs(property));
                    break;
                }
            }
            else
            {
                if (_typeConfig.Members.TryGetValue(property, out var memConfig))
                {
                    if (change.Type == ChangeType.Add)
                    {
                        _values.Add(property, _manager.DeserializeAndApplyAdd(this, property, memConfig.TypeConfiguration, change, context));
                        _propertyChangedHandler?.Invoke(Proxy, new PropertyChangedEventArgs(property));
                    }
                    else
                    {
                        if (change.Type == ChangeType.Replace)
                        {
                            _manager.DeserializeIgnoredValue(context, change, "Unsync on replace");
                        }
                    }
                }
                else
                {
                    if (change.Type != ChangeType.Remove)
                    {
                        _manager.DeserializeIgnoredValue(context, change, "Unknown property");
                    }
                }
            }
        }
 public void Deserialize(DeserializingContext context)
 {
     while (context.Reader.Read() && context.Reader.TokenType != Newtonsoft.Json.JsonToken.EndObject)
     {
         string property = (string)context.Reader.Value;
         context.Reader.Read();
         if (_typeConfig.Members.TryGetValue(property, out var memberConfig))
         {
             _values[property] = _manager.DeserializeMemeberValue(memberConfig.TypeConfiguration, this, property, context);
         }
         else
         {
             _manager.DeserializeIgnoredValue(context, null, $"Unsync on Deserialize '{property}'");
         }
     }
 }
Exemplo n.º 11
0
        public void DeserializeAndApplyChangeValue(Change change, DeserializingContext context)
        {
            var property = change.Key;

            if (_inner.TryGetValue(property, out var oldValue))
            {
                switch (change.Type)
                {
                case ChangeType.Add:
                    _manager.DeserializeIgnoredValue(context, change, "Unsync on dict add");
                    break;

                case ChangeType.Remove:
                    _manager.DeserializeAndApplyRemove(oldValue, change);
                    InternalRemove(property, oldValue);
                    break;

                case ChangeType.Replace:
                    InternalReplace(property, oldValue, _manager.DeserializeAndApplyReplace(this, property, oldValue, _typeConfig.ValueType, change, context));
                    break;
                }
            }
            else
            {
                if (change.Type == ChangeType.Add)
                {
                    InternalAdd(property, _manager.DeserializeAndApplyAdd(this, property, _typeConfig.ValueType, change, context));
                }
                else
                {
                    if (change.Type == ChangeType.Replace)
                    {
                        _manager.DeserializeIgnoredValue(context, change, "Unsync on dict replace");
                    }
                }
            }
        }
Exemplo n.º 12
0
 public object DeserializeAndApplyReplace(IEntityHandler parent, string key, object oldValue, TypeConfiguration typeConfig, Change change, DeserializingContext context)
 {
     change.OldValue = oldValue;
     SetObjectParent(oldValue, null, null);
     change.Value = DeserializeMemeberValue(typeConfig, parent, key, context);
     return(change.Value);
 }
Exemplo n.º 13
0
 public object DeserializeAndApplyAdd(IEntityHandler parent, string key, TypeConfiguration typeConfig, Change change, DeserializingContext context)
 {
     change.Value = DeserializeMemeberValue(typeConfig, parent, key, context);
     return(change.Value);
 }
Exemplo n.º 14
0
 public object DeserializeMemeberValue(TypeConfiguration typeConfig, IEntityHandler parent, string property, DeserializingContext context)
 {
     if (typeConfig.IsExternalType)
     {
         return(context.Serializer.Deserialize(context.Reader, typeConfig.Type));
     }
     else if (typeConfig.Creator != null)
     {
         var result  = typeConfig.Creator(parent, property);
         var handler = (IEntityHandlerProvider)result;
         handler.GetHandler().Deserialize(context);
         return(result);
     }
     else
     {
         throw new Exception();
     }
 }