Exemple #1
0
        public object Read(Format format, FormatReader reader)
        {
            IDictionary dictionary = (IDictionary)Activator.CreateInstance(type);

            if (format.IsNil)
            {
                return(dictionary);
            }
            int size = reader.ReadMapLength(format);

            while (size > 0)
            {
                object key   = keyHandler.Read(reader.ReadFormat(), reader);
                object value = valueHandler.Read(reader.ReadFormat(), reader);
                dictionary.Add(key, value);
                size = size - 1;
            }
            return(dictionary);
        }
        public object Read(Format format, FormatReader reader)
        {
            MapDefinition definition = lazyDefinition.Value;

            if (format.IsMapFamily)
            {
                object obj = Activator.CreateInstance(definition.Type);
                InvokeCallback <OnDeserializingAttribute>(obj, definition);
                int size = reader.ReadMapLength(format);
                while (size > 0)
                {
                    string name = (string)nameHandler.Read(reader.ReadFormat(), reader);
                    name = nameConverter.OnUnpack(name, definition);

                    if (definition.FieldHandlers.ContainsKey(name))
                    {
                        object value = definition.FieldHandlers[name].Read(reader.ReadFormat(), reader);
                        definition.FieldInfos[name].SetValue(obj, value);
                    }
                    else if (context.MapOptions.IgnoreUnknownFieldOnUnpack)
                    {
                        reader.Skip();
                    }
                    else
                    {
                        throw new MissingFieldException(name + " does not exist for type: " + definition.Type);
                    }
                    size = size - 1;
                }
                InvokeCallback <OnDeserializedAttribute>(obj, definition);
                return(obj);
            }
            if (format.IsEmptyArray && context.MapOptions.AllowEmptyArrayOnUnpack)
            {
                return(Activator.CreateInstance(definition.Type));
            }
            if (format.IsNil)
            {
                return(null);
            }
            throw new FormatException(this, format, reader);
        }
        void ReadMap(Format format)
        {
            int size = reader.ReadMapLength(format);

            if (size == 0)
            {
                Append("{}");
                return;
            }
            Append("{").ValueSeparator();
            indentationSize += 1;
            for (int i = 0; i < size; i++)
            {
                Indent().AppendStream().Append(":").AppendIfPretty(" ").AppendStream();
                if (i < size - 1)
                {
                    Append(",");
                }
                ValueSeparator();
            }
            indentationSize -= 1;
            Indent().Append("}");
        }