public void Generate_RequiredMembersClass() { JsonSchemaGenerator generator = new JsonSchemaGenerator(); JsonSchema schema = generator.Generate(typeof(RequiredMembersClass)); Assert.AreEqual(JsonSchemaType.String, schema.Properties["FirstName"].Type); Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["MiddleName"].Type); Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["LastName"].Type); Assert.AreEqual(JsonSchemaType.String, schema.Properties["BirthDate"].Type); }
public void CreatePropertySetter() { Action <object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name")); Movie m = new Movie(); setter(m, "OH HAI!"); Assert.AreEqual("OH HAI!", m.Name); }
public void EmitDefaultValueIgnoreAndPopulate() { string str = "{}"; TestClass obj = JsonConvert.DeserializeObject <TestClass>(str, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate }); Assert.AreEqual("fff", obj.Field1); }
public void ExistsQuery() { JArray a = new JArray(new JObject(new JProperty("hi", "ho")), new JObject(new JProperty("hi2", "ha"))); IList <JToken> t = a.SelectTokens("[ ?( @.hi ) ]").ToList(); Assert.IsNotNull(t); Assert.AreEqual(1, t.Count); Assert.IsTrue(JToken.DeepEquals(new JObject(new JProperty("hi", "ho")), t[0])); }
public void EvaluateArray() { JArray a = new JArray(1, 2, 3, 4); JToken t = a.SelectToken("[1]"); Assert.IsNotNull(t); Assert.AreEqual(JTokenType.Integer, t.Type); Assert.AreEqual(2, (int)t); }
public void ConstructorParametersRespectDefaultValueTest() { var testObject = JsonConvert.DeserializeObject <ConstructorParametersRespectDefaultValue>("{}", new JsonSerializerSettings() { ContractResolver = ConstructorParameterDefaultStringValueContractResolver.Instance }); Assert.AreEqual("Default Value", testObject.Parameter1); Assert.AreEqual("Default Value", testObject.Parameter2); }
public void GenerateSchemaForDBNull() { JsonSchemaGenerator generator = new JsonSchemaGenerator(); generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName; JsonSchema schema = generator.Generate(typeof(DBNull)); Assert.AreEqual(JsonSchemaType.Null, schema.Type); }
public void DeserializeNullableProperty() { string json = @"{ ""LastActivityDate"":null, ""CustomField1"":""Testing"" }"; var c = JsonConvert.DeserializeObject <TestClass>(json); Assert.AreEqual(null, c.LastActivityDate); Assert.AreEqual(1, c.CustomFields.Count); Assert.AreEqual("Testing", (string)c.CustomFields["CustomField1"]); }
public void SetValue() { object key = 0; JArray a = new JArray((object)null); a[key] = new JValue(3); Assert.AreEqual(3, (int)a[key]); }
public void ExtensionDataAttributesInheritanceTest() { var c = JsonConvert.DeserializeObject <ExtensionDataAttributesInheritanceTestClass>("{'first':1,'second':2}"); Assert.AreEqual(null, c.ExtensionData1); Assert.AreEqual(null, c.ExtensionData2); Assert.AreEqual(2, c.ExtensionData0.Count); Assert.AreEqual(1, (int)c.ExtensionData0["first"]); Assert.AreEqual(2, (int)c.ExtensionData0["second"]); }
public void DeserializeDirectoryAccount() { string json = @"{'DisplayName':'John Smith', 'SAMAccountName':'contoso\\johns'}"; DirectoryAccount account = JsonConvert.DeserializeObject <DirectoryAccount>(json); Assert.AreEqual("John Smith", account.DisplayName); Assert.AreEqual("contoso", account.Domain); Assert.AreEqual("johns", account.UserName); }
public void UniqueItems_SimpleUnique() { JsonSchema schema = new JsonSchema(); schema.UniqueItems = true; JArray a = new JArray(1, 2, 3); Assert.IsTrue(a.IsValid(schema)); }
public void NoPathQuery() { JArray a = new JArray(1, 2, 3); IList <JToken> t = a.SelectTokens("[ ?( @ > 1 ) ]").ToList(); Assert.IsNotNull(t); Assert.AreEqual(2, t.Count); Assert.AreEqual(2, (int)t[0]); Assert.AreEqual(3, (int)t[1]); }
public void QuoteName() { JObject o = new JObject( new JProperty("Blah", 1)); JToken t = o.SelectToken("['Blah']"); Assert.IsNotNull(t); Assert.AreEqual(JTokenType.Integer, t.Type); Assert.AreEqual(1, (int)t); }
public void EvaluateSingleProperty() { JObject o = new JObject( new JProperty("Blah", 1)); JToken t = o.SelectToken("Blah"); Assert.IsNotNull(t); Assert.AreEqual(JTokenType.Integer, t.Type); Assert.AreEqual(1, (int)t); }
public void InvalidUnicodeSequence() { string json1 = @"{'prop':'\u123!'}"; JsonTextReader r = new JsonTextReader(new StringReader(json1)); Assert.IsTrue(r.Read()); Assert.IsTrue(r.Read()); ExceptionAssert.Throws <JsonReaderException>(() => { r.Read(); }, @"Invalid Unicode escape sequence: \u123!. Path 'prop', line 1, position 11."); }
public void FloatParseHandling() { JValue v = (JValue)JToken.ReadFrom( new JsonTextReader(new StringReader("9.9")) { FloatParseHandling = Json.FloatParseHandling.Decimal }); Assert.AreEqual(9.9m, v.Value); Assert.AreEqual(typeof(decimal), v.Value.GetType()); }
public void CreateComment() { JValue commentValue = JValue.CreateComment(null); Assert.AreEqual(null, commentValue.Value); Assert.AreEqual(JTokenType.Comment, commentValue.Type); commentValue.Value = "Comment"; Assert.AreEqual("Comment", commentValue.Value); Assert.AreEqual(JTokenType.Comment, commentValue.Type); }
public void SinglePropertyAndFilterWithDoubleEscape() { JPath path = new JPath(@"Blah[ ?( @.name=='h\\i' ) ]"); Assert.AreEqual(2, path.Filters.Count); Assert.AreEqual("Blah", ((FieldFilter)path.Filters[0]).Name); BooleanQueryExpression expressions = (BooleanQueryExpression)((QueryFilter)path.Filters[1]).Expression; Assert.AreEqual(QueryOperator.Equals, expressions.Operator); Assert.AreEqual("h\\i", (string)expressions.Value); }
public void InsertAddEnd() { JArray array = new JArray(); array.Insert(0, 123); array.Insert(1, 456); Assert.AreEqual(2, array.Count); Assert.AreEqual(123, (int)array[0]); Assert.AreEqual(456, (int)array[1]); }
public void DefaultValueHandlingTest() { JsonPropertyAttribute attribute = new JsonPropertyAttribute(); Assert.AreEqual(null, attribute._defaultValueHandling); Assert.AreEqual(DefaultValueHandling.Include, attribute.DefaultValueHandling); attribute.DefaultValueHandling = DefaultValueHandling.Ignore; Assert.AreEqual(DefaultValueHandling.Ignore, attribute._defaultValueHandling); Assert.AreEqual(DefaultValueHandling.Ignore, attribute.DefaultValueHandling); }
public void FieldExtensionDataAttributeTest_Serialize() { FieldExtensionDataAttributeTestClass c = new FieldExtensionDataAttributeTestClass { ExtensionData = new Dictionary <object, object>() }; string json = JsonConvert.SerializeObject(c); Assert.AreEqual("{}", json); }
public void Load() { JsonReader reader = new JsonTextReader(new StringReader("new Date(123)")); reader.Read(); JConstructor constructor = JConstructor.Load(reader); Assert.AreEqual("Date", constructor.Name); Assert.IsTrue(JToken.DeepEquals(new JValue(123), constructor.Values().ElementAt(0))); }
public void SinglePropertyAndFilterWithNull() { JPath path = new JPath("Blah[ ?( @.name==null ) ]"); Assert.AreEqual(2, path.Filters.Count); Assert.AreEqual("Blah", ((FieldFilter)path.Filters[0]).Name); BooleanQueryExpression expressions = (BooleanQueryExpression)((QueryFilter)path.Filters[1]).Expression; Assert.AreEqual(QueryOperator.Equals, expressions.Operator); Assert.AreEqual(null, expressions.Value.Value); }
public void NullValueHandlingTest() { JsonPropertyAttribute attribute = new JsonPropertyAttribute(); Assert.AreEqual(null, attribute._nullValueHandling); Assert.AreEqual(NullValueHandling.Include, attribute.NullValueHandling); attribute.NullValueHandling = NullValueHandling.Ignore; Assert.AreEqual(NullValueHandling.Ignore, attribute._nullValueHandling); Assert.AreEqual(NullValueHandling.Ignore, attribute.NullValueHandling); }
public void MultiContentConstructor() { JProperty p = new JProperty("error", new List <string> { "one", "two" }); JArray a = (JArray)p.Value; Assert.AreEqual(a.Count, 2); Assert.AreEqual("one", (string)a[0]); Assert.AreEqual("two", (string)a[1]); }
public void AddArrayToSelf() { JArray a = new JArray(1, 2); a.Add(a); Assert.AreEqual(3, a.Count); Assert.AreEqual(1, (int)a[0]); Assert.AreEqual(2, (int)a[1]); Assert.AreNotSame(a, a[2]); }
public void ConvertBigIntegerToDouble() { var jObject = JObject.Parse("{ maxValue:10000000000000000000}"); JsonReader reader = jObject.CreateReader(); Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read()); Assert.AreEqual(10000000000000000000d, reader.ReadAsDouble()); Assert.IsTrue(reader.Read()); }
public void Clear() { JArray a = new JArray { 1 }; Assert.AreEqual(1, a.Count); a.Clear(); Assert.AreEqual(0, a.Count); }
public void Float_NaN_ReadAsInt32() { const string testJson = "{float: NaN}"; JsonTextReader reader = new JsonTextReader(new StringReader(testJson)); Assert.IsTrue(reader.Read()); Assert.IsTrue(reader.Read()); ExceptionAssert.Throws <JsonReaderException>(() => reader.ReadAsInt32(), "Cannot read NaN value. Path 'float', line 1, position 11."); }