public string[] GetPropertiesByGroup(Type klass, IParentFieldData parentData, params string[] group)
        {
            InitializeMetadata(klass);

            if (!ContainsMetadata(klass))
            {
                return(new string[0]);
            }

            var properties = new List <string>();

            var metadata = GetMetadata(klass);

            if (metadata.HasProperties())
            {
                properties.AddRange(from property in metadata.GetProperties()
                                    where parentData == null || !parentData.IsRecursive(klass, property.GetPropertyName())
                                    where property.HasGroups()
                                    where property.AppliesToGroups(new List <string>(@group))
                                    select property.GetPropertyName());
            }

            if (metadata.HasVirtualProperties())
            {
                properties.AddRange(from property in metadata.GetVirtualProperties()
                                    where parentData == null || !parentData.IsRecursive(klass, property.GetPropertyName())
                                    where property.HasGroups()
                                    where property.AppliesToGroups(new List <string>(@group))
                                    select property.GetPropertyName());
            }

            return(properties.ToArray());
        }
Exemple #2
0
        private void FillWith(bool asArray, JsonWriter gen, IParentFieldData parentData, object ob, string[] group)
        {
            var klass  = ob.GetType();
            var fields = _provider.GetPropertiesByGroup(klass, parentData, group);

            if (asArray)
            {
                gen.WriteStartArray();
            }
            else
            {
                gen.WriteStartObject();
            }

            if (_typeChecker.IsSerializableObject(ob))
            {
                foreach (var fieldName in fields)
                {
                    var accessor = BuildFieldAccessor(ob, fieldName);

                    if (accessor.Get <object>() == null)
                    {
                        if (accessor.Exists() && _configurationManager.GetIncludeNullValues())
                        {
                            gen.WritePropertyName(GetSerializedName(klass, fieldName));
                            gen.WriteNull();
                        }

                        continue;
                    }

                    if (!_typeChecker.IsUnserializableObject(accessor.Get <object>()))
                    {
                        var fieldData = new ParentFieldData(klass, fieldName, group);

                        gen.WritePropertyName(GetSerializedName(klass, fieldName));
                        FillRawValue(gen, fieldData, accessor.Get <object>(), group);
                    }
                }
            }
            else if (_typeChecker.IsIterable(ob))
            {
                foreach (var arrValue in (IEnumerable)ob)
                {
                    var fieldData = new ParentFieldData(arrValue.GetType(), null, group);

                    FillRawValue(gen, fieldData, arrValue, group);
                }
            }

            if (asArray)
            {
                gen.WriteEndArray();
            }
            else
            {
                gen.WriteEndObject();
            }
        }
Exemple #3
0
        private void FillRawValue(JsonWriter gen, IParentFieldData parentFieldData, object value, string[] group)
        {
            _typeChecker.Check(value,
                               scalar => gen.WriteValue(scalar),
                               dictionary =>
            {
                gen.WriteStartObject();
                var sortedKeys = new SortedList(dictionary);
                foreach (var key in sortedKeys.Keys)
                {
                    var parentGroups = _provider.GetGroupsByFieldName(parentFieldData.GetKlass(),
                                                                      parentFieldData.GetFieldName());
                    var fieldData =
                        new ParentFieldData(parentFieldData.GetKlass(), parentFieldData.GetFieldName(),
                                            parentGroups);

                    gen.WritePropertyName(key.ToString());
                    FillRawValue(gen, fieldData, dictionary[key], group);
                }

                gen.WriteEndObject();
            },
                               enumerable =>
            {
                gen.WriteStartArray();
                foreach (var arrValue in enumerable)
                {
                    var parentGroups = _provider.GetGroupsByFieldName(parentFieldData.GetKlass(),
                                                                      parentFieldData.GetFieldName());
                    var fieldData =
                        new ParentFieldData(parentFieldData.GetKlass(), parentFieldData.GetFieldName(),
                                            parentGroups);

                    FillRawValue(gen, fieldData, arrValue, group);
                }

                gen.WriteEndArray();
            },
                               date => FillDateValue(gen, date),
                               stringSerializable =>
            {
                if (parentFieldData != null && parentFieldData.GetKlass() == stringSerializable.GetType())
                {
                    FillWith(false, gen, parentFieldData, value, parentFieldData.GetGroups());
                }
                else
                {
                    FillWith(false, gen, parentFieldData, value, group);
                }
            });
        }