Пример #1
0
        protected override IList <JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            HalJsonTypeConfiguration config;

            if (!_configuration.TryGetTypeConfiguration(type, out config))
            {
                if (!(typeof(IHaveHalJsonLinks).IsAssignableFrom(type) || typeof(IHaveHalJsonEmbedded).IsAssignableFrom(type)))
                {
                    return(base.CreateProperties(type, memberSerialization));
                }
                config = new HalJsonTypeConfiguration();
            }

            var lst = new List <JsonProperty>();

            foreach (var member in GetSerializableMembers(type))
            {
                var prop = CreateProperty(member, memberSerialization);
                if (config.HiddenProperties.Contains(member))
                {
                    prop.Readable = false;
                }
                lst.Add(prop);
            }

            if (config.Embedded.Count != 0 || typeof(IHaveHalJsonEmbedded).IsAssignableFrom(type))
            {
                var property = new JsonProperty
                {
                    PropertyType  = typeof(IDictionary <string, object>),
                    DeclaringType = type,
                    Writable      = false,
                    Readable      = true,
                    ValueProvider = new EmbeddedValueProvider(config.Embedded),
                    PropertyName  = "_embedded",
                    Order         = _configuration.HalJsonPropertiesOrder
                };
                lst.Insert(0, property);
            }
            if (config.Links.Count != 0 || typeof(IHaveHalJsonLinks).IsAssignableFrom(type))
            {
                lst.Insert(0, new JsonProperty
                {
                    PropertyType  = typeof(IDictionary <string, object>),
                    DeclaringType = type,
                    Writable      = false,
                    Readable      = true,
                    ValueProvider = new LinksValueProvider(config.Links, _configuration),
                    PropertyName  = "_links",
                    Order         = _configuration.HalJsonPropertiesOrder
                });
            }
            lst = lst.OrderBy(p => p.Order ?? -1).ToList();
            return(lst);
        }
Пример #2
0
        public bool TryGetTypeConfiguration(Type type, out HalJsonTypeConfiguration config)
        {
            var rv = _config.TryGet(type, out config);

            if (!rv && AttributeConfigurationResolver.GetConfigurationOrNull(type) != null)         // No configuration in cache, but found attribute-based one
            {
                config = GetOrCreateTypeConfiguration(type);
                return(true);
            }
            return(rv);
        }