تنظیمات و نکات کاربردی کتابخانهی JSON.NET
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۶/۱۴ ۱۱:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
var json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}); using Newtonsoft.Json;
namespace JsonNetTests
{
public enum Color
{
Red,
Green,
Blue,
White
}
public class Item
{
public string Name { set; get; }
public Color Color { set; get; }
}
public class EnumTests
{
public string GetJson()
{
var item = new Item
{
Name = "Item 1",
Color = Color.Blue
};
return JsonConvert.SerializeObject(item, Formatting.Indented);
}
}
} {
"Name": "Item 1",
"Color": 2
} [JsonConverter(typeof(StringEnumConverter))]
public Color Color { set; get; } return JsonConvert.SerializeObject(item, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
Converters = { new StringEnumConverter() }
}); public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
public Category()
{
Products = new List<Product>();
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Category Category { get; set; }
} using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace JsonNetTests
{
public class SelfReferencingLoops
{
public string GetJson()
{
var category = new Category
{
Id = 1,
Name = "Category 1"
};
var product = new Product
{
Id = 1,
Name = "Product 1"
};
category.Products.Add(product);
product.Category = category;
return JsonConvert.SerializeObject(category, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
Converters = { new StringEnumConverter() }
});
}
}
} An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll Additional information: Self referencing loop detected for property 'Category' with type 'JsonNetTests.Category'. Path 'Products[0]'.
return JsonConvert.SerializeObject(category, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Converters = { new StringEnumConverter() }
}); return JsonConvert.SerializeObject(category, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
Converters = { new StringEnumConverter() }
}); {
"$id": "1",
"Id": 1,
"Name": "Category 1",
"Products": [
{
"$id": "2",
"Id": 1,
"Name": "Product 1",
"Category": {
"$ref": "1"
}
}
]
} public class OrderedContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(
System.Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization).OrderBy(p => p.PropertyName).ToList();
}
} return JsonConvert.SerializeObject(data, new JsonSerializerSettings
{
ContractResolver = new OrderedContractResolver()
});