示例#1
0
        public void Serialize <T>(IWriteVisitor visitor, T graph)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }

            var traveller = _rootTravellers.GetOrAdd <T>(out var level);

            if (level == LevelType.Value)
            {
                var valueVisitor = ValueVisitor.Create <T>();
                valueVisitor.VisitValue(visitor, VisitArgs.CreateRoot(level), graph);
            }
            else
            {
                var rootArgs = VisitArgs.CreateRoot(level);
                visitor.Visit(graph, rootArgs);
                traveller.Travel(visitor, graph);
                visitor.Leave(graph, rootArgs);
            }
        }
示例#2
0
        public object Deserialize(IReadVisitor visitor, Type type)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var traveller = _rootTravellers.GetOrAdd(type, out var level);

            if (level == LevelType.Value)
            {
                var valueVisitor = ValueVisitor.Create(type);
                return(valueVisitor.TryVisitValue(visitor, VisitArgs.CreateRoot(level), out var value)
                    ? value : default);