private bool CanDeserialize(IList <INodeData> nodes, out Type type, out Metadata metadata, DeserializationContext context)
        {
            if (nodes[0].Type != NodeType.OBJECT)
            {
                type     = null;
                metadata = null;
                return(false);
            }

            INodeData typeNode = nodes[0].SubNodes[1].SubNodes.FirstOrDefault(n => n.Type == NodeType.METADATA);

            CustomSerializer serializer = new CustomSerializer(typeof(Metadata));

            metadata = (Metadata)serializer.ReadObjectData(new List <INodeData>()
            {
                typeNode
            }, context, -1);

            if (!CustomType.IsAssignableFrom(metadata.Type))
            {
                type = metadata.Type;
                return(false);
            }

            type = metadata.Type;
            return(true);
        }
Ejemplo n.º 2
0
        object ISerializerInternal.DeserializeInternal(string data, DeserializationContext context)
        {
            if (data.Equals(EmptySymbol))
            {
                return(EmptyValue);
            }

            MainParser        parser = new MainParser();
            IList <INodeData> list;

            parser.ExtractNodeData(data, out list);

            // get the metadata
            List <INodeData> validNodes = list[0].SubNodes[1].SubNodes.Where(n => !NodeUtils.IgnoreOnDeserialization(n.Type)).ToList();

            INodeData typeNode = list[0].SubNodes[1].SubNodes.FirstOrDefault(n => n.Type == NodeType.METADATA);

            CustomSerializer serializer = SerializerUtils.MetadataSerializer;

            Metadata metadata = (Metadata)serializer.ReadObjectData(new List <INodeData>()
            {
                typeNode
            }, context, -1);

            // check if the object is already in cache
            // if that's the case then return it
            if (context.TryGet(metadata.ReferenceTracker, out object cacheFound))
            {
                return(cacheFound);
            }

            // validate the node structure
            if (!ValidateNodes(list))
            {
                return(EmptyValue);
            }

            // else do the normal deserialization
            object result = ReadObjectData(list, context, metadata.ReferenceTracker);

            return(result);
        }