本文整理汇总了C#中Newtonsoft.Json.Linq.JValue类的典型用法代码示例。如果您正苦于以下问题:C# JValue类的具体用法?C# JValue怎么用?C# JValue使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JValue类属于Newtonsoft.Json.Linq命名空间,在下文中一共展示了JValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Transform
public static IExpressionConstant Transform(JValue obj)
{
// TODO: make this do something
//var val = obj.Value;
if (obj.Type.Equals(JTokenType.Integer))
{
return NumericValue.Create(obj.ToObject<int>());
}
else if (obj.Type.Equals(JTokenType.Float))
{
return NumericValue.Create(obj.ToObject<decimal>());
}
else if (obj.Type.Equals(JTokenType.String))
{
return new StringValue(obj.ToObject<String>());
}
else if (obj.Type.Equals(JTokenType.Boolean))
{
return new BooleanValue(obj.ToObject<bool>());
}
else if (obj.Type.Equals(JTokenType.Null))
{
//throw new ApplicationException("NULL Not implemented yet");
return null; // TODO: Change this to an option
}
else
{
throw new Exception("Don't know how to transform a " +obj.GetType()+ ".");
}
}
示例2: ConvertPesudoTypes
public static JToken ConvertPesudoTypes(JToken data, FormatOptions fmt)
{
var reqlTypes = data.SelectTokens("$..$reql_type$").ToList();
foreach ( var typeToken in reqlTypes )
{
var reqlType = typeToken.Value<string>();
//JObject -> JProerty -> JVaule:$reql_type$, go backup the chain.
var pesudoObject = typeToken.Parent.Parent as JObject;
JToken convertedValue = null;
if( reqlType == Time )
{
if( fmt.RawTime )
continue;
convertedValue = new JValue(GetTime(pesudoObject));
}
else if( reqlType == GroupedData )
{
if( fmt.RawGroups )
continue;
convertedValue = new JValue(GetGrouped(pesudoObject));
}
else if( reqlType == Binary )
{
if( fmt.RawBinary )
continue;
convertedValue = new JArray(GetBinary(pesudoObject));
}
pesudoObject.Replace(convertedValue);
}
return data;
}
示例3: FromObjectTimeSpan
public void FromObjectTimeSpan()
{
var token1 = new JValue(TimeSpan.FromDays(1));
var token2 = JToken.FromObject(token1);
Assert.IsTrue(JToken.DeepEquals(token1, token2));
Assert.AreEqual(token1.Type, token2.Type);
}
示例4: Process
/// <summary>
/// Processes the specified context.
/// </summary>
/// <param name="context">The context to process.</param>
public override void Process(FormBuilderContext context)
{
if (context.Property.GetCustomAttribute<FormDisplayAttribute>() != null)
{
FormDisplayAttribute formDisplayAttribute = context.Property.GetCustomAttribute<FormDisplayAttribute>();
// Build a new hierarchy object for the element
JObject hierachyObject = new JObject();
JArray hierarcyItems = new JArray();
hierachyObject["type"] = new JValue("section");
hierachyObject["items"] = hierarcyItems;
if (context.CurrentFormElement != null)
{
// Move the current element into the form hierarcy
hierarcyItems.Add(context.CurrentFormElement);
context.CurrentFormElementParent.Remove(context.CurrentFormElement);
context.CurrentFormElementParent.Add(hierachyObject);
}
// Set the new parent element to the current hierarchy
context.CurrentFormElementParent = hierarcyItems;
string cssClasses = ConvertDisplayWidthToCssClass(formDisplayAttribute.DisplayWidth);
if (!string.IsNullOrEmpty(cssClasses))
{
hierachyObject["htmlClass"] = new JValue(cssClasses);
}
}
}
示例5: LabelToTrafficLight
private string LabelToTrafficLight(JValue value)
{
if ((double) value > 0 && (double) value < 60) return "green";
if ((double)value >= 60 && (double)value < 200) return "yellow";
if ((double)value >= 200) return "red";
return null;
}
示例6: FromObjectUri
public void FromObjectUri()
{
var token1 = new JValue(new Uri("http://www.newtonsoft.com"));
var token2 = JToken.FromObject(token1);
Assert.IsTrue(JToken.DeepEquals(token1, token2));
Assert.AreEqual(token1.Type, token2.Type);
}
示例7: ToJsonObject
public object ToJsonObject()
{
var content = new JObject(new JProperty(NAME, new JObject()));
if (_boost == null && _minSimilarity == null && _prefixLength == null)
{
content[NAME][_name] = new JValue(_value);
}
else
{
content[NAME][_name] = new JObject();
content[NAME][_name]["value"] = new JValue(_value);
if (_boost != null)
{
content[NAME][_name]["boost"] = _boost;
}
if (_minSimilarity != null)
{
content[NAME][_name]["min_similarity"] = _minSimilarity;
}
if (_prefixLength != null)
{
content[NAME][_name]["prefix_length"] = _prefixLength;
}
}
return content;
}
示例8: FromObjectGuid
public void FromObjectGuid()
{
var token1 = new JValue(Guid.NewGuid());
var token2 = JToken.FromObject(token1);
Assert.IsTrue(JToken.DeepEquals(token1, token2));
Assert.AreEqual(token1.Type, token2.Type);
}
示例9: ChangeValue
public void ChangeValue()
{
JValue v = new JValue(true);
Assert.AreEqual(true, v.Value);
Assert.AreEqual(JTokenType.Boolean, v.Type);
v.Value = "Pie";
Assert.AreEqual("Pie", v.Value);
Assert.AreEqual(JTokenType.String, v.Type);
v.Value = null;
Assert.AreEqual(null, v.Value);
Assert.AreEqual(JTokenType.Null, v.Type);
v.Value = (int?)null;
Assert.AreEqual(null, v.Value);
Assert.AreEqual(JTokenType.Null, v.Type);
v.Value = "Pie";
Assert.AreEqual("Pie", v.Value);
Assert.AreEqual(JTokenType.String, v.Type);
v.Value = DBNull.Value;
Assert.AreEqual(DBNull.Value, v.Value);
Assert.AreEqual(JTokenType.Null, v.Type);
byte[] data = new byte[0];
v.Value = data;
Assert.AreEqual(data, v.Value);
Assert.AreEqual(JTokenType.Bytes, v.Type);
}
示例10: ToJsonObject
public object ToJsonObject()
{
var content = new JObject();
content[_fieldName] = new JObject();
if (_order != SortOrder.ASC)
{
content[_fieldName]["order"] = "desc";
}
if (_missing != null)
{
content[_fieldName]["missing"] = new JValue(_missing);
}
if (_ignoreUnampped != null)
{
content[_fieldName]["ignore_unmapped"] = _ignoreUnampped;
}
if (_nestedFilter != null)
{
content[_fieldName]["nested_filter"] = _nestedFilter.ToJsonObject() as JObject;
}
return content;
}
示例11: Process
/// <summary>
/// Processes the specified context.
/// </summary>
/// <param name="context">The context to process.</param>
public override void Process(FormBuilderContext context)
{
// Get all help attributes
List<FormHelpAttribute> helps = context.Property.GetCustomAttributes<FormHelpAttribute>().ToList();
// If the current property has no helps this module has nothing to to
if (!helps.Any())
{
return;
}
foreach (FormHelpAttribute help in helps)
{
string helpCssClases = DetermineHelpCssClasses(help.HelpType);
string helpText = GetTextForKey(help.HelpText, context);
// Create the help JSON object
JObject helpObject = new JObject();
helpObject["type"] = new JValue("help");
helpObject["helpvalue"] = new JValue("<div class=\"" + helpCssClases + "\" >" + helpText + "</div>");
if (!string.IsNullOrEmpty(help.Condition))
{
helpObject["condition"] = ConvertConditionToAbsolutePath(context.DtoType.Name, context.FullPropertyPath, help.Condition);
}
context.CurrentFormElementParent.Add(helpObject);
}
}
示例12: Example
public void Example()
{
#region Usage
JValue s1 = new JValue("A string");
JValue s2 = new JValue("A string");
JValue s3 = new JValue("A STRING");
Console.WriteLine(JToken.DeepEquals(s1, s2));
// true
Console.WriteLine(JToken.DeepEquals(s2, s3));
// false
JObject o1 = new JObject
{
{ "Integer", 12345 },
{ "String", "A string" },
{ "Items", new JArray(1, 2) }
};
JObject o2 = new JObject
{
{ "Integer", 12345 },
{ "String", "A string" },
{ "Items", new JArray(1, 2) }
};
Console.WriteLine(JToken.DeepEquals(o1, o2));
// true
Console.WriteLine(JToken.DeepEquals(s1, o1["String"]));
// true
#endregion
}
示例13: Process
/// <summary>
/// Processes the specified context.
/// </summary>
/// <param name="context">The context to process.</param>
public override void Process(FormBuilderContext context)
{
FormArrayAttribute arrayAttribute = context.Property.GetCustomAttribute<FormArrayAttribute>();
// Test to the form subobject attribute
if (arrayAttribute != null)
{
if (context.Property.PropertyType.GetInterfaces()
.Where(i => i.GetTypeInfo().IsGenericType)
.Select(i => i.GetGenericTypeDefinition()).Any(i => i == typeof (IEnumerable<>)))
{
// Get the subtype from a generic type argument
Type subType = context.Property.PropertyType.GetGenericArguments()[0];
// Create the subform
JContainer properties = context.FormBuilder.BuildForm(subType, context.OriginDtoType, context.TargetCulture, context.FullPropertyPath + "[]");
// Merge the properties of the sub object into the current context
JObject currentFormElement = context.GetOrCreateCurrentFormElement();
currentFormElement["key"] = context.FullPropertyPath;
currentFormElement["items"] = properties;
if (!string.IsNullOrEmpty(arrayAttribute.AddButtonTitle))
{
string addText = GetTextForKey(arrayAttribute.AddButtonTitle, context);
currentFormElement["add"] = new JValue(addText);
}
}
else
{
throw new InvalidOperationException("An FormArrayAttribute must always be on a property with a type derived from IEnumerable<>");
}
}
}
示例14: FirebasePriority
internal FirebasePriority(JValue priority)
{
if (priority == null || priority.Type == JTokenType.Null)
{
Type = PriorityType.None;
return;
}
switch (priority.Type)
{
case JTokenType.None:
Type = PriorityType.None;
return;
case JTokenType.Integer:
case JTokenType.Float:
Type = PriorityType.Numeric;
_fp = priority.Value<float>();
return;
case JTokenType.String:
int value;
if (int.TryParse(priority.Value<string>(), out value))
{
Type = PriorityType.Numeric;
_fp = value;
}
else
{
Type = PriorityType.String;
_sp = priority.Value<string>();
}
return;
default:
throw new Exception(string.Format("Unable to load priority of type: {0}", priority.Type));
}
}
示例15: Enquote
///// <summary>
///// Produce a string in double quotes with backslash sequences in all the right places.
/////
///// 常用的用法:String.Format("{0}.setValue({1});", ClientJavascriptID, JsHelper.Enquote(Text))
///// 大部分情况下,可以使用 GetJsString 函数代替此函数
///// 此函数返回的是双引号括起来的字符串,用来作为JSON属性比较合适,一般用在OnAjaxPreRender
///// 但是作为HTML属性时,由于HTML属性本身就是双引号括起来的,就容易引起冲突
/////
///// </summary>
///// <param name="s">A String</param>
///// <returns>A String correctly formatted for insertion in a JSON message.</returns>
/// <summary>
/// 返回的是双引号括起来的字符串,用来作为JSON属性比较合适
/// </summary>
/// <param name="s">源字符串</param>
/// <returns>双引号括起来的字符串</returns>
public static string Enquote(string s)
{
string jsonString = new JValue(s).ToString(Formatting.None);
// The browser HTML parser will see the </script> within the string and it will interpret it as the end of the script element.
// http://www.xiaoxiaozi.com/2010/02/24/1708/
// http://stackoverflow.com/questions/1659749/script-tag-in-javascript-string
jsonString = jsonString.Replace("</script>", @"<\/script>");
return jsonString;
/*
if (s == null || s.Length == 0)
{
return "\"\"";
}
char c;
int i;
int len = s.Length;
StringBuilder sb = new StringBuilder(len + 4);
string t;
sb.Append('"');
for (i = 0; i < len; i += 1)
{
c = s[i];
if ((c == '\\') || (c == '"') || (c == '>'))
{
sb.Append('\\');
sb.Append(c);
}
else if (c == '\b')
sb.Append("\\b");
else if (c == '\t')
sb.Append("\\t");
else if (c == '\n')
sb.Append("\\n");
else if (c == '\f')
sb.Append("\\f");
else if (c == '\r')
sb.Append("\\r");
else
{
if (c < ' ')
{
//t = "000" + Integer.toHexString(c);
string tmp = new string(c, 1);
t = "000" + int.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
sb.Append("\\u" + t.Substring(t.Length - 4));
}
else
{
sb.Append(c);
}
}
}
sb.Append('"');
return sb.ToString();
* */
}