Exemple #1
0
 public void SaveConfig(string path, Dictionary <string, object> config)
 {
     using (var jr = new Json.JsonWriter(path))
     {
         jr.Settings.PrettyPrint = true;
         jr.Write(config);
     }
 }
Exemple #2
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());
        }
Exemple #3
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());
        }
Exemple #4
0
        public void SerializeEditorSettings(GraphEditorBase[] editors)
        {
            if (editors == null || !settings.editorSettings) return;

            for (int i=0;i<editors.Length;i++) {
                if (editors[i] == null) return;

                System.Text.StringBuilder output = GetStringBuilder ();//new System.Text.StringBuilder();
                JsonWriter writer = new JsonWriter (output,writerSettings);
                writer.Write (editors[i]);

                byte[] bytes = encoding.GetBytes (output.ToString());

                //Less or equal to 2 bytes means that nothing was saved (file is "{}")
                if (bytes.Length <= 2)
                    continue;

                AddChecksum(bytes);
                zip.AddEntry ("graph"+i+"_editor"+jsonExt,bytes);
            }
        }
Exemple #5
0
        /** Serialize metadata about alll graphs */
        private byte[] SerializeMeta()
        {
            meta.version = AstarPath.Version;
            meta.graphs = data.graphs.Length;
            meta.guids = new string[data.graphs.Length];
            meta.typeNames = new string[data.graphs.Length];
            meta.nodeCounts = new int[data.graphs.Length];
            //meta.settings = settings;

            for (int i=0;i<data.graphs.Length;i++) {
                meta.guids[i] = data.graphs[i].guid.ToString();
                meta.typeNames[i] = data.graphs[i].GetType().FullName;

                meta.nodeCounts[i] = data.graphs[i].nodes==null?0:data.graphs[i].nodes.Length;
            }

            System.Text.StringBuilder output = GetStringBuilder ();//new System.Text.StringBuilder();
            JsonWriter writer = new JsonWriter (output,writerSettings);
            writer.Write (meta);

            return encoding.GetBytes (output.ToString());
        }
Exemple #6
0
        public void SerializeUserConnections(UserConnection[] conns)
        {
            if (conns == null) conns = new UserConnection[0];

            System.Text.StringBuilder output = GetStringBuilder ();//new System.Text.StringBuilder();
            JsonWriter writer = new JsonWriter (output,writerSettings);
            writer.Write (conns);

            byte[] bytes = encoding.GetBytes (output.ToString());
            output = null;

            //If length is <= 2 that means nothing was serialized (file is "[]")
            if (bytes.Length <= 2) return;

            AddChecksum (bytes);
            zip.AddEntry ("connections"+jsonExt,bytes);
        }
Exemple #7
0
        /** Serializes the graph settings to JSON and returns the data */
        public byte[] Serialize(NavGraph graph)
        {
            System.Text.StringBuilder output = GetStringBuilder ();//new System.Text.StringBuilder();
            JsonWriter writer = new JsonWriter (output,writerSettings);
            writer.Write (graph);

            return encoding.GetBytes (output.ToString());
        }
		public void Write (JsonWriter writer, Type type, object value) {
			Dictionary<string,object> dict = WriteJson (type,value);
			writer.Write (dict);
		}
		/** Serializes the graph settings to JSON and returns the data */
		public byte[] Serialize (NavGraph graph) {
#if !ASTAR_NO_JSON
			// Grab a cached string builder to avoid allocations
			var output = GetStringBuilder ();
			var writer = new JsonWriter (output,writerSettings);
			writer.Write (graph);
			
			return encoding.GetBytes (output.ToString());
#else
			var mem = new System.IO.MemoryStream();
			var writer = new System.IO.BinaryWriter(mem);
			var ctx = new GraphSerializationContext (writer);
			graph.SerializeSettings (ctx);
			return mem.ToArray();
#endif
		}
		/** Serialize metadata about all graphs */
		byte[] SerializeMeta () {

			meta.version = AstarPath.Version;
			meta.graphs = data.graphs.Length;
			meta.guids = new string[data.graphs.Length];
			meta.typeNames = new string[data.graphs.Length];
			meta.nodeCounts = new int[data.graphs.Length];

			// For each graph, save the guid
			// of the graph and the type of it
			for (int i=0;i<data.graphs.Length;i++) {
				if (data.graphs[i] == null) continue;
				
				meta.guids[i] = data.graphs[i].guid.ToString();
				meta.typeNames[i] = data.graphs[i].GetType().FullName;
			}

#if !ASTAR_NO_JSON
			// Grab a cached string builder to avoid allocations
			var output = GetStringBuilder ();
			var writer = new JsonWriter (output,writerSettings);
			writer.Write (meta);
			
			return encoding.GetBytes (output.ToString());
#else
			// Serialize the metadata without using json for compatibility
			var mem = new System.IO.MemoryStream();
			var writer = new System.IO.BinaryWriter(mem);
			writer.Write ( "A*" ); // Magic string
			writer.Write (meta.version.Major);
			writer.Write (meta.version.Minor);
			writer.Write (meta.version.Build);
			writer.Write (meta.version.Revision);
			writer.Write (meta.graphs);

			writer.Write (meta.guids.Length);
			for ( int i = 0; i < meta.guids.Length; i++ ) writer.Write ( meta.guids[i] ?? "" );

			writer.Write (meta.typeNames.Length);
			for ( int i = 0; i < meta.typeNames.Length; i++ ) writer.Write ( meta.typeNames[i] ?? "" );

			writer.Write (meta.nodeCounts.Length);
			for ( int i = 0; i < meta.nodeCounts.Length; i++ ) writer.Write ( meta.nodeCounts[i] );

			return mem.ToArray();
#endif
		}
Exemple #11
0
		void IJsonSerializable.WriteJson(JsonWriter writer)
		{
			if (String.IsNullOrEmpty(this.identifier))
			{
				writer.TextWriter.Write("null");
			}
			else
			{
				// TODO: determine if this should output parens around identifier
				writer.TextWriter.Write(this.identifier);
			}
		}
Exemple #12
0
		/// <summary>
		/// Outputs a .NET Regex as an ECMAScript RegExp literal.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="regex"></param>
		/// <param name="isGlobal"></param>
		/// <remarks>
		/// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
		/// </remarks>
		public static void WriteEcmaScriptRegExp(JsonWriter writer, Regex regex, bool isGlobal)
		{
			if (regex == null)
			{
				writer.TextWriter.Write(JsonReader.LiteralNull);
				return;
			}

			// Regex.ToString() returns the original pattern
			string pattern = regex.ToString();
			if (String.IsNullOrEmpty(pattern))
			{
				// must output something otherwise becomes a code comment
				pattern = EcmaScriptWriter.EmptyRegExpLiteral;
			}

			string modifiers = isGlobal ? "g" : "";
			switch (regex.Options & (RegexOptions.IgnoreCase|RegexOptions.Multiline))
			{
				case RegexOptions.IgnoreCase:
				{
					modifiers += "i";
					break;
				}
				case RegexOptions.Multiline:
				{
					modifiers += "m";
					break;
				}
				case RegexOptions.IgnoreCase|RegexOptions.Multiline:
				{
					modifiers += "im";
					break;
				}
			}

			writer.TextWriter.Write(EcmaScriptWriter.RegExpLiteralDelim);

			int length = pattern.Length;
			int start = 0;

			for (int i = start; i < length; i++)
			{
				switch (pattern[i])
				{
					case EcmaScriptWriter.RegExpLiteralDelim:
					{
						writer.TextWriter.Write(pattern.Substring(start, i - start));
						start = i + 1;
						writer.TextWriter.Write(EcmaScriptWriter.OperatorCharEscape);
						writer.TextWriter.Write(pattern[i]);
						break;
					}
				}
			}

			writer.TextWriter.Write(pattern.Substring(start, length - start));
			writer.TextWriter.Write(EcmaScriptWriter.RegExpLiteralDelim);
			writer.TextWriter.Write(modifiers);
		}
Exemple #13
0
		/// <summary>
		/// Outputs a .NET Regex as an ECMAScript RegExp literal.
		/// Defaults to global matching off.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="regex"></param>
		/// <remarks>
		/// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
		/// </remarks>
		public static void WriteEcmaScriptRegExp(JsonWriter writer, Regex regex)
		{
			EcmaScriptWriter.WriteEcmaScriptRegExp(writer, regex, false);
		}
Exemple #14
0
		public static void WriteEcmaScriptDate(JsonWriter writer, DateTime value)
		{
			if (value.Kind == DateTimeKind.Unspecified)
			{
				// unknown timezones serialize directly to become browser-local
				writer.TextWriter.Write(
					EcmaScriptWriter.EcmaScriptDateCtor7,
					value.Year,			// yyyy
					value.Month-1,		// 0-11
					value.Day,			// 1-31
					value.Hour,			// 0-23
					value.Minute,		// 0-60
					value.Second,		// 0-60
					value.Millisecond);	// 0-999
				return;
			}

			if (value.Kind == DateTimeKind.Local)
			{
				// convert server-local to UTC
				value = value.ToUniversalTime();
			}

			// find the time since Jan 1, 1970
			TimeSpan duration = value.Subtract(EcmaScriptWriter.EcmaScriptEpoch);

			// get the total milliseconds
			long ticks = (long)duration.TotalMilliseconds;

			// write out as a Date constructor
			writer.TextWriter.Write(
				EcmaScriptWriter.EcmaScriptDateCtor1,
				ticks);
		}
		/** Serialize metadata about alll graphs */
		private byte[] SerializeMeta () {
			
			meta.version = AstarPath.Version;
			meta.graphs = data.graphs.Length;
			meta.guids = new string[data.graphs.Length];
			meta.typeNames = new string[data.graphs.Length];
			meta.nodeCounts = new int[data.graphs.Length];
			//meta.settings = settings;
			
			for (int i=0;i<data.graphs.Length;i++) {
				if (data.graphs[i] == null) continue;
				
				meta.guids[i] = data.graphs[i].guid.ToString();
				meta.typeNames[i] = data.graphs[i].GetType().FullName;
				
				//meta.nodeCounts[i] = data.graphs[i].nodes==null?0:data.graphs[i].nodes.Length;
			}

#if !ASTAR_NO_JSON
			System.Text.StringBuilder output = GetStringBuilder ();//new System.Text.StringBuilder();
			JsonWriter writer = new JsonWriter (output,writerSettings);
			writer.Write (meta);
			
			return encoding.GetBytes (output.ToString());
#else
			var mem = new System.IO.MemoryStream();
			var writer = new System.IO.BinaryWriter(mem);
			writer.Write ( "A*" ); // Magic string
			writer.Write (meta.version.Major);
			writer.Write (meta.version.Minor);
			writer.Write (meta.version.Build);
			writer.Write (meta.version.Revision);
			writer.Write (meta.graphs);
			writer.Write (meta.guids.Length);
			for ( int i = 0; i < meta.guids.Length; i++ ) writer.Write ( meta.guids[i] );
			writer.Write (meta.typeNames.Length);
			for ( int i = 0; i < meta.typeNames.Length; i++ ) writer.Write ( meta.typeNames[i] );
			writer.Write (meta.nodeCounts.Length);
			for ( int i = 0; i < meta.nodeCounts.Length; i++ ) writer.Write ( meta.nodeCounts[i] );
			return mem.ToArray();
#endif
		}
Exemple #16
0
		/// <summary>
		/// A helper method for serializing an object to JSON
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string Serialize(object value)
		{
			StringBuilder output = new StringBuilder();

			using (JsonWriter writer = new JsonWriter(output))
			{
				writer.Write(value);
			}

			return output.ToString();
		}