Example #1
0
        private void ToJson(Stream stream)
        {
            using (JsonWriter jsonWriter = new JsonWriter(stream))
            {
                jsonWriter.WriteObject();

                jsonWriter.WriteMember("Items");
                jsonWriter.WriteArray();

                jsonWriter.WriteObject();
                LinkExtensions.Serialise(GetLinks(), jsonWriter);

                jsonWriter.WriteMember("TimeTriggered");
                jsonWriter.WriteValue(_WebhookNotification.TimeTriggered);

                jsonWriter.WriteMember("SubscriptionType");
                jsonWriter.WriteValue(_WebhookNotification.SubscriptionType);

                if (_WebhookNotification.ObjectDefinition != null)
                {
                    jsonWriter.WriteMember("Value");
                    ObjectInstance instance = new ObjectInstance(_WebhookNotification.ObjectDefinition, _WebhookNotification.ChangedObject);
                    instance.Links = new List <Link>();
                    instance.Serialise(jsonWriter);
                }

                jsonWriter.WriteEndObject();

                jsonWriter.WriteEndArray();
                jsonWriter.WriteEndObject();
                jsonWriter.Flush();
            }
            stream.Position = 0;
        }
Example #2
0
        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();
        }
Example #3
0
 private static void WritePhoneNumber(JsonWriter writer, string location, string number)
 {
     writer.WriteStartObject();      //  {
     writer.WriteMember("Location"); //      "Location" :
     writer.WriteString(location);   //          "...",
     writer.WriteMember("Number");   //      "Number" :
     writer.WriteString(number);     //          "..."
     writer.WriteEndObject();        //  }
 }
Example #4
0
        //
        // NOTE: For sake of brevity, the following WriteRss* methods do not
        // write out all the members of the RichSiteSummary and related types.
        //

        private static void WriteRssToJson(RichSiteSummary rss, JsonWriter writer)
        {
            writer.WriteStartObject();
            writer.WriteMember("version");
            writer.WriteString(rss.version);
            writer.WriteMember("channel");
            WriteRssToJson(rss.channel, writer);
            writer.WriteEndObject();
        }
Example #5
0
 private static void WriteRssToJson(Item item, JsonWriter writer)
 {
     writer.WriteStartObject();
     writer.WriteMember("title");
     writer.WriteString(item.title);
     writer.WriteMember("description");
     writer.WriteString(item.description);
     writer.WriteMember("link");
     writer.WriteString(item.link);
     writer.WriteEndObject();
 }
Example #6
0
 private static void WriteRssToJson(Channel channel, JsonWriter writer)
 {
     writer.WriteStartObject();
     writer.WriteMember("title");
     writer.WriteString(channel.title);
     writer.WriteMember("link");
     writer.WriteString(channel.link);
     writer.WriteMember("items");
     writer.WriteStartArray();
     foreach (Item item in channel.item)
     {
         WriteRssToJson(item, writer);
     }
     writer.WriteEndArray();
     writer.WriteEndObject();
 }
Example #7
0
        private static void ExportDataSet(ExportContext context, DataSet dataSet, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(dataSet != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();
    
            foreach (DataTable table in dataSet.Tables)
            {
                writer.WriteMember(table.TableName);

                //
                // If there is an exporter (perhaps an override) for the 
                // DataTable in effect then use it. Otherwise our 
                // DataTableExporter.
                //

                IExporter tableExporter = context.FindExporter(table.GetType());
                
                if (tableExporter != null)
                    tableExporter.Export(context, table, writer);
                else
                    DataTableExporter.ExportTable(context, table, writer);
            }
    
            writer.WriteEndObject();
        }
        protected override void FormatOther(object o, JsonWriter writer)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");

            if (JNull.LogicallyEquals(o))
            {
                FormatNull(o, writer);
                return;
            }

            writer.WriteStartObject();

            if (_properties == null)
                _properties = TypeDescriptor.GetProperties(o);

            foreach (PropertyDescriptor property in _properties)
            {
                // TODO: Allow someone to indicate via an attribute (e.g. JsonIgnore) that a property should be excluded.

                object value = property.GetValue(o);
                
                if (!JNull.LogicallyEquals(value))
                {
                    writer.WriteMember(property.Name);
                    writer.WriteValue(value);
                }
            }

            writer.WriteEndObject();
        }
Example #9
0
        private static void WriteTerm(JsonWriter writer, Spec.Term term)
        {
            if (term.type == Term.TermType.DATUM)
            {
                WriteDatum(writer, term.datum);
                return;
            }

            writer.BeginArray();
            writer.WriteNumber((int)term.type);
            if (term.args.Count > 0 || term.optargs.Count > 0)
            {
                writer.BeginArray();
                foreach (var arg in term.args)
                {
                    WriteTerm(writer, arg);
                }
                writer.EndArray();
                writer.BeginObject();
                foreach (var opt in term.optargs)
                {
                    writer.WriteMember(opt.key);
                    WriteTerm(writer, opt.val);
                }
                writer.EndObject();
            }
            writer.EndArray();
        }
Example #10
0
        private static void ExportDataSet(ExportContext context, DataSet dataSet, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(dataSet != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            foreach (DataTable table in dataSet.Tables)
            {
                writer.WriteMember(table.TableName);

                //
                // If there is an exporter (perhaps an override) for the
                // DataTable in effect then use it. Otherwise our
                // DataTableExporter.
                //

                IExporter tableExporter = context.FindExporter(table.GetType());

                if (tableExporter != null)
                {
                    tableExporter.Export(context, table, writer);
                }
                else
                {
                    DataTableExporter.ExportTable(context, table, writer);
                }
            }

            writer.WriteEndObject();
        }
Example #11
0
            public void Export(ExportContext context, JsonWriter writer, object source)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
                if (writer == null)
                {
                    throw new ArgumentNullException("writer");
                }
                if (source == null)
                {
                    throw new ArgumentNullException("source");
                }

                object value = _property.GetValue(source);

                if (JsonNull.LogicallyEquals(value) || value.Equals(_defaultValue))
                {
                    return;
                }

                writer.WriteMember(_property.Name);
                context.Export(value, writer);
            }
        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());
                return;
            }
            ObjectReferenceTracker objectReferenceTracker = null;

            try
            {
                writer.WriteStartObject();
                foreach (PropertyDescriptor property in _properties)
                {
                    object value2 = property.GetValue(value);
                    if (!JsonNull.LogicallyEquals(value2))
                    {
                        writer.WriteMember(property.Name);
                        if (objectReferenceTracker == null)
                        {
                            objectReferenceTracker = TrackObject(context, value);
                        }
                        context.Export(value2, writer);
                    }
                }
                writer.WriteEndObject();
            }
            finally
            {
                objectReferenceTracker?.Pop(value);
            }
        }
Example #13
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();
        }
        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
            {
                writer.WriteStartObject();

                foreach (PropertyDescriptor property in _properties)
                {
                    object propertyValue = property.GetValue(value);
                
                    if (!JsonNull.LogicallyEquals(propertyValue))
                    {
                        writer.WriteMember(property.Name);
                        context.Export(propertyValue, writer);
                    }
                }

                writer.WriteEndObject();
            }
        }
Example #15
0
 static void Write(JsonWriter json)
 {
     json.WriteObjectStart();
     json.WriteAttribute("age", 30);
     json.WriteAttribute("first", "John");
     json.WriteAttribute("last", "Smith");
     json.WriteMember("phoneNumbers");
     json.WriteArrayStart();
     json.WriteString("425-000-1212");
     json.WriteString("425-000-1213");
     json.WriteArrayEnd();
     json.WriteMember("address");
     json.WriteObjectStart();
     json.WriteAttribute("street", "1 Microsoft Way");
     json.WriteAttribute("city", "Redmond");
     json.WriteAttribute("zip", 98052);
     json.WriteObjectEnd();
     json.WriteObjectEnd();
 }
Example #16
0
 private static void WriteContact()
 {
     using (JsonWriter writer = CreateJsonWriter(Console.Out))
     {
         writer.WriteStartObject();                         //  {
         writer.WriteMember("Name");                        //      "Name" :
         writer.WriteString("John Doe");                    //          "John Doe",
         writer.WriteMember("PermissionToCall");            //      "PermissionToCall" :
         writer.WriteBoolean(true);                         //          true,
         writer.WriteMember("PhoneNumbers");                //      "PhoneNumbers" :
         writer.WriteStartArray();                          //          [
         WritePhoneNumber(writer,                           //            { "Location": "Home",
                          "Home", "555-555-1234");          //              "Number": "555-555-1234" },
         WritePhoneNumber(writer,                           //            { "Location": "Work",
                          "Work", "555-555-9999 Ext. 123"); //              "Number": "555-555-9999 Ext. 123" }
         writer.WriteEndArray();                            //          ]
         writer.WriteEndObject();                           //  }
     }
 }
Example #17
0
 static void Write(ref JsonWriter <ArrayFormatter> json)
 {
     json.WriteObjectStart();
     json.WriteAttribute("age", 30);
     json.WriteAttribute("first", "John");
     json.WriteAttribute("last", "Smith");
     json.WriteMember("phoneNumbers");
     json.WriteArrayStart();
     json.WriteString("425-000-1212");
     json.WriteString("425-000-1213");
     json.WriteArrayEnd();
     json.WriteMember("address");
     json.WriteObjectStart();
     json.WriteAttribute("street", "1 Microsoft Way");
     json.WriteAttribute("city", "Redmond");
     json.WriteAttribute("zip", 98052);
     json.WriteObjectEnd();
     json.WriteObjectEnd();
 }
Example #18
0
        private void AddJsonProperties(JsonWriter writer, PropertyInfo[] properties, bool inArray)
        {
            writer.WriteObject();
            foreach (PropertyInfo property in properties)
            {
                if (IsValidProperty(property))
                {
                    string jsonPropertyType = GetPropertyTypeName(property.PropertyType, TDataExchangeFormat.Json);

                    if (!inArray)
                    {
                        writer.WriteMember(property.Name);
                        writer.WriteObject();
                    }

                    writer.WriteMember("type");
                    writer.WriteValue(jsonPropertyType);

                    if (jsonPropertyType.Equals("object") || jsonPropertyType.Equals("array"))
                    {
                        bool inArray2 = false;
                        if (jsonPropertyType.Equals("object"))
                        {
                            writer.WriteMember("properties");
                            inArray2 = false;
                        }
                        else
                        {
                            writer.WriteMember("items");
                            inArray2 = true;
                        }

                        AddJsonProperties(writer, property.PropertyType.GetProperties(), inArray2);
                    }
                    if (!inArray)
                    {
                        writer.WriteEndObject();
                    }
                }
            }
            writer.WriteEndObject();
        }
Example #19
0
        private string GenerateJsonSchema()
        {
            List <string> required = new List <string>();
            MemoryStream  stream   = new MemoryStream();
            JsonWriter    writer   = new JsonWriter(stream);

            writer.WriteObject();

            writer.WriteMember("$schema");
            writer.WriteValue("http://json-schema.org/draft-04/schema");

            writer.WriteMember("title");
            writer.WriteValue(Object.Name);

            writer.WriteMember("type");
            writer.WriteValue("object");

            writer.WriteMember("properties");

            AddJsonProperties(writer, Object.GetProperties(), false);

            if (required.Count > 0)
            {
                writer.WriteMember("required");
                writer.WriteArray();
                foreach (string requirement in required)
                {
                    writer.WriteValue(requirement);
                }
                writer.WriteEndArray();
            }

            writer.WriteEndObject();

            writer.Flush();
            stream.Position = 0;

            string unformattedJsonBody = new StreamReader(stream).ReadToEnd();
            object parsedJson          = JsonConvert.DeserializeObject(unformattedJsonBody);

            return(JsonConvert.SerializeObject(parsedJson, Newtonsoft.Json.Formatting.Indented));
        }
Example #20
0
        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);
                    }
                }
            }
        }
Example #21
0
        private static void ExportMembers(ExportContext context, IEnumerable<KeyValuePair<string, object>> members, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(members != null);
            Debug.Assert(writer != null);

            foreach (var member in members)
            {
                writer.WriteMember(member.Key);
                context.Export(member.Value, writer);
            }
        }
Example #22
0
        private static void ExportMembers(ExportContext context, IEnumerable <KeyValuePair <string, object> > members, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(members != null);
            Debug.Assert(writer != null);

            foreach (var member in members)
            {
                writer.WriteMember(member.Key);
                context.Export(member.Value, writer);
            }
        }
     private static void FormatDataSet(DataSet dataSet, JsonWriter writer)
     {
         writer.WriteStartObject();
 
         foreach (DataTable table in dataSet.Tables)
         {
             writer.WriteMember(table.TableName);
             DataTableFormatter.FormatTable(table, writer);
         }
 
         writer.WriteEndObject();
     }
        internal static void FormatTable(DataTable table, JsonWriter writer)
        {
            Debug.Assert(table != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();
            
            writer.WriteMember("rows");
            writer.WriteValue(table.Rows);

            writer.WriteEndObject();
        }
     private static void FormatRowView(DataRowView rowView, JsonWriter writer)
     {
         writer.WriteStartObject();
 
         foreach (DataColumn column in rowView.DataView.Table.Columns)
         {
             writer.WriteMember(column.ColumnName);
             writer.WriteValue(rowView[column.Ordinal]);
         }
 
         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);
                }
            }
        }
Example #27
0
 public void Serialise(JsonWriter writer)
 {
     writer.WriteObject();
     if (Links != null)
     {
         Links.Serialise(writer);
     }
     if (!string.IsNullOrEmpty(_Resource.InstanceID))
     {
         writer.WriteMember("InstanceID");
         writer.WriteValue(_Resource.InstanceID);
     }
     foreach (Model.PropertyDefinition propertyMetadata in ObjectDefinition.Properties)
     {
         Model.Property property = _Resource.GetProperty(propertyMetadata.PropertyID);
         if (property != null)
         {
             if (propertyMetadata.IsCollection)
             {
                 if ((property.Values != null) && (property.Values.Count > 0))
                 {
                     writer.WriteMember(propertyMetadata.SerialisationName);
                     writer.WriteArray();
                     foreach (Model.PropertyValue item in property.Values)
                     {
                         Serialise(writer, propertyMetadata.DataType, item);
                     }
                     writer.WriteEndArray();
                 }
             }
             else
             {
                 writer.WriteMember(propertyMetadata.SerialisationName);
                 Serialise(writer, propertyMetadata.DataType, property.Value);
             }
         }
     }
     writer.WriteEndObject();
 }
Example #28
0
 private static void ExportDataSet(ExportContext context, DataSet dataSet, JsonWriter writer)
 {
     Debug.Assert(context != null);
     Debug.Assert(dataSet != null);
     Debug.Assert(writer != null);
     writer.WriteStartObject();
     foreach (DataTable table in dataSet.Tables)
     {
         writer.WriteMember(table.TableName);
         DataTableExporter.ExportTable(context, table, writer);
     }
     writer.WriteEndObject();
 }
Example #29
0
 private static void ExportRowView(ExportContext context, DataRowView rowView, JsonWriter writer)
 {
     Debug.Assert(context != null);
     Debug.Assert(rowView != null);
     Debug.Assert(writer != null);
     writer.WriteStartObject();
     foreach (DataColumn column in rowView.DataView.Table.Columns)
     {
         writer.WriteMember(column.ColumnName);
         context.Export(rowView[column.Ordinal], writer);
     }
     writer.WriteEndObject();
 }
        internal static void FormatView(DataView view, JsonWriter writer)
        {
            Debug.Assert(view != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            writer.WriteMember("columns");

            writer.WriteStartArray();
            foreach (DataColumn column in view.Table.Columns)
                writer.WriteValue(column.ColumnName);
            writer.WriteEndArray();

            writer.WriteMember("rows");

            writer.WriteStartArray();
            foreach (DataRowView row in view)
                writer.WriteValue(row.Row.ItemArray);
            writer.WriteEndArray();

            writer.WriteEndObject();
        }
 protected override void ExportValue(object value, JsonWriter writer)
 {
     writer.WriteStartObject();
     
     IDictionary dictionary = (IDictionary) value;
     
     foreach (DictionaryEntry entry in dictionary)
     {
         writer.WriteMember(entry.Key.ToString());
         writer.WriteValue(entry.Value);
     }
     
     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);
            writer.WriteStartObject();
            IDictionary dictionary = (IDictionary)value;

            foreach (DictionaryEntry item in dictionary)
            {
                writer.WriteMember(item.Key.ToString());
                context.Export(item.Value, writer);
            }
            writer.WriteEndObject();
        }
        private static void ExportDataSet(DataSet dataSet, JsonWriter writer)
        {
            Debug.Assert(dataSet != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();
    
            foreach (DataTable table in dataSet.Tables)
            {
                writer.WriteMember(table.TableName);
                DataTableExporter.ExportTable(table, writer);
            }
    
            writer.WriteEndObject();
        }
        internal static void FormatDataRow(DataRow row, JsonWriter writer)
        {
            Debug.Assert(row != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();
    
            foreach (DataColumn column in row.Table.Columns)
            {
                writer.WriteMember(column.ColumnName);
                writer.WriteValue(row[column]);
            }
    
            writer.WriteEndObject();
        }
Example #36
0
        internal static void ExportRecord(ExportContext context, ICustomTypeDescriptor record, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(record != null);
            Debug.Assert(writer != null);
            
            writer.WriteStartObject();
                        
            foreach (PropertyDescriptor property in record.GetProperties())
            {
                writer.WriteMember(property.Name);
                context.Export(property.GetValue(record), writer);
            }

            writer.WriteEndObject();
        }
        internal static void ExportRecord(ExportContext context, ICustomTypeDescriptor record, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(record != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            foreach (PropertyDescriptor property in record.GetProperties())
            {
                writer.WriteMember(property.Name);
                context.Export(property.GetValue(record), writer);
            }

            writer.WriteEndObject();
        }
        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();
        }
Example #39
0
    public void Export(ExportContext context, object value, JsonWriter writer)
    {
        var properties = value.GetType().GetProperties();

        writer.WriteStartObject();
        foreach (var property in properties)
        {
            var propertyValue = property.GetValue(value, null);
            if (!JsonNull.LogicallyEquals(propertyValue))
            {
                writer.WriteMember(property.Name);
                context.Export(propertyValue, writer);
            }
        }
        writer.WriteEndObject();
    }
Example #40
0
 private static void WriteQuery(JsonWriter writer, Spec.Query query)
 {
     writer.BeginArray();
     writer.WriteNumber((int)query.type);
     if (query.type == Spec.Query.QueryType.START)
     {
         WriteTerm(writer, query.query);
         writer.BeginObject();
         foreach (var opt in query.global_optargs)
         {
             writer.WriteMember(opt.key);
             WriteTerm(writer, opt.val);
         }
         writer.EndObject();
     }
     writer.EndArray();
 }
        protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(value != null);
            Debug.Assert(writer != null);

            writer.WriteStartObject();

            var dictionary = (IDictionary)value;

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

            writer.WriteEndObject();
        }
Example #42
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 DictionaryHelper.GetEntries(dictionary))
            {
                writer.WriteMember(entry.Key.ToString());
                context.Export(entry.Value, 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);

            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();
        }
        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();
        }
        private static void ExportCollection(NameValueCollection collection, JsonWriter writer)
        {
            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)
                    writer.WriteValue(values);
                else
                    writer.WriteValue(values[0]);
            }

            writer.WriteEndObject();
        }
Example #46
0
        private void ToJson(Stream stream)
        {
            JsonWriter writer = new JsonWriter(stream);

            writer.WriteObject();
            _Instances.Links.Serialise(writer);
            _Instances.PageInfo.Serialise(writer);

            writer.WriteMember("Items");
            writer.WriteArray();
            if (_Instances.Items != null)
            {
                foreach (ObjectInstance instance in _Instances.Items)
                {
                    instance.Serialise(writer);
                }
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
            writer.Flush();

            stream.Position = 0;
        }
Example #47
0
 public void Serialise(JsonWriter writer)
 {
     writer.WriteObject();
     if (Links != null)
     {
         Links.Serialise(writer);
     }
     if (!string.IsNullOrEmpty(_Resource.InstanceID))
     {
         writer.WriteMember("InstanceID");
         writer.WriteValue(_Resource.InstanceID);
     }
     foreach (Model.PropertyDefinition propertyMetadata in ObjectDefinition.Properties)
     {
         Model.Property property = _Resource.GetProperty(propertyMetadata.PropertyID);
         if (property != null)
         {
             if (propertyMetadata.IsCollection)
             {
                 if ((property.Values != null) && (property.Values.Count > 0))
                 {
                     writer.WriteMember(propertyMetadata.SerialisationName);
                     writer.WriteArray();
                     foreach (Model.PropertyValue item in property.Values)
                     {
                         Serialise(writer, propertyMetadata.DataType, item);
                     }
                     writer.WriteEndArray();
                 }
             }
             else
             {
                 writer.WriteMember(propertyMetadata.SerialisationName);
                 Serialise(writer, propertyMetadata.DataType, property.Value);
             }
         }
     }
     writer.WriteEndObject();
 }
        private void ToJson(Stream stream)
        {
            using (JsonWriter jsonWriter = new JsonWriter(stream))
            {
                jsonWriter.WriteObject();

                jsonWriter.WriteMember("Items");
                jsonWriter.WriteArray();

                jsonWriter.WriteObject();
                LinkExtensions.Serialise(GetLinks(), jsonWriter);

                jsonWriter.WriteMember("TimeTriggered");
                jsonWriter.WriteValue(_WebhookNotification.TimeTriggered);

                jsonWriter.WriteMember("SubscriptionType");
                jsonWriter.WriteValue(_WebhookNotification.SubscriptionType);

                if (_WebhookNotification.ObjectDefinition != null)
                {
                    jsonWriter.WriteMember("Value");
                    ObjectInstance instance = new ObjectInstance(_WebhookNotification.ObjectDefinition, _WebhookNotification.ChangedObject);
                    instance.Links = new List<Link>();
                    instance.Serialise(jsonWriter);
                }

                jsonWriter.WriteEndObject();

                jsonWriter.WriteEndArray();
                jsonWriter.WriteEndObject();
                jsonWriter.Flush();
            }
            stream.Position = 0;
        }
Example #49
0
        /// <summary>
        /// Converts XML to JsonML array form.
        /// </summary>

        public static void EncodeArrayForm(XmlReader reader, JsonWriter writer)
        {
            if (reader == null) throw new ArgumentNullException("reader");
            if (writer == null) throw new ArgumentNullException("writer");

            if (reader.MoveToContent() != XmlNodeType.Element)
                throw new ArgumentException(null, "reader");

            writer.WriteStartArray();
            writer.WriteString(reader.Name);

            //
            // Write attributes
            //

            if (reader.MoveToFirstAttribute())
            {
                writer.WriteStartObject();

                do
                {
                    writer.WriteMember(reader.Name);
                    writer.WriteString(reader.Value);
                }
                while (reader.MoveToNextAttribute());

                writer.WriteEndObject();
                reader.MoveToElement();
            }

            bool isEmpty = reader.IsEmptyElement;

            if (!isEmpty)
            {
                reader.Read();

                //
                // Write child nodes (text, CDATA and element)
                //

                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA)
                    {
                        writer.WriteString(reader.Value);
                        reader.Read();
                    }
                    else if (reader.NodeType == XmlNodeType.Element)
                    {
                        Encode(reader, writer);
                    }
                    else
                    {
                        reader.Read();
                    }
                }
            }

            writer.WriteEndArray();
            reader.Read();
        }
Example #50
0
        /// <summary>
        /// Converts XML to JsonML object form.
        /// </summary>

        public static void EncodeObjectForm(XmlReader reader, JsonWriter writer)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (reader.MoveToContent() != XmlNodeType.Element)
            {
                throw new ArgumentException(null, "reader");
            }

            writer.WriteStartObject();
            writer.WriteMember("tagName");
            writer.WriteString(reader.Name);

            //
            // Write attributes
            //

            if (reader.MoveToFirstAttribute())
            {
                do
                {
                    writer.WriteMember(reader.Name);
                    writer.WriteString(reader.Value);
                }while (reader.MoveToNextAttribute());

                reader.MoveToElement();
            }

            bool isEmpty = reader.IsEmptyElement;

            if (!isEmpty)
            {
                reader.Read();

                int childCount = 0;

                //
                // Write child nodes (text, CDATA and element)
                //

                XmlNodeType nodeType;
                while ((nodeType = reader.NodeType) != XmlNodeType.EndElement)
                {
                    if (nodeType == XmlNodeType.Text ||
                        nodeType == XmlNodeType.CDATA ||
                        nodeType == XmlNodeType.Element)
                    {
                        if (childCount == 0)
                        {
                            writer.WriteMember("childNodes");
                            writer.WriteStartArray();
                        }

                        if (nodeType == XmlNodeType.Text || nodeType == XmlNodeType.CDATA)
                        {
                            writer.WriteString(reader.Value);
                            reader.Read();
                        }
                        else if (nodeType == XmlNodeType.Element)
                        {
                            EncodeObjectForm(reader, writer);
                        }

                        childCount++;
                    }
                    else
                    {
                        reader.Read();
                    }
                }

                if (childCount > 0)
                {
                    writer.WriteEndArray();
                }
            }

            writer.WriteEndObject();
            reader.Read();
        }
Example #51
0
        protected override void ProcessRequest()
        {
            string httpMethod = Request.RequestType;

            if (!CaselessString.Equals(httpMethod, "GET") &&
                !CaselessString.Equals(httpMethod, "HEAD"))
            {
                throw new JsonRpcException(string.Format("HTTP {0} is not supported for RPC execution. Use HTTP GET or HEAD only.", httpMethod));
            }

            string callback = Mask.NullString(Request.QueryString["jsonp"]);
            bool   padded   = callback.Length > 0;

            //
            // The response type depends on whether JSONP (JSON with
            // Padding) is in effect or not.
            //

            Response.ContentType = padded ? "text/javascript" : "application/json";

            //
            // Validate that the JSONP callback method conforms to the
            // allowed syntax. If not, issue a client-side exception
            // that will certainly help to bring problem to light, even if
            // a little too aggressively.
            //

            if (padded)
            {
                if (!_jsonpex.IsMatch(callback))
                {
                    Response.Write("throw new Error('Invalid JSONP callback parameter value.');");
                    Response.End();
                }
            }

            //
            // Convert the query string into a call object.
            //

            StringWriter sw     = new StringWriter();
            JsonWriter   writer = JsonText.CreateWriter(sw);

            writer.WriteStartObject();

            writer.WriteMember("id");
            writer.WriteNumber(-1);

            writer.WriteMember("method");

            string methodName = Mask.NullString(Request.PathInfo);

            if (methodName.Length == 0)
            {
                writer.WriteNull();
            }
            else
            {
                //
                // If the method name contains periods then we replace it
                // with dashes to mean the one and same thing. This is
                // done to provide dashes as an alternative to some periods
                // since some web servers may block requests (for security
                // reasons) if a path component of the URL contains more
                // than one period.
                //

                writer.WriteString(methodName.Substring(1).Replace('-', '.'));
            }

            writer.WriteMember("params");
            NameValueCollection query = new NameValueCollection(Request.QueryString);

            query.Remove(string.Empty);
            JsonConvert.Export(Request.QueryString, writer);

            writer.WriteEndObject();

            //
            // Delegate rest of the work to JsonRpcDispatcher.
            //

            JsonRpcDispatcher dispatcher = JsonRpcDispatcherFactory.CreateDispatcher(Service);

            using (new JsonRpcDispatchScope(dispatcher, Context))
            {
                dispatcher.RequireIdempotency = true;

                if (padded)
                {
                    //
                    // For JSONP, see details here:
                    // http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
                    //

                    Response.Write(callback);
                    Response.Write('(');
                }

                dispatcher.Process(new StringReader(sw.ToString()), Response.Output, true);

                if (padded)
                {
                    Response.Write(')');
                }
            }
        }
Example #52
0
        /// <summary>
        /// Converts XML to JsonML object form.
        /// </summary>

        public static void EncodeObjectForm(XmlReader reader, JsonWriter writer)
        {
            if (reader == null) throw new ArgumentNullException("reader");
            if (writer == null) throw new ArgumentNullException("writer");

            if (reader.MoveToContent() != XmlNodeType.Element)
                throw new ArgumentException(null, "reader");

            writer.WriteStartObject();
            writer.WriteMember("tagName");
            writer.WriteString(reader.Name);

            //
            // Write attributes
            //

            if (reader.MoveToFirstAttribute())
            {
                do
                {
                    writer.WriteMember(reader.Name);
                    writer.WriteString(reader.Value);
                }
                while (reader.MoveToNextAttribute());

                reader.MoveToElement();
            }

            bool isEmpty = reader.IsEmptyElement;

            if (!isEmpty)
            {
                reader.Read();

                int childCount = 0;

                //
                // Write child nodes (text, CDATA and element)
                //

                XmlNodeType nodeType;
                while ((nodeType = reader.NodeType) != XmlNodeType.EndElement)
                {
                    if (nodeType == XmlNodeType.Text 
                        || nodeType == XmlNodeType.CDATA 
                        || nodeType == XmlNodeType.Element)
                    {
                        if (childCount == 0)
                        {
                            writer.WriteMember("childNodes");
                            writer.WriteStartArray();
                        }

                        if (nodeType == XmlNodeType.Text || nodeType == XmlNodeType.CDATA)
                        {
                            writer.WriteString(reader.Value);
                            reader.Read();
                        }
                        else if (nodeType == XmlNodeType.Element)
                        {
                            EncodeObjectForm(reader, writer);
                        }

                        childCount++;
                    }
                    else
                    {
                        reader.Read();
                    }
                }

                if (childCount > 0)
                    writer.WriteEndArray();
            }

            writer.WriteEndObject();
            reader.Read();
        }
Example #53
0
 public override void WriteMember(string name)
 {
     Palette.Member.Apply();
     inner.WriteMember(name);
 }