我有以下课程:
public class TriGrid
{
private List<HexTile> _hexes;
//other private fields...
//other public proprerties
}
Run Code Online (Sandbox Code Playgroud)
我的目标是仅序列化_hexes字段,因此我创建了以下ContractResolver:
internal class TriGridContractResolver : DefaultContractResolver
{
protected override List<MemberInfo> GetSerializableMembers(Type objectType)
{
return new List<MemberInfo> { objectType.GetMember("_hexes", BindingFlags.NonPublic | BindingFlags.Instance)[0] };
}
}
Run Code Online (Sandbox Code Playgroud)
当我想序列化TriGrid的一个实例时,我做了:
var settings = new JsonSerializerSettings()
{
ContractResolver = new TriGridContractResolver()
};
var json = JsonConvert.SerializeObject(someTriGrid, settings);
string strintJson = json.ToString();
Run Code Online (Sandbox Code Playgroud)
但当我检查的价值strintJson总是"{}".该_hexes具有的元素,它不是空的.如果我序列化一个特定的HexTile它按预期工作.我在这做错了什么?
我有一个properties有私人制定者的类,我希望这些属性可以使用deSerialized Json.Net.我知道我可以使用该[JsonProperty]属性来做这一点我想通过实现这一点来做到这一点DefaultContractResolver.这是我一直在使用的一些示例代码,但这个代理工作.
static void Main(string[] args)
{
var a = new a();
a.s = "somestring";
a.set();
Console.WriteLine(JsonConvert.SerializeObject(a));
var strrr = JsonConvert.SerializeObject(a);
var strobj = JsonConvert.DeserializeObject<a>(strrr,new JsonSerializerSettings
{
ContractResolver = new PrivateSetterContractResolver()
});
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
这是我要序列化的类
public class a
{
private int test;
public int Test
{
get { return test; }
private set { test = value; }
}
public string s { get; set; }
public void set()
{
test = 33; …Run Code Online (Sandbox Code Playgroud) 假设我有以下课程 -
public class A
{
public int P1 { get; internal set; }
}
Run Code Online (Sandbox Code Playgroud)
使用json.net,我能够使用P1属性序列化类型.但是,在反序列化期间,未设置P1.如果不修改A类,是否有一种构建方式来处理这个问题?在我的情况下,我使用来自不同程序集的类,不能修改它.
我正在使用Json.Net解决以下类的(反)序列化问题:
public class CoinsWithdrawn
{
public DateTimeOffset WithdrawlDate { get; private set; }
public Dictionary<CoinType, int> NumberOfCoinsByType { get; private set; }
public CoinsWithdrawn(DateTimeOffset withdrawDate, Dictionary<CoinType, int> numberOfCoinsByType)
{
WithdrawlDate = withdrawDate;
NumberOfCoinsByType = numberOfCoinsByType;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是构造函数参数"withdrawDate"的名称与属性名称"WithDrawlDate"不同.使名称匹配(甚至忽略大小写)修复了问题.
但是,我想要更好地理解这一点,所以我恢复了代码并在将两个setter公之后进行了测试.这也解决了这个问题.
最后,我从自动属性切换到具有支持字段的属性,以便我可以完全调试并查看实际发生的情况:
public class CoinsWithdrawn
{
private DateTimeOffset _withdrawlDate;
private Dictionary<CoinType, int> _numberOfCoinsByType;
public DateTimeOffset WithdrawlDate
{
get { return _withdrawlDate; }
set { _withdrawlDate = value; }
}
public Dictionary<CoinType, int> NumberOfCoinsByType
{
get { return _numberOfCoinsByType; }
set { _numberOfCoinsByType …Run Code Online (Sandbox Code Playgroud) 我有一个带有内部列表的类。我不希望这个类的用户能够直接与列表交互,因为我希望在返回它之前保持它的排序并运行一个计算(这取决于顺序)。
我暴露
AddItem(Item x)
Run Code Online (Sandbox Code Playgroud)
和一个
IEnumerable<Item> Items
{
get { // returns a projection of internal list }
}
Run Code Online (Sandbox Code Playgroud)
序列化工作正常,但反序列化使列表为空。我想这是因为我没有二传手。所以我添加了一个允许你设置列表的,但前提是内部列表为空。但这并没有解决问题,事实证明 NewtonSoft 没有调用 setter,它只调用 getter 来获取列表,然后将每个项目添加到其中,因为我的 getter 返回了一个投影列表,所以这些项目被添加反序列化完成后立即处理的对象。
我如何保持对我的列表的只读访问,同时允许一些简单的反序列化?
我试图从JObject创建一个.NET对象,但我得到Object的所有属性作为默认值(对于字符串为null,对于int等为0)
我正在创建一个简单的Jobject:
var jsonObject = new JObject();
jsonObject.Add("type", "Fiat");
jsonObject.Add("model", 500);
jsonObject.Add("color", "white");
Run Code Online (Sandbox Code Playgroud)
汽车类是:
public class Car
{
string type {get;set;}
int model {get ;set;}
string color {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
反序列化在这里:
Car myCar = jsonObject.ToObject<Car>();
Run Code Online (Sandbox Code Playgroud)
但运行时的结果是默认值: 运行时图片
我想知道它为什么会发生,我该怎么做呢,
谢谢
我正在尝试将JSON反序列化为自定义对象,但是我所有的属性都设置为null,并且不确定发生了什么。有人看错吗?
JSON范例
{
"Keys": [
{
"RegistrationKey": "asdfasdfa",
"ValidationStatus": "Valid",
"ValidationDescription": null,
"Properties": [
{
"Key": "Guid",
"Value": "i0asd23165323sdfs68661358"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码,其中strResponseValid是上面的JSON。
Keys myDeserializedObjValid = (Keys)JsonConvert.DeserializeObject(strResponseValid, typeof(Keys));
validationStatusValid = myDeserializedObjValid.ValidationStatus;
Run Code Online (Sandbox Code Playgroud)
这是我的课
public class Keys
{
public string RegistrationKey { get; set; }
public string ValidationStatus { get; set; }
public string ValidationDescription { get; set; }
public List<Properties> PropertiesList { get; set; }
}
public class Properties
{
public string Key { get; set; }
public string …Run Code Online (Sandbox Code Playgroud)