Example #1
0
        public static object toImmutable(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            // TODO - this isn't quite right, need to clean up with FanObj.isImmutable
            if (obj is Double)
            {
                return(((Double)obj).doubleValue());
            }
            if (obj is Long)
            {
                return(((Long)obj).longValue());
            }

            if (FanObj.isImmutable(obj))
            {
                return(obj);
            }
            if (obj is List)
            {
                return(((List)obj).toImmutable());
            }
            if (obj is Map)
            {
                return(((Map)obj).toImmutable());
            }
            throw NotImmutableErr.make(FanObj.@typeof(obj).toStr()).val;
        }
Example #2
0
        //////////////////////////////////////////////////////////////////////////
        // Is/As
        //////////////////////////////////////////////////////////////////////////

        public static bool @is(object instance, Type type)
        {
            if (instance == null)
            {
                return(false);
            }
            return(FanObj.@typeof(instance).fits(type));
        }
Example #3
0
            public override void checkInCtor(object it)
            {
                if (it == m_inCtor)
                {
                    return;
                }
                string msg = it == null ? "null" : FanObj.@typeof(it).qname();

                throw ConstErr.make(msg).val;
            }
Example #4
0
        //////////////////////////////////////////////////////////////////////////
        // Write
        //////////////////////////////////////////////////////////////////////////

        public void writeObj(object obj)
        {
            if (obj == null)
            {
                w("null");
                return;
            }

            if (obj.GetType().FullName[0] == 'S')
            {
                if (obj is bool && (bool)obj)
                {
                    w("true");  return;
                }
                if (obj is bool && !(bool)obj)
                {
                    w("false"); return;
                }
                if (obj is double)
                {
                    FanFloat.encode((double)obj, this); return;
                }
                if (obj is long)
                {
                    FanInt.encode((long)obj, this); return;
                }
                if (obj is string)
                {
                    wStrLiteral(obj.ToString(), '"'); return;
                }
            }

            if (obj.GetType().FullName[0] == 'F')
            {
                if (obj is Boolean && (obj as Boolean).booleanValue())
                {
                    w("true"); return;
                }
                if (obj is Boolean && !(obj as Boolean).booleanValue())
                {
                    w("false"); return;
                }
                if (obj is Double)
                {
                    FanFloat.encode((obj as Double).doubleValue(), this); return;
                }
                if (obj is Long)
                {
                    FanInt.encode((obj as Long).longValue(), this); return;
                }
                if (obj is BigDecimal)
                {
                    FanDecimal.encode((BigDecimal)obj, this); return;
                }
            }

            if (obj is Literal)
            {
                ((Literal)obj).encode(this);
                return;
            }

            Type         type = FanObj.@typeof(obj);
            Serializable ser  = (Serializable)type.facet(Sys.SerializableType, false);

            if (ser != null)
            {
                if (ser.m_simple)
                {
                    writeSimple(type, obj);
                }
                else
                {
                    writeComplex(type, obj, ser);
                }
            }
            else
            {
                if (skipErrors)
                {
                    w("null /* Not serializable: ").w(type.qname()).w(" */");
                }
                else
                {
                    throw IOErr.make("Not serializable: " + type).val;
                }
            }
        }
Example #5
0
        //////////////////////////////////////////////////////////////////////////
        // Complex
        //////////////////////////////////////////////////////////////////////////

        private void writeComplex(Type type, object obj, Serializable ser)
        {
            wType(type);

            bool   first  = true;
            object defObj = null;

            if (skipDefaults)
            {
                defObj = FanObj.@typeof(obj).make();
            }

            List fields = type.fields();

            for (int i = 0; i < fields.sz(); ++i)
            {
                Field f = (Field)fields.get(i);

                // skip static, transient, and synthetic (once) fields
                if (f.isStatic() || f.isSynthetic() || f.hasFacet(Sys.TransientType))
                {
                    continue;
                }

                // get the value
                object val = f.get(obj);

                // if skipping defaults
                if (defObj != null)
                {
                    object defVal = f.get(defObj);
                    if (OpUtil.compareEQ(val, defVal))
                    {
                        continue;
                    }
                }

                // if first then open braces
                if (first)
                {
                    w('\n').wIndent().w('{').w('\n'); level++; first = false;
                }

                // field name =
                wIndent().w(f.name()).w('=');

                // field value
                curFieldType = f.type().toNonNullable();
                writeObj(val);
                curFieldType = null;

                w('\n');
            }

            // if collection
            if (ser.m_collection)
            {
                first = writeCollectionItems(type, obj, first);
            }

            // if we output fields, then close braces
            if (!first)
            {
                level--; wIndent().w('}');
            }
        }