Controls the serialization settings for JsonWriter
예제 #1
0
		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="settings">JsonWriterSettings</param>
		public JsonDataWriter(JsonWriterSettings settings)
		{
			if (settings == null)
			{
				throw new ArgumentNullException("settings");
			}
			this.Settings = settings;
		}
예제 #2
0
    void OnGUI()
    {
        content = GUI.TextArea(new Rect(100, 100, 400, 100), content);

           /* if (GUI.Button(new Rect(330, 60, 60, 30), "Little jasson"))
        {
            Debug.Log("write jsson");

            TextWriter wr = new StreamWriter("students.txt");
            JsonWriter jsonwr = new JsonWriter(wr);
            jsonwr.IndentValue = 0;
            JsonMapper.ToJson(st1, jsonwr);
            jsonwr.WriteObjectStart();

            JsonMapper.ToJson(st2, jsonwr);
            wr.Close();

        }*/

        if (GUI.Button(new Rect(380, 60, 60, 30), "FX jasson"))
        {
            /*Классы короткая запись*/
            string json = JsonFx.Json.JsonWriter.Serialize(st1);
           // Debug.Log(json);
            Student st3 = JsonFx.Json.JsonReader.Deserialize<Student>(json);
            //Debug.Log("age " + st3.age + " count " + st3.count[5] + " name " + st3.name);

            /*массив*/

            JsonWriterSettings settw = new JsonWriterSettings();
            settw.TypeHintName = "__type";
            JsonReaderSettings settr = new JsonReaderSettings();
            settr.TypeHintName = "__type";

            /*пример чтоб разобраться, но либа работает только с классами нормально. */
            System.Text.StringBuilder builder=new System.Text.StringBuilder();
            JsonWriter wr = new JsonWriter(builder,settw);
                Student[] arr = new Student[3];
                arr[0] = st1;
                arr[1] = st2;
                arr[2] = st3;
                wr.Write(arr);
                System.IO.File.WriteAllText("testJSON.txt", builder.ToString());

            string jsonText = System.IO.File.ReadAllText("testJSON.txt");
            //Debug.Log(""+jsonText);
            Student[] tempSt = JsonReader.Deserialize<Student[]>(jsonText);
            content = "";
                foreach( var s in tempSt)
                    content +="" + s.name+System.Environment.NewLine;

        }
    }
예제 #3
0
파일: Packet.cs 프로젝트: osROSE/UnityRose
        public Packet()
        {
            type = 0;
            operation = 0;

            output = new StringBuilder();
            JsonWriterSettings settings = new JsonWriterSettings();
            settings.PrettyPrint = false;
            settings.AddTypeConverter (new VectorConverter());
            settings.AddTypeConverter( new QuaternionConverter());
            // TODO: Add any other TypeConverters here
            writer = new JsonWriter (output,settings);
        }
예제 #4
0
 public JsonWriter(TextWriter output, JsonWriterSettings settings)
 {
     if (output == null)
     {
         throw new ArgumentNullException("output");
     }
     if (settings == null)
     {
         throw new ArgumentNullException("settings");
     }
     Writer         = output;
     this.settings  = settings;
     Writer.NewLine = this.settings.NewLine;
 }
예제 #5
0
 public JsonWriter(Stream output, JsonWriterSettings settings)
 {
     if (output == null)
     {
         throw new ArgumentNullException("output");
     }
     if (settings == null)
     {
         throw new ArgumentNullException("settings");
     }
     Writer         = new StreamWriter(output, Encoding.UTF8);
     this.settings  = settings;
     Writer.NewLine = this.settings.NewLine;
 }
예제 #6
0
 public JsonWriter(StringBuilder output, JsonWriterSettings settings)
 {
     if (output == null)
     {
         throw new ArgumentNullException("output");
     }
     if (settings == null)
     {
         throw new ArgumentNullException("settings");
     }
     Writer         = new StringWriter(output, CultureInfo.InvariantCulture);
     this.settings  = settings;
     Writer.NewLine = this.settings.NewLine;
 }
예제 #7
0
파일: JsonWriter.cs 프로젝트: jtabG/Alone
        /// <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;
        }
예제 #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);

            Writer         = new StreamWriter(stream, Encoding.UTF8);
            this.settings  = settings;
            Writer.NewLine = this.settings.NewLine;
        }
예제 #9
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> ();
			}
		}
예제 #10
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
        }
예제 #11
0
        /// <summary>
        /// A helper method for serializing an object to JSON
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Serialize(object value, JsonWriterSettings settings = null)
        {
            StringBuilder output = new StringBuilder();

            if (settings != null)
            {
                using (JsonWriter writer = new JsonWriter(output, settings))
                {
                    writer.Write(value);
                }
            }
            else
            {
                using (JsonWriter writer = new JsonWriter(output))
                {
                    writer.Write(value);
                }
            }

            return(output.ToString());
        }
예제 #12
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;
        }
예제 #13
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;
        }
예제 #14
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;
        }
예제 #15
0
파일: Score.cs 프로젝트: ChrisMaire/BigDogs
    void Awake()
    {
        jsonOutput = new System.Text.StringBuilder();

        JsonReaderSettings readSettings = new JsonReaderSettings();
        readSettings.AddTypeConverter(new RecordConverter());
        jsonReader = new JsonReader(jsonOutput, readSettings);

        JsonWriterSettings writeSettings = new JsonWriterSettings();
        writeSettings.PrettyPrint = true;
        writeSettings.AddTypeConverter(new RecordConverter());
        jsonWriter = new JsonWriter(jsonOutput, writeSettings);

        m_filePath = Application.dataPath;
        if (Application.platform == RuntimePlatform.OSXDashboardPlayer || Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXWebPlayer)
        {
            m_filePath += "/Score/";
        }
        else
        {
            m_filePath += "\\Score\\";
        }

        m_levelScore = 0f;
    }
예제 #16
0
	void PrintResults(string rawJson) {
		// Raw output:
		/*DB.Log(DC.Log("******** raw string from Twitter ********"));
		DB.Log(DC.Log(rawJson));
		
		
		// Turn the JSON into C# objects
		var search = JsonReader.Deserialize<TwitterSearchResults>(rawJson);
		
		
		// iterate through the array of results;
		DB.Log(DC.Log("******** search results ********"));

	
		foreach (var tweet in search.results) {
			DB.Log(DC.Log(tweet.from_user_name + " : " + tweet.text));
		}

		DB.Log(DC.Log("******** serialize an entity ********"));

		JsonWriterSettings settings = new JsonWriterSettings();
		settings.PrettyPrint = true;
		
		System.Text.StringBuilder output = new System.Text.StringBuilder();
		
		JsonWriter writer = new JsonWriter (output,settings);
		writer.Write (search.results[0]);
		
		// this turns a C# object into a JSON string.
		string json = output.ToString();//JsonWriter.Serialize();

		DB.Log(DC.Log(json));*/
		
		for (int i=0;i<10;i++) {
		System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
		watch.Start();
		System.Text.StringBuilder output = new System.Text.StringBuilder();
		
		Debug.Log ("+++ Serializing +++");
		JsonWriterSettings settings = new JsonWriterSettings();
		settings.PrettyPrint = false;
		settings.AddTypeConverter (new VectorConverter());
		
		TestClass test = new TestClass();
		test.vec.y = 128.513589999F;
		JsonWriter writer = new JsonWriter (output,settings);
			
		Debug.Log ("+++ Writing +++");
		writer.Write (test);
		
		if (i==0)
			Debug.Log (output.ToString());
		
		Debug.Log ("+++ Deserializing - Init +++");
		JsonReaderSettings settings2 = new JsonReaderSettings();
		settings2.AddTypeConverter (new VectorConverter());
		JsonReader reader = new JsonReader(output.ToString(),settings2);
			
		Debug.Log ("+++ Deserializing +++");
		TestClass deserialized = reader.Deserialize<TestClass>();
		
		watch.Stop();
		Debug.Log ((watch.ElapsedTicks*0.0001).ToString("0.00"));
		Debug.Log (deserialized.vec.y.ToString("r"));
		}
	}
 public CustomJsonWriter(Stream output, JsonWriterSettings settings) :base (output, settings) {
 }
예제 #18
0
        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;
        }
예제 #19
0
        public static void RunTest(TextWriter writer, string unitTestsFolder, string outputFolder)
        {
            string[] unitTests = Directory.GetFiles(unitTestsFolder, UnitTestsFiles, SearchOption.AllDirectories);
            if (unitTests.Length > 0)
            {
                JsonReaderSettings readerSettings = new JsonReaderSettings();
                readerSettings.TypeHintName = StronglyTyped.MyTypeHintName;
                readerSettings.AllowNullValueTypes = true;
                readerSettings.AllowUnquotedObjectKeys = true;

                JsonWriterSettings writerSettings = new JsonWriterSettings();
                writerSettings.TypeHintName = StronglyTyped.MyTypeHintName;
                writerSettings.PrettyPrint = false;
                writerSettings.MaxDepth = 100;

                writer.WriteLine(JsonText.Seperator);
                writer.WriteLine("JsonReaderSettings:");
                new JsonWriter(writer).Write(readerSettings);

                writer.WriteLine(JsonText.Seperator);
                writer.WriteLine("JsonWriterSettings:");
                new JsonWriter(writer).Write(writerSettings);

                foreach (string unitTest in unitTests)
                {
                    string source = String.Empty;

                    try
                    {
                        writer.WriteLine(JsonText.Seperator);

                        source = File.ReadAllText(unitTest);
                        JsonReader jsonReader = new JsonReader(source, readerSettings);

                        object obj, obj2;
                        obj2 = obj = jsonReader.Deserialize();

                        do
                        {
                            writer.WriteLine("READ: {0}", unitTest.Replace(unitTestsFolder, ""));
                            writer.WriteLine("Result: {0}", (obj == null) ? "null" : obj.GetType().FullName);

                            obj = jsonReader.Deserialize();
                        } while (obj != null);

                        string outputFile = unitTest.Replace(unitTestsFolder, outputFolder);
                        string outputDir = Path.GetDirectoryName(outputFile);
                        if (!Directory.Exists(outputDir))
                        {
                            Directory.CreateDirectory(outputDir);
                        }
                        using (JsonWriter jsonWriter = new JsonWriter(outputFile, writerSettings))
                        {
                            jsonWriter.Write(obj2);
                        }
                    }
                    catch (JsonDeserializationException ex)
                    {
                        int col, line;
                        ex.GetLineAndColumn(source, out line, out col);

                        writer.WriteLine("ERROR: {0}", unitTest.Replace(unitTestsFolder, ""));
                        writer.WriteLine("-- \"{0}\" ({1}, {2})", ex.Message, line, col);
                        continue;
                    }
                    catch (Exception ex)
                    {
                        writer.WriteLine("ERROR: {0}", unitTest.Replace(unitTestsFolder, ""));
                        writer.WriteLine("-- \"{0}\"", ex.Message);
                        continue;
                    }
                }
            }
            else
            {
                writer.WriteLine(ErrorMessage);
            }
        }
        public void SavePreset(string fileName, PresetData preset) {
            if (File.Exists(fileName)) {
                File.Delete(fileName);
            }

            // ファイル出力
            var jws = new JsonWriterSettings();
            //jws.Tab = "  ";
            jws.MaxDepth = 200;
            jws.PrettyPrint = true;
            using (FileStream fs = File.OpenWrite(fileName))
            using (var writer = new CustomJsonWriter(fs, jws)) {
                writer.ignoreNull = true;
                writer.Write(preset);
            }
            LogUtil.Debug("preset saved...", fileName);
        }
예제 #21
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;
		}