Beispiel #1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                return;
            }

            try{
                JaDocument jaDoc = null;

                IEnumerable <IResource> resources = value as IEnumerable <IResource>;

                if (resources == null)
                {
                    jaDoc = value is JaDocument document ? document : new JaDocument((IResource)value);
                }
                else
                {
                    jaDoc = new JaDocument(resources);
                }

                var context = new JaBuilderContext(message);

                jaDoc.Serialize(context).WriteTo(writer);
            }catch (Exception e) {
                throw e;
            }
        }
Beispiel #2
0
        public override JToken Serialize(JaBuilderContext context)
        {
            JObject template = GetTemplate();

            List <string> propertiesToRemove = new List <string>();

            foreach (var property in template.Properties())
            {
                if (property.Name.Equals(Constants.KEY_RELATIONSHIPS))
                {
                    JToken jt = BuildRelationships(property.Value, context);

                    if (jt == null || jt.IsEmpty())
                    {
                        propertiesToRemove.Add(property.Name);
                    }
                    else
                    {
                        property.Value = jt;
                    }
                }
                else
                {
                    context.Populate(property, this);
                }
            }

            propertiesToRemove.ForEach(n => template.Remove(n));

            return(template);
        }
Beispiel #3
0
        private JToken BuildResources(JaBuilderContext context, ICollection <IResource> items, bool forceToUseArray = false)
        {
            if (items == null || !items.Any())
            {
                return(null);
            }

            if (items.Count > 1 || forceToUseArray || hasError)
            {
                JArray array = new JArray();
                foreach (var item in items)
                {
                    if (hasError && !(item is JaError))
                    {
                        continue;                                 //make sure error and data are not co-existed.
                    }
                    array.Add(item.Serialize(context));
                }

                return(array);
            }
            else
            {
                return(items.First().Serialize(context));
            }
        }
Beispiel #4
0
 public virtual JToken Serialize(JaBuilderContext context)
 {
     foreach (var property in template.Properties())
     {
         context.Populate(property, this);
     }
     return(template);
 }
Beispiel #5
0
        private JToken SerializeSingleResource(IResource resource, JaBuilderContext context)
        {
            context.AddIncludedResources(resource);

            JObject jo = new JObject();

            jo.Add("type", ((JaResourceBase)resource).Type);
            jo.Add("id", ((JaResourceBase)resource).Id);
            return(jo);
        }
        public JToken Build(JaBuilderContext context)
        {
            IEnumerable <IResource> elements = (IEnumerable <IResource>)context.Value;

            JContainer ja = elements.FirstOrDefault()?.GetContainer() ?? new JArray();

            foreach (var resource in elements)
            {
                ja.Add(resource.Serialize(context));
            }

            return(ja);
        }
        public override JToken Serialize(JaBuilderContext context)
        {
            JObject jb = new JObject();

            foreach (var property in GetType().GetProperties())
            {
                if (property.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                jb.Add(property.Name.ToLower(), context.GetPropertyValue(property.Name, this));
            }

            return(new JProperty(Name, jb));
        }
        public JToken Build(JaBuilderContext context)
        {
            JObject j = new JObject();

            IDictionary <string, object> dic = (IDictionary <string, object>)context.Value;

            if (dic.Count == 0)
            {
                return(new JObject());
            }

            foreach (KeyValuePair <string, object> kv in dic)
            {
                j.Add(new JProperty(kv.Key, new JValue(kv.Value)));
            }

            return(j);
        }
Beispiel #9
0
        public override JToken Serialize(JaBuilderContext context)
        {
            PreSetTemplate(context.ActionTemplate);

            JObject masterTemplate = GetTemplate(Constants.MASTER_TEMPLATE_NAME);

            List <string> propertiesNeedToRemove = new List <string>();

            foreach (var property in masterTemplate.Properties())
            {
                if (!hasError && property.Name.Equals("data"))
                {
                    property.Value = BuildResources(context);
                }
                else if (!hasError && property.Name.Equals("included"))
                {
                    property.Value = BuildResources(new JaBuilderContext(context.RequestMessage), context.IncludedResources, true);
                }
                else if (hasError && property.Name.Equals("errors"))
                {
                    property.Value = BuildResources(context);
                }
                else
                {
                    string key = property.EvaulationKey();

                    if (key == null)
                    {
                        continue;
                    }

                    property.Value = context.GetPropertyValue(key, this);
                }

                if (property.Value == null || property.Value.IsEmpty())
                {
                    propertiesNeedToRemove.Add(property.Name);
                }
            }

            propertiesNeedToRemove.ForEach(p => masterTemplate.Remove(p));

            return(masterTemplate);
        }
Beispiel #10
0
        private JToken BuildData(JaBuilderContext context)
        {
            var resouce = value as IEnumerable <IResource>;

            if (resouce == null)
            {
                return(SerializeSingleResource((IResource)value, context));
            }
            else
            {
                JArray ja = new JArray();
                foreach (var r in resouce)
                {
                    ja.Add(SerializeSingleResource(r, context));
                }

                return(ja);
            }
        }
Beispiel #11
0
        public JToken Serialize(JaBuilderContext context)
        {
            foreach (var t in template.Properties())
            {
                if (t.Name.Equals("data"))
                {
                    t.Value = BuildData(context);
                }
                else
                {
                    string key = t.EvaulationKey();

                    if (key == null)
                    {
                        continue;
                    }

                    JToken jt = context.GetPropertyValue(key, GetPrimaryObject());
                    t.Value = jt ?? t.Value;
                }
            }

            return(template);
        }
Beispiel #12
0
        public virtual JToken BuildRelationships(JToken relationships, JaBuilderContext context)
        {
            JObject jObject = relationships as JObject; //relationships must be a object

            List <string> propertiesToRemove = new List <string>();

            foreach (var property in jObject.Properties())             //need to know what need to be built
            {
                JToken jt = context.GetPropertyValue(property.Name.Pascalize(), this, true);

                if (jt == null || jt.IsEmpty())
                {
                    propertiesToRemove.Add(property.Name);
                }
                else
                {
                    property.Value = jt;
                }
            }

            propertiesToRemove.ForEach(n => jObject.Remove(n));

            return(jObject);
        }
Beispiel #13
0
 public JToken Build(JaBuilderContext context)
 {
     return(new JValue(((Uri)context.Value).AbsolutePath));
 }
 public JToken Build(JaBuilderContext context)
 {
     return(new JValue(context.Value));
 }
Beispiel #15
0
 public override JToken Serialize(JaBuilderContext context)
 {
     return(new JProperty(Name, Href.AbsolutePath));
 }
Beispiel #16
0
 public abstract JToken Serialize(JaBuilderContext context);
 public abstract JToken Build(JaBuilderContext context);
Beispiel #18
0
 private JToken BuildResources(JaBuilderContext context)
 {
     return(BuildResources(context, resources));
 }
 public JToken Build(JaBuilderContext context)
 {
     return(((IResource)context.Value).Serialize(context));
 }
Beispiel #20
0
 public virtual JToken Serialize(JaBuilderContext context)
 {
     throw new NotImplementedException();
 }