Example #1
0
 public SerializeAs(SerializeAsOptions how)
 {
     this.How = how;
 }
Example #2
0
 public SerializeAs(SerializeAsOptions how)
 {
     this.How = how;
 }
Example #3
0
        protected void Serialize(object value, Stream writeStream, JsonWriter writer, JsonSerializer serializer, RelationAggregator aggregator)
        {
            writer.WriteStartObject();

            // Do the Id now...
            writer.WritePropertyName("id");
            var idProp = _modelManager.GetIdProperty(value.GetType());

            writer.WriteValue(GetValueForIdProperty(idProp, value));

            // Leverage the cached map to avoid another costly call to System.Type.GetProperties()
            PropertyInfo[] props = _modelManager.GetProperties(value.GetType());

            // Do non-model properties first, everything else goes in "links"
            //TODO: Unless embedded???
            IList <PropertyInfo> modelProps = new List <PropertyInfo>();

            foreach (PropertyInfo prop in props)
            {
                if (prop == idProp)
                {
                    continue;
                }

                if (this.CanWriteTypeAsPrimitive(prop.PropertyType))
                {
                    if (prop.GetCustomAttributes().Any(attr => attr is JsonIgnoreAttribute))
                    {
                        continue;
                    }

                    // numbers, strings, dates...
                    writer.WritePropertyName(_modelManager.GetJsonKeyForProperty(prop));

                    var propertyValue = prop.GetValue(value, null);

                    if (prop.PropertyType == typeof(string) &&
                        prop.GetCustomAttributes().Any(attr => attr is SerializeStringAsRawJsonAttribute))
                    {
                        if (propertyValue == null)
                        {
                            writer.WriteNull();
                        }
                        else
                        {
                            var minifiedValue = JsonHelpers.MinifyJson((string)propertyValue);
                            writer.WriteRawValue(minifiedValue);
                        }
                    }
                    else
                    {
                        serializer.Serialize(writer, propertyValue);
                    }
                }
                else
                {
                    modelProps.Add(prop);
                    continue;
                }
            }

            // Now do other stuff
            if (modelProps.Count() > 0)
            {
                writer.WritePropertyName("links");
                writer.WriteStartObject();
            }
            foreach (PropertyInfo prop in modelProps)
            {
                bool               skip = false, iip = false;
                string             lt = null;
                SerializeAsOptions sa = SerializeAsOptions.Ids;

                object[] attrs = prop.GetCustomAttributes(true);

                foreach (object attr in attrs)
                {
                    Type attrType = attr.GetType();
                    if (typeof(JsonIgnoreAttribute).IsAssignableFrom(attrType))
                    {
                        skip = true;
                        continue;
                    }
                    if (typeof(IncludeInPayload).IsAssignableFrom(attrType))
                    {
                        iip = ((IncludeInPayload)attr).Include;
                    }
                    if (typeof(SerializeAs).IsAssignableFrom(attrType))
                    {
                        sa = ((SerializeAs)attr).How;
                    }
                    if (typeof(LinkTemplate).IsAssignableFrom(attrType))
                    {
                        lt = ((LinkTemplate)attr).TemplateString;
                    }
                }
                if (skip)
                {
                    continue;
                }

                writer.WritePropertyName(_modelManager.GetJsonKeyForProperty(prop));

                // Look out! If we want to SerializeAs a link, computing the property is probably
                // expensive...so don't force it just to check for null early!
                if (sa != SerializeAsOptions.Link && prop.GetValue(value, null) == null)
                {
                    writer.WriteNull();
                    continue;
                }

                // Now look for enumerable-ness:
                if (typeof(IEnumerable <Object>).IsAssignableFrom(prop.PropertyType))
                {
                    switch (sa)
                    {
                    case SerializeAsOptions.Ids:
                        //writer.WritePropertyName(ContractResolver._modelManager.GetJsonKeyForProperty(prop));
                        IEnumerable <object> items = (IEnumerable <object>)prop.GetValue(value, null);
                        if (items == null)
                        {
                            writer.WriteValue((IEnumerable <object>)null); //TODO: Is it okay with the spec and Ember Data to return null for an empty array?
                            break;                                         // LOOK OUT! Ending this case block early here!!!
                        }
                        this.WriteIdsArrayJson(writer, items, serializer);
                        if (iip)
                        {
                            Type itemType;
                            if (prop.PropertyType.IsGenericType)
                            {
                                itemType = prop.PropertyType.GetGenericArguments()[0];
                            }
                            else
                            {
                                // Must be an array at this point, right??
                                itemType = prop.PropertyType.GetElementType();
                            }
                            if (aggregator != null)
                            {
                                aggregator.Add(itemType, items);                         // should call the IEnumerable one...right?
                            }
                        }
                        break;

                    case SerializeAsOptions.Link:
                        if (lt == null)
                        {
                            throw new JsonSerializationException("A property was decorated with SerializeAs(SerializeAsOptions.Link) but no LinkTemplate attribute was provided.");
                        }
                        //TODO: Check for "{0}" in linkTemplate and (only) if it's there, get the Ids of all objects and "implode" them.
                        string href = String.Format(lt, null, GetIdFor(value));
                        //writer.WritePropertyName(ContractResolver._modelManager.GetJsonKeyForProperty(prop));
                        //TODO: Support ids and type properties in "link" object
                        writer.WriteStartObject();
                        writer.WritePropertyName("href");
                        writer.WriteValue(href);
                        writer.WriteEndObject();
                        break;

                    case SerializeAsOptions.Embedded:
                        // Not really supported by Ember Data yet, incidentally...but easy to implement here.
                        //writer.WritePropertyName(ContractResolver._modelManager.GetJsonKeyForProperty(prop));
                        //serializer.Serialize(writer, prop.GetValue(value, null));
                        this.Serialize(prop.GetValue(value, null), writeStream, writer, serializer, aggregator);
                        break;
                    }
                }
                else
                {
                    var propertyValue = prop.GetValue(value, null);
                    if (propertyValue == null)
                    {
                        writer.WriteNull();
                    }
                    else
                    {
                        string objId = GetIdFor(propertyValue);

                        switch (sa)
                        {
                        case SerializeAsOptions.Ids:
                            //writer.WritePropertyName(ContractResolver._modelManager.GetJsonKeyForProperty(prop));
                            serializer.Serialize(writer, objId);
                            if (iip)
                            {
                                if (aggregator != null)
                                {
                                    aggregator.Add(prop.PropertyType, prop.GetValue(value, null));
                                }
                            }
                            break;

                        case SerializeAsOptions.Link:
                            if (lt == null)
                            {
                                throw new JsonSerializationException(
                                          "A property was decorated with SerializeAs(SerializeAsOptions.Link) but no LinkTemplate attribute was provided.");
                            }
                            string link = String.Format(lt, objId,
                                                        GetIdFor(value)); //value.GetType().GetProperty("Id").GetValue(value, null));

                            //writer.WritePropertyName(ContractResolver._modelManager.GetJsonKeyForProperty(prop));
                            writer.WriteStartObject();
                            writer.WritePropertyName("href");
                            writer.WriteValue(link);
                            writer.WriteEndObject();
                            break;

                        case SerializeAsOptions.Embedded:
                            // Not really supported by Ember Data yet, incidentally...but easy to implement here.
                            //writer.WritePropertyName(ContractResolver._modelManager.GetJsonKeyForProperty(prop));
                            //serializer.Serialize(writer, prop.GetValue(value, null));
                            this.Serialize(prop.GetValue(value, null), writeStream, writer, serializer, aggregator);
                            break;
                        }
                    }
                }
            }
            if (modelProps.Count() > 0)
            {
                writer.WriteEndObject();
            }

            writer.WriteEndObject();
        }