Esempio n. 1
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="settings">JsonWriterSettings</param>
 public JsonDataWriter(JsonWriterSettings settings)
 {
     if (settings == null)
     {
         throw new ArgumentNullException("settings");
     }
     this.Settings = settings;
 }
Esempio n. 2
0
 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;
 }
Esempio n. 3
0
        public static string ToJSONstring(this object obj, int recursiondepth)
        {
            var settings = new jsonfx.JsonWriterSettings();

            //settings.PrettyPrint = true;

            System.Text.StringBuilder output = new System.Text.StringBuilder();
            var writer = new jsonfx.JsonWriter(output, settings);

            writer.Settings.HandleCyclicReferences = true;
            writer.Settings.MaxDepth = recursiondepth;
            writer.Write(obj);
            return(output.ToString());
        }
Esempio n. 4
0
 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, CultureInfo.InvariantCulture);
     this.settings       = settings;
     this.Writer.NewLine = this.settings.NewLine;
 }
Esempio n. 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;
        }
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="output">Stream for writing</param>
        /// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(System.IO.Stream output, JsonWriterSettings settings)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            this.Writer         = new MarkerMetro.Unity.WinLegacy.IO.StreamWriter(output, Encoding.UTF8);
            this.settings       = settings;
            this.Writer.NewLine = this.settings.NewLine;
        }
Esempio n. 7
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;
		}
Esempio n. 8
0
        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;
        }
Esempio n. 9
0
        public static string ToJSONstring(this object obj)
        {
            var settings = new jsonfx.JsonWriterSettings();

            //settings.PrettyPrint = true;

            System.Text.StringBuilder output = new System.Text.StringBuilder();
            var writer = new jsonfx.JsonWriter(output, settings);

            //writer.Settings.HandleCyclicReferences = true;
            if (obj.GetType() == typeof(VariableReference))
            {
                Debug.Log(((VariableReference)obj).VariableName);
            }
            writer.Write(obj);
            return(output.ToString());
        }
Esempio n. 10
0
		public void OpenSerialize () {
			zip = new ZipFile();
			zip.AlternateEncoding = System.Text.Encoding.UTF8;
			zip.AlternateEncodingUsage = ZipOption.Always;
			
			writerSettings = new JsonWriterSettings();
			writerSettings.AddTypeConverter (new VectorConverter());
			writerSettings.AddTypeConverter (new BoundsConverter());
			writerSettings.AddTypeConverter (new LayerMaskConverter());
			writerSettings.AddTypeConverter (new MatrixConverter());
			writerSettings.AddTypeConverter (new GuidConverter());
			writerSettings.AddTypeConverter (new UnityObjectConverter());
			
			//writerSettings.DebugMode = true;
			writerSettings.PrettyPrint = settings.prettyPrint;
			
			meta = new GraphMeta();
		}
Esempio n. 11
0
 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>();
     }
 }
Esempio n. 12
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
        }
Esempio n. 13
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;
		}
        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;
        }
Esempio n. 15
0
		public void OpenSerialize () {
			// Create a new zip file, here we will store all the data
			zip = new ZipFile();
			zip.AlternateEncoding = System.Text.Encoding.UTF8;
			zip.AlternateEncodingUsage = ZipOption.Always;

#if !ASTAR_NO_JSON
			// Add some converters so that we can serialize some Unity types
			writerSettings = new JsonWriterSettings();
			writerSettings.AddTypeConverter (new VectorConverter());
			writerSettings.AddTypeConverter (new BoundsConverter());
			writerSettings.AddTypeConverter (new LayerMaskConverter());
			writerSettings.AddTypeConverter (new MatrixConverter());
			writerSettings.AddTypeConverter (new GuidConverter());
			writerSettings.AddTypeConverter (new UnityObjectConverter());

			writerSettings.PrettyPrint = settings.prettyPrint;
#endif			
			meta = new GraphMeta();
		}
Esempio n. 16
0
		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="output">Stream for writing</param>
		/// <param name="settings">JsonWriterSettings</param>
        public JsonWriter(System.IO.Stream output, JsonWriterSettings settings)
		{
		
			if (output == null)
			{
				throw new ArgumentNullException("output");
			}
			if (settings == null)
			{
				throw new ArgumentNullException("settings");
			}

			this.Writer = new System.IO.StreamWriter(output, Encoding.UTF8);
			this.settings = settings;
			this.Writer.NewLine = this.settings.NewLine;
		}
Esempio n. 17
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 System.IO.StreamWriter(stream, Encoding.UTF8);
			this.settings = settings;
			this.Writer.NewLine = this.settings.NewLine;
		}
Esempio n. 18
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> ();
			}
		}
Esempio n. 19
0
        /// <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;
        }
Esempio n. 20
0
        public void GetMemberWritingMap(Type objectType, JsonWriterSettings settings, out Member[] outMembers)
        {
            if (writingMaps == null)
            {
                writingMaps = new Dictionary <Type, Member[]> ();
            }

            if (writingMaps.TryGetValue(objectType, out outMembers))
            {
                return;
            }

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

            Type tp = objectType;

            if (memberList == null)
            {
                memberList = new List <Member> ();
            }

            memberList.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;
                    }

                    memberList.Add(new Member(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;
                    }

                    memberList.Add(new Member(propertyName, property));
                }

                tp = tp.BaseType;
            }

            for (int i = 0; i < memberList.Count; i++)
            {
                memberList [i].index = i;
            }

            memberList.Sort((a, b) => {
                var attrA  = Attribute.GetCustomAttribute(a.member, typeof(JsonOrderAttribute), true) as JsonOrderAttribute;
                var attrB  = Attribute.GetCustomAttribute(b.member, typeof(JsonOrderAttribute), true) as JsonOrderAttribute;
                var orderA = attrA != null ? attrA.order : 0;
                var orderB = attrB != null ? attrB.order : 0;
                var c      = orderA.CompareTo(orderB);
                if (c != 0)
                {
                    return(c);
                }

                // Ensure stable sort
                return(a.index.CompareTo(b.index));
            });

            outMembers = memberList.ToArray();
            writingMaps[objectType] = outMembers;
        }