public StructMemberConverter(JsonSerializerOptions options, IMemberMapping memberMapping)
 {
     MemberNameAsString = memberMapping.MemberName;
     _memberName        = Encoding.UTF8.GetBytes(MemberNameAsString);
     _memberGetter      = GenerateGetter(memberMapping.MemberInfo);
     _memberSetter      = GenerateSetter(memberMapping.MemberInfo);
     _jsonConverter     = (JsonConverter <TM>)memberMapping.Converter;
     _defaultValue      = (TM)memberMapping.DefaultValue;
     _ignoreIfDefault   = memberMapping.IgnoreIfDefault;
     _requirementPolicy = memberMapping.RequirementPolicy;
 }
Ejemplo n.º 2
0
        public void TestRead(RequirementPolicy requirementPolicy, string hexBuffer, Type expectedExceptionType)
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <StringObject>(objectMapping =>
                                                                           objectMapping
                                                                           .AutoMap()
                                                                           .ClearMemberMappings()
                                                                           .MapMember(o => o.String).SetRequired(requirementPolicy)
                                                                           );

            Helper.TestRead <StringObject>(hexBuffer, expectedExceptionType, options);
        }
Ejemplo n.º 3
0
        public void TestRead(RequirementPolicy requirementPolicy, string json, Type expectedExceptionType)
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            options.GetObjectMappingRegistry().Register <StringObject>(objectMapping =>
                                                                       objectMapping
                                                                       .AutoMap()
                                                                       .ClearMemberMappings()
                                                                       .MapMember(o => o.String).SetRequired(requirementPolicy)
                                                                       );

            Helper.TestRead <StringObject>(json, options, expectedExceptionType);
        }
 public MemberConverter(JsonSerializerOptions options, IMemberMapping memberMapping)
 {
     MemberNameAsString              = memberMapping.MemberName;
     _memberName                     = Encoding.UTF8.GetBytes(MemberNameAsString);
     _memberGetter                   = GenerateGetter(memberMapping.MemberInfo);
     _memberSetter                   = GenerateSetter(memberMapping.MemberInfo);
     _jsonConverter                  = (JsonConverter <TM>)memberMapping.Converter;
     _defaultValue                   = (TM)memberMapping.DefaultValue;
     _ignoreIfDefault                = memberMapping.IgnoreIfDefault;
     _shouldSerializeMethod          = memberMapping.ShouldSerializeMethod;
     _requirementPolicy              = memberMapping.RequirementPolicy;
     _deserializableReadOnlyProperty = options.GetReadOnlyPropertyHandling() == ReadOnlyPropertyHandling.Read ||
                                       (memberMapping.MemberInfo.IsDefined(typeof(JsonDeserializeAttribute)) && options.GetReadOnlyPropertyHandling() == ReadOnlyPropertyHandling.Default);
 }
Ejemplo n.º 5
0
        public void TestWrite(RequirementPolicy requirementPolicy, string value, Type expectedExceptionType)
        {
            CborOptions options = new CborOptions();

            options.Registry.ObjectMappingRegistry.Register <StringObject>(objectMapping =>
                                                                           objectMapping
                                                                           .AutoMap()
                                                                           .ClearMemberMappings()
                                                                           .MapMember(o => o.String).SetRequired(requirementPolicy)
                                                                           );

            StringObject obj = new StringObject
            {
                String = value
            };

            Helper.TestWrite(obj, expectedExceptionType, options);
        }
Ejemplo n.º 6
0
        public StructMemberConverter(JsonSerializerOptions options, IMemberMapping memberMapping)
        {
            MemberInfo?memberInfo = memberMapping.MemberInfo;

            if (memberInfo == null)
            {
                throw new JsonException("MemberInfo must not be null");
            }

            MemberNameAsString = memberMapping.MemberName !;
            _memberName        = Encoding.UTF8.GetBytes(MemberNameAsString);
            _memberGetter      = GenerateGetter(memberInfo);
            _memberSetter      = GenerateSetter(memberInfo);
            _jsonConverter     = (JsonConverter <TM>)memberMapping.Converter !;
            _defaultValue      = (TM)memberMapping.DefaultValue !;
            _ignoreIfDefault   = memberMapping.IgnoreIfDefault;
            _requirementPolicy = memberMapping.RequirementPolicy;
        }
Ejemplo n.º 7
0
        public MemberConverter(CborConverterRegistry registry, IMemberMapping memberMapping)
        {
            MemberInfo?memberInfo = memberMapping.MemberInfo;

            if (memberInfo == null)
            {
                throw new CborException("MemberInfo must not be null");
            }

            _memberName            = Encoding.UTF8.GetBytes(memberMapping.MemberName !);
            _memberGetter          = GenerateGetter(memberInfo);
            _memberSetter          = GenerateSetter(memberInfo);
            _converter             = (ICborConverter <TM>)memberMapping.Converter !;
            _defaultValue          = (TM)memberMapping.DefaultValue !;
            _ignoreIfDefault       = memberMapping.IgnoreIfDefault;
            _shouldSerializeMethod = memberMapping.ShouldSerializeMethod;
            _lengthMode            = memberMapping.LengthMode;
            _requirementPolicy     = memberMapping.RequirementPolicy;
        }
Ejemplo n.º 8
0
 public MemberMapping <T> SetRequired(RequirementPolicy requirementPolicy)
 {
     RequirementPolicy = requirementPolicy;
     return(this);
 }
 public JsonRequiredAttribute(RequirementPolicy policy = RequirementPolicy.Always)
 {
     Policy = policy;
 }