本文整理汇总了C#中JsonData.GetValueSafe方法的典型用法代码示例。如果您正苦于以下问题:C# JsonData.GetValueSafe方法的具体用法?C# JsonData.GetValueSafe怎么用?C# JsonData.GetValueSafe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonData
的用法示例。
在下文中一共展示了JsonData.GetValueSafe方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AjaxContext
internal AjaxContext(JsonData input)
{
// get the api out...
string apiKey = input.GetValueSafe<string>("apiKey");
if (string.IsNullOrEmpty(apiKey))
throw new InvalidOperationException("The 'apiKey' value was not specified in the request.");
this.ApiUser = ApiUser.GetOrCreateApiUser(new Guid(apiKey));
if(ApiUser == null)
throw new InvalidOperationException("'ApiUser' is null.");
// do we have a logon token?
string asString = input.GetValueSafe<string>("logonToken");
if (!(string.IsNullOrEmpty(asString)))
{
Token token = Token.GetByToken(this.ApiUser, asString);
if (token == null)
throw new InvalidOperationException(string.Format("A token with ID '{0}' was not found.", asString));
// update...
token.UpdateExpiration();
// set...
this.Token = token;
}
}
示例2: TestGetReportsByUser
public void TestGetReportsByUser()
{
ResetReports();
// create some reports..
User user = this.Creator.CreateUser();
Report report1 = this.Creator.CreateReport(user);
Report report2 = this.Creator.CreateReport(user);
Report report3 = this.Creator.CreateReport(user);
Report report4 = this.Creator.CreateReport(user);
Report report5 = this.Creator.CreateReport(user);
// get...
HandleGetReportsByUser handler = new HandleGetReportsByUser();
JsonData output = new JsonData();
handler.DoRequest(this.CreateJsonData(user), output);
// check...
string asString = output.GetValueSafe<string>("reports");
IList reports = (IList)new JavaScriptSerializer().DeserializeObject(asString);
Assert.AreEqual(5, reports.Count);
// check...
Assert.AreEqual(this.ApiKey, ((IDictionary)reports[0])["apiKey"]);
Assert.AreEqual(user.IdAsString, ((IDictionary)reports[0])["ownerUserId"]);
Assert.AreEqual(this.ApiKey, ((IDictionary)reports[1])["apiKey"]);
Assert.AreEqual(user.IdAsString, ((IDictionary)reports[1])["ownerUserId"]);
Assert.AreEqual(this.ApiKey, ((IDictionary)reports[2])["apiKey"]);
Assert.AreEqual(user.IdAsString, ((IDictionary)reports[2])["ownerUserId"]);
Assert.AreEqual(this.ApiKey, ((IDictionary)reports[3])["apiKey"]);
Assert.AreEqual(user.IdAsString, ((IDictionary)reports[3])["ownerUserId"]);
Assert.AreEqual(this.ApiKey, ((IDictionary)reports[4])["apiKey"]);
Assert.AreEqual(user.IdAsString, ((IDictionary)reports[4])["ownerUserId"]);
}
示例3: buttonEnsureSampleData_Click
void buttonEnsureSampleData_Click(object sender, EventArgs e)
{
this.labelMessage.Text = string.Empty;
// get...
string apiKey = this.textApiKey.Text.Trim();
if (string.IsNullOrEmpty(apiKey))
{
this.labelMessage.Text = "You must enter the API key.";
return;
}
string username = this.textUsername.Text.Trim();
if (string.IsNullOrEmpty(username))
{
this.labelMessage.Text = "You must enter the username.";
return;
}
// load the api...
Entities.ApiUser api = Entities.ApiUser.GetOrCreateApiUser(new Guid(apiKey));
if(api == null)
throw new InvalidOperationException("'api' is null.");
// get the user...
Entities.User user = Entities.User.GetByUsername(api, username);
if(user == null)
{
this.labelMessage.Text = string.Format("The user '{0}' could not be found.", username);
return;
}
// input...
JsonData input = new JsonData();
input["apiKey"] = apiKey;
input["logonToken"] = Entities.Token.CreateToken(api, user).TheToken;
// create...
HandleEnsureTestReports handler = new HandleEnsureTestReports();
JsonData output = new JsonData();
handler.DoRequest(input, output);
// ok...
if (output.GetValueSafe<bool>("isOk"))
this.labelMessage.Text = string.Format("Sample data is available for '{0}'.", username);
else
this.labelMessage.Text = output.GetValueSafe<string>("error");
}
示例4: DoRequest
protected override void DoRequest(AjaxContext context, JsonData input, JsonData output)
{
// get...
string name = input.GetValueSafe<string>("name");
// return...
output["result"] = string.Format("Hello, {0}.", name);
}
示例5: TestHelloWorld
public void TestHelloWorld()
{
JsonData input = new JsonData();
ApplyApiKey(input);
input["name"] = "Martha";
// run...
HelloWorldHandler handler = new HelloWorldHandler();
JsonData output = new JsonData();
handler.DoRequest(input, output);
// dump...
string result = output.GetValueSafe<string>("result");
Console.WriteLine(result);
Assert.AreEqual("Hello, Martha.", result);
}
示例6: TestEnsureTestReports
public void TestEnsureTestReports()
{
ResetReports();
// create some reports..
User user = this.Creator.CreateUser();
// check...
Assert.AreEqual(0, user.GetReports(this).Count<Report>());
// get...
HandleEnsureTestReports handler = new HandleEnsureTestReports();
JsonData output = new JsonData();
handler.DoRequest(this.CreateJsonData(user), output);
// check...
string asString = output.GetValueSafe<string>("reports");
IList reports = (IList)new JavaScriptSerializer().DeserializeObject(asString);
Assert.AreEqual(50, reports.Count);
}
示例7: GetRequiredValue
private object GetRequiredValue(JsonData input, string name)
{
object value = input.GetValueSafe(name);
if(value == null || (value is string && ((string)value).Length == 0))
this.Missings.Add(name);
return value;
}