/// <summary>
        /// Builds a <see cref="BytesSchema" />.
        /// </summary>
        /// <returns>
        /// A successful <see cref="SchemaBuilderCaseResult" /> with a <see cref="BytesSchema" />
        /// if <paramref name="type" /> is <see cref="T:System.Byte[]" />; an unsuccessful
        /// <see cref="SchemaBuilderCaseResult" /> with an <see cref="UnsupportedTypeException" />
        /// otherwise.
        /// </returns>
        /// <inheritdoc />
        public virtual SchemaBuilderCaseResult BuildSchema(Type type, SchemaBuilderContext context)
        {
            if (type == typeof(byte[]))
            {
                var bytesSchema = new BytesSchema();

                Schema schema = bytesSchema;

                if (!type.IsValueType && NullableReferenceTypeBehavior == NullableReferenceTypeBehavior.All)
                {
                    if (!context.Schemas.TryGetValue(NullableType, out var nullSchema))
                    {
                        context.Schemas.Add(NullableType, nullSchema = new NullSchema());
                    }

                    schema = new UnionSchema(new[] { nullSchema, schema });
                }

                try
                {
                    context.Schemas.Add(type, schema);
                }
                catch (ArgumentException exception)
                {
                    throw new InvalidOperationException($"A schema for {type} already exists on the schema builder context.", exception);
                }

                return(SchemaBuilderCaseResult.FromSchema(schema));
            }
            else
            {
                return(SchemaBuilderCaseResult.FromException(new UnsupportedTypeException(type, $"{nameof(BytesSchemaBuilderCase)} can only be applied to the {typeof(byte[])} type.")));
            }
        }
Beispiel #2
0
        public void Int32Values(int value)
        {
            var schema = new NullSchema();

            var deserializer = DeserializerBuilder.BuildDeserializer <int>(schema);
            var serializer   = SerializerBuilder.BuildSerializer <int>(schema);

            Assert.Equal(default, deserializer.Deserialize(serializer.Serialize(value)));
Beispiel #3
0
        public void Int32Values(int value)
        {
            var schema = new NullSchema();

            var deserialize = deserializerBuilder.BuildDelegate <int>(schema);
            var serialize   = serializerBuilder.BuildDelegate <int>(schema);

            using (stream)
            {
                serialize(value, new BinaryWriter(stream));
            }

            var reader = new BinaryReader(stream.ToArray());

            Assert.Equal(default, deserialize(ref reader));
 /// <summary>
 /// Writes an <see cref="PrimitiveSchema" />.
 /// </summary>
 /// <inheritdoc />
 public virtual JsonSchemaWriterCaseResult Write(Schema schema, Utf8JsonWriter json, bool canonical, JsonSchemaWriterContext context)
 {
     if (schema is PrimitiveSchema primitiveSchema)
     {
         json.WriteStringValue(primitiveSchema switch
         {
             BooleanSchema _ => JsonSchemaToken.Boolean,
             BytesSchema _ => JsonSchemaToken.Bytes,
             DoubleSchema _ => JsonSchemaToken.Double,
             FloatSchema _ => JsonSchemaToken.Float,
             IntSchema _ => JsonSchemaToken.Int,
             LongSchema _ => JsonSchemaToken.Long,
             NullSchema _ => JsonSchemaToken.Null,
             StringSchema _ => JsonSchemaToken.String,
             _ => throw new UnsupportedSchemaException(schema, $"Unknown primitive schema {schema}."),
         });
Beispiel #5
0
        public void DynamicObjectValues(dynamic value)
        {
            var schema = new NullSchema();

            var deserialize = deserializerBuilder.BuildDelegate <dynamic>(schema);
            var serialize   = serializerBuilder.BuildDelegate <dynamic>(schema);

            using (stream)
            {
                serialize(value, new BinaryWriter(stream));
            }

            var reader = new BinaryReader(stream.ToArray());

            Assert.Equal(null, deserialize(ref reader));
        }
        public void NullableNullWriterTest()
        {
            var writerSchema = new NullSchema();
            var readerSchema = new UnionSchema(new NullSchema(), new StringSchema());
            var writer       = new DatumWriter <string>(writerSchema);
            var reader       = new DatumReader <string>(readerSchema, writerSchema);

            var expectedValueNotNull = "Some String";
            var expectedValueNull    = null as string;

            using (var stream = new MemoryStream())
                using (var encoder = new BinaryEncoder(stream))
                    using (var decoder = new BinaryDecoder(stream))
                    {
                        writer.Write(encoder, expectedValueNotNull);
                        stream.Seek(0, SeekOrigin.Begin);
                        Assert.AreEqual(expectedValueNull, reader.Read(decoder));
                        stream.Seek(0, SeekOrigin.Begin);
                        writer.Write(encoder, expectedValueNull);
                        stream.Seek(0, SeekOrigin.Begin);
                        Assert.AreEqual(expectedValueNull, reader.Read(decoder));
                    }
        }
        /// <summary>
        /// Builds an <see cref="ArraySchema" />.
        /// </summary>
        /// <returns>
        /// A successful <see cref="SchemaBuilderCaseResult" /> with an <see cref="ArraySchema" />
        /// if <paramref name="type" /> implements <see cref="IEnumerable{T}" />; an unsuccessful
        /// <see cref="SchemaBuilderCaseResult" /> with an <see cref="UnsupportedTypeException" />
        /// otherwise.
        /// </returns>
        /// <inheritdoc />
        public virtual SchemaBuilderCaseResult BuildSchema(Type type, SchemaBuilderContext context)
        {
            if (GetEnumerableType(type) is Type itemType)
            {
                // defer setting the item schema so the array schema can be cached:
                var arraySchema = ReflectionExtensions.GetUninitializedInstance <ArraySchema>();

                Schema schema = arraySchema;

                if (!type.IsValueType && NullableReferenceTypeBehavior == NullableReferenceTypeBehavior.All)
                {
                    if (!context.Schemas.TryGetValue(NullableType, out var nullSchema))
                    {
                        context.Schemas.Add(NullableType, nullSchema = new NullSchema());
                    }

                    schema = new UnionSchema(new[] { nullSchema, schema });
                }

                try
                {
                    context.Schemas.Add(type, schema);
                }
                catch (ArgumentException exception)
                {
                    throw new InvalidOperationException($"A schema for {type} already exists on the schema builder context.", exception);
                }

                arraySchema.Item = SchemaBuilder.BuildSchema(itemType, context);

                return(SchemaBuilderCaseResult.FromSchema(schema));
            }
            else
            {
                return(SchemaBuilderCaseResult.FromException(new UnsupportedTypeException(type, $"{nameof(ArraySchemaBuilderCase)} can only be applied to enumerable types.")));
            }
        }
 private void VisitCore(NullSchema s)
 {
     s.Serializer = new NullSerializer(s);
 }