/// <summary>
        ///  If writer's is a union, but reader's is not then
        ///  if the reader's schema matches the selected writer's schema, it is recursively resolved against it. If they do not match, an error is signalled.
        /// </summary>
        /// <param name="w">The writer schema.</param>
        /// <param name="r">The reader schema.</param>
        /// <returns>True if match.</returns>
        private TypeSchema BuildCore(UnionSchema w, TypeSchema r)
        {
            var        schemas         = new List <TypeSchema>();
            TypeSchema schemaToReplace = null;
            TypeSchema newSchema       = null;

            foreach (var ws in w.Schemas)
            {
                newSchema = this.BuildDynamic(ws, r);
                if (newSchema != null)
                {
                    schemaToReplace = ws;
                    break;
                }
            }

            if (newSchema == null)
            {
                throw new SerializationException("Cannot match the union schema.");
            }

            foreach (var s in w.Schemas)
            {
                schemas.Add(s == schemaToReplace ? newSchema : s);
            }

            return(new UnionSchema(schemas, newSchema.RuntimeType));
        }
        private TypeSchema BuildCore(UnionSchema w, UnionSchema r)
        {
            var unionSchemas = new List <TypeSchema>();

            foreach (var writerSchema in w.Schemas)
            {
                TypeSchema schema = null;
                foreach (var readerSchema in r.Schemas)
                {
                    schema = this.BuildDynamic(writerSchema, readerSchema);
                    if (schema != null)
                    {
                        break;
                    }
                }

                if (schema == null)
                {
                    return(null);
                }

                unionSchemas.Add(schema);
            }
            return(new UnionSchema(unionSchemas, r.RuntimeType));
        }
        /// <summary>
        /// Parses a union token.
        /// </summary>
        /// <param name="unionToken">The union token.</param>
        /// <param name="parent">The parent.</param>
        /// <param name="namedSchemas">The named schemas.</param>
        /// <returns>
        /// Schema internal representation.
        /// </returns>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when union schema type is invalid.</exception>
        private TypeSchema ParseUnionType(JArray unionToken, NamedSchema parent, Dictionary <string, NamedSchema> namedSchemas)
        {
            var schemas = new List <TypeSchema>();

            foreach (var typeAlternative in unionToken.Children())
            {
                var schema = this.Parse(typeAlternative, parent, namedSchemas);
                if (schema.Type == Token.Union)
                {
                    throw new SerializationException(
                              string.Format(CultureInfo.InvariantCulture, "Union schemas cannot be nested:'{0}'.", unionToken));
                }

                if (schemas.Any(s => UnionSchema.IsSameTypeAs(s, schema)))
                {
                    throw new SerializationException(
                              string.Format(CultureInfo.InvariantCulture, "Unions cannot contains schemas of the same type: '{0}'.", schema.Type));
                }

                schemas.Add(schema);
            }

            return(new UnionSchema(schemas, typeof(object)));
        }
 private void VisitCore(UnionSchema s)
 {
     s.Serializer = new UnionSerializer(s);
     foreach (var schema in s.Schemas)
     {
         this.VisitDynamic(schema);
     }
 }
 /// <summary>
 ///  If reader's is a union, but writer's is not the first schema in the reader's union
 ///  that matches the writer's schema is recursively resolved against it. If none match, an error is signalled.
 /// </summary>
 /// <param name="w">The writer schema.</param>
 /// <param name="r">The reader schema.</param>
 /// <returns>True if match.</returns>
 private TypeSchema BuildCore(TypeSchema w, UnionSchema r)
 {
     return(r.Schemas.Select(rs => this.BuildDynamic(w, rs)).SingleOrDefault(s => s != null));
 }
        /// <summary>
        ///  If writer's is a union, but reader's is not then
        ///  if the reader's schema matches the selected writer's schema, it is recursively resolved against it. If they do not match, an error is signalled.
        /// </summary>
        /// <param name="w">The writer schema.</param>
        /// <param name="r">The reader schema.</param>
        /// <returns>True if match.</returns>
        private TypeSchema BuildCore(UnionSchema w, TypeSchema r)
        {
            var schemas = new List<TypeSchema>();
            TypeSchema schemaToReplace = null;
            TypeSchema newSchema = null;
            foreach (var ws in w.Schemas)
            {
                newSchema = this.BuildDynamic(ws, r);
                if (newSchema != null)
                {
                    schemaToReplace = ws;
                    break;
                }
            }

            if (newSchema == null)
            {
                throw new SerializationException("Cannot match the union schema.");
            }

            foreach (var s in w.Schemas)
            {
                schemas.Add(s == schemaToReplace ? newSchema : s);
            }

            return new UnionSchema(schemas, newSchema.RuntimeType);
        }
 /// <summary>
 ///  If reader's is a union, but writer's is not the first schema in the reader's union 
 ///  that matches the writer's schema is recursively resolved against it. If none match, an error is signalled.
 /// </summary>
 /// <param name="w">The writer schema.</param>
 /// <param name="r">The reader schema.</param>
 /// <returns>True if match.</returns>
 private TypeSchema BuildCore(TypeSchema w, UnionSchema r)
 {
     return r.Schemas.Select(rs => this.BuildDynamic(w, rs)).SingleOrDefault(s => s != null);
 }
        private TypeSchema BuildCore(UnionSchema w, UnionSchema r)
        {
            var unionSchemas = new List<TypeSchema>();
            foreach (var writerSchema in w.Schemas)
            {
                TypeSchema schema = null;
                foreach (var readerSchema in r.Schemas)
                {
                    schema = this.BuildDynamic(writerSchema, readerSchema);
                    if (schema != null)
                    {
                        break;
                    }
                }

                if (schema == null)
                {
                    return null;
                }

                unionSchemas.Add(schema);
            }
            return new UnionSchema(unionSchemas, r.RuntimeType);
        }