本文整理汇总了C#中Newtonsoft.Json.Linq.JValue.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# JValue.ToString方法的具体用法?C# JValue.ToString怎么用?C# JValue.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Linq.JValue
的用法示例。
在下文中一共展示了JValue.ToString方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ValidateOne
private ValidationResult ValidateOne(string path, IMetadataDefinition definition, JValue value)
{
if (definition.ChoiceSet.Contains(value))
{
return ValidationResult.Success;
}
return ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.UndefinedValue, $"Bad metadata: Value {value.ToString()} is undefined for {path}.", path);
}
示例2: ToStringFormat
public void ToStringFormat()
{
JValue v = new JValue(new DateTime(2013, 02, 01, 01, 02, 03, 04));
Assert.AreEqual("2013", v.ToString("yyyy"));
}
示例3: WriteSingle
public void WriteSingle()
{
float f = 5.2f;
JValue value = new JValue(f);
string json = value.ToString(Formatting.None);
Assert.AreEqual("5.2", json);
}
示例4: JValueToString
public void JValueToString()
{
JValue v;
v = new JValue(true);
Assert.AreEqual("True", v.ToString());
v = new JValue(Encoding.UTF8.GetBytes("Blah"));
Assert.AreEqual("System.Byte[]", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue("I am a string!");
Assert.AreEqual("I am a string!", v.ToString());
v = new JValue(null, JTokenType.Null);
Assert.AreEqual("", v.ToString());
v = new JValue(null, JTokenType.Null);
Assert.AreEqual("", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new Uri("http://json.codeplex.com/"));
Assert.AreEqual("http://json.codeplex.com/", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(TimeSpan.FromDays(1));
Assert.AreEqual("1.00:00:00", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5"));
Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture));
#if !(NET20 || NET35 || PORTABLE || PORTABLE40)
v = new JValue(BigInteger.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990"));
Assert.AreEqual("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990", v.ToString(null, CultureInfo.InvariantCulture));
#endif
}
示例5: EnumTests
public void EnumTests()
{
JValue v = new JValue(StringComparison.Ordinal);
Assert.AreEqual(JTokenType.Integer, v.Type);
string s = v.ToString();
Assert.AreEqual("Ordinal", s);
StringComparison e = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e);
dynamic d = new JValue(StringComparison.CurrentCultureIgnoreCase);
StringComparison e2 = (StringComparison)d;
Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, e2);
string s1 = d.ToString();
Assert.AreEqual("CurrentCultureIgnoreCase", s1);
string s2 = (string)d;
Assert.AreEqual("CurrentCultureIgnoreCase", s2);
d = new JValue("OrdinalIgnoreCase");
StringComparison e3 = (StringComparison)d;
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e3);
v = new JValue("ORDINAL");
d = v;
StringComparison e4 = (StringComparison)d;
Assert.AreEqual(StringComparison.Ordinal, e4);
StringComparison e5 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e5);
v = new JValue((int)StringComparison.OrdinalIgnoreCase);
Assert.AreEqual(JTokenType.Integer, v.Type);
StringComparison e6 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6);
}
示例6: EnumTests
public void EnumTests()
{
JValue v = new JValue(StringComparison.Ordinal);
Assert.AreEqual(JTokenType.Integer, v.Type);
string s = v.ToString();
Assert.AreEqual("Ordinal", s);
StringComparison e = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e);
dynamic d = new JValue(StringComparison.CurrentCultureIgnoreCase);
StringComparison e2 = (StringComparison)d;
Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, e2);
string s1 = d.ToString();
Assert.AreEqual("CurrentCultureIgnoreCase", s1);
string s2 = (string)d;
Assert.AreEqual("CurrentCultureIgnoreCase", s2);
d = new JValue("OrdinalIgnoreCase");
StringComparison e3 = (StringComparison)d;
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e3);
v = new JValue("ORDINAL");
d = v;
StringComparison e4 = (StringComparison)d;
Assert.AreEqual(StringComparison.Ordinal, e4);
StringComparison e5 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.Ordinal, e5);
v = new JValue((int)StringComparison.OrdinalIgnoreCase);
Assert.AreEqual(JTokenType.Integer, v.Type);
StringComparison e6 = v.ToObject<StringComparison>();
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6);
// does not support EnumMember. breaking change to add
ExceptionAssert.Throws<ArgumentException>(() =>
{
d = new JValue("value_a");
EnumA e7 = (EnumA)d;
Assert.AreEqual(EnumA.ValueA, e7);
}, "Requested value 'value_a' was not found.");
}
示例7: ToString
public void ToString()
{
JValue v;
v = new JValue(true);
Assert.AreEqual("True", v.ToString());
v = new JValue(Encoding.UTF8.GetBytes("Blah"));
Assert.AreEqual("System.Byte[]", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue("I am a string!");
Assert.AreEqual("I am a string!", v.ToString());
v = new JValue(null, JTokenType.Null);
Assert.AreEqual("", v.ToString());
v = new JValue(null, JTokenType.Null);
Assert.AreEqual("", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
}
示例8: NumericCanRoundtrip
/// <summary>
/// Ensure that a numeric value can safely roundtrip as a double.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="desiredType">The desired type.</param>
/// <returns>
/// True if the value can roundtrip, false otherwise.
/// </returns>
private static bool NumericCanRoundtrip(object value, Type desiredType)
{
// Make sure that the value can roundtrip
bool canRoundtrip = false;
object original = Convert.ChangeType(value, desiredType, CultureInfo.InvariantCulture);
double converted = Convert.ToDouble(value, CultureInfo.InvariantCulture);
try
{
JValue jv = new JValue(converted);
string jvString = jv.ToString();
JValue reverse = JValue.Parse(jvString) as JValue;
if (reverse != null)
{
object roundtripped = Convert.ChangeType(reverse.Value, desiredType, CultureInfo.InvariantCulture);
canRoundtrip = original.Equals(roundtripped);
}
}
catch (InvalidCastException)
{
}
catch (OverflowException)
{
}
return canRoundtrip;
}
示例9: getAuthToken
private string getAuthToken() {
bool needNewToken = false;
if (token == null || token.Equals("")) {
needNewToken = true;
Log.Info("HockeyStreams - Need to login");
} else if (tokenCreateDate == null || tokenCreateDate.AddHours(tokenResetDurationHours) > DateTime.Now) {
// current token is more than 12 hours old
needNewToken = true;
Log.Info("HockeyStreams - Token has expired, need to login again");
}
if (needNewToken) {
string postData = string.Format("username={0}&password={1}&key={2}", username, password, apiKey);
// get the authentication token
String jsonResponse = GetWebData(apiUrl_login,postData,null,null,null, false, false, useragent, null, null, false);
JObject jsonLogin = JObject.Parse(jsonResponse);
JToken authToken = new JValue("");
bool yes = jsonLogin.TryGetValue("token",out authToken);
token = authToken.ToString();
tokenCreateDate = DateTime.Now;
}
return token;
}
示例10: VisitValue
JToken VisitValue(JValue jValue)
{
if (jValue.Type == JTokenType.Boolean)
{
sb.Append(jValue.ToString().ToLower());
}
else if (jValue.Type == JTokenType.String)
{
string s = (string)jValue.Value;
bool needsQuotes =
(s.Length == 0 ||
s.IndexOfAny(new char[] { '"', '\\', '/', '\b', '\f', '\n', '\r', '\t', '#', ':', ',', '[', ']', '{', '}' }) != -1);
// NOTE: The order is important here; \\ should get replaced last
s = s
.Replace("\\", "\\\\")
.Replace("\"", "\\\"")
.Replace("/", "\\/")
.Replace("\b", "\\b")
.Replace("\f", "\\f")
.Replace("\n", "\\n")
.Replace("\r", "\\r")
.Replace("\t", "\\t");
if (needsQuotes)
{
sb.AppendFormat("\"{0}\"", s);
}
else
{
sb.Append(s);
}
}
else if (jValue.Type == JTokenType.Array || jValue.Type == JTokenType.Object)
{
Visit(jValue);
}
else
{
sb.Append(jValue.Value.ToString());
}
return jValue;
}