本文整理汇总了C#中JsonReader.ReadValue方法的典型用法代码示例。如果您正苦于以下问题:C# JsonReader.ReadValue方法的具体用法?C# JsonReader.ReadValue怎么用?C# JsonReader.ReadValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonReader
的用法示例。
在下文中一共展示了JsonReader.ReadValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetResultFromJson
public static object GetResultFromJson(string responseText)
{
var jsonReader = new JsonReader(responseText);
if (responseText == string.Empty) return new JsonObject();
dynamic result = jsonReader.ReadValue();
return result;
}
示例2: while
object IJsonSerializable.Deserialize(JsonReader reader)
{
string property;
while (reader.ReadProperty(out property))
{
switch (property)
{
case "type":
Type = reader.ReadValue<string>();
break;
case "message":
Message = reader.ReadValue<string>();
break;
case "stackTrace":
StackTrace = reader.ReadValue<string>();
break;
case "url":
Url = reader.ReadValue<string>();
break;
case "refererUrl":
RefererUrl = reader.ReadValue<string>();
break;
case "additionalInfo":
AdditionalInfo = reader.ReadValue<Dictionary<string, object>>();
break;
default:
throw new ArgumentException("The specified property could not be deserialized.", property);
}
}
return this;
}
示例3: JsonReader_Normal
public void JsonReader_Normal()
{
string json = @"{ name: ""foo"",
description: ""hello me""}";
using (var jsonReader = new JsonReader(json, false))
{
dynamic requestBodyJson = (IDictionary<string, object>)jsonReader.ReadValue();
Assert.AreEqual("foo", requestBodyJson.name);
Assert.AreEqual("hello me", requestBodyJson.description);
}
}
示例4: TestReadValueObjectEmpty
public void TestReadValueObjectEmpty()
{
var jsonReader = new JsonReader("{}");
object value = jsonReader.ReadValue();
Assert.IsTrue(
value is DynamicDictionary,
"Value read is not a DynamicDictionary");
var dictionary = value as DynamicDictionary;
Assert.AreEqual(0, dictionary.Count);
}
示例5: JsonReader_InvalidFormat
public void JsonReader_InvalidFormat()
{
string json = "{ name: \"";
using (var jsonReader = new JsonReader(json, false))
{
try
{
jsonReader.ReadValue();
Assert.Fail("Expected FormatException to be thrown.");
}
catch (FormatException)
{
}
}
}
示例6: ImportValue
protected override object ImportValue(JsonReader reader)
{
if (reader == null)
throw new ArgumentNullException("reader");
reader.ReadToken(JsonTokenClass.Object);
object o = Activator.CreateInstance(OutputType);
while (reader.TokenClass != JsonTokenClass.EndObject)
{
string memberName = reader.ReadMember();
PropertyDescriptor property = _properties[memberName];
if (property != null && !property.IsReadOnly)
property.SetValue(o, reader.ReadValue(property.PropertyType));
else
reader.Skip();
}
return o;
}
示例7: JsonReader_EscapedCharacters
public void JsonReader_EscapedCharacters()
{
string json = "{" +
"\"b\":\"\\b\"," +
"\"t\":\"\\t\"," +
"\"n\":\"\\n\"," +
"\"f\":\"\\f\"," +
"\"r\":\"\\r\"," +
"\"q\":\"\\\"\"," +
"\"rs\":\"\\\\\"" + "}";
using (var jsonReader = new JsonReader(json, false))
{
dynamic requestBodyJson = (IDictionary<string, object>)jsonReader.ReadValue();
Assert.AreEqual("\b", requestBodyJson.b);
Assert.AreEqual("\t", requestBodyJson.t);
Assert.AreEqual("\n", requestBodyJson.n);
Assert.AreEqual("\f", requestBodyJson.f);
Assert.AreEqual("\r", requestBodyJson.r);
Assert.AreEqual("\"", requestBodyJson.q);
Assert.AreEqual("\\", requestBodyJson.rs);
}
}
示例8: TestReadInvalidJsonText
public void TestReadInvalidJsonText()
{
var jsonReader = new JsonReader("klasdjfas89123;kja#!");
try
{
jsonReader.ReadValue();
Assert.Fail("Expected FormatException to be thrown.");
}
catch (FormatException)
{
}
}
示例9: TestReadInvalidJsonTextEmptyString
public void TestReadInvalidJsonTextEmptyString()
{
var jsonReader = new JsonReader("");
try
{
jsonReader.ReadValue();
Assert.Fail("Expected FormatException to be thrown.");
}
catch (FormatException)
{
}
}
示例10: TestReadValueNull
public void TestReadValueNull()
{
var jsonReader = new JsonReader("{ \"data\": null }");
dynamic value = jsonReader.ReadValue();
Assert.IsNull(value.data, "Expected Value read to be null");
}
示例11: TestReadValueBooleanFalse
public void TestReadValueBooleanFalse()
{
var jsonReader = new JsonReader("{ \"data\": false }");
dynamic value = jsonReader.ReadValue();
Assert.IsTrue(
value.data is bool,
"Value read is not a bool");
Assert.AreEqual(false, (bool)value.data);
}
示例12: TestReadValueFloatWithExpentionalNotationNegative
public void TestReadValueFloatWithExpentionalNotationNegative()
{
// ToString and Back loses some fidelity so we do it now
// to be able to compare the return value from ReadValue
float min = float.Parse(float.MinValue.ToString());
var jsonReader = new JsonReader("{ \"data\": " + min.ToString() + " }");
dynamic value = jsonReader.ReadValue();
Assert.IsTrue(
value.data is float,
"Value read is not a float");
Assert.AreEqual(min, (float)value.data);
}
示例13: Deserialize
public object Deserialize(JsonReader reader)
{
string property;
while (reader.ReadProperty(out property))
{
switch (property)
{
case "source":
Source = reader.ReadValue<ChangeSource>();
break;
case "changes":
Changes = reader.ReadValue<List<ModelEvent>>();
break;
default:
throw new ArgumentException("The specified property could not be deserialized.", property);
}
}
return this;
}
示例14: while
object IJsonSerializable.Deserialize(JsonReader reader)
{
ModelInstance instance = null;
string eventName = null;
string property;
Type eventType = null;
DomainEvent domainEvent = null;
ModelMethod method = null;
object[] methodArgs = null;
while (reader.ReadProperty(out property))
{
switch (property)
{
// Get the event target
case "instance":
instance = reader.ReadValue<ModelInstance>();
break;
// Get the property paths to include
case "include":
Include = reader.ReadValue<string[]>();
break;
// Get the type of event
case "type":
eventName = reader.ReadValue<string>();
break;
// Get the event data
case "event":
if (eventName != null && instance != null)
eventType = instance.Type.GetEventType(eventName);
// Deserialize the strongly-typed event
if (eventType != null)
{
// Create a generic event instance
domainEvent = (DomainEvent)typeof(DomainEvent<>)
.MakeGenericType(eventType)
.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { eventType }, null)
.Invoke(new object[] { reader.ReadValue(eventType) });
// Set the event target and inclusion paths
domainEvent.Instance = instance;
domainEvent.Include = Include;
}
// Otherwise, see if it is a model method invocation
else
{
var separator = eventName.LastIndexOf(".");
if (separator > 0)
{
var type = ModelContext.Current.GetModelType(eventName.Substring(0, separator));
var methodName = eventName.Substring(separator + 1);
while (type != null && method == null)
{
method = type.Methods[methodName];
type = type.BaseType;
}
if (method != null && reader.TokenType == Newtonsoft.Json.JsonToken.StartObject && reader.Read())
{
methodArgs = new object[method.Parameters.Count];
string parameter;
while (reader.ReadProperty(out parameter))
{
// Get the parameter with the specified name
var p = method.Parameters.FirstOrDefault(pm => pm.Name == parameter);
if (p == null)
throw new ArgumentException("The parameter '" + property + "' is not valid for method '" + method.DeclaringType.Name + "." + method.Name + "'.");
// Value type
if (p.ReferenceType == null)
methodArgs[p.Index] = reader.ReadValue(p.ParameterType);
// List type
else if (p.IsList)
methodArgs[p.Index] = reader.ReadValue<ModelInstance[]>();
// Reference type
else
methodArgs[p.Index] = reader.ReadValue<ModelInstance>();
}
reader.Read();
}
}
}
break;
default:
throw new ArgumentException("The specified property could not be deserialized.", property);
}
}
// Custom domain event
if (domainEvent != null)
return domainEvent;
//.........这里部分代码省略.........
示例15: TestReadValueArrayEmpty
public void TestReadValueArrayEmpty()
{
var jsonReader = new JsonReader("[]");
object value = jsonReader.ReadValue();
Assert.IsTrue(
value is List<object>,
"Value read is not a List<object>");
var list = value as List<object>;
Assert.AreEqual(0, list.Count);
}