Пример #1
0
        public IEnumerable<EncodingInstruction> ExecuteEncode(EncodingContext context)
        {
            if (_mapping.UsesReferencing)
            {
                if (context.Process.References.HasReferenceTo(context.Value))
                {
                    double reference = context.Process.References.GetReferenceTo(context.Value);
                    context.Output += JsonToken.Parse(reference);

                    yield break;
                }
                else
                {
                    context.Process.References.Reference(context.Value);
                }
            }

            context.Output += JsonToken.BeginObject;

            Dictionary<string, JsonFieldMappingBase>.Enumerator fields = _mapping.FieldMappings.GetEnumerator();
            for (int index = 0; fields.MoveNext(); index++ )
            {
                JsonFieldMappingBase fieldMapping = fields.Current.Value;

                if (index > 0)
                {
                    context.Output += JsonToken.ValueSeperator;
                }

                object value = fieldMapping.Get(context.Value);

                context.Output += JsonToken.Parse(fieldMapping.JsonField, JsonTokenType.String);
                context.Output += JsonToken.NameSeperator;

                DoEncode instruction = new DoEncode(fieldMapping.Encode(value), fieldMapping.DesiredType);
                yield return instruction;

                context.Output += instruction.Output;
            }

            context.Output += JsonToken.EndObject;
        }
Пример #2
0
        public IEnumerable<EncodingInstruction> ExecuteEncode(EncodingContext context)
        {
            IList list = (IList)context.Value;
            bool parallel = false;

            // See if this list would benefit from parallel processing
            if (context.Process.IsParallel && _genericArgument != null && !context.Process.RequiresReferencing && TypeHelper.IsParallelBeneficial(_genericArgument))
            {
                foreach (object el in list)
                {
                    // Do parallel
                    yield return new DoParallelEncode(el, _genericArgument);
                }

                parallel = true;
            }

            context.Output += JsonToken.BeginArray;

            for (int index = 0; index < list.Count; index++)
            {
                object element = list[index];

                if (index > 0)
                {
                    context.Output += JsonToken.ValueSeperator;
                }
                else if (parallel)
                {
                    // Wait untill all parallel tasks are finished.
                    yield return new SyncParallelEncode();
                }

                DoEncode instruction = new DoEncode(element, _genericArgument);
                yield return instruction;

                context.Output += instruction.Output;

            }

            context.Output += JsonToken.EndArray;
        }
Пример #3
0
        public IEnumerable<EncodingInstruction> ExecuteEncode(EncodingContext context)
        {
            #if NET40
            IDictionary<string, object> expando = null;
            IDictionary dict = null;
            #else
            IDictionary dict = (IDictionary)context.Value;
            #endif

            #if NET40
            if(_dictType == typeof(ExpandoObject))
            {
                expando = (IDictionary<string, object>)context.Value;
            }
            else
            {
                dict = (IDictionary)context.Value;
            }
            #endif
            
            bool parallel = false;

            // See if this list would benefit from parallel processing
            if (context.Process.IsParallel && _genericArgument != null && !context.Process.RequiresReferencing && TypeHelper.IsParallelBeneficial(_genericArgument))
            {
                #if NET40
                foreach (object el in (dict != null ? (ICollection)dict.Values : (ICollection)expando.Values))
                #else
                foreach (object el in dict.Values)
                #endif
                {
                    // Do parallel
                    yield return new DoParallelEncode(el, _genericArgument);
                }

                parallel = true;
            }

            context.Output += JsonToken.BeginObject;

            object key;
            object value;

            #if NET40
            IEnumerator enumerator = dict != null ? dict.GetEnumerator() : (IEnumerator)expando.GetEnumerator();
            #else
            IDictionaryEnumerator enumerator = dict.GetEnumerator();
            #endif
            for (int index = 0; enumerator.MoveNext(); index++)
            {
                #if NET40
                if (expando != null)
                {
                    KeyValuePair<string, object> pair = (KeyValuePair<string, object>)enumerator.Current;
                    key = pair.Key;
                    value = pair.Value;
                }
                else
                {
                    DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
                    key = entry.Key;
                    value = entry.Value;
                }
                #else
                key = enumerator.Key;
                value = enumerator.Value;
                #endif

                if (index > 0)
                {
                    context.Output += JsonToken.ValueSeperator;
                }
                else if (parallel)
                {
                    // Wait untill all parallel tasks are finished.
                    yield return new SyncParallelEncode();
                }

                context.Output += JsonToken.Parse(key, JsonTokenType.String);
                context.Output += JsonToken.NameSeperator;

                DoEncode instruction = new DoEncode(value, _genericArgument);
                yield return instruction;

                context.Output += instruction.Output;
            }

            context.Output += JsonToken.EndObject;
        }