Esempio n. 1
0
        private static void ExportCollection(ExportContext context, NameValueCollection collection, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(collection != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            for (int i = 0; i < collection.Count; i++)
            {
                writer.WriteMember(collection.GetKey(i));

                string[] values = collection.GetValues(i);

                if (values == null)
                {
                    writer.WriteNull();
                }
                else if (values.Length > 1)
                {
                    context.Export(values, writer);
                }
                else
                {
                    context.Export(values[0], writer);
                }
            }

            writer.WriteEndObject();
        }
        internal static void ExportView(ExportContext context, DataView view, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(view != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            writer.WriteMember("columns");

            writer.WriteStartArray();
            foreach (DataColumn column in view.Table.Columns)
            {
                context.Export(column.ColumnName, writer);
            }
            writer.WriteEndArray();

            writer.WriteMember("rows");

            writer.WriteStartArray();
            foreach (DataRowView row in view)
            {
                context.Export(row.Row.ItemArray, writer);
            }
            writer.WriteEndArray();

            writer.WriteEndObject();
        }
        protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(value != null);
            Debug.Assert(writer != null);

            if (_properties.Count == 0)
            {
                writer.WriteString(value.ToString());
            }
            else
            {
                ObjectReferenceTracker tracker = null;

                try
                {
                    writer.WriteStartObject();

                    foreach (PropertyDescriptor property in _properties)
                    {
                        object propertyValue = property.GetValue(value);

                        if (!JsonNull.LogicallyEquals(propertyValue))
                        {
                            writer.WriteMember(property.Name);

                            if (tracker == null)
                            {
                                //
                                // We are about to enter a deeper scope so
                                // start tracking the current object being
                                // exported. This will help to detect
                                // recursive references that may occur
                                // through this exporter deeper in the tree.
                                //

                                tracker = TrackObject(context, value);
                            }

                            context.Export(propertyValue, writer);
                        }
                    }

                    writer.WriteEndObject();
                }
                finally
                {
                    if (tracker != null)
                    {
                        tracker.Pop(value);
                    }
                }
            }
        }
Esempio n. 4
0
        public static void Export(object value, JsonWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            ExportContext context = new ExportContext();

            context.Export(value, writer);
        }
Esempio n. 5
0
        internal static void ExportRow(ExportContext context, DataRow row, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(row != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            foreach (DataColumn column in row.Table.Columns)
            {
                writer.WriteMember(column.ColumnName);
                context.Export(row[column], writer);
            }

            writer.WriteEndObject();
        }
        protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(value != null);
            Debug.Assert(writer != null);

            IEnumerable items = (IEnumerable)value;

            writer.WriteStartArray();

            foreach (object item in items)
            {
                context.Export(item, writer);
            }

            writer.WriteEndArray();
        }
Esempio n. 7
0
        protected virtual void Export(ExportContext context, JsonWriter writer)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            writer.WriteStartArray();

            foreach (object value in this)
            {
                context.Export(value, writer);
            }

            writer.WriteEndArray();
        }
Esempio n. 8
0
        protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(value != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            IDictionary dictionary = (IDictionary)value;

            foreach (DictionaryEntry entry in dictionary)
            {
                writer.WriteMember(entry.Key.ToString());
                context.Export(entry.Value, writer);
            }

            /*
             * FIXME: Use IDictionaryEnumerator.Entry instead and enumerate manually (faster and more robust).
             * It is faster because unboxing is avoided by going over
             * IDictionaryEnumerator.Entry rather than
             * IDictionaryEnumerator.Current. It is more robust because many
             * people may get the implementation of IDictionary.GetEnumerator
             * wrong, especially if they are implementing IDictionary<K, V> in
             * 2.0. If they simply return the enumerator from the wrapped
             * dictionary then Current will return KeyValuePair<K, V> instead
             * of DictionaryEntry and therefore cause a casting exception.
             *
             * using (IDictionaryEnumerator e = dictionary.GetEnumerator())
             * {
             *  while (e.MoveNext())
             *  {
             *      writer.WriteMember(e.Entry.Key.ToString());
             *      context.Export(e.Entry.Value, writer);
             *  }
             * }
             */

            writer.WriteEndObject();
        }
Esempio n. 9
0
        protected virtual void Export(ExportContext context, JsonWriter writer)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            writer.WriteStartObject();

            foreach (string name in NameIndexList)
            {
                writer.WriteMember(name);
                context.Export(InnerHashtable[name], writer);
            }

            writer.WriteEndObject();
        }