Example #1
0
        /// <summary>
        /// Serialize the specified object to the wire
        /// </summary>
        public void Serialize(JsonWriter w, IdentifiedData o, JsonSerializationContext context)
        {
            if (o == null)
            {
                throw new ArgumentNullException(nameof(o));
            }

            // For each item in the property ...
            bool loadedProperties = false;

            // Iterate properties
            foreach (var propertyInfo in o.GetType().GetRuntimeProperties())
            {
                // Get the property name
                var propertyName = GetPropertyName(propertyInfo);
                if (propertyName == null || propertyName.StartsWith("$")) // Skip internal property names
                {
                    continue;
                }

                // Serialization decision
                if (!context.ShouldSerialize(propertyName))
                {
                    continue;
                }

                // Get the property
                var value = propertyInfo.GetValue(o);

                // Null ,do we want to force load?
                if (value == null || (value as IList)?.Count == 0)
                {
                    var tkey = o.Key.HasValue ? o.Key.Value : Guid.NewGuid();
                    if (context.ShouldForceLoad(propertyName, tkey))
                    {
                        if (value is IList && !propertyInfo.PropertyType.IsArray)
                        {
                            if (o.Key.HasValue)
                            {
                                value = context.JsonContext.LoadCollection(propertyInfo.PropertyType, (Guid)o.Key);
                            }
                            propertyInfo.SetValue(o, value);
                            loadedProperties = (value as IList).Count > 0;
                        }
                        else
                        {
                            var keyPropertyRef = propertyInfo.GetCustomAttribute <SerializationReferenceAttribute>();
                            if (keyPropertyRef != null)
                            {
                                var keyProperty = o.GetType().GetRuntimeProperty(keyPropertyRef.RedirectProperty);
                                var key         = keyProperty.GetValue(o);
                                if (key != null)
                                {
                                    value = context.JsonContext.LoadRelated(propertyInfo.PropertyType, (Guid)key);
                                    propertyInfo.SetValue(o, value);
                                    loadedProperties = value != null;
                                }
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                // TODO: Classifier
                context.JsonContext.WritePropertyUtil(w, propertyName, value, context);
            }

            // Loaded something, let's cache it
            if (loadedProperties && o.Key.HasValue && ((o as IVersionedEntity) == null || (o as IVersionedEntity)?.VersionKey.HasValue == true) &&
                o.LoadState != LoadState.New)
            {
                (ApplicationServiceContext.Current.GetService(typeof(IDataCachingService)) as IDataCachingService).Add(o);
            }
        }