Example #1
0
            public Boolean MoveNext(Boolean omitKeys)
            {
                switch (m_data.Type)
                {
                case PipelineContextDataType.Array:
                    var array = m_data.AssertArray("array");
                    if (++m_index < array.Count)
                    {
                        Current = array[m_index];
                        return(true);
                    }
                    else
                    {
                        Current = null;
                        return(false);
                    }

                case PipelineContextDataType.Dictionary:
                    var dictionary = m_data.AssertDictionary("dictionary");

                    // Return the value
                    if (m_isKey)
                    {
                        m_isKey = false;
                        Current = dictionary[m_index].Value;
                        return(true);
                    }

                    if (++m_index < dictionary.Count)
                    {
                        // Skip the key, return the value
                        if (omitKeys)
                        {
                            m_isKey = false;
                            Current = dictionary[m_index].Value;
                            return(true);
                        }

                        // Return the key
                        m_isKey = true;
                        Current = new StringContextData(dictionary[m_index].Key);
                        return(true);
                    }

                    Current = null;
                    return(false);

                default:
                    throw new NotSupportedException($"Unexpected {nameof(PipelineContextData)} type '{m_data.Type}'");
                }
            }
        public override Object ReadJson(
            JsonReader reader,
            Type objectType,
            Object existingValue,
            JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.String:
                return(new StringContextData(reader.Value.ToString()));

            case JsonToken.Boolean:
                return(new BooleanContextData((Boolean)reader.Value));

            case JsonToken.Float:
                return(new NumberContextData((Double)reader.Value));

            case JsonToken.Integer:
                return(new NumberContextData((Double)(Int64)reader.Value));

            case JsonToken.StartObject:
                break;

            default:
                return(null);
            }

            Int32?  type  = null;
            JObject value = JObject.Load(reader);

            if (!value.TryGetValue("t", StringComparison.OrdinalIgnoreCase, out JToken typeValue))
            {
                type = PipelineContextDataType.String;
            }
            else if (typeValue.Type == JTokenType.Integer)
            {
                type = (Int32)typeValue;
            }
            else
            {
                return(existingValue);
            }

            Object newValue = null;

            switch (type)
            {
            case PipelineContextDataType.String:
                newValue = new StringContextData(null);
                break;

            case PipelineContextDataType.Array:
                newValue = new ArrayContextData();
                break;

            case PipelineContextDataType.Dictionary:
                newValue = new DictionaryContextData();
                break;

            case PipelineContextDataType.Boolean:
                newValue = new BooleanContextData(false);
                break;

            case PipelineContextDataType.Number:
                newValue = new NumberContextData(0);
                break;

            case PipelineContextDataType.CaseSensitiveDictionary:
                newValue = new CaseSensitiveDictionaryContextData();
                break;

            default:
                throw new NotSupportedException($"Unexpected {nameof(PipelineContextDataType)} '{type}'");
            }

            if (value != null)
            {
                using (JsonReader objectReader = value.CreateReader())
                {
                    serializer.Populate(objectReader, newValue);
                }
            }

            return(newValue);
        }