/// <summary>
 /// Ctor
 /// </summary>
 /// <param name="settings">JsonWriterSettings</param>
 public JsonDataWriter(JsonWriterSettings settings)
 {
     if (settings == null)
     {
         throw new ArgumentNullException("settings");
     }
     this.Settings = settings;
 }
Example #2
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">Stream for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(Stream output, JsonWriterSettings settings)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.Writer         = new StreamWriter(output, Encoding.UTF8);
            this.settings       = settings;
            this.Writer.NewLine = this.settings.NewLine;
        }
Example #3
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">StringBuilder for appending</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(StringBuilder output, JsonWriterSettings settings)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.Writer         = new StringWriter(output, System.Globalization.CultureInfo.InvariantCulture);
            this.settings       = settings;
            this.Writer.NewLine = this.settings.NewLine;
        }
Example #4
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">file name for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(string outputFileName, JsonWriterSettings settings)
        {
            if (outputFileName == null)
            {
                throw new ArgumentNullException("outputFileName");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            Stream stream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write, FileShare.Read);

            this.Writer         = new StreamWriter(stream, Encoding.UTF8);
            this.settings       = settings;
            this.Writer.NewLine = this.settings.NewLine;
        }
Example #5
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">TextWriter for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(TextWriter output, JsonWriterSettings settings)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.Writer         = output;
            this.settings       = settings;
            this.Writer.NewLine = this.settings.NewLine;

            if (settings.HandleCyclicReferences)
            {
                this.previouslySerializedObjects = new Dictionary <object, int> ();
            }
        }
Example #6
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">file name for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(string outputFileName, JsonWriterSettings settings)
        {
#if WINDOWS_STORE && !DEBUG
            throw new System.NotSupportedException("Not supported on this platform");
#else
            if (outputFileName == null)
            {
                throw new ArgumentNullException("outputFileName");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            Stream stream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
            this.Writer         = new StreamWriter(stream, Encoding.UTF8);
            this.settings       = settings;
            this.Writer.NewLine = this.settings.NewLine;
#endif
        }
		public void GetMemberWritingMap (Type objectType, JsonWriterSettings settings, out KeyValuePair<string,FieldInfo>[] outFields, out KeyValuePair<string,PropertyInfo>[] outProps) {

			if ( writingMaps == null ) {
				writingMaps = new Dictionary<Type,KeyValuePair< KeyValuePair<string,FieldInfo>[] , KeyValuePair<string,PropertyInfo>[] >> ();
			}

			KeyValuePair< KeyValuePair<string,FieldInfo>[] , KeyValuePair<string,PropertyInfo>[]> pair;
			if (writingMaps.TryGetValue (objectType, out pair)) {
				outFields = pair.Key;
				outProps = pair.Value;
				return;
			}

			bool anonymousType = objectType.IsGenericType && objectType.Name.StartsWith(JsonWriter.AnonymousTypePrefix);

			Type tp = objectType;

			if (fieldList == null)
				fieldList = new List<KeyValuePair<string, FieldInfo>> ();

			if (propList == null)
				propList = new List<KeyValuePair<string, PropertyInfo>> ();

			fieldList.Clear ();
			propList.Clear ();

			while (tp != null) {

				FieldInfo[] fields = tp.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
				for (int j = 0; j < fields.Length; j++ )
				{
					FieldInfo field = fields[j];

					if (field.IsStatic || (!field.IsPublic && field.GetCustomAttributes (typeof(JsonMemberAttribute), true).Length == 0)) {
						//if (Settings.DebugMode)
							//	Console.WriteLine ("Cannot serialize " + field.Name + " : not public or is static (and does not have a JsonMember attribute)");
						continue;
					}

					if (settings.IsIgnored (objectType, field, null)) {
						//if (Settings.DebugMode)
						//	Console.WriteLine ("Cannot serialize " + field.Name + " : ignored by settings");
						continue;
					}

					// use Attributes here to control naming
					string fieldName = JsonNameAttribute.GetJsonName (field);
					if (String.IsNullOrEmpty (fieldName))
						fieldName = field.Name;

					fieldList.Add (new KeyValuePair<string, FieldInfo> (fieldName, field));
				}

				PropertyInfo[] properties = tp.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
				for (int j = 0; j < properties.Length; j++ )
				{
					PropertyInfo property  = properties[j];

					//Console.WriteLine (property.Name);
					if (!property.CanRead) {
						//if (Settings.DebugMode)
						//	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot read");
						continue;
					}

					if (!property.CanWrite && !anonymousType) {
						//if (Settings.DebugMode)
						//	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot write");
						continue;
					}

					if (settings.IsIgnored(objectType, property, null)) {
						//if (Settings.DebugMode)
						//	Console.WriteLine ("Cannot serialize "+property.Name+" : is ignored by settings");
						continue;
					}

					if (property.GetIndexParameters ().Length != 0) {
						//if (Settings.DebugMode)
						//	Console.WriteLine ("Cannot serialize "+property.Name+" : is indexed");
						continue;
					}


					// use Attributes here to control naming
					string propertyName = JsonNameAttribute.GetJsonName(property);
					if (String.IsNullOrEmpty(propertyName))
						propertyName = property.Name;

					propList.Add ( new KeyValuePair<string, PropertyInfo>(propertyName, property));
				}

				tp = tp.BaseType;
			}

			outFields = fieldList.ToArray();
			outProps = propList.ToArray();

			pair = new KeyValuePair< KeyValuePair<string,FieldInfo>[] , KeyValuePair<string,PropertyInfo>[]> ( outFields, outProps );

			writingMaps[objectType] = pair;
		}
        /// <summary>
        /// Builds a common settings objects
        /// </summary>
        /// <param name="prettyPrint"></param>
        /// <returns></returns>
        public static JsonWriterSettings CreateSettings(bool prettyPrint)
        {
            JsonWriterSettings settings = new JsonWriterSettings();

            settings.PrettyPrint = prettyPrint;

            return settings;
        }
        public void GetMemberWritingMap(Type objectType, JsonWriterSettings settings, out KeyValuePair <string, FieldInfo>[] outFields, out KeyValuePair <string, PropertyInfo>[] outProps)
        {
            if (writingMaps == null)
            {
                writingMaps = new Dictionary <Type, KeyValuePair <KeyValuePair <string, FieldInfo>[], KeyValuePair <string, PropertyInfo>[]> > ();
            }

            KeyValuePair <KeyValuePair <string, FieldInfo>[], KeyValuePair <string, PropertyInfo>[]> pair;

            if (writingMaps.TryGetValue(objectType, out pair))
            {
                outFields = pair.Key;
                outProps  = pair.Value;
                return;
            }

            bool anonymousType = objectType.IsGenericType && objectType.Name.StartsWith(JsonWriter.AnonymousTypePrefix);

            Type tp = objectType;

            if (fieldList == null)
            {
                fieldList = new List <KeyValuePair <string, FieldInfo> > ();
            }

            if (propList == null)
            {
                propList = new List <KeyValuePair <string, PropertyInfo> > ();
            }

            fieldList.Clear();
            propList.Clear();

            while (tp != null)
            {
                FieldInfo[] fields = tp.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                for (int j = 0; j < fields.Length; j++)
                {
                    FieldInfo field = fields[j];

                    if (field.IsStatic || (!field.IsPublic && field.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize " + field.Name + " : not public or is static (and does not have a JsonMember attribute)");
                        continue;
                    }

                    if (settings.IsIgnored(objectType, field, null))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize " + field.Name + " : ignored by settings");
                        continue;
                    }

                    // use Attributes here to control naming
                    string fieldName = JsonNameAttribute.GetJsonName(field);
                    if (String.IsNullOrEmpty(fieldName))
                    {
                        fieldName = field.Name;
                    }

                    fieldList.Add(new KeyValuePair <string, FieldInfo> (fieldName, field));
                }

                PropertyInfo[] properties = tp.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                for (int j = 0; j < properties.Length; j++)
                {
                    PropertyInfo property = properties[j];

                    //Console.WriteLine (property.Name);
                    if (!property.CanRead)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot read");
                        continue;
                    }

                    if (!property.CanWrite && !anonymousType)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : cannot write");
                        continue;
                    }

                    if (settings.IsIgnored(objectType, property, null))
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : is ignored by settings");
                        continue;
                    }

                    if (property.GetIndexParameters().Length != 0)
                    {
                        //if (Settings.DebugMode)
                        //	Console.WriteLine ("Cannot serialize "+property.Name+" : is indexed");
                        continue;
                    }


                    // use Attributes here to control naming
                    string propertyName = JsonNameAttribute.GetJsonName(property);
                    if (String.IsNullOrEmpty(propertyName))
                    {
                        propertyName = property.Name;
                    }

                    propList.Add(new KeyValuePair <string, PropertyInfo>(propertyName, property));
                }

                tp = tp.BaseType;
            }

            outFields = fieldList.ToArray();
            outProps  = propList.ToArray();

            pair = new KeyValuePair <KeyValuePair <string, FieldInfo>[], KeyValuePair <string, PropertyInfo>[]> (outFields, outProps);

            writingMaps[objectType] = pair;
        }
Example #10
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">StringBuilder for appending</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(StringBuilder output, JsonWriterSettings settings)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.Writer = new StringWriter(output, System.Globalization.CultureInfo.InvariantCulture);
            this.settings = settings;
            this.Writer.NewLine = this.settings.NewLine;
        }
Example #11
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">file name for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(string outputFileName, JsonWriterSettings settings)
        {
            #if WINDOWS_STORE && !DEBUG
            throw new System.NotSupportedException ("Not supported on this platform");
            #else
            if (outputFileName == null)
            {
                throw new ArgumentNullException("outputFileName");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            Stream stream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
            this.Writer = new StreamWriter(stream, Encoding.UTF8);
            this.settings = settings;
            this.Writer.NewLine = this.settings.NewLine;
            #endif
        }
Example #12
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">Stream for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(Stream output, JsonWriterSettings settings)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.Writer = new StreamWriter(output, Encoding.UTF8);
            this.settings = settings;
            this.Writer.NewLine = this.settings.NewLine;
        }
Example #13
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">TextWriter for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(TextWriter output, JsonWriterSettings settings)
        {
            if (output == null) {
                throw new ArgumentNullException ("output");
            }
            if (settings == null) {
                throw new ArgumentNullException ("settings");
            }

            this.Writer = output;
            this.settings = settings;
            this.Writer.NewLine = this.settings.NewLine;

            if (settings.HandleCyclicReferences)
            {
                this.previouslySerializedObjects = new Dictionary<object, int> ();
            }
        }