Esempio n. 1
0
        private static void _AddSample(Type type, JsonArray json, JsonSerializer serializer)
        {
            var elementType = _GetElementType(type);
            var buildMethod = TemplateGenerator.GetBuildMethod(elementType);
            var value       = buildMethod.Invoke(null, new object[] { serializer.Options });
            var serialize   = SerializerCache.GetSerializeMethod(elementType);

            json.Add((JsonValue)serialize(serializer, value));
        }
Esempio n. 2
0
        private static Dictionary <SerializationInfo, JsonValue> _SerializeValues <T>(T obj, JsonSerializer serializer, IEnumerable <SerializationInfo> properties)
        {
            var dict = new Dictionary <SerializationInfo, JsonValue>();

            foreach (var property in properties)
            {
                object value;
                Type   type;
                if (property.MemberInfo is PropertyInfo propertyInfo)
                {
                    if (propertyInfo.GetIndexParameters().Length > 0)
                    {
                        continue;
                    }
                    value = propertyInfo.GetValue(obj, null);
                    if (value == null && !serializer.Options.EncodeDefaultValues)
                    {
                        continue;
                    }
                    type = propertyInfo.PropertyType;
                }
                else
                {
                    var fieldInfo = (FieldInfo)property.MemberInfo;
                    value = fieldInfo.GetValue(obj);
                    if (value == null && !serializer.Options.EncodeDefaultValues)
                    {
                        continue;
                    }
                    type = fieldInfo.FieldType;
                }
                var json = JsonValue.Null;
                if (value != null)
                {
                    var serialize = SerializerCache.GetSerializeMethod(type);
                    json = (JsonValue)serialize(serializer, value);
                }
                if ((json == JsonValue.Null) && !serializer.Options.EncodeDefaultValues)
                {
                    continue;
                }
                if (serializer.Options.IncludeContentSample && json.Type == JsonValueType.Array)
                {
                    _AddSample(type, json.Array, serializer);
                }

                dict.Add(property, json);
            }
            return(dict);
        }
Esempio n. 3
0
        private static Dictionary <SerializationInfo, JsonValue> _SerializeTypeValues(JsonSerializer serializer, IEnumerable <SerializationInfo> properties)
        {
            var dict = new Dictionary <SerializationInfo, JsonValue>();

            foreach (var memberInfo in properties)
            {
                object value;
                Type   type;
                if (memberInfo.MemberInfo is PropertyInfo propertyInfo)
                {
                    if (propertyInfo.GetIndexParameters().Any())
                    {
                        continue;
                    }
                    value = propertyInfo.GetValue(null, null);
                    if (value == null)
                    {
                        continue;
                    }
                    type = propertyInfo.PropertyType;
                }
                else
                {
                    var fieldInfo = (FieldInfo)memberInfo.MemberInfo;
                    value = fieldInfo.GetValue(null);
                    if (value == null)
                    {
                        continue;
                    }
                    type = fieldInfo.FieldType;
                }
                var serialize = SerializerCache.GetSerializeMethod(type);
                var json      = (JsonValue)serialize(serializer, value);
                if ((json == JsonValue.Null) && !serializer.Options.EncodeDefaultValues)
                {
                    continue;
                }
                dict.Add(memberInfo, json);
            }
            return(dict);
        }