Exemple #1
0
        void WriteObject(object obj)
        {
            depth += 1;
            if (depth > jsonCtx.maxDepth)
            {
                throw new Exception("Serializer reaches max depth: " + jsonCtx.maxDepth);
            }

            sb.Append(JsonContext.CURLY_OPEN);

            var getters = refCtx.GetMembers(obj.GetType());

            bool isFirst = true;

            for (int i = 0, length = getters.Length; i < length; i++)
            {
                if (!isFirst)
                {
                    sb.Append(JsonContext.COMMA);
                }
                isFirst = false;

                var g = getters[i];
                WritePairUnsafe(g.name, g.Get(obj));
            }

            sb.Append(JsonContext.CURLY_CLOSE);

            depth -= 1;
        }
Exemple #2
0
        object ToObject(Type t, object obj)
        {
            var dict = (Dictionary<string, Object>)obj;
            var setters = refCtx.GetMembers(t);

            var res = Activator.CreateInstance(t);
            for (int i = 0, length = setters.Length; i < length; i++) {
                var setter = setters[i];

                object value;
                if (dict.TryGetValue(setter.name, out value)) {
                    setter.Set(res, ToValue(setter.type, value));
                }
            }

            return res;
        }