本文整理汇总了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;
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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""");
}
示例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();
};
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
示例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");
}
}
示例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;
}
示例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;
};
}
示例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();
};
}