0

Say I have an object with this format:

new LookUpResponse
            {
                LookUpDetails =
                    new List<LookUpDetail>
                    {
                        new LookUpDetail
                        {
                            LookUpCode = "GB00B6RLLV55",
                            CitiCode = "GTUI",
                            UnitName = "Liontrust Asia Income A Inc",
                            UniverseCode = "O",
                            IsFundDriver = false
                        }
                    }
            };

What I want is to allow users to select which child properties to return (e.g. selectField="LookUpDetail[LookUpCode,CitiCode]"), then my code parses it, so I know for type LookupDetail,we want to return Lookkupcode and citicode.

Is it possible with newton.json?

I tried this:

public class SelectFieldsResolver : DefaultContractResolver
    {
        private readonly IEnumerable<string> _selectFields;

        public SelectFieldsResolver(IEnumerable<string> selectFields)
        {
            _selectFields = selectFields;
        }
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            property.ShouldSerialize = instance => {
                if (!_selectFields.IsNullOrEmpty() && !_selectFields.Any(f => string.Equals(f, property.PropertyName, StringComparison.InvariantCultureIgnoreCase))) return false;

                var value = instance?.GetType().GetProperty(property.PropertyName)?.GetValue(instance, null);

                return value != null;
            };

            return property;
        }

        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            return type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Select(p => {
                    var jp = CreateProperty(p, memberSerialization);
                    jp.ValueProvider = new NullToEmptyStringValueProvider(p);
                    return jp;
                }).ToList();
        }
    }

However, the method CreateProperty and CreateProperties are only called on the root level (called on the LookUpDetails property), and what I want is that when the child object LookUpDetail is serialised, these functions should be called as well, so I can exclude the fields on children.

Is it Possible?

daxu
  • 3,514
  • 5
  • 38
  • 76
  • You could enhance `SelectFieldsResolver` so that the user can select fields to return *per type*, i.e. through a `Dictionary` input. But the root object can't control which properties of child object(s) to serialize, see [How to perform partial object serialization providing “paths” using Newtonsoft JSON.NET](https://stackoverflow.com/q/30304128/3744182) for an alternative approach. – dbc Oct 25 '19 at 16:38
  • *However, the method CreateProperty and CreateProperties are only called on the root level* - can you share a [mcve] that includes `LookUpResponse` and `LookUpDetail`? – dbc Oct 25 '19 at 16:39

0 Answers0