当前位置: 首页>>代码示例>>C#>>正文


C# JavaScriptSerializer.Serialize方法代码示例

本文整理汇总了C#中JavaScriptSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# JavaScriptSerializer.Serialize方法的具体用法?C# JavaScriptSerializer.Serialize怎么用?C# JavaScriptSerializer.Serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JavaScriptSerializer的用法示例。


在下文中一共展示了JavaScriptSerializer.Serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SerializeToJsonResponse

 //will attempt to serialize an object of any type
 public static Response SerializeToJsonResponse(dynamic input)
 {
     Nancy.Json.JavaScriptSerializer ser = new JavaScriptSerializer();
     var response = (Response)ser.Serialize(input);
     response.ContentType = "application/json";
     return response;
 }
开发者ID:kevnls,项目名称:StockQuoteService,代码行数:8,代码来源:Utilities.cs

示例2: Should_register_converters_when_asked

        public void Should_register_converters_when_asked()
        {
            // Given
            var defaultSerializer = new JavaScriptSerializer();
            var configuration = new JsonConfiguration(Encoding.UTF8, new[] { new TestConverter() }, new[] { new TestPrimitiveConverter() }, false, false);

            // When
            var serializer = new JavaScriptSerializer(configuration, true, GlobalizationConfiguration.Default);

            var data =
                new TestData()
                {
                    ConverterData =
                        new TestConverterType()
                        {
                            Data = 42,
                        },

                    PrimitiveConverterData =
                        new TestPrimitiveConverterType()
                        {
                            Data = 1701,
                        },
                };

            const string ExpectedJSON = @"{""converterData"":{""dataValue"":42},""primitiveConverterData"":1701}";

            // Then
            serializer.Serialize(data).ShouldEqual(ExpectedJSON);

            serializer.Deserialize<TestData>(ExpectedJSON).ShouldEqual(data);
        }
开发者ID:uliian,项目名称:Nancy,代码行数:32,代码来源:JavaScriptSerializerFixture.cs

示例3: Should_not_register_converters_when_not_asked

        public void Should_not_register_converters_when_not_asked()
        {
            // Given
            var defaultSerializer = new JavaScriptSerializer();

            // When
            var serializer = new JavaScriptSerializer(JsonConfiguration.Default, GlobalizationConfiguration.Default);

            var data =
                new TestData()
                {
                    ConverterData =
                        new TestConverterType()
                        {
                            Data = 42,
                        },

                    PrimitiveConverterData =
                        new TestPrimitiveConverterType()
                        {
                            Data = 1701,
                        },
                };

            const string ExpectedJSON = @"{""converterData"":{""data"":42},""primitiveConverterData"":{""data"":1701}}";

            // Then
            serializer.Serialize(data).ShouldEqual(ExpectedJSON);

            serializer.Deserialize<TestData>(ExpectedJSON).ShouldEqual(data);
        }
开发者ID:uliian,项目名称:Nancy,代码行数:31,代码来源:JavaScriptSerializerFixture.cs

示例4: ToJSON

 public static string ToJSON(this object obj, int recursionDepth)
 {
     JavaScriptSerializer serializer = new JavaScriptSerializer();
     serializer.MaxJsonLength = 2147483647;
     serializer.RecursionLimit = recursionDepth;
     return serializer.Serialize(obj);
 }
开发者ID:kumarkaran,项目名称:MyBrownListAPI,代码行数:7,代码来源:clsDEN.cs

示例5: Should_return_null_if_msg_is_null

        public void Should_return_null_if_msg_is_null()
        {
            // Arrange
            var serializer = new JavaScriptSerializer();


            // Action
            var str = serializer.Serialize<Customer>(null);

            // Asert
            Assert.IsNull(str);
        }
开发者ID:joefeser,项目名称:Burrow.NET,代码行数:12,代码来源:MethodSerialize.cs

示例6: Should_be_able_to_serialise_datetimeoffset_iso_format

        public void Should_be_able_to_serialise_datetimeoffset_iso_format()
        {
            // Given
            var serializer = new JavaScriptSerializer();
            var offset = new DateTimeOffset(2014, 12, 1, 17, 0, 0, new TimeSpan(0, 6, 0, 0));

            // When
            var result = serializer.Serialize(offset);

            // Then
            result.ShouldEqual(@"""2014-12-01T17:00:00.0000000+06:00""");
        }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:12,代码来源:JsonSerializerFixture.cs

示例7: GetJsonContents

        private static Action<Stream> GetJsonContents(object model)
        {
            return stream =>
            {
                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(model);

                var writer = new StreamWriter(stream);

                writer.Write(json);
                writer.Flush();
            };
        }
开发者ID:markrendle,项目名称:Heckle,代码行数:13,代码来源:JsonResponse.cs

示例8: Should_be_able_to_serialise_and_deserialise_datetimeoffset_iso_format

        public void Should_be_able_to_serialise_and_deserialise_datetimeoffset_iso_format()
        {
            // Given
            var serializer = new JavaScriptSerializer();
            var offset = new DateTimeOffset(2014, 12, 1, 17, 0, 0, new TimeSpan(0, 6, 0, 0));

            // When
            var result = serializer.Serialize(offset);

            var actual = serializer.Deserialize<DateTimeOffset>(result);

            // Then
            actual.ShouldEqual(offset);
        }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:14,代码来源:JsonSerializerFixture.cs

示例9: Can_serialize_object

        public void Can_serialize_object()
        {
            // Arrange
            var serializer = new JavaScriptSerializer();


            // Action
            var str = serializer.Serialize(new Customer { FullName = "Bunny", Title = "Mr" });
            Customer obj = serializer.Deserialize<Customer>(str);

            // Asert
            Assert.AreEqual("Bunny", obj.FullName);
            Assert.AreEqual("Mr", obj.Title);
        }
开发者ID:joefeser,项目名称:Burrow.NET,代码行数:14,代码来源:MethodSerialize.cs

示例10: GetProgramListAsJSON

        public static dynamic GetProgramListAsJSON()
        {
            using (var db = new ProgramContext())
            {
                var programs = from p in db.Programs
                               select new
                                   {
                                       Name = p.ProgramName
                                   };
                var serializer = new JavaScriptSerializer();
                return serializer.Serialize(programs);

            }
        }
开发者ID:karl-henrik,项目名称:ScriptsAndStuff,代码行数:14,代码来源:DataHandler.cs

示例11: Should_be_able_to_serialise_and_deserialise_datetimeoffset_iso_format

        public void Should_be_able_to_serialise_and_deserialise_datetimeoffset_iso_format()
        {
            // Given
            var serializer = new JavaScriptSerializer();
            var sb = new StringBuilder();
            var offset = new DateTimeOffset(2014, 12, 1, 17, 0, 0, new TimeSpan(0, 6, 0, 0));

            // When
            serializer.Serialize(offset, sb);
            Console.WriteLine(sb.ToString());
            var actual = serializer.Deserialize<DateTimeOffset>(sb.ToString());

            // Then
            actual.ShouldEqual(offset);
        }
开发者ID:afwilliams,项目名称:Nancy,代码行数:15,代码来源:JsonSerializerFixture.cs

示例12: FireUnityJavascriptEvent

		public static void FireUnityJavascriptEvent (string method, object data)
		{

			UIViewController viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;

			JavaScriptSerializer Serialiser = new JavaScriptSerializer (); 
			string dataJSONString = "null";
			if (data != null) {
				dataJSONString = Serialiser.Serialize (data);
				if (data is String) {
					dataJSONString = "'" + (data as String) + "'";
				}
			}
			string jsCallbackFunction = "if("+method+"){"+method+"("+dataJSONString+");}";
			//only for testing 
			SystemLogger.Log(SystemLogger.Module.PLATFORM, "NotifyJavascript (single object): " + method + ", dataJSONString: " + dataJSONString);

			bool webViewFound = false;
			if (viewController != null && viewController.View != null) {

				UIView[] subViews = viewController.View.Subviews;

				foreach(UIView subView in subViews) {
					if (subView is UIWebView) {
						webViewFound = true;

						// evaluate javascript as a UIWebView
						(subView as UIWebView).EvaluateJavascript (jsCallbackFunction);

					} else if (subView is WKWebView) {
						webViewFound = true;

						// evaluate javascript as a WKWebView
						(subView as WKWebView).EvaluateJavaScript (new NSString(jsCallbackFunction), delegate (NSObject result, NSError error) {
							SystemLogger.Log (SystemLogger.Module.PLATFORM, "NotifyJavascript COMPLETED (" + method + ")");
						});
					}
				}
			} 

			if (webViewFound) {
				SystemLogger.Log (SystemLogger.Module.PLATFORM, "NotifyJavascript EVALUATED (" + method + ")");
			} else {
				SystemLogger.Log (SystemLogger.Module.PLATFORM, "It was not possible to find a WebView to evaluate the javascript method");
			}

		}
开发者ID:lsp1357,项目名称:appverse-mobile,代码行数:47,代码来源:BeaconUtils.cs

示例13: GetJSONObjects

        public IDictionary<int, string> GetJSONObjects(int price)
        {
            var computers = this.sqlDatabase.COMPUTERS.Where(c => c.Price >= price).Select(c => new
            {
                VENDORNAME = c.VENDORNAME,
                PRICE = c.Price
            });

            JavaScriptSerializer js = new JavaScriptSerializer();
            var jsonObjects = new Dictionary<int, string>();
            int fileNameIndexer = 1;
            foreach (var computer in computers)
            {
                jsonObjects.Add(fileNameIndexer++, js.Serialize(computer));
            }

            return jsonObjects;
        }
开发者ID:TeamBromine,项目名称:Main,代码行数:18,代码来源:JsonReportGenerator.cs

示例14: IndexModule

        public IndexModule()
        {
            Get["/test"] = parameters => DataHandler.GetProgramListAsJSON();

            Get["/admin"] = parameter => View["admin.sshtml"];

            Get["/"] = parameters => View["index.sshtml"];

            Post["/update"] = parameters =>
                {
                    var pack = new PackageHandler();

                    pack.ClearPackages();
                    pack.UpdatePackages();

                    return "success";
                };

            Post["/list"] = parameters =>
                {
                    var model = this.Bind<Rootobject>();
                    var scriptFilePath = DataHandler.CreateTempScriptFile(model);
                    var serializer = new JavaScriptSerializer();
                    return serializer.Serialize(scriptFilePath);
                };

            Get["/scriptfile/{scriptFile}"] = parameters =>
                {
                    var response = new Response();

                    var path = HttpRuntime.AppDomainAppPath + @"temp\" + parameters.scriptFile;

                    var buffer = DataHandler.CopyFileToBuffer(path);

                    response.Headers.Add("Content-Disposition", "attachment; filename=" + parameters.scriptFile);

                    DataHandler.CreateResponse(response, buffer);

                    File.Delete(path);

                    return response;

                };
        }
开发者ID:karl-henrik,项目名称:ScriptsAndStuff,代码行数:44,代码来源:IndexModule.cs

示例15: Init

        protected override void Init()
        {
            StatusCode = _statusCode;
            ContentType = "application/json";
            Contents = stream => {
                var streamWriter = new StreamWriter(stream, Encoding.UTF8);
                var bodySerializer = new JavaScriptSerializer();
                object objBody;
                if (string.IsNullOrEmpty(_description))
                    objBody = new { error = GetErrorString(_errorType) };
                else
                    objBody = new { error = GetErrorString(_errorType), error_description = _description };

                var body = bodySerializer.Serialize(objBody);

                streamWriter.Write(body);
                streamWriter.Flush();
            };
        }
开发者ID:simsod,项目名称:Nancy,代码行数:19,代码来源:ErrorResponse.cs


注:本文中的JavaScriptSerializer.Serialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。