Beispiel #1
0
 public override Object AfterDeserialize(Type t, Object value)
 {
     var bar = value.AssertCast<IBar>();
     if (bar == null) return null;
     bar.Baz = bar.Baz ?? "null";
     return bar;
 }
Beispiel #2
0
        public Json(Object value, MemberInfo descriptor)
        {
            if (value == null && descriptor == null) return;
            var mi = descriptor ?? (value == null ? null : value.GetType());
            var pi = mi as PropertyInfo;
            var t = mi is Type ? (Type)mi : (value == null ? null : value.GetType());

            pi.Config().Validators.ForEach(validator => validator.Validate(pi, value));
            t.Config().Validators.ForEach(validator => validator.Validate(t, value));
            // todo. after BeforeSerialize value might have become undesirably changed in-place!
            // how do we revert such changes and hydrate the object back to life?!
            value = pi.Config().Adapters.Fold(value, (curr, adapter) => adapter.BeforeSerialize(pi, curr));
            value = t.Config().Adapters.Fold(value, (curr, adapter) => adapter.BeforeSerialize(t, curr));

            if (value == null)
            {
                _my_state = State.Primitive;
                _my_primitive = null;
            }
            else if (value is Json)
            {
                _wrappee = value.AssertCast<Json>();
            }
            else
            {
                var engine = pi.Config().Engine ?? t.Config().Engine ?? (Engine)new DefaultEngine();
                if (engine is TypeEngine && !(mi is Type)) mi = mi.Type();
                _wrappee = engine.Serialize(mi, value);
            }
        }
Beispiel #3
0
        public override void GetData(Object target, Stream outgoingData)
        {
            var curr = target.AssertCast<IImmutableHierarchy>();
            var root = curr.Hierarchy().Last();

            var map = new Dictionary<IImmutableHierarchy, HierarchyNode>();
            root.Family().ForEach(node =>
            {
                var m_node = new HierarchyNode();
                map.Add(node, m_node);

                var m_parent = node.Parent == null ? null : map[node.Parent];
                if (m_parent != null)
                {
                    m_node.Parent = m_parent;
                    m_parent.Children.Add(m_node);
                }
            });

            var model = new HierarchyModel(map[root], map[curr]);
            new BinaryFormatter().Serialize(outgoingData, model);
        }
Beispiel #4
0
 public override Object BeforeSerialize(PropertyInfo pi, Object value)
 {
     var s_value = value.AssertCast<String>();
     return s_value == null ? null : Convert.ToBase64String(Encoding.UTF8.GetBytes(s_value));
 }
Beispiel #5
0
 public override Object AfterDeserialize(PropertyInfo pi, Object value)
 {
     var s_value = value.AssertCast<String>();
     return s_value == null ? null : Encoding.UTF8.GetString(Convert.FromBase64String(s_value));
 }
Beispiel #6
0
 public override void Validate(PropertyInfo pi, Object value)
 {
     var s_value = value.AssertCast<String>();
     s_value.AssertMatch(Pattern);
 }
Beispiel #7
0
        public override Json Serialize(Type t, Object obj)
        {
            if (obj == null) return new Json(null);
            if (t.IsNullable()) return Json.Serialize(obj.UndecorateNullable(), t.UndecorateNullable());
            if (t == typeof(DateTime)) return new Json(obj.AssertCast<DateTime>().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffZ"));

            var cfg = t.Config().DefaultEngine().Config;
            if (cfg.IsPrimitive)
            {
                if (t.IsJsonPrimitive())
                {
                    return new JsonPrimitive(obj);
                }
                else
                {
                    t.SupportsSerializationToString().AssertTrue();
                    return new JsonPrimitive(obj.ToInvariantString());
                }
            }
            else
            {
                if (cfg.IsHash)
                {
                    var cast = typeof(EnumerableExtensions).GetMethod("Cast", BF.PublicStatic).AssertNotNull();
                    cast = cast.XMakeGenericMethod(typeof(String), typeof(Object));
                    var hash = (IDictionary<String, Object>)cast.Invoke(null, new[]{obj});

                    var json = new JsonObject();
                    hash.ForEach(kvp =>
                    {
                        var key = kvp.Key;
                        var value = Json.Serialize(kvp.Value);
                        json.Add(key, value);
                    });

                    return json;
                }
                else if (cfg.IsList)
                {
                    var list = ((IEnumerable)obj).Cast<Object>();

                    var json = new JsonArray();
                    list.ForEach(value =>
                    {
                        var j_value = Json.Serialize(value);
                        json.Add(j_value);
                    });

                    return json;
                }
                else if (cfg.IsObject)
                {
                    var json = new JsonObject();

                    cfg.Slots.Where(mi => mi.CanRead()).ForEach(mi =>
                    {
                        var a_include = mi.AttrOrNull<JsonIncludeAttribute>();
                        var a_key = a_include == null ? null : a_include.Name;
                        var key = a_key ?? (cfg.LowercaseSlotNames ? mi.Name.ToLower() : mi.Name);
                        var value = mi.GetValue(obj);
                        var j_value = Json.Serialize(value, mi);
                        json.Add(key, j_value);
                    });

                    return json;
                }
                else
                {
                    throw AssertionHelper.Fail();
                }
            }
        }
 public override void GetData(Object target, Stream outgoingData)
 {
     var debugView = target.AssertCast<Node.INodeDebugView>();
     base.GetData(debugView.Node.AssertNotNull(), outgoingData);
 }
Beispiel #9
0
 public override void GetData(Object target, Stream outgoingData)
 {
     var cfg = target.AssertCast<BaseControlFlowGraph>();
     new BinaryFormatter().Serialize(outgoingData, cfg.DumpAsText());
 }