Exemple #1
0
        static JToken CreateJToken(this IContentNode n, ListNode <object> branch)
        {
            if (n is IRawValueWrapper wrapper)
            {
                branch = branch.Append(wrapper.RawValue);
            }

            switch (n)
            {
            case ContentNull nil:
                return(JValue.CreateNull());

            case ContentList list:
                var jArray = new JArray();
                foreach (var i in list)
                {
                    jArray.Add(i.CreateJToken(branch));
                }
                return(jArray);

            case ContentObject obj:
                var jObject  = new JObject();
                var objPairs = obj.Where(prop =>
                                         !(prop.Value is IRawValueWrapper w && branch.Contains(w.RawValue)));

                foreach (var pair in objPairs)
                {
                    obj.TryEvaluate(pair.Path, out IContentNode result);
                    jObject.Add(pair.Path.Last().ToString(), result.CreateJToken(branch));
                }
                return(jObject);

            case ContentBoolean b:
                return(new JValue(b.Value));

            case ContentDateTime dt:
                return(new JValue(dt.Value));

            case ContentNumber num:
                return(new JValue(num.Value));

            case ContentText text:
                return(new JValue(text.Value));

            default:
                throw new NotSupportedException($"Content of type {n.GetType()} cannot be converted to JSON");
            }
        }
Exemple #2
0
        static IEnumerable <ContentPathValue> ProcessVisit(this IContentNode n, ListNode <object> branch)
        {
            yield return(new ContentPathValue(ContentPath.Root, n));

            if (n is IRawValueWrapper wrapper)
            {
                branch = branch.Append(wrapper.RawValue);
            }

            if (n is ContentObject o)
            {
                var descendents = o
                                  .Where(prop => !(prop.Value is IRawValueWrapper w && branch.Contains(w.RawValue)))
                                  .SelectMany(prop =>
                                              prop.Value.ProcessVisit(branch)
                                              .Select(subProp =>
                                                      new ContentPathValue(prop.Path.Append(subProp.Path), subProp.Value)));

                foreach (var i in descendents)
                {
                    yield return(i);
                }
            }

            else if (n is ContentList list)
            {
                var descendents = list.SelectMany((item, idx) =>
                                                  item.ProcessVisit(branch).Select(pair =>
                                                                                   new ContentPathValue(
                                                                                       ContentPath.Root.Append(idx).Append(pair.Path),
                                                                                       pair.Value)));

                foreach (var i in descendents)
                {
                    yield return(i);
                }
            }
        }